@middy/core 4.6.0 → 4.6.2

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 (3) hide show
  1. package/index.cjs +18 -6
  2. package/index.js +18 -6
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";
1
+ /* global awslambda */ "use strict";
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
@@ -17,9 +17,10 @@ const defaultPlugin = {
17
17
  timeoutEarlyResponse: ()=>{
18
18
  throw new Error('Timeout');
19
19
  },
20
- streamifyResponse: false
20
+ streamifyResponse: false // Deprecate need for this when AWS provides a flag for when it's looking for it
21
21
  };
22
22
  const middy = (lambdaHandler = defaultLambdaHandler, plugin = {})=>{
23
+ // Allow base handler to be set using .handler()
23
24
  if (typeof lambdaHandler !== 'function') {
24
25
  plugin = lambdaHandler;
25
26
  lambdaHandler = defaultLambdaHandler;
@@ -57,12 +58,14 @@ const middy = (lambdaHandler = defaultLambdaHandler, plugin = {})=>{
57
58
  handlerBody = handlerResponse.body ?? '';
58
59
  responseStream = awslambda.HttpResponseStream.from(responseStream, handlerResponse);
59
60
  }
61
+ // Source @datastream/core (MIT)
60
62
  let handlerStream;
61
63
  if (handlerBody._readableState) {
62
64
  handlerStream = handlerBody;
63
65
  } else if (typeof handlerBody === 'string') {
64
66
  function* iterator(input) {
65
- const size = 16384;
67
+ const size = 16384 // 16 * 1024 // Node.js default
68
+ ;
66
69
  let position = 0;
67
70
  const length = input.length;
68
71
  while(position < length){
@@ -94,6 +97,7 @@ const middy = (lambdaHandler = defaultLambdaHandler, plugin = {})=>{
94
97
  }
95
98
  return middy;
96
99
  };
100
+ // Inline Middlewares
97
101
  middy.before = (beforeMiddleware)=>{
98
102
  beforeMiddlewares.push(beforeMiddleware);
99
103
  return middy;
@@ -114,9 +118,11 @@ const middy = (lambdaHandler = defaultLambdaHandler, plugin = {})=>{
114
118
  };
115
119
  const runRequest = async (request, beforeMiddlewares, lambdaHandler, afterMiddlewares, onErrorMiddlewares, plugin)=>{
116
120
  let timeoutAbort;
117
- const timeoutEarly = plugin.timeoutEarly && request.context.getRemainingTimeInMillis;
121
+ const timeoutEarly = plugin.timeoutEarly && request.context.getRemainingTimeInMillis // disable when AWS context missing (tests, containers)
122
+ ;
118
123
  try {
119
124
  await runMiddlewares(request, beforeMiddlewares, plugin);
125
+ // Check if before stack hasn't exit early
120
126
  if (typeof request.response === 'undefined') {
121
127
  plugin.beforeHandler?.();
122
128
  const handlerAbort = new AbortController();
@@ -132,21 +138,26 @@ const runRequest = async (request, beforeMiddlewares, lambdaHandler, afterMiddle
132
138
  return plugin.timeoutEarlyResponse();
133
139
  }) : Promise.race([])
134
140
  ]);
135
- timeoutAbort?.abort();
141
+ timeoutAbort?.abort() // lambdaHandler may not be a promise
142
+ ;
136
143
  plugin.afterHandler?.();
137
144
  await runMiddlewares(request, afterMiddlewares, plugin);
138
145
  }
139
146
  } catch (e) {
140
- timeoutAbort?.abort();
147
+ timeoutAbort?.abort() // timeout should be aborted on errors
148
+ ;
149
+ // Reset response changes made by after stack before error thrown
141
150
  request.response = undefined;
142
151
  request.error = e;
143
152
  try {
144
153
  await runMiddlewares(request, onErrorMiddlewares, plugin);
145
154
  } catch (e) {
155
+ // Save error that wasn't handled
146
156
  e.originalError = request.error;
147
157
  request.error = e;
148
158
  throw request.error;
149
159
  }
160
+ // Catch if onError stack hasn't handled the error
150
161
  if (typeof request.response === 'undefined') throw request.error;
151
162
  } finally{
152
163
  await plugin.requestEnd?.(request);
@@ -158,6 +169,7 @@ const runMiddlewares = async (request, middlewares, plugin)=>{
158
169
  plugin.beforeMiddleware?.(nextMiddleware.name);
159
170
  const res = await nextMiddleware(request);
160
171
  plugin.afterMiddleware?.(nextMiddleware.name);
172
+ // short circuit chaining and respond early
161
173
  if (typeof res !== 'undefined') {
162
174
  request.response = res;
163
175
  return;
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Readable } from 'node:stream';
1
+ /* global awslambda */ import { Readable } from 'node:stream';
2
2
  import { pipeline } from 'node:stream/promises';
3
3
  import { setTimeout } from 'node:timers/promises';
4
4
  const defaultLambdaHandler = ()=>{};
@@ -7,9 +7,10 @@ const defaultPlugin = {
7
7
  timeoutEarlyResponse: ()=>{
8
8
  throw new Error('Timeout');
9
9
  },
10
- streamifyResponse: false
10
+ streamifyResponse: false // Deprecate need for this when AWS provides a flag for when it's looking for it
11
11
  };
12
12
  const middy = (lambdaHandler = defaultLambdaHandler, plugin = {})=>{
13
+ // Allow base handler to be set using .handler()
13
14
  if (typeof lambdaHandler !== 'function') {
14
15
  plugin = lambdaHandler;
15
16
  lambdaHandler = defaultLambdaHandler;
@@ -47,12 +48,14 @@ const middy = (lambdaHandler = defaultLambdaHandler, plugin = {})=>{
47
48
  handlerBody = handlerResponse.body ?? '';
48
49
  responseStream = awslambda.HttpResponseStream.from(responseStream, handlerResponse);
49
50
  }
51
+ // Source @datastream/core (MIT)
50
52
  let handlerStream;
51
53
  if (handlerBody._readableState) {
52
54
  handlerStream = handlerBody;
53
55
  } else if (typeof handlerBody === 'string') {
54
56
  function* iterator(input) {
55
- const size = 16384;
57
+ const size = 16384 // 16 * 1024 // Node.js default
58
+ ;
56
59
  let position = 0;
57
60
  const length = input.length;
58
61
  while(position < length){
@@ -84,6 +87,7 @@ const middy = (lambdaHandler = defaultLambdaHandler, plugin = {})=>{
84
87
  }
85
88
  return middy;
86
89
  };
90
+ // Inline Middlewares
87
91
  middy.before = (beforeMiddleware)=>{
88
92
  beforeMiddlewares.push(beforeMiddleware);
89
93
  return middy;
@@ -104,9 +108,11 @@ const middy = (lambdaHandler = defaultLambdaHandler, plugin = {})=>{
104
108
  };
105
109
  const runRequest = async (request, beforeMiddlewares, lambdaHandler, afterMiddlewares, onErrorMiddlewares, plugin)=>{
106
110
  let timeoutAbort;
107
- const timeoutEarly = plugin.timeoutEarly && request.context.getRemainingTimeInMillis;
111
+ const timeoutEarly = plugin.timeoutEarly && request.context.getRemainingTimeInMillis // disable when AWS context missing (tests, containers)
112
+ ;
108
113
  try {
109
114
  await runMiddlewares(request, beforeMiddlewares, plugin);
115
+ // Check if before stack hasn't exit early
110
116
  if (typeof request.response === 'undefined') {
111
117
  plugin.beforeHandler?.();
112
118
  const handlerAbort = new AbortController();
@@ -122,21 +128,26 @@ const runRequest = async (request, beforeMiddlewares, lambdaHandler, afterMiddle
122
128
  return plugin.timeoutEarlyResponse();
123
129
  }) : Promise.race([])
124
130
  ]);
125
- timeoutAbort?.abort();
131
+ timeoutAbort?.abort() // lambdaHandler may not be a promise
132
+ ;
126
133
  plugin.afterHandler?.();
127
134
  await runMiddlewares(request, afterMiddlewares, plugin);
128
135
  }
129
136
  } catch (e) {
130
- timeoutAbort?.abort();
137
+ timeoutAbort?.abort() // timeout should be aborted on errors
138
+ ;
139
+ // Reset response changes made by after stack before error thrown
131
140
  request.response = undefined;
132
141
  request.error = e;
133
142
  try {
134
143
  await runMiddlewares(request, onErrorMiddlewares, plugin);
135
144
  } catch (e) {
145
+ // Save error that wasn't handled
136
146
  e.originalError = request.error;
137
147
  request.error = e;
138
148
  throw request.error;
139
149
  }
150
+ // Catch if onError stack hasn't handled the error
140
151
  if (typeof request.response === 'undefined') throw request.error;
141
152
  } finally{
142
153
  await plugin.requestEnd?.(request);
@@ -148,6 +159,7 @@ const runMiddlewares = async (request, middlewares, plugin)=>{
148
159
  plugin.beforeMiddleware?.(nextMiddleware.name);
149
160
  const res = await nextMiddleware(request);
150
161
  plugin.afterMiddleware?.(nextMiddleware.name);
162
+ // short circuit chaining and respond early
151
163
  if (typeof res !== 'undefined') {
152
164
  request.response = res;
153
165
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/core",
3
- "version": "4.6.0",
3
+ "version": "4.6.2",
4
4
  "description": "🛵 The stylish Node.js middleware engine for AWS Lambda (core package)",
5
5
  "type": "module",
6
6
  "engines": {
@@ -66,5 +66,5 @@
66
66
  "@types/aws-lambda": "^8.10.76",
67
67
  "@types/node": "^20.0.0"
68
68
  },
69
- "gitHead": "34e52521a81a224e3d97de6171604c49e676f0d4"
69
+ "gitHead": "8b03a01abf5a9c08231ec5ced775e87f8be8f67d"
70
70
  }