@middy/core
Version:
🛵 The stylish Node.js middleware engine for AWS Lambda (core package)
67 lines (62 loc) • 1.73 kB
JavaScript
// Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors.
// SPDX-License-Identifier: MIT
import { withDurableExecution } from "@aws/durable-execution-sdk-js";
import { executionContextKeys, lambdaContextKeys } from "@middy/util";
export const executionModeDurableContext = (
{ middyRequest, runRequest },
beforeMiddlewares,
lambdaHandler,
afterMiddlewares,
onErrorMiddlewares,
plugin,
) => {
const middy = withDurableExecution(async (event, context) => {
const request = middyRequest(event, context);
plugin.requestStart(request);
// normalize context with executionModeStandard
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-context.html
// Idea: Use Proxy instead of copying. Faster for common use case?
copyKeys(
request.context,
request.context.executionContext,
executionContextKeys,
);
copyKeys(request.context, request.context.lambdaContext, lambdaContextKeys);
// See executionModeStandard for the .cause-chaining rationale.
let handlerError;
let response;
try {
response = await runRequest(
request,
beforeMiddlewares,
lambdaHandler,
afterMiddlewares,
onErrorMiddlewares,
plugin,
);
} catch (err) {
handlerError = err;
}
try {
await plugin.requestEnd(request);
} catch (hookErr) {
if (handlerError) {
handlerError.cause ??= hookErr;
} else {
throw hookErr;
}
}
if (handlerError) throw handlerError;
return response;
});
middy.handler = (replaceLambdaHandler) => {
lambdaHandler = replaceLambdaHandler;
return middy;
};
return middy;
};
const copyKeys = (to, from, keys) => {
for (let i = 0, len = keys.length; i < len; i++) {
to[keys[i]] = from[keys[i]];
}
};