Skip to content

Commit

Permalink
Fix legacy issue with context.done callbacks (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarcyRaynerDD authored Oct 21, 2020
1 parent 17550be commit c646c70
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/utils/handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,67 @@ describe("wrap", () => {
expect(result).toEqual({ statusCode: 204, body: "The callback response" });
expect(calledOriginalHandler).toBeFalsy();
});

it("completes when calling context.done", async () => {
const handler: Handler = async (event, context, callback) => {
context.done(undefined, { statusCode: 204, body: "The callback response" });
};

let calledOriginalHandler = false;

const wrappedHandler = wrap(
handler,
async () => {},
async () => {},
);

const result = await wrappedHandler({}, mockContext, () => {
calledOriginalHandler = true;
});

expect(result).toEqual({ statusCode: 204, body: "The callback response" });
expect(calledOriginalHandler).toBeFalsy();
});
it("completes when calling context.succeed", async () => {
const handler: Handler = async (event, context, callback) => {
context.succeed({ statusCode: 204, body: "The callback response" });
};

let calledOriginalHandler = false;

const wrappedHandler = wrap(
handler,
async () => {},
async () => {},
);

const result = await wrappedHandler({}, mockContext, () => {
calledOriginalHandler = true;
});

expect(result).toEqual({ statusCode: 204, body: "The callback response" });
expect(calledOriginalHandler).toBeFalsy();
});

it("throws error when calling context.fail", async () => {
const handler: Handler = async (event, context, callback) => {
context.fail(new Error("Some error"));
};

let calledOriginalHandler = false;

const wrappedHandler = wrap(
handler,
async () => {},
async () => {},
);

const result = wrappedHandler({}, mockContext, () => {
calledOriginalHandler = true;
});

await expect(result).rejects.toEqual(new Error("Some error"));

expect(calledOriginalHandler).toBeFalsy();
});
});
24 changes: 24 additions & 0 deletions src/utils/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export function promisifiedHandler<TEvent, TResult>(handler: Handler<TEvent, TRe
// 2. Returning a value directly from the function using a promise.

let modifiedCallback: Callback<TResult> = () => {};
let modifiedLegacyDoneCallback: Callback<TResult> = () => {};
let modifiedLegacySucceedCallback: (res: any) => void = () => {};
let modifiedLegacyFailCallback: (err: any) => void = () => {};

const callbackProm = new Promise<TResult>((resolve, reject) => {
modifiedCallback = (err, result) => {
Expand All @@ -76,7 +79,28 @@ export function promisifiedHandler<TEvent, TResult>(handler: Handler<TEvent, TRe
resolve(result);
}
};
// Legacy done callback finished immediately, and doesn't wait for pending
// event loop
modifiedLegacyDoneCallback = (err, result) => {
context.callbackWaitsForEmptyEventLoop = false;
if (err !== undefined && err !== null) {
reject(err);
} else {
resolve(result);
}
};
modifiedLegacySucceedCallback = (result) => {
context.callbackWaitsForEmptyEventLoop = false;
resolve(result);
};
modifiedLegacyFailCallback = (err: any) => {
context.callbackWaitsForEmptyEventLoop = false;
reject(err);
};
});
context.done = modifiedLegacyDoneCallback;
context.succeed = modifiedLegacySucceedCallback;
context.fail = modifiedLegacyFailCallback;

const asyncProm = handler(event, context, modifiedCallback) as Promise<TResult> | undefined;
let promise: Promise<TResult> = callbackProm;
Expand Down

0 comments on commit c646c70

Please sign in to comment.