diff --git a/src/handlers/aws.ts b/src/handlers/aws.ts index 2baed23..e289c43 100644 --- a/src/handlers/aws.ts +++ b/src/handlers/aws.ts @@ -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); } diff --git a/src/serverless-api.ts b/src/serverless-api.ts index 6faac07..eef0a6c 100644 --- a/src/serverless-api.ts +++ b/src/serverless-api.ts @@ -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, 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); }