Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
fix: add origin context for normal event (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Apr 28, 2020
1 parent 72b758a commit 9d5b353
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
19 changes: 11 additions & 8 deletions packages/serverless-fc-starter/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import * as getRawBody from 'raw-body';
import {
FAAS_ARGS_KEY,
ServerlessLightRuntime,
} from '@midwayjs/runtime-engine';
import { FAAS_ARGS_KEY, ServerlessLightRuntime, } from '@midwayjs/runtime-engine';
import { Context } from '@midwayjs/serverless-http-parser';
import * as util from 'util';

Expand All @@ -27,23 +24,24 @@ export class FCRuntime extends ServerlessLightRuntime {
}

async wrapperWebInvoker(handler, req, res, context) {
let ctx: Context;
let ctx: Context & { logger?: any };
const args = [];
// for web
const isHTTPMode =
req.constructor.name === 'EventEmitter' || util.types.isProxy(req); // for local test
if (isHTTPMode) {
// http
const rawBody = await getRawBody(req);
// const rawBody = 'test';
// req.rawBody = rawBody;
req.body = rawBody; // TODO: body parser
req.body = await getRawBody(req); // TODO: body parser
ctx = new Context(req, context);
ctx.logger = context.logger || console;
// ctx.EventType = 'fc_http';
args.push(ctx);
} else {
// api gateway
ctx = new Context(req, context);
ctx.logger = context.logger || console;
// ctx.EventType = 'fc_apigw';
args.push(ctx);
// Pass original event
Expand Down Expand Up @@ -156,8 +154,13 @@ export class FCRuntime extends ServerlessLightRuntime {
}
args.push(event);
}
// format context
const newCtx = {
logger: context.logger || console,
originContext: context,
};
// 其他事件场景
return this.invokeHandlerWrapper(context, async () => {
return this.invokeHandlerWrapper(newCtx, async () => {
return handler.apply(handler, args);
});
}
Expand Down
4 changes: 4 additions & 0 deletions packages/serverless-fc-starter/test/fixtures/apigw/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { asyncWrapper, start } from '../../../src';
import { deepStrictEqual } from 'assert';

let runtime;
let inited;
Expand All @@ -9,6 +10,9 @@ exports.handler = asyncWrapper(async (...args) => {
runtime = await start();
}
return runtime.asyncEvent(async function (ctx, event) {
deepStrictEqual(ctx.logger, console);
deepStrictEqual(event.path, '/test');
deepStrictEqual(event.httpMethod, 'POST');
ctx.status = 200;
ctx.body = {
headers: ctx.headers,
Expand Down
11 changes: 8 additions & 3 deletions packages/serverless-scf-starter/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export class SCFRuntime extends ServerlessLightRuntime {
event: SCF.APIGatewayEvent,
context: SCF.RequestContext
) {
const ctx = new Context(event, context);
const ctx: Context & { logger?: any } = new Context(event, context);
ctx.logger = console;
const args = [ctx, event];

const result = await this.invokeHandlerWrapper(context, async () => {
Expand Down Expand Up @@ -72,8 +73,12 @@ export class SCFRuntime extends ServerlessLightRuntime {
}

async wrapperEventInvoker(handler, event: any, context: SCF.RequestContext) {
const ctx = new Context({}, context);
const args = [ctx, event];
// format context
const newCtx = {
logger: console,
originContext: context,
};
const args = [newCtx, event];
// 其他事件场景
return this.invokeHandlerWrapper(context, async () => {
if (!handler) {
Expand Down

0 comments on commit 9d5b353

Please sign in to comment.