Skip to content

Commit cb0eb23

Browse files
committed
fix: buffer event parse
Signed-off-by: seven <zilisheng1996@gmail.com>
1 parent cfa3e73 commit cb0eb23

File tree

5 files changed

+10
-21
lines changed

5 files changed

+10
-21
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@geek-fun/serverless-adapter",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "Adapter for web frame work express, koa, springboot to run in serverless function as backend of apigateway cross multi cloud provider like aliyun, huawei",
55
"homepage": "https://www.geekfun.club/",
66
"main": "dist/src/index.js",

src/context.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ServerlessEvent, Context, Event } from './types';
1+
import { Context, ServerlessEvent } from './types';
22
import ServerlessRequest from './serverlessRequest';
33
import url from 'node:url';
44
import { debug } from './common';
@@ -11,7 +11,7 @@ import { debug } from './common';
1111
// };
1212

1313
const requestBody = (event: ServerlessEvent) => {
14-
if (event.body === undefined || event.body === null) {
14+
if (!event.body) {
1515
return undefined;
1616
}
1717
const type = typeof event.body;
@@ -43,9 +43,9 @@ const requestHeaders = (event: ServerlessEvent) => {
4343
}, initialHeader);
4444
};
4545

46-
export const constructFrameworkContext = (rawEvent: Event, rawContext: Context) => {
46+
export const constructFrameworkContext = (rawEvent: Buffer, rawContext: Context) => {
4747
debug(`constructFrameworkContext: ${JSON.stringify({ rawEvent, rawContext })}`);
48-
const event = JSON.parse(Buffer.from(rawEvent['data']).toString()) as ServerlessEvent;
48+
const event = JSON.parse(Buffer.from(rawEvent).toString()) as ServerlessEvent;
4949
const body = requestBody(event);
5050
const headers = requestHeaders(event);
5151

src/types.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ import { Express } from 'express';
22
import Application from 'koa';
33
import { IncomingHttpHeaders } from 'http';
44

5-
type AliyunApiGatewayEventEvent = {
6-
type: 'Buffer';
7-
data: Buffer;
8-
};
9-
105
type AliyunApiGatewayContext = {
116
requestId: string;
127
region: string;
@@ -45,7 +40,7 @@ type AliyunApiGatewayContext = {
4540
};
4641
};
4742

48-
export type Event = AliyunApiGatewayEventEvent;
43+
export type Event = Buffer;
4944
export type Context = AliyunApiGatewayContext;
5045

5146
export type ServerlessEvent = {
@@ -54,7 +49,7 @@ export type ServerlessEvent = {
5449
headers: Record<string, string>;
5550
queryParameters: Record<string, string>;
5651
pathParameters: Record<string, string>;
57-
body?: string;
52+
body: string | undefined;
5853
isBase64Encoded: boolean;
5954
};
6055

tests/fixtures/requestHelper.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,5 @@ export const sendRequest = async (
88
event: Record<string, unknown>,
99
context: Record<string, unknown>,
1010
) => {
11-
return serverlessAdapter(app)(
12-
{
13-
type: 'Buffer',
14-
data: Buffer.from(JSON.stringify(event)),
15-
},
16-
context as Context,
17-
);
11+
return serverlessAdapter(app)(Buffer.from(JSON.stringify(event)), context as Context);
1812
};

0 commit comments

Comments
 (0)