Skip to content

Commit

Permalink
fix: register faas ctx (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar committed Jul 26, 2020
1 parent 106bfac commit d9e1764
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/faas-cli-plugin-invoke/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export class FaaSInvokePlugin extends BasePlugin {
return;
}
this.skipTsBuild = false;
// 是否使用ts模式进行运行
const isTsMode = checkIsTsMode();
if (!isTsMode) {
process.env.MIDWAY_TS_MODE = 'false';
Expand Down Expand Up @@ -343,6 +344,7 @@ export class FaaSInvokePlugin extends BasePlugin {
}

async setFunctionList() {
// 这里是必须的,用以其他插件动态修改 functions,比如 hooks
this.setStore('functions', this.core.service.functions);
}

Expand Down
12 changes: 10 additions & 2 deletions packages/faas-cli/bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
'use strict';
const { debugWrapper } = require('@midwayjs/debugger');
const { existsSync, remove } = require('fs-extra');
const { resolve } = require('path');
const cliFun = async argv => {
require('source-map-support/register');
const { CLI } = require('../dist');
const buildDirectionPath = resolve(process.cwd(), '.faas_debug_tmp');
if (existsSync(buildDirectionPath)) {
await remove(buildDirectionPath);
}

const cli = new CLI(argv);
cli
.start()
.then(() => {
process.exit();
})
.catch(e => {
process.exit();
.catch(() => {
process.exitCode = 1;
process.exit(1);
});
};

Expand Down
1 change: 1 addition & 0 deletions packages/faas-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@midwayjs/fcli-plugin-test": "^1.1.5",
"@midwayjs/serverless-spec-builder": "^1.1.5",
"enquirer": "^2.3.4",
"fs-extra": "^8.1.0",
"light-spinner": "^1.0.0",
"minimist": "^1.2.5",
"source-map-support": "^0.5.19"
Expand Down
4 changes: 2 additions & 2 deletions packages/faas-dev-pack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"body-parser": "^1.19.0",
"compose-middleware": "^5.0.1",
"koa-bodyparser": "^4.2.1",
"koa-compose": "^4.1.0"
"koa-compose": "^4.1.0",
"fs-extra": "^8.1.0"
},
"devDependencies": {
"@midwayjs/faas": "^1.1.4",
"@types/express": "^4.17.0",
"@types/koa": "^2.0.49",
"express": "^4.17.1",
"fs-extra": "^8.1.0",
"koa": "^2.11.0",
"midway-bin": "^2.0.0",
"supertest": "^4.0.2"
Expand Down
12 changes: 11 additions & 1 deletion packages/faas-dev-pack/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { invoke } from '@midwayjs/serverless-invoke';

import { resolve } from 'path';
import { existsSync, removeSync } from 'fs-extra';
export function resolveModule(gatewayName: string) {
const gatewayJSON = require('../gateway.json');
if (gatewayJSON[gatewayName]) {
Expand All @@ -10,5 +11,14 @@ export function resolveModule(gatewayName: string) {
}

export async function invokeFunction(options) {
if (!process.env.MIDWAY_DEV_PACK_RUNNING) {
const baseDir = options.functionDir || process.cwd();
const buildDir = resolve(baseDir, '.faas_debug_tmp');
if (existsSync(buildDir)) {
removeSync(buildDir);
}
process.env.MIDWAY_DEV_PACK_RUNNING = 'true';
}
options.incremental = options.incremental ?? true;
return invoke(options);
}
1 change: 0 additions & 1 deletion packages/serverless-spec-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"ejs": "^3.1.3",
"js-yaml": "^3.13.0",
"json-cycle": "^1.3.0",
"lodash.get": "^4.4.2",
"mkdirp": "^0.5.1"
},
"devDependencies": {
Expand Down
14 changes: 11 additions & 3 deletions packages/serverless-spec-builder/registerFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const {
saveProviderId,
FUNC_KEY,
attachClassMetadata,
savePropertyInject,
} = require('@midwayjs/decorator');
const { join } = require('path');
const { existsSync } = require('fs');
const get = require('lodash.get');

const registerFunctionToIoc = (container, functionName, func) => {
class FunctionContainer {
Expand All @@ -19,14 +19,22 @@ const registerFunctionToIoc = (container, functionName, func) => {
/**
* HTTP Case
*/
const args = get(this.ctx, 'request.body.args', []);
const args =
(this.ctx &&
this.ctx.request &&
this.ctx.request.body &&
this.ctx.request.args) ||
[];

return func.bind(bindCtx)(...args);
}
}

const id = 'bind_func::' + functionName;

savePropertyInject({
target: FunctionContainer.prototype,
targetKey: 'ctx',
});
saveProviderId(id, FunctionContainer, true);
container.bind(id, FunctionContainer);
saveModule(FUNC_KEY, FunctionContainer);
Expand Down

0 comments on commit d9e1764

Please sign in to comment.