@middy/core
Advanced tools
@@ -16,3 +16,3 @@ // Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors. | ||
| const request = middyRequest(event, context); | ||
| plugin.requestStart?.(request); | ||
| plugin.requestStart(request); | ||
@@ -37,3 +37,3 @@ // normalize context with executionModeStandard | ||
| ); | ||
| await plugin.requestEnd?.(request); | ||
| await plugin.requestEnd(request); | ||
| return response; | ||
@@ -49,5 +49,5 @@ }); | ||
| const copyKeys = (to, from, keys) => { | ||
| keys.forEach((key) => { | ||
| to[key] = from[key]; | ||
| }); | ||
| for (let i = 0, len = keys.length; i < len; i++) { | ||
| to[keys[i]] = from[keys[i]]; | ||
| } | ||
| }; |
@@ -13,3 +13,3 @@ // Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors. | ||
| const request = middyRequest(event, context); | ||
| plugin.requestStart?.(request); | ||
| plugin.requestStart(request); | ||
@@ -24,3 +24,3 @@ const response = await runRequest( | ||
| ); | ||
| await plugin.requestEnd?.(request); | ||
| await plugin.requestEnd(request); | ||
| return response; | ||
@@ -27,0 +27,0 @@ }; |
@@ -19,3 +19,3 @@ // Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors. | ||
| const request = middyRequest(event, context); | ||
| plugin.requestStart?.(request); | ||
| plugin.requestStart(request); | ||
| const handlerResponse = await runRequest( | ||
@@ -59,3 +59,3 @@ request, | ||
| await pipeline(handlerStream, responseStream); | ||
| await plugin.requestEnd?.(request); | ||
| await plugin.requestEnd(request); | ||
| }, | ||
@@ -62,0 +62,0 @@ ); |
+10
-10
@@ -20,3 +20,3 @@ // Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors. | ||
| interface PluginObject { | ||
| internal?: any; | ||
| internal?: Record<string, unknown>; | ||
| beforePrefetch?: PluginHook; | ||
@@ -35,3 +35,3 @@ requestStart?: PluginHook; | ||
| export interface Request< | ||
| TEvent = any, | ||
| TEvent = unknown, | ||
| TResult = any, | ||
@@ -44,5 +44,5 @@ TErr = Error, | ||
| context: TContext; | ||
| response: TResult | null; | ||
| response: TResult | undefined; | ||
| earlyResponse?: TResult | null | undefined; | ||
| error: TErr | null; | ||
| error: TErr | undefined; | ||
| internal: TInternal; | ||
@@ -52,3 +52,3 @@ } | ||
| declare type MiddlewareFn< | ||
| TEvent = any, | ||
| TEvent = unknown, | ||
| TResult = any, | ||
@@ -98,3 +98,3 @@ TErr = Error, | ||
| export interface MiddyfiedHandler< | ||
| TEvent = any, | ||
| TEvent = unknown, | ||
| TResult = any, | ||
@@ -130,3 +130,3 @@ TErr = Error, | ||
| declare type AttachMiddlewareFn< | ||
| TEvent = any, | ||
| TEvent = unknown, | ||
| TResult = any, | ||
@@ -141,3 +141,3 @@ TErr = Error, | ||
| declare type AttachMiddlewareObj< | ||
| TEvent = any, | ||
| TEvent = unknown, | ||
| TResult = any, | ||
@@ -152,3 +152,3 @@ TErr = Error, | ||
| declare type UseFn< | ||
| TEvent = any, | ||
| TEvent = unknown, | ||
| TResult = any, | ||
@@ -198,3 +198,3 @@ TErr = Error, | ||
| TResult = any, | ||
| TEvent = any, | ||
| TEvent = unknown, | ||
| > = | ||
@@ -201,0 +201,0 @@ THandler extends LambdaHandler<TEvent, TResult> // always true |
+33
-21
@@ -7,2 +7,3 @@ // Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors. | ||
| const defaultLambdaHandler = () => {}; | ||
| const noop = () => {}; | ||
| const defaultPluginConfig = { | ||
@@ -33,2 +34,10 @@ timeoutEarlyInMillis: 5, | ||
| // Pre-compute single-call plugin hooks as noop to avoid optional chaining | ||
| // Note: beforeMiddleware/afterMiddleware kept as optional chaining in runMiddlewares | ||
| // because V8 optimizes ?.() null-checks faster than noop calls in tight loops | ||
| plugin.requestStart ??= noop; | ||
| plugin.requestEnd ??= noop; | ||
| plugin.beforeHandler ??= noop; | ||
| plugin.afterHandler ??= noop; | ||
| plugin.beforePrefetch?.(); | ||
@@ -100,2 +109,3 @@ const beforeMiddlewares = []; | ||
| let handlerAbort = new AbortController(); | ||
| let abortOpts = { signal: handlerAbort.signal }; | ||
| const runRequest = async ( | ||
@@ -120,4 +130,4 @@ request, | ||
| // Check if before stack hasn't exit early | ||
| if (!Object.hasOwn(request, "earlyResponse")) { | ||
| plugin.beforeHandler?.(); | ||
| if (!("earlyResponse" in request)) { | ||
| plugin.beforeHandler(); | ||
@@ -128,8 +138,4 @@ // Can't manually abort and timeout with same AbortSignal | ||
| handlerAbort = new AbortController(); | ||
| abortOpts = { signal: handlerAbort.signal }; | ||
| } | ||
| const promises = [ | ||
| lambdaHandler(request.event, request.context, { | ||
| signal: handlerAbort.signal, | ||
| }), | ||
| ]; | ||
@@ -139,2 +145,7 @@ // clearTimeout pattern is 10x faster than using AbortController | ||
| // Required --test-force-exit to ignore unresolved timeoutPromise | ||
| const handlerResult = lambdaHandler( | ||
| request.event, | ||
| request.context, | ||
| abortOpts, | ||
| ); | ||
| if (timeoutEarly) { | ||
@@ -147,4 +158,4 @@ let timeoutResolve; | ||
| resolve(plugin.timeoutEarlyResponse()); | ||
| } catch (e) { | ||
| reject(e); | ||
| } catch (err) { | ||
| reject(err); | ||
| } | ||
@@ -157,5 +168,6 @@ }; | ||
| ); | ||
| promises.push(timeoutPromise); | ||
| request.response = await Promise.race([handlerResult, timeoutPromise]); | ||
| } else { | ||
| request.response = await handlerResult; | ||
| } | ||
| request.response = await Promise.race(promises); | ||
@@ -166,6 +178,6 @@ if (timeoutID) { | ||
| plugin.afterHandler?.(); | ||
| plugin.afterHandler(); | ||
| await runMiddlewares(request, afterMiddlewares, plugin); | ||
| } | ||
| } catch (e) { | ||
| } catch (err) { | ||
| // timeout should be aborted when errors happen in handler | ||
@@ -178,9 +190,9 @@ if (timeoutID) { | ||
| request.response = undefined; | ||
| request.error = e; | ||
| request.error = err; | ||
| try { | ||
| await runMiddlewares(request, onErrorMiddlewares, plugin); | ||
| } catch (e) { | ||
| } catch (err) { | ||
| // Save error that wasn't handled | ||
| e.originalError = request.error; | ||
| request.error = e; | ||
| err.originalError = request.error; | ||
| request.error = err; | ||
@@ -196,7 +208,7 @@ throw request.error; | ||
| const runMiddlewares = async (request, middlewares, pluginConfig) => { | ||
| const runMiddlewares = async (request, middlewares, plugin) => { | ||
| for (const nextMiddleware of middlewares) { | ||
| pluginConfig.beforeMiddleware?.(nextMiddleware.name); | ||
| plugin.beforeMiddleware?.(nextMiddleware.name); | ||
| const res = await nextMiddleware(request); | ||
| pluginConfig.afterMiddleware?.(nextMiddleware.name); | ||
| plugin.afterMiddleware?.(nextMiddleware.name); | ||
| // short circuit chaining and respond early | ||
@@ -207,3 +219,3 @@ if (typeof res !== "undefined") { | ||
| // earlyResponse pattern added in 6.0.0 to handle undefined values | ||
| if (Object.hasOwn(request, "earlyResponse")) { | ||
| if ("earlyResponse" in request) { | ||
| request.response = request.earlyResponse; | ||
@@ -210,0 +222,0 @@ return; |
+5
-3
| { | ||
| "name": "@middy/core", | ||
| "version": "7.1.2", | ||
| "version": "7.1.3", | ||
| "description": "🛵 The stylish Node.js middleware engine for AWS Lambda (core package)", | ||
@@ -99,2 +99,5 @@ "type": "module", | ||
| }, | ||
| "dependencies": { | ||
| "@middy/util": "7.1.3" | ||
| }, | ||
| "peerDependencies": { | ||
@@ -114,4 +117,3 @@ "@aws/durable-execution-sdk-js": "^1.0.0" | ||
| "@types/node": "^22.0.0" | ||
| }, | ||
| "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431" | ||
| } | ||
| } |
+1
-1
@@ -45,3 +45,3 @@ <div align="center"> | ||
| For documentation and examples, refers to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org). | ||
| For documentation and examples, refer to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org). | ||
@@ -48,0 +48,0 @@ |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
24932
2.26%569
1.97%2
100%+ Added
+ Added
+ Added
- Removed