Socket
Socket
Sign inDemoInstall

@middy/http-router

Package Overview
Dependencies
1
Maintainers
3
Versions
100
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 5.0.0-alpha.0 to 5.0.0-alpha.1

14

index.d.ts

@@ -8,17 +8,9 @@ import middy, { MiddyfiedHandler } from '@middy/core'

APIGatewayProxyResult,
APIGatewayProxyResultV2,
Handler as LambdaHandler
} from 'aws-lambda'
export enum Method {
Get = 'GET',
Post = 'POST',
Put = 'PUT',
Patch = 'PATCH',
Delete = 'DELETE',
Options = 'OPTIONS',
Head = 'HEAD',
Any = 'ANY'
}
export type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'ANY'
type TResult = ALBResult | APIGatewayProxyResult
type TResult = ALBResult | APIGatewayProxyResult | APIGatewayProxyResultV2

@@ -25,0 +17,0 @@ export interface Route<TEvent> {

@@ -1,94 +0,121 @@

import { createError } from '@middy/util';
const httpRouteHandler = (routes)=>{
const routesStatic = {};
const routesDynamic = {};
const enumMethods = methods.concat('ANY');
for (const route of routes){
let { method , path , handler } = route;
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);
import { createError } from '@middy/util'
const httpRouteHandler = (routes) => {
const routesStatic = {}
const routesDynamic = {}
const enumMethods = methods.concat('ANY')
for (const route of routes) {
let { method, path, handler } = route
// Prevents `routesType[method][path] = handler` from flagging: This assignment may alter Object.prototype if a malicious '__proto__' string is injected from library input.
if (!enumMethods.includes(method)) {
throw new Error('Method not allowed', {
cause: { package: '@middy/http-router' }
})
}
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 (typeof handler !== 'undefined') {
return handler(event, context, abort);
}
for (const route of routesDynamic[method] ?? []){
const match = path.match(route.path);
if (match) {
event.pathParameters = {
...match.groups,
...event.pathParameters
};
return route.handler(event, context, abort);
}
}
throw createError(404, 'Route does not exist');
};
};
const regexpDynamicWildcards = /\/\{(proxy)\+\}$/;
const regexpDynamicParameters = /\/\{([^/]+)\}/g;
const methods = [
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'OPTIONS',
'HEAD'
];
const attachStaticRoute = (method, path, handler, routesType)=>{
if (method === 'ANY') {
for (const method of methods){
attachStaticRoute(method, path, handler, routesType);
}
return;
// remove trailing slash, but not if it's the first one
if (path.endsWith('/') && path !== '/') {
path = path.substr(0, path.length - 1)
}
if (!routesType[method]) {
routesType[method] = {};
// Static
if (path.indexOf('{') < 0) {
attachStaticRoute(method, path, handler, routesStatic)
continue
}
routesType[method][path] = handler;
routesType[method][path + '/'] = handler;
};
const attachDynamicRoute = (method, path, handler, routesType)=>{
if (method === 'ANY') {
for (const method of methods){
attachDynamicRoute(method, path, handler, routesType);
// Dynamic
attachDynamicRoute(method, path, handler, routesDynamic)
}
return (event, context, abort) => {
const { method, path } = getVersionRoute[pickVersion(event)]?.(event)
if (!method) {
throw new Error('Unknown http event format', {
cause: { package: '@middy/http-router', data: event }
})
}
// Static
const handler = routesStatic[method]?.[path]
if (typeof handler !== 'undefined') {
return handler(event, context, abort)
}
// Dynamic
for (const route of routesDynamic[method] ?? []) {
const match = path.match(route.path)
if (match) {
event.pathParameters = {
...match.groups,
...event.pathParameters
}
return;
return route.handler(event, context, abort)
}
}
if (!routesType[method]) {
routesType[method] = [];
// Not Found
throw createError(404, 'Route does not exist', {
cause: { pacakge: '@middy/http-router', data: path }
})
}
}
const regexpDynamicWildcards = /\/\{(proxy)\+\}$/
const regexpDynamicParameters = /\/\{([^/]+)\}/g
const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'] // ANY excluded by design
const attachStaticRoute = (method, path, handler, routesType) => {
if (method === 'ANY') {
for (const method of methods) {
attachStaticRoute(method, path, handler, routesType)
}
path = path.replace(regexpDynamicWildcards, '/?(?<$1>.*)').replace(regexpDynamicParameters, '/(?<$1>[^/]+)');
path = new RegExp(`^${path}/?$`);
routesType[method].push({
path,
handler
});
};
return
}
if (!routesType[method]) {
routesType[method] = {}
}
routesType[method][path] = handler
routesType[method][path + '/'] = handler // Optional `/`
}
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, '/?(?<$1>.*)')
.replace(regexpDynamicParameters, '/(?<$1>[^/]+)')
path = new RegExp(`^${path}/?$`) // Adds in optional `/`
routesType[method].push({ path, handler })
}
const pickVersion = (event) => {
// '1.0' is a safer default
return event.version ?? (event.method ? 'vpc' : '1.0')
}
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;
'1.0': (event) => ({
method: event.httpMethod,
path: event.path
}),
'2.0': (event) => ({
method: event.requestContext.http.method,
path: event.requestContext.http.path
}),
vpc: (event) => ({
method: event.method,
path: event.raw_path.split('?')[0]
})
}
export default httpRouteHandler
{
"name": "@middy/http-router",
"version": "5.0.0-alpha.0",
"version": "5.0.0-alpha.1",
"description": "HTTP event router for the middy framework",

@@ -13,3 +13,2 @@ "type": "module",

},
"main": "./index.cjs",
"module": "./index.js",

@@ -21,6 +20,2 @@ "exports": {

"default": "./index.js"
},
"require": {
"types": "./index.d.ts",
"default": "./index.cjs"
}

@@ -32,3 +27,2 @@ }

"index.js",
"index.cjs",
"index.d.ts"

@@ -72,9 +66,9 @@ ],

"dependencies": {
"@middy/util": "5.0.0-alpha.0"
"@middy/util": "5.0.0-alpha.1"
},
"devDependencies": {
"@middy/core": "5.0.0-alpha.0",
"@middy/core": "5.0.0-alpha.1",
"@types/aws-lambda": "^8.10.97"
},
"gitHead": "08c35e3dba9efdad0b86666ce206ce302cc65d07"
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
}

@@ -22,4 +22,5 @@ <div align="center">

</a>
<a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
<a href="https://github.com/middyjs/middy/actions/workflows/sast.yml">
<img src="https://github.com/middyjs/middy/actions/workflows/sast.yml/badge.svg
?branch=main&event=push" alt="CodeQL" style="max-width:100%;">
</a>

@@ -26,0 +27,0 @@ <a href="https://bestpractices.coreinfrastructure.org/projects/5280">

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc