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

Commit

Permalink
fix: invoke debug (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar committed Feb 17, 2020
1 parent 4976d7c commit 9a3e2b4
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 66 deletions.
21 changes: 0 additions & 21 deletions packages/serverless-invoke/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
export interface IFaaSStarter {
start(opts?: any);
handleInvokeWrapper(handlerMapping: string, isDebug?: boolean);
getApplicationContext();
getContext(ctx);
}

export type ContextHandler = object | ((ctx: object) => void);

export interface FaaSMockContext {}

export interface MatchPattern {
[matchText: string]: any;
}

export interface FaasContextMocker {
getContext(): ContextHandler;
}

export interface MidwayContainer {}

export interface InvokeOptions {
functionDir?: string; // 函数所在目录
functionName: string; // 函数名
Expand Down
46 changes: 1 addition & 45 deletions packages/serverless-invoke/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const WebSocketClient = require('websocket').client;
import { tmpdir } from 'os';
import { existsSync, writeFileSync, readFileSync, unlinkSync } from 'fs';
import { join, resolve } from 'path';
import { MidwayContainer, IFaaSStarter } from './interface';
// 传输大数据
const maxSize = 0xf00;
export function send(key, data) {
Expand Down Expand Up @@ -134,47 +133,4 @@ export const exportMidwayFaaS = (() => {
}
})();

export class FaaSStarterClass extends exportMidwayFaaS.FaaSStarter implements IFaaSStarter {
constructor(opts) {
super(opts);
}

handleInvokeWrapper(handlerMapping: string, debug?: boolean) {
const funModule = this.funMappingStore.get(handlerMapping);
return async (...args) => {
if (args.length === 0) {
throw new Error('first parameter must be function context');
}
const context: any = this.getContext(args.shift() || {});
if (funModule) {
const funModuleIns = await context.requestContext.getAsync(funModule);
return this.invokeHandler(funModuleIns, this.getFunctionHandler(context, args, funModuleIns), args, debug);
}
throw new Error(`function handler = ${handlerMapping} not found`);
};
}

async invokeHandler(funModule, handlerName, args, debug) {
handlerName = handlerName || this.defaultHandlerMethod;
if (funModule[ handlerName ]) {
// invoke real method
if (debug) {
return funModule[ handlerName ].bind(funModule, ...args);
} else {
return funModule[ handlerName ].apply(funModule, args);
}
}
}

getApplicationContext(): MidwayContainer {
return this.loader.getApplicationContext();
}

async start(opts?: any) {
return super.start(opts);
}

getContext(context) {
return super.getContext(context);
}
}
export const FaaSStarterClass = exportMidwayFaaS.FaaSStarter;

0 comments on commit 9a3e2b4

Please sign in to comment.