Skip to content

Commit cfa3e73

Browse files
committed
feat: accepts buffer event
Signed-off-by: seven <[email protected]>
1 parent 6b0382e commit cfa3e73

File tree

8 files changed

+71
-42
lines changed

8 files changed

+71
-42
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.2",
3+
"version": "0.1.3",
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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Context, Event } from './types';
1+
import { ServerlessEvent, Context, Event } from './types';
22
import ServerlessRequest from './serverlessRequest';
33
import url from 'node:url';
44
import { debug } from './common';
@@ -10,7 +10,7 @@ import { debug } from './common';
1010
// return event.requestContext.identity.sourceIp;
1111
// };
1212

13-
const requestBody = (event: Event) => {
13+
const requestBody = (event: ServerlessEvent) => {
1414
if (event.body === undefined || event.body === null) {
1515
return undefined;
1616
}
@@ -27,7 +27,7 @@ const requestBody = (event: Event) => {
2727
throw new Error(`Unexpected event.body type: ${typeof event.body}`);
2828
};
2929

30-
const requestHeaders = (event: Event) => {
30+
const requestHeaders = (event: ServerlessEvent) => {
3131
const initialHeader = {} as Record<string, string>;
3232

3333
// if (event.multiValueHeaders) {
@@ -43,8 +43,9 @@ const requestHeaders = (event: Event) => {
4343
}, initialHeader);
4444
};
4545

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

src/types.ts

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

5-
type AliyunApiGatewayEvent = {
6-
path: string;
7-
httpMethod: string;
8-
headers: Record<string, string>;
9-
queryParameters: Record<string, string>;
10-
pathParameters: Record<string, string>;
11-
body?: string;
12-
isBase64Encoded: boolean;
5+
type AliyunApiGatewayEventEvent = {
6+
type: 'Buffer';
7+
data: Buffer;
138
};
149

1510
type AliyunApiGatewayContext = {
@@ -50,9 +45,19 @@ type AliyunApiGatewayContext = {
5045
};
5146
};
5247

53-
export type Event = AliyunApiGatewayEvent;
48+
export type Event = AliyunApiGatewayEventEvent;
5449
export type Context = AliyunApiGatewayContext;
5550

51+
export type ServerlessEvent = {
52+
path: string;
53+
httpMethod: string;
54+
headers: Record<string, string>;
55+
queryParameters: Record<string, string>;
56+
pathParameters: Record<string, string>;
57+
body?: string;
58+
isBase64Encoded: boolean;
59+
};
60+
5661
export type ServerlessAdapter = (app: Express | Application) => (
5762
event: Event,
5863
context: Context,

tests/fixtures/fcContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Context, Event } from '../../src/types';
1+
import { Context, ServerlessEvent } from '../../src/types';
22

3-
export const defaultEvent: Event = {
3+
export const defaultEvent: ServerlessEvent = {
44
path: '/api/test',
55
httpMethod: 'GET',
66
headers: {

tests/fixtures/requestHelper.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Express } from 'express';
2+
import Application from 'koa';
3+
import serverlessAdapter from '../../src';
4+
import { Context } from '../../src/types';
5+
6+
export const sendRequest = async (
7+
app: Express | Application,
8+
event: Record<string, unknown>,
9+
context: Record<string, unknown>,
10+
) => {
11+
return serverlessAdapter(app)(
12+
{
13+
type: 'Buffer',
14+
data: Buffer.from(JSON.stringify(event)),
15+
},
16+
context as Context,
17+
);
18+
};

tests/index-express.test.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import express, { Express, Request, Response } from 'express';
22
import bodyParser from 'body-parser';
3-
import serverlessAdapter from '../src';
43
import { defaultContext, defaultEvent } from './fixtures/fcContext';
4+
import { sendRequest } from './fixtures/requestHelper';
55

66
describe('express', () => {
77
let app: Express;
@@ -15,7 +15,7 @@ describe('express', () => {
1515
res.status(418).send(`I'm a teapot`);
1616
});
1717

18-
const response = await serverlessAdapter(app)(defaultEvent, defaultContext);
18+
const response = await sendRequest(app, defaultEvent, defaultContext);
1919
expect(response.statusCode).toEqual(418);
2020
expect(response.body).toEqual(`I'm a teapot`);
2121
});
@@ -25,7 +25,8 @@ describe('express', () => {
2525
app.use((req: Request, res: Response) => {
2626
res.status(200).send(req.body);
2727
});
28-
const response = await serverlessAdapter(app)(
28+
const response = await sendRequest(
29+
app,
2930
{
3031
...defaultEvent,
3132
httpMethod: 'GET',
@@ -47,7 +48,8 @@ describe('express', () => {
4748
res.status(200).send(req.body.hello);
4849
});
4950

50-
const response = await serverlessAdapter(app)(
51+
const response = await sendRequest(
52+
app,
5153
{
5254
...defaultEvent,
5355
httpMethod: 'GET',
@@ -71,7 +73,8 @@ describe('express', () => {
7173
res.status(200).send(req.body.hello);
7274
});
7375

74-
const response = await serverlessAdapter(app)(
76+
const response = await sendRequest(
77+
app,
7578
{
7679
...defaultEvent,
7780
httpMethod: 'GET',
@@ -92,7 +95,8 @@ describe('express', () => {
9295
res.status(200).send(req.query.foo as string);
9396
});
9497

95-
const response = await serverlessAdapter(app)(
98+
const response = await sendRequest(
99+
app,
96100
{
97101
...defaultEvent,
98102
httpMethod: 'GET',
@@ -115,10 +119,7 @@ describe('express', () => {
115119
res.status(201).send('bar');
116120
});
117121

118-
const response = await serverlessAdapter(app)(
119-
{ ...defaultEvent, httpMethod: 'PUT' },
120-
defaultContext,
121-
);
122+
const response = await sendRequest(app, { ...defaultEvent, httpMethod: 'PUT' }, defaultContext);
122123

123124
expect(response.statusCode).toEqual(201);
124125
expect(response.body).toEqual('bar');
@@ -127,7 +128,8 @@ describe('express', () => {
127128
it('should serve files', async () => {
128129
app.use(express.static('tests/fixtures'));
129130

130-
const response = await serverlessAdapter(app)(
131+
const response = await sendRequest(
132+
app,
131133
{
132134
...defaultEvent,
133135
httpMethod: 'GET',
@@ -146,7 +148,8 @@ describe('express', () => {
146148
res.json({ test: 'test' });
147149
});
148150

149-
const response = await serverlessAdapter(app)(
151+
const response = await sendRequest(
152+
app,
150153
{
151154
...defaultEvent,
152155
httpMethod: 'GET',

tests/index-koa.test.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import Koa from 'koa';
22
import Router from '@koa/router';
33
import koaBody from 'koa-body';
44
import serve from 'koa-static';
5-
import serverlessAdapter from '../src';
65
import { defaultContext, defaultEvent } from './fixtures/fcContext';
6+
import { sendRequest } from './fixtures/requestHelper';
77

88
describe('koa', () => {
99
let app: Koa;
@@ -21,7 +21,7 @@ describe('koa', () => {
2121
});
2222
app.use(router.routes());
2323

24-
const response = await serverlessAdapter(app)(defaultEvent, defaultContext);
24+
const response = await sendRequest(app, defaultEvent, defaultContext);
2525

2626
expect(response.statusCode).toEqual(418);
2727
expect(response.body).toEqual('Hello, world koa!');
@@ -34,7 +34,8 @@ describe('koa', () => {
3434
});
3535
app.use(router.routes());
3636

37-
const response = await serverlessAdapter(app)(
37+
const response = await sendRequest(
38+
app,
3839
{
3940
...defaultEvent,
4041
httpMethod: 'POST',
@@ -58,7 +59,8 @@ describe('koa', () => {
5859
});
5960
app.use(router.routes());
6061

61-
const response = await serverlessAdapter(app)(
62+
const response = await sendRequest(
63+
app,
6264
{
6365
...defaultEvent,
6466
httpMethod: 'POST',
@@ -80,7 +82,8 @@ describe('koa', () => {
8082
});
8183
app.use(router.routes());
8284

83-
const response = await serverlessAdapter(app)(
85+
const response = await sendRequest(
86+
app,
8487
{
8588
...defaultEvent,
8689
httpMethod: 'POST',
@@ -103,7 +106,8 @@ describe('koa', () => {
103106
});
104107
app.use(router.routes());
105108

106-
const response = await serverlessAdapter(app)(
109+
const response = await sendRequest(
110+
app,
107111
{
108112
...defaultEvent,
109113
httpMethod: 'GET',
@@ -128,10 +132,7 @@ describe('koa', () => {
128132
});
129133
app.use(router.routes());
130134

131-
const response = await serverlessAdapter(app)(
132-
{ ...defaultEvent, httpMethod: 'PUT' },
133-
defaultContext,
134-
);
135+
const response = await sendRequest(app, { ...defaultEvent, httpMethod: 'PUT' }, defaultContext);
135136

136137
expect(response.statusCode).toEqual(201);
137138
expect(response.body).toEqual('bar');
@@ -140,7 +141,8 @@ describe('koa', () => {
140141
it('should serve files', async () => {
141142
app.use(serve('tests/fixtures'));
142143

143-
const response = await serverlessAdapter(app)(
144+
const response = await sendRequest(
145+
app,
144146
{
145147
...defaultEvent,
146148
httpMethod: 'GET',

0 commit comments

Comments
 (0)