Skip to content

Commit

Permalink
feat: accepts buffer event
Browse files Browse the repository at this point in the history
Signed-off-by: seven <zilisheng1996@gmail.com>
  • Loading branch information
Blankll committed Dec 28, 2024
1 parent 6b0382e commit cfa3e73
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 42 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@geek-fun/serverless-adapter",
"version": "0.1.2",
"version": "0.1.3",
"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",
"homepage": "https://www.geekfun.club/",
"main": "dist/src/index.js",
Expand Down
11 changes: 6 additions & 5 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Event } from './types';
import { ServerlessEvent, Context, Event } from './types';
import ServerlessRequest from './serverlessRequest';
import url from 'node:url';
import { debug } from './common';
Expand All @@ -10,7 +10,7 @@ import { debug } from './common';
// return event.requestContext.identity.sourceIp;
// };

const requestBody = (event: Event) => {
const requestBody = (event: ServerlessEvent) => {
if (event.body === undefined || event.body === null) {
return undefined;
}
Expand All @@ -27,7 +27,7 @@ const requestBody = (event: Event) => {
throw new Error(`Unexpected event.body type: ${typeof event.body}`);
};

const requestHeaders = (event: Event) => {
const requestHeaders = (event: ServerlessEvent) => {
const initialHeader = {} as Record<string, string>;

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

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

Expand Down
23 changes: 14 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import { Express } from 'express';
import Application from 'koa';
import { IncomingHttpHeaders } from 'http';

type AliyunApiGatewayEvent = {
path: string;
httpMethod: string;
headers: Record<string, string>;
queryParameters: Record<string, string>;
pathParameters: Record<string, string>;
body?: string;
isBase64Encoded: boolean;
type AliyunApiGatewayEventEvent = {
type: 'Buffer';
data: Buffer;
};

type AliyunApiGatewayContext = {
Expand Down Expand Up @@ -50,9 +45,19 @@ type AliyunApiGatewayContext = {
};
};

export type Event = AliyunApiGatewayEvent;
export type Event = AliyunApiGatewayEventEvent;
export type Context = AliyunApiGatewayContext;

export type ServerlessEvent = {
path: string;
httpMethod: string;
headers: Record<string, string>;
queryParameters: Record<string, string>;
pathParameters: Record<string, string>;
body?: string;
isBase64Encoded: boolean;
};

export type ServerlessAdapter = (app: Express | Application) => (
event: Event,
context: Context,
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/fcContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context, Event } from '../../src/types';
import { Context, ServerlessEvent } from '../../src/types';

export const defaultEvent: Event = {
export const defaultEvent: ServerlessEvent = {
path: '/api/test',
httpMethod: 'GET',
headers: {
Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/requestHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Express } from 'express';
import Application from 'koa';
import serverlessAdapter from '../../src';
import { Context } from '../../src/types';

export const sendRequest = async (
app: Express | Application,
event: Record<string, unknown>,
context: Record<string, unknown>,
) => {
return serverlessAdapter(app)(
{
type: 'Buffer',
data: Buffer.from(JSON.stringify(event)),
},
context as Context,
);
};
27 changes: 15 additions & 12 deletions tests/index-express.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express, { Express, Request, Response } from 'express';
import bodyParser from 'body-parser';
import serverlessAdapter from '../src';
import { defaultContext, defaultEvent } from './fixtures/fcContext';
import { sendRequest } from './fixtures/requestHelper';

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

const response = await serverlessAdapter(app)(defaultEvent, defaultContext);
const response = await sendRequest(app, defaultEvent, defaultContext);
expect(response.statusCode).toEqual(418);
expect(response.body).toEqual(`I'm a teapot`);
});
Expand All @@ -25,7 +25,8 @@ describe('express', () => {
app.use((req: Request, res: Response) => {
res.status(200).send(req.body);
});
const response = await serverlessAdapter(app)(
const response = await sendRequest(
app,
{
...defaultEvent,
httpMethod: 'GET',
Expand All @@ -47,7 +48,8 @@ describe('express', () => {
res.status(200).send(req.body.hello);
});

const response = await serverlessAdapter(app)(
const response = await sendRequest(
app,
{
...defaultEvent,
httpMethod: 'GET',
Expand All @@ -71,7 +73,8 @@ describe('express', () => {
res.status(200).send(req.body.hello);
});

const response = await serverlessAdapter(app)(
const response = await sendRequest(
app,
{
...defaultEvent,
httpMethod: 'GET',
Expand All @@ -92,7 +95,8 @@ describe('express', () => {
res.status(200).send(req.query.foo as string);
});

const response = await serverlessAdapter(app)(
const response = await sendRequest(
app,
{
...defaultEvent,
httpMethod: 'GET',
Expand All @@ -115,10 +119,7 @@ describe('express', () => {
res.status(201).send('bar');
});

const response = await serverlessAdapter(app)(
{ ...defaultEvent, httpMethod: 'PUT' },
defaultContext,
);
const response = await sendRequest(app, { ...defaultEvent, httpMethod: 'PUT' }, defaultContext);

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

const response = await serverlessAdapter(app)(
const response = await sendRequest(
app,
{
...defaultEvent,
httpMethod: 'GET',
Expand All @@ -146,7 +148,8 @@ describe('express', () => {
res.json({ test: 'test' });
});

const response = await serverlessAdapter(app)(
const response = await sendRequest(
app,
{
...defaultEvent,
httpMethod: 'GET',
Expand Down
24 changes: 13 additions & 11 deletions tests/index-koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import Koa from 'koa';
import Router from '@koa/router';
import koaBody from 'koa-body';
import serve from 'koa-static';
import serverlessAdapter from '../src';
import { defaultContext, defaultEvent } from './fixtures/fcContext';
import { sendRequest } from './fixtures/requestHelper';

describe('koa', () => {
let app: Koa;
Expand All @@ -21,7 +21,7 @@ describe('koa', () => {
});
app.use(router.routes());

const response = await serverlessAdapter(app)(defaultEvent, defaultContext);
const response = await sendRequest(app, defaultEvent, defaultContext);

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

const response = await serverlessAdapter(app)(
const response = await sendRequest(
app,
{
...defaultEvent,
httpMethod: 'POST',
Expand All @@ -58,7 +59,8 @@ describe('koa', () => {
});
app.use(router.routes());

const response = await serverlessAdapter(app)(
const response = await sendRequest(
app,
{
...defaultEvent,
httpMethod: 'POST',
Expand All @@ -80,7 +82,8 @@ describe('koa', () => {
});
app.use(router.routes());

const response = await serverlessAdapter(app)(
const response = await sendRequest(
app,
{
...defaultEvent,
httpMethod: 'POST',
Expand All @@ -103,7 +106,8 @@ describe('koa', () => {
});
app.use(router.routes());

const response = await serverlessAdapter(app)(
const response = await sendRequest(
app,
{
...defaultEvent,
httpMethod: 'GET',
Expand All @@ -128,10 +132,7 @@ describe('koa', () => {
});
app.use(router.routes());

const response = await serverlessAdapter(app)(
{ ...defaultEvent, httpMethod: 'PUT' },
defaultContext,
);
const response = await sendRequest(app, { ...defaultEvent, httpMethod: 'PUT' }, defaultContext);

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

const response = await serverlessAdapter(app)(
const response = await sendRequest(
app,
{
...defaultEvent,
httpMethod: 'GET',
Expand Down

0 comments on commit cfa3e73

Please sign in to comment.