Skip to content

Commit

Permalink
Disable callbackWaitsForEmptyEventLoop for promise based handlers (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
RafalSumislawski authored Jul 11, 2024
1 parent 9c5a661 commit 0a29c0d
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions nodejs/packages/cx-wrapper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,35 @@ export const handler = (event: any, context: Context, callback: Callback) => {
process.env.CX_ORIGINAL_HANDLER
).then(
(originalHandler) => {
diag.debug(`Instrumenting handler`);
const patchedHandler = lambdaInstrumentation.getPatchHandler(originalHandler) as any as Handler;
diag.debug(`Running CX handler and redirecting to ${process.env.CX_ORIGINAL_HANDLER}`)
const maybePromise = patchedHandler(event, context, callback);
if (typeof maybePromise?.then === 'function') {
maybePromise.then(
value => {
callback(null, value)
},
(err: Error | string) => {
callback(err, null)
}
);
try {
diag.debug(`Instrumenting handler`);
const patchedHandler = lambdaInstrumentation.getPatchHandler(originalHandler) as any as Handler;
diag.debug(`Running CX handler and redirecting to ${process.env.CX_ORIGINAL_HANDLER}`)
const maybePromise = patchedHandler(event, context, callback);
if (typeof maybePromise?.then === 'function') {
maybePromise.then(
value => {
context.callbackWaitsForEmptyEventLoop = false;
callback(null, value);
},
(err: Error | string | null | undefined) => {
if (err === undefined || err === null) {
context.callbackWaitsForEmptyEventLoop = false;
callback('handled', null);
} else {
context.callbackWaitsForEmptyEventLoop = false;
callback(err, null);
}
}
);
}
} catch (err: any) {
context.callbackWaitsForEmptyEventLoop = false;
callback(err, null);
}
},
(err: Error | string) => {
context.callbackWaitsForEmptyEventLoop = false;
callback(err, null)
}
);
Expand Down

0 comments on commit 0a29c0d

Please sign in to comment.