Skip to content

Commit

Permalink
fix: support async entry for koa/express (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Aug 26, 2020
1 parent e462d77 commit 19bb466
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
6 changes: 4 additions & 2 deletions packages/express-layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ module.exports = engine => {
engine.addRuntimeExtension({
async beforeRuntimeStart(runtime) {
const baseDir = runtime.getPropertyParser().getEntryDir();
const app = require(join(baseDir, 'app'));
// handleRequest = koaApp.callback();
let app = require(join(baseDir, 'app'));
if (typeof app === 'function' && !app['emit']) {
app = await app();
}
if (fs.existsSync(socketPath)) {
fs.unlinkSync(socketPath);
}
Expand Down
14 changes: 8 additions & 6 deletions packages/express-layer/test/fixtures/app-fc/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ const express = require('express');
const app = express();
const bodyParser = require('body-parser');

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/get', (req, res) => {
res.type('html')
res.type('html');
res.send('Hello World');
});

app.get('/get/query', (req, res) => {
res.send({
query: req.query
query: req.query,
});
});

Expand All @@ -22,10 +22,12 @@ app.post('/post', (req, res) => {

app.post('/post/body', (req, res) => {
res.send({
body: req.body
body: req.body,
});
});

// app.listen(3000);

module.exports = app;
module.exports = async () => {
return app;
};
5 changes: 4 additions & 1 deletion packages/koa-layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ module.exports = engine => {
engine.addRuntimeExtension({
async beforeRuntimeStart(runtime) {
const baseDir = runtime.getPropertyParser().getEntryDir();
const app = require(join(baseDir, 'app'));
let app = require(join(baseDir, 'app'));
if (typeof app === 'function') {
app = await app();
}
// handleRequest = koaApp.callback();
if (fs.existsSync(socketPath)) {
fs.unlinkSync(socketPath);
Expand Down
8 changes: 5 additions & 3 deletions packages/koa-layer/test/fixtures/app-fc/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ router.get('/get', (ctx, next) => {

router.get('/get/query', (ctx, next) => {
ctx.body = {
query: ctx.query
query: ctx.query,
};
});

Expand All @@ -22,7 +22,7 @@ router.post('/post', (ctx, next) => {

router.post('/post/body', (ctx, next) => {
ctx.body = {
body: ctx.request.body
body: ctx.request.body,
};
});

Expand All @@ -31,4 +31,6 @@ app.use(router.routes()).use(router.allowedMethods());

// app.listen(3000);

module.exports = app;
module.exports = async () => {
return app;
};
3 changes: 2 additions & 1 deletion packages/runtime-engine/src/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export enum EnvPropertyKey {
EAGLEEYE_FLAG = 'EAGLE_FLAG', // layer
}

export class EnvPropertyParser<T> extends Map<string, T>
export class EnvPropertyParser<T>
extends Map<string, T>
implements PropertyParser<T> {
setProperty(propertyKey: string, value) {
process.env[propertyKey] = value;
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-engine/src/lightRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { LightRuntime } from './interface';
* A lightweight runtime implementation for a platform like aliyun fc that does not provide a custom runtime
* So you can wrapper invoke handler use asyncEvent method
*/
export abstract class ServerlessLightRuntime extends ServerlessBaseRuntime
export abstract class ServerlessLightRuntime
extends ServerlessBaseRuntime
implements LightRuntime {
async invokeHandlerWrapper(context, invokeHandler) {
// load context extension
Expand Down

0 comments on commit 19bb466

Please sign in to comment.