Skip to content

Commit

Permalink
chore: handle buffer events
Browse files Browse the repository at this point in the history
  • Loading branch information
svedova committed Feb 21, 2024
1 parent 877f727 commit f455756
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/handlers/aws.ts
Expand Up @@ -19,11 +19,14 @@ type App = (

export default (app: App): AWSHandler =>
async (event, context, callback) => {
// Alibaba sends a buffer instead of a string.
const e = Buffer.isBuffer(event) ? JSON.parse(event.toString()) : event;

// https://www.jeremydaly.com/reuse-database-connections-aws-lambda/
context.callbackWaitsForEmptyEventLoop = false;

try {
callback(null, await handleSuccess(app, event, context));
callback(null, await handleSuccess(app, e, context));
} catch (e) {
handleError(callback)(e as Error);
}
Expand Down
9 changes: 6 additions & 3 deletions src/serverless-api.ts
Expand Up @@ -24,16 +24,19 @@ export default (dirname: string): HandlerFunction => {
}
}

// Otherwise fallback to AWS.
// Otherwise fallback to default syntax.
return async (
event: RequestEvent,
event: RequestEvent | Buffer,
context: Record<string, any>,
callback: AwsCallback
) => {
context.callbackWaitsForEmptyEventLoop = false;

// Alibaba sends a buffer instead of a string.
const e = Buffer.isBuffer(event) ? JSON.parse(event.toString()) : event;

try {
callback(null, await handleApi(event, dirname));
callback(null, await handleApi(e, dirname));
} catch (e) {
handleError(callback)(e as Error);
}
Expand Down

0 comments on commit f455756

Please sign in to comment.