@middy/core 7.2.3 → 7.3.0

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.
@@ -25,8 +25,11 @@ export const executionModeDurableContext = (
25
25
  );
26
26
  copyKeys(request.context, request.context.lambdaContext, lambdaContextKeys);
27
27
 
28
+ // See executionModeStandard for the .cause-chaining rationale.
29
+ let handlerError;
30
+ let response;
28
31
  try {
29
- const response = await runRequest(
32
+ response = await runRequest(
30
33
  request,
31
34
  beforeMiddlewares,
32
35
  lambdaHandler,
@@ -34,10 +37,20 @@ export const executionModeDurableContext = (
34
37
  onErrorMiddlewares,
35
38
  plugin,
36
39
  );
37
- return response;
38
- } finally {
40
+ } catch (err) {
41
+ handlerError = err;
42
+ }
43
+ try {
39
44
  await plugin.requestEnd(request);
45
+ } catch (hookErr) {
46
+ if (handlerError) {
47
+ handlerError.cause ??= hookErr;
48
+ } else {
49
+ throw hookErr;
50
+ }
40
51
  }
52
+ if (handlerError) throw handlerError;
53
+ return response;
41
54
  });
42
55
  middy.handler = (replaceLambdaHandler) => {
43
56
  lambdaHandler = replaceLambdaHandler;
@@ -11,8 +11,14 @@ export const executionModeStandard = (
11
11
  const middy = async (event, context) => {
12
12
  const request = middyRequest(event, context);
13
13
  plugin.requestStart(request);
14
+ // Run requestEnd without letting a throw in the hook replace the
15
+ // handler's original error. If only requestEnd throws, it propagates
16
+ // (same as a naive finally). If both throw, the hook error is attached
17
+ // as `.cause` on the handler error (only if no cause is already set).
18
+ let handlerError;
19
+ let response;
14
20
  try {
15
- const response = await runRequest(
21
+ response = await runRequest(
16
22
  request,
17
23
  beforeMiddlewares,
18
24
  lambdaHandler,
@@ -20,10 +26,20 @@ export const executionModeStandard = (
20
26
  onErrorMiddlewares,
21
27
  plugin,
22
28
  );
23
- return response;
24
- } finally {
29
+ } catch (err) {
30
+ handlerError = err;
31
+ }
32
+ try {
25
33
  await plugin.requestEnd(request);
34
+ } catch (hookErr) {
35
+ if (handlerError) {
36
+ handlerError.cause ??= hookErr;
37
+ } else {
38
+ throw hookErr;
39
+ }
26
40
  }
41
+ if (handlerError) throw handlerError;
42
+ return response;
27
43
  };
28
44
  middy.handler = (replaceLambdaHandler) => {
29
45
  lambdaHandler = replaceLambdaHandler;
@@ -54,11 +54,23 @@ export const executionModeStreamifyResponse = (
54
54
  });
55
55
  }
56
56
 
57
+ // See executionModeStandard for the .cause-chaining rationale.
58
+ let handlerError;
57
59
  try {
58
60
  await pipeline(handlerStream, responseStream);
59
- } finally {
61
+ } catch (err) {
62
+ handlerError = err;
63
+ }
64
+ try {
60
65
  await plugin.requestEnd(request);
66
+ } catch (hookErr) {
67
+ if (handlerError) {
68
+ handlerError.cause ??= hookErr;
69
+ } else {
70
+ throw hookErr;
71
+ }
61
72
  }
73
+ if (handlerError) throw handlerError;
62
74
  },
63
75
  );
64
76
 
package/index.d.ts CHANGED
@@ -229,4 +229,8 @@ declare namespace middy {
229
229
  };
230
230
  }
231
231
 
232
+ export declare function middyValidateOptions(
233
+ options?: Record<string, unknown>,
234
+ ): void;
235
+
232
236
  export default middy;
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors.
2
2
  // SPDX-License-Identifier: MIT
3
3
  import { setTimeout } from "node:timers";
4
+ import { validateOptions } from "@middy/util";
4
5
  import { executionModeStandard } from "./executionModeStandard.js";
5
6
 
6
7
  const defaultLambdaHandler = () => {};
@@ -17,6 +18,23 @@ const defaultPluginConfig = {
17
18
  executionMode: executionModeStandard,
18
19
  };
19
20
 
21
+ const optionSchema = {
22
+ internal: "object?",
23
+ beforePrefetch: "function?",
24
+ requestStart: "function?",
25
+ beforeMiddleware: "function?",
26
+ afterMiddleware: "function?",
27
+ beforeHandler: "function?",
28
+ timeoutEarlyInMillis: "number?",
29
+ timeoutEarlyResponse: "function?",
30
+ afterHandler: "function?",
31
+ requestEnd: "function?",
32
+ executionMode: "function?",
33
+ };
34
+
35
+ export const middyValidateOptions = (options) =>
36
+ validateOptions("@middy/core", optionSchema, options);
37
+
20
38
  export const middy = (setupLambdaHandler, pluginConfig) => {
21
39
  let lambdaHandler;
22
40
  let plugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/core",
3
- "version": "7.2.3",
3
+ "version": "7.3.0",
4
4
  "description": "🛵 The stylish Node.js middleware engine for AWS Lambda (core package)",
5
5
  "type": "module",
6
6
  "engines": {
@@ -17,36 +17,24 @@
17
17
  "import": {
18
18
  "types": "./index.d.ts",
19
19
  "default": "./index.js"
20
- },
21
- "require": {
22
- "default": "./index.js"
23
20
  }
24
21
  },
25
22
  "./Standard": {
26
23
  "import": {
27
24
  "types": "./executionModeStandard.d.ts",
28
25
  "default": "./executionModeStandard.js"
29
- },
30
- "require": {
31
- "default": "./executionModeStandard.js"
32
26
  }
33
27
  },
34
28
  "./DurableContext": {
35
29
  "import": {
36
30
  "types": "./executionModeDurableContext.d.ts",
37
31
  "default": "./executionModeDurableContext.js"
38
- },
39
- "require": {
40
- "default": "./executionModeDurableContext.js"
41
32
  }
42
33
  },
43
34
  "./StreamifyResponse": {
44
35
  "import": {
45
36
  "types": "./executionModeStreamifyResponse.d.ts",
46
37
  "default": "./executionModeStreamifyResponse.js"
47
- },
48
- "require": {
49
- "default": "./executionModeStreamifyResponse.js"
50
38
  }
51
39
  }
52
40
  },
@@ -97,7 +85,7 @@
97
85
  "url": "https://github.com/sponsors/willfarrell"
98
86
  },
99
87
  "dependencies": {
100
- "@middy/util": "7.2.3"
88
+ "@middy/util": "7.3.0"
101
89
  },
102
90
  "peerDependencies": {
103
91
  "@aws/durable-execution-sdk-js": "^1.0.0"
@@ -110,7 +98,7 @@
110
98
  "devDependencies": {
111
99
  "@aws/durable-execution-sdk-js": "^1.0.0",
112
100
  "@aws/durable-execution-sdk-js-testing": "^1.0.0",
113
- "@datastream/core": "0.1.6",
101
+ "@datastream/core": "0.3.2",
114
102
  "@types/aws-lambda": "^8.0.0",
115
103
  "@types/node": "^22.0.0"
116
104
  }