Socket
Socket
Sign inDemoInstall

@middy/http-router

Package Overview
Dependencies
Maintainers
3
Versions
106
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@middy/http-router - npm Package Compare versions

Comparing version 3.0.3 to 3.0.4

88

index.js

@@ -1,3 +0,89 @@

import{createError}from"@middy/util";const httpRouteHandler=routes=>{const routesStatic={};const routesDynamic={};const enumMethods=methods.concat("ANY");for(const route1 of routes){let{method,path,handler}=route1;if(!enumMethods.includes(method)){throw new Error("[http-router] Method not allowed")}if(path.endsWith("/")&&path!=="/"){path=path.substr(0,path.length-1)}if(path.indexOf("{")<0){attachStaticRoute(method,path,handler,routesStatic);continue}attachDynamicRoute(method,path,handler,routesDynamic)}return(event,context,abort)=>{const{method,path}=getVersionRoute[event.version??"1.0"]?.(event);if(!method){throw new Error("[http-router] Unknown http event format")}const handler=routesStatic[method]?.[path];if(handler!==undefined){return handler(event,context,abort)}for(const route of routesDynamic[method]??[]){if(route.path.test(path)){return route.handler(event,context,abort)}}throw createError(404,"Route does not exist")}};const regexpDynamicWildcards=/\/\{proxy\+\}/g;const regexpDynamicParameters=/\/\{.+\}/g;const methods=["GET","POST","PUT","PATCH","DELETE","OPTIONS"];const attachStaticRoute=(method,path,handler,routesType)=>{if(method==="ANY"){for(const method of methods){attachStaticRoute(method,path,handler,routesType)}return}if(!routesType[method]){routesType[method]={}}routesType[method][path]=handler};const attachDynamicRoute=(method,path,handler,routesType)=>{if(method==="ANY"){for(const method of methods){attachDynamicRoute(method,path,handler,routesType)}return}if(!routesType[method]){routesType[method]=[]}path=path.replace(regexpDynamicWildcards,"/?.*").replace(regexpDynamicParameters,"/.+");path=new RegExp(`^${path}$`);routesType[method].push({path,handler})};const getVersionRoute={"1.0":event=>({method:event.httpMethod,path:event.path}),"2.0":event=>({method:event.requestContext.http.method,path:event.requestContext.http.path})};export default httpRouteHandler
import { createError } from '@middy/util';
const httpRouteHandler = (routes)=>{
const routesStatic = {};
const routesDynamic = {};
const enumMethods = methods.concat('ANY');
for (const route1 of routes){
let { method , path , handler } = route1;
if (!enumMethods.includes(method)) {
throw new Error('[http-router] Method not allowed');
}
if (path.endsWith('/') && path !== '/') {
path = path.substr(0, path.length - 1);
}
if (path.indexOf('{') < 0) {
attachStaticRoute(method, path, handler, routesStatic);
continue;
}
attachDynamicRoute(method, path, handler, routesDynamic);
}
return (event, context, abort)=>{
const { method , path } = getVersionRoute[event.version ?? '1.0']?.(event);
if (!method) {
throw new Error('[http-router] Unknown http event format');
}
const handler = routesStatic[method]?.[path];
if (handler !== undefined) {
return handler(event, context, abort);
}
for (const route of routesDynamic[method] ?? []){
if (route.path.test(path)) {
return route.handler(event, context, abort);
}
}
throw createError(404, 'Route does not exist');
};
};
const regexpDynamicWildcards = /\/\{proxy\+\}/g;
const regexpDynamicParameters = /\/\{.+\}/g;
const methods = [
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'OPTIONS'
];
const attachStaticRoute = (method, path, handler, routesType)=>{
if (method === 'ANY') {
for (const method of methods){
attachStaticRoute(method, path, handler, routesType);
}
return;
}
if (!routesType[method]) {
routesType[method] = {};
}
routesType[method][path] = handler;
};
const attachDynamicRoute = (method, path, handler, routesType)=>{
if (method === 'ANY') {
for (const method of methods){
attachDynamicRoute(method, path, handler, routesType);
}
return;
}
if (!routesType[method]) {
routesType[method] = [];
}
path = path.replace(regexpDynamicWildcards, '/?.*').replace(regexpDynamicParameters, '/.+');
path = new RegExp(`^${path}$`);
routesType[method].push({
path,
handler
});
};
const getVersionRoute = {
'1.0': (event)=>({
method: event.httpMethod,
path: event.path
}),
'2.0': (event)=>({
method: event.requestContext.http.method,
path: event.requestContext.http.path
})
};
export default httpRouteHandler;
//# sourceMappingURL=index.js.map

8

package.json
{
"name": "@middy/http-router",
"version": "3.0.3",
"version": "3.0.4",
"description": "HTTP event router for the middy framework",

@@ -58,9 +58,9 @@ "type": "module",

"dependencies": {
"@middy/util": "3.0.3"
"@middy/util": "3.0.4"
},
"devDependencies": {
"@middy/core": "3.0.3",
"@middy/core": "3.0.4",
"@types/aws-lambda": "^8.10.97"
},
"gitHead": "ea9e5e8cce754d0c467c7dd3ac9a7601149efea2"
"gitHead": "3e9bc83e791f943c71cd7003fc27f0a3692d83a1"
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc