@middy/core 7.2.3 → 7.3.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.
- package/executionModeDurableContext.js +16 -3
- package/executionModeStandard.js +19 -3
- package/executionModeStreamifyResponse.js +13 -1
- package/index.d.ts +9 -3
- package/index.js +32 -0
- package/package.json +3 -15
|
@@ -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
|
-
|
|
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
|
-
|
|
38
|
-
|
|
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;
|
package/executionModeStandard.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
24
|
-
|
|
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
|
-
}
|
|
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
|
@@ -8,9 +8,11 @@ import type { DurableContext as LambdaContextDurable } from "@aws/durable-execut
|
|
|
8
8
|
|
|
9
9
|
declare type PluginHook = () => void;
|
|
10
10
|
declare type PluginHookWithMiddlewareName = (middlewareName: string) => void;
|
|
11
|
+
declare type PluginHookWithRequest = (request: Request) => void;
|
|
11
12
|
declare type PluginHookPromise = (
|
|
12
13
|
request: Request,
|
|
13
14
|
) => Promise<unknown> | unknown;
|
|
15
|
+
declare type PluginTimeoutEarlyResponse = () => unknown;
|
|
14
16
|
export type PluginExecutionMode = () => void;
|
|
15
17
|
export declare const executionModeStandard: PluginExecutionMode;
|
|
16
18
|
export declare const executionModeDurableContext: PluginExecutionMode;
|
|
@@ -19,14 +21,14 @@ export declare const executionModeStreamifyResponse: PluginExecutionMode;
|
|
|
19
21
|
interface PluginObject {
|
|
20
22
|
internal?: Record<string, unknown>;
|
|
21
23
|
beforePrefetch?: PluginHook;
|
|
22
|
-
requestStart?:
|
|
24
|
+
requestStart?: PluginHookWithRequest;
|
|
23
25
|
beforeMiddleware?: PluginHookWithMiddlewareName;
|
|
24
26
|
afterMiddleware?: PluginHookWithMiddlewareName;
|
|
25
27
|
beforeHandler?: PluginHook;
|
|
26
|
-
timeoutEarlyInMillis?: number;
|
|
27
|
-
timeoutEarlyResponse?: PluginHook;
|
|
28
28
|
afterHandler?: PluginHook;
|
|
29
29
|
requestEnd?: PluginHookPromise;
|
|
30
|
+
timeoutEarlyInMillis?: number;
|
|
31
|
+
timeoutEarlyResponse?: PluginTimeoutEarlyResponse;
|
|
30
32
|
executionMode?: PluginExecutionMode;
|
|
31
33
|
}
|
|
32
34
|
|
|
@@ -229,4 +231,8 @@ declare namespace middy {
|
|
|
229
231
|
};
|
|
230
232
|
}
|
|
231
233
|
|
|
234
|
+
export declare function middyValidateOptions(
|
|
235
|
+
options?: Record<string, unknown>,
|
|
236
|
+
): void;
|
|
237
|
+
|
|
232
238
|
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,37 @@ const defaultPluginConfig = {
|
|
|
17
18
|
executionMode: executionModeStandard,
|
|
18
19
|
};
|
|
19
20
|
|
|
21
|
+
// JSON-Schema for `pluginConfig` passed to `middy(handler, pluginConfig)`.
|
|
22
|
+
// All options are optional; `additionalProperties: false` catches typos
|
|
23
|
+
// (e.g. `timeoutEarlyMillis` instead of `timeoutEarlyInMillis`).
|
|
24
|
+
// Properties listed in hook execution order.
|
|
25
|
+
const optionSchema = {
|
|
26
|
+
type: "object",
|
|
27
|
+
properties: {
|
|
28
|
+
// Pre-computed request state seeded into `request.internal`.
|
|
29
|
+
internal: { type: "object", additionalProperties: true },
|
|
30
|
+
// Lifecycle hooks (see docs/intro/hooks).
|
|
31
|
+
beforePrefetch: { instanceof: "Function" },
|
|
32
|
+
requestStart: { instanceof: "Function" },
|
|
33
|
+
beforeMiddleware: { instanceof: "Function" },
|
|
34
|
+
afterMiddleware: { instanceof: "Function" },
|
|
35
|
+
beforeHandler: { instanceof: "Function" },
|
|
36
|
+
afterHandler: { instanceof: "Function" },
|
|
37
|
+
requestEnd: { instanceof: "Function" },
|
|
38
|
+
// Early-timeout configuration. `timeoutEarlyInMillis` reserves N ms
|
|
39
|
+
// before Lambda timeout for `timeoutEarlyResponse` to run.
|
|
40
|
+
timeoutEarlyInMillis: { type: "integer", minimum: 0 },
|
|
41
|
+
timeoutEarlyResponse: { instanceof: "Function" },
|
|
42
|
+
// Execution mode (standard, durable-context, streamify-response, or custom).
|
|
43
|
+
executionMode: { instanceof: "Function" },
|
|
44
|
+
},
|
|
45
|
+
required: [],
|
|
46
|
+
additionalProperties: false,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const middyValidateOptions = (options) =>
|
|
50
|
+
validateOptions("@middy/core", optionSchema, options);
|
|
51
|
+
|
|
20
52
|
export const middy = (setupLambdaHandler, pluginConfig) => {
|
|
21
53
|
let lambdaHandler;
|
|
22
54
|
let plugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/core",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.3.1",
|
|
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.
|
|
88
|
+
"@middy/util": "7.3.1"
|
|
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.
|
|
101
|
+
"@datastream/core": "0.4.0",
|
|
114
102
|
"@types/aws-lambda": "^8.0.0",
|
|
115
103
|
"@types/node": "^22.0.0"
|
|
116
104
|
}
|