@middy/http-router 5.1.0 → 5.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +113 -107
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,115 +1,121 @@
1
- import { createError } from '@middy/util';
2
- const httpRouteHandler = (routes)=>{
3
- const routesStatic = {};
4
- const routesDynamic = {};
5
- const enumMethods = methods.concat('ANY');
6
- for (const route of routes){
7
- let { method, path, handler } = route;
8
- if (!enumMethods.includes(method)) {
9
- throw new Error('Method not allowed', {
10
- cause: {
11
- package: '@middy/http-router'
12
- }
13
- });
14
- }
15
- if (path.endsWith('/') && path !== '/') {
16
- path = path.substr(0, path.length - 1);
17
- }
18
- if (path.indexOf('{') < 0) {
19
- attachStaticRoute(method, path, handler, routesStatic);
20
- continue;
21
- }
22
- attachDynamicRoute(method, path, handler, routesDynamic);
1
+ import { createError } from '@middy/util'
2
+
3
+ const httpRouteHandler = (routes) => {
4
+ const routesStatic = {}
5
+ const routesDynamic = {}
6
+ const enumMethods = methods.concat('ANY')
7
+ for (const route of routes) {
8
+ let { method, path, handler } = route
9
+
10
+ // Prevents `routesType[method][path] = handler` from flagging: This assignment may alter Object.prototype if a malicious '__proto__' string is injected from library input.
11
+ if (!enumMethods.includes(method)) {
12
+ throw new Error('Method not allowed', {
13
+ cause: { package: '@middy/http-router' }
14
+ })
23
15
  }
24
- return (event, context, abort)=>{
25
- const { method, path } = getVersionRoute[pickVersion(event)]?.(event);
26
- if (!method) {
27
- throw new Error('Unknown http event format', {
28
- cause: {
29
- package: '@middy/http-router',
30
- data: event
31
- }
32
- });
33
- }
34
- const handler = routesStatic[method]?.[path];
35
- if (typeof handler !== 'undefined') {
36
- return handler(event, context, abort);
37
- }
38
- for (const route of routesDynamic[method] ?? []){
39
- const match = path.match(route.path);
40
- if (match) {
41
- event.pathParameters = {
42
- ...match.groups,
43
- ...event.pathParameters
44
- };
45
- return route.handler(event, context, abort);
46
- }
47
- }
48
- throw createError(404, 'Route does not exist', {
49
- cause: {
50
- package: '@middy/http-router',
51
- data: path
52
- }
53
- });
54
- };
55
- };
56
- const regexpDynamicWildcards = /\/\{(proxy)\+\}$/;
57
- const regexpDynamicParameters = /\/\{([^/]+)\}/g;
58
- const methods = [
59
- 'GET',
60
- 'POST',
61
- 'PUT',
62
- 'PATCH',
63
- 'DELETE',
64
- 'OPTIONS',
65
- 'HEAD'
66
- ];
67
- const attachStaticRoute = (method, path, handler, routesType)=>{
68
- if (method === 'ANY') {
69
- for (const method of methods){
70
- attachStaticRoute(method, path, handler, routesType);
71
- }
72
- return;
16
+
17
+ // remove trailing slash, but not if it's the first one
18
+ if (path.endsWith('/') && path !== '/') {
19
+ path = path.substr(0, path.length - 1)
73
20
  }
74
- if (!routesType[method]) {
75
- routesType[method] = {};
21
+
22
+ // Static
23
+ if (path.indexOf('{') < 0) {
24
+ attachStaticRoute(method, path, handler, routesStatic)
25
+ continue
76
26
  }
77
- routesType[method][path] = handler;
78
- routesType[method][path + '/'] = handler;
79
- };
80
- const attachDynamicRoute = (method, path, handler, routesType)=>{
81
- if (method === 'ANY') {
82
- for (const method of methods){
83
- attachDynamicRoute(method, path, handler, routesType);
27
+
28
+ // Dynamic
29
+ attachDynamicRoute(method, path, handler, routesDynamic)
30
+ }
31
+
32
+ return (event, context, abort) => {
33
+ const { method, path } = getVersionRoute[pickVersion(event)]?.(event)
34
+ if (!method) {
35
+ throw new Error('Unknown http event format', {
36
+ cause: { package: '@middy/http-router', data: event }
37
+ })
38
+ }
39
+
40
+ // Static
41
+ const handler = routesStatic[method]?.[path]
42
+ if (typeof handler !== 'undefined') {
43
+ return handler(event, context, abort)
44
+ }
45
+
46
+ // Dynamic
47
+ for (const route of routesDynamic[method] ?? []) {
48
+ const match = path.match(route.path)
49
+ if (match) {
50
+ event.pathParameters = {
51
+ ...match.groups,
52
+ ...event.pathParameters
84
53
  }
85
- return;
54
+ return route.handler(event, context, abort)
55
+ }
56
+ }
57
+
58
+ // Not Found
59
+ throw createError(404, 'Route does not exist', {
60
+ cause: { package: '@middy/http-router', data: path }
61
+ })
62
+ }
63
+ }
64
+
65
+ const regexpDynamicWildcards = /\/\{(proxy)\+\}$/
66
+ const regexpDynamicParameters = /\/\{([^/]+)\}/g
67
+
68
+ const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'] // ANY excluded by design
69
+
70
+ const attachStaticRoute = (method, path, handler, routesType) => {
71
+ if (method === 'ANY') {
72
+ for (const method of methods) {
73
+ attachStaticRoute(method, path, handler, routesType)
86
74
  }
87
- if (!routesType[method]) {
88
- routesType[method] = [];
75
+ return
76
+ }
77
+ if (!routesType[method]) {
78
+ routesType[method] = {}
79
+ }
80
+ routesType[method][path] = handler
81
+ routesType[method][path + '/'] = handler // Optional `/`
82
+ }
83
+
84
+ const attachDynamicRoute = (method, path, handler, routesType) => {
85
+ if (method === 'ANY') {
86
+ for (const method of methods) {
87
+ attachDynamicRoute(method, path, handler, routesType)
89
88
  }
90
- path = path.replace(regexpDynamicWildcards, '/?(?<$1>.*)').replace(regexpDynamicParameters, '/(?<$1>[^/]+)');
91
- path = new RegExp(`^${path}/?$`);
92
- routesType[method].push({
93
- path,
94
- handler
95
- });
96
- };
97
- const pickVersion = (event)=>{
98
- return event.version ?? (event.method ? 'vpc' : '1.0');
99
- };
89
+ return
90
+ }
91
+ if (!routesType[method]) {
92
+ routesType[method] = []
93
+ }
94
+ path = path
95
+ .replace(regexpDynamicWildcards, '/?(?<$1>.*)')
96
+ .replace(regexpDynamicParameters, '/(?<$1>[^/]+)')
97
+ path = new RegExp(`^${path}/?$`) // Adds in optional `/`
98
+ routesType[method].push({ path, handler })
99
+ }
100
+
101
+ const pickVersion = (event) => {
102
+ // '1.0' is a safer default
103
+ return event.version ?? (event.method ? 'vpc' : '1.0')
104
+ }
105
+
100
106
  const getVersionRoute = {
101
- '1.0': (event)=>({
102
- method: event.httpMethod,
103
- path: event.path
104
- }),
105
- '2.0': (event)=>({
106
- method: event.requestContext.http.method,
107
- path: event.requestContext.http.path
108
- }),
109
- vpc: (event)=>({
110
- method: event.method,
111
- path: event.raw_path.split('?')[0]
112
- })
113
- };
114
- export default httpRouteHandler;
107
+ '1.0': (event) => ({
108
+ method: event.httpMethod,
109
+ path: event.path
110
+ }),
111
+ '2.0': (event) => ({
112
+ method: event.requestContext.http.method,
113
+ path: event.requestContext.http.path
114
+ }),
115
+ vpc: (event) => ({
116
+ method: event.method,
117
+ path: event.raw_path.split('?')[0]
118
+ })
119
+ }
115
120
 
121
+ export default httpRouteHandler
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-router",
3
- "version": "5.1.0",
3
+ "version": "5.2.1",
4
4
  "description": "HTTP event router for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -60,11 +60,11 @@
60
60
  "url": "https://github.com/sponsors/willfarrell"
61
61
  },
62
62
  "dependencies": {
63
- "@middy/util": "5.1.0"
63
+ "@middy/util": "5.2.1"
64
64
  },
65
65
  "devDependencies": {
66
- "@middy/core": "5.1.0",
66
+ "@middy/core": "5.2.1",
67
67
  "@types/aws-lambda": "^8.10.97"
68
68
  },
69
- "gitHead": "bbdaf5843914921804ba085dd58117273febe6b5"
69
+ "gitHead": "4d55da221b9165b4b3e59a12632fd40a149a1e92"
70
70
  }