This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: support dev pack * fix: support dev pack
- Loading branch information
Showing
76 changed files
with
1,946 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,6 @@ module.exports = { | |
{ | ||
locales: ['zh-cn'], | ||
}, | ||
], | ||
'@ali/ice-plugin-faas', | ||
'@ali/ice-plugin-fake-page', | ||
] | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Midway FaaS Dev | ||
|
||
本模块用于 faas 代码的本地调试,用于接入现有的前端 DevServer,提供一个统一的中间件调用。 | ||
|
||
|
||
## Usage | ||
|
||
```ts | ||
import { useExpressDevPack } from '@midwayjs/faas-dev-pack'; | ||
|
||
// dev server 代码 | ||
app = express(); | ||
|
||
// 加载中间件 | ||
app.use( | ||
useExpressDevPack({ | ||
functionDir: join(__dirname, './fixtures/ice-demo-repo'), | ||
sourceDir: 'src/apis', | ||
}) | ||
); | ||
``` | ||
|
||
## API | ||
|
||
暴露出两个中间件方法,用于上层集成。 | ||
|
||
- `useExpressDevPack(options: DevOptions)` | ||
- `useKoaDevPack(options: DevOptions)` | ||
|
||
DevOptions | ||
|
||
```ts | ||
export interface DevOptions { | ||
functionDir: string; // 本地目录,默认 process.cwd() | ||
sourceDir?: string; // 一体化调用是,需要知道当前的函数目录结构 | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"http": "@midwayjs/gateway-http" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "@midwayjs/faas-dev-pack", | ||
"version": "0.2.76", | ||
"main": "dist/index", | ||
"typings": "dist/index.d.ts", | ||
"dependencies": { | ||
"@midwayjs/serverless-invoke": "^0.2.76", | ||
"@midwayjs/gateway-core": "^0.2.76", | ||
"@midwayjs/serverless-spec-builder": "^0.2.76", | ||
"@midwayjs/gateway-http": "^0.2.76", | ||
"body-parser": "^1.19.0", | ||
"compose-middleware": "^5.0.1", | ||
"koa-bodyparser": "^4.2.1", | ||
"koa-compose": "^4.1.0" | ||
}, | ||
"devDependencies": { | ||
"@midwayjs/faas": "^0.2.76", | ||
"@types/express": "^4.17.0", | ||
"@types/koa": "^2.0.49", | ||
"express": "^4.17.1", | ||
"fs-extra": "^8.0.0", | ||
"koa": "^2.11.0", | ||
"midway-bin": "2", | ||
"supertest": "^4.0.2" | ||
}, | ||
"files": [ | ||
"dist", | ||
"src", | ||
"gateway.json" | ||
], | ||
"scripts": { | ||
"build": "midway-bin build -c", | ||
"test": "NODE_ENV=test midway-bin test --ts --full-trace", | ||
"debug": "NODE_ENV=test midway-bin test --ts --full-trace --inspect-brk=9229", | ||
"cov": "NODE_ENV=unittest midway-bin cov --ts", | ||
"clean": "midway-bin clean", | ||
"autod": "midway-bin autod" | ||
}, | ||
"license": "MIT", | ||
"publishConfig": { | ||
"registry": "http://registry.npm.alibaba-inc.com" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { transform } from '@midwayjs/serverless-spec-builder'; | ||
import { join } from 'path'; | ||
import { invoke } from '@midwayjs/serverless-invoke'; | ||
|
||
const matchTrigger = ( | ||
config: any, | ||
functionName: string, | ||
triggerType: string | ||
): boolean => { | ||
// 拿到函数名,通用 mtop 的情况 | ||
if (config.functions?.[functionName]?.events?.[0]?.[triggerType]) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
export async function getGatewayFromConfig( | ||
baseDir: string, | ||
sourceDir: string, | ||
req | ||
) { | ||
const config: any = transform(join(baseDir, 'f.yml')); | ||
const gatewayType = config?.apiGateway?.type; | ||
if (gatewayType) { | ||
// mtop 自定义网关 | ||
return gatewayType; | ||
} else { | ||
const functionName = req.query['functionName']; | ||
// 拿到函数名,通用 mtop 的情况 | ||
if (functionName && matchTrigger(config, functionName, 'mtop')) { | ||
return 'mtop'; | ||
} | ||
|
||
return 'http'; | ||
} | ||
} | ||
|
||
export function resolveModule(gatewayName: string) { | ||
const gatewayJSON = require('../gateway.json'); | ||
if (gatewayJSON[gatewayName]) { | ||
return require(gatewayJSON[gatewayName]); | ||
} else { | ||
throw new Error(`unsupport gateway type ${gatewayName}`); | ||
} | ||
} | ||
|
||
export async function invokeFunction(options) { | ||
return invoke(options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { getGatewayFromConfig, resolveModule, invokeFunction } from './common'; | ||
import { | ||
DevPackOptions, | ||
CreateExpressGateway, | ||
CreateKoaGateway, | ||
} from '@midwayjs/gateway-core'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { Context } from 'koa'; | ||
import * as koaBodyParser from 'koa-bodyparser'; | ||
import * as expressBodyParser from 'body-parser'; | ||
import * as compose from 'koa-compose'; | ||
import { compose as expressCompose } from 'compose-middleware'; | ||
|
||
export function useExpressDevPack(options: DevPackOptions) { | ||
options.functionDir = options.functionDir || process.cwd(); | ||
|
||
return expressCompose([ | ||
expressBodyParser.urlencoded({ extended: false }), | ||
expressBodyParser.json(), | ||
async (req: Request, res: Response, next: NextFunction) => { | ||
const gatewayName = await getGatewayFromConfig( | ||
options.functionDir, | ||
options.sourceDir, | ||
req | ||
); | ||
if (!gatewayName) { | ||
return next(); | ||
} | ||
const createExpressGateway: CreateExpressGateway = resolveModule( | ||
gatewayName | ||
).createExpressGateway; | ||
options.originGatewayName = gatewayName; | ||
const gateway = createExpressGateway(options); | ||
gateway.transform(req, res, next, invokeFunction); | ||
}, | ||
]); | ||
} | ||
|
||
export function useKoaDevPack(options: DevPackOptions) { | ||
options.functionDir = options.functionDir || process.cwd(); | ||
|
||
return compose([ | ||
koaBodyParser({ | ||
enableTypes: ['form', 'json'], | ||
}), | ||
async (ctx: Context, next: () => Promise<any>) => { | ||
const gatewayName = await getGatewayFromConfig( | ||
options.functionDir, | ||
options.sourceDir, | ||
ctx.request | ||
); | ||
if (!gatewayName) { | ||
await next(); | ||
} else { | ||
const createKoaGateway: CreateKoaGateway = resolveModule(gatewayName) | ||
.createKoaGateway; | ||
|
||
options.originGatewayName = gatewayName; | ||
const gateway = createKoaGateway(options); | ||
await gateway.transform(ctx, next, invokeFunction); | ||
} | ||
}, | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface InvokeParams { | ||
functionName: string; | ||
data?: any; | ||
contextData?: string; | ||
eventType?: string; | ||
verbose?: boolean; | ||
} |
Oops, something went wrong.