Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: complete static-layer #908

Merged
merged 3 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages-serverless/egg-layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ module.exports = engine => {
headers: context.headers,
followRedirect: false,
};
if (context.method === 'GET') {
// get 统一使用 buffer 返回,避免静态资源无法展示
requestOption.encoding = null;
}
if (
(context.headers['content-type'] || '').indexOf('application/json') >=
0
Expand Down
4 changes: 4 additions & 0 deletions packages-serverless/express-layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ module.exports = engine => {
headers: context.headers,
followRedirect: false,
};
if (context.method === 'GET') {
// get 统一使用 buffer 返回,避免静态资源无法展示
requestOption.encoding = null;
}
if (
(context.headers['content-type'] || '').indexOf('application/json') >=
0
Expand Down
4 changes: 4 additions & 0 deletions packages-serverless/koa-layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ module.exports = engine => {
headers: context.headers,
followRedirect: false,
};
if (context.method === 'GET') {
// get 统一使用 buffer 返回,避免静态资源无法展示
requestOption.encoding = null;
}
if (
(context.headers['content-type'] || '').indexOf('application/json') >=
0
Expand Down
2 changes: 1 addition & 1 deletion packages-serverless/koa-layer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@midwayjs/serverless-scf-starter": "^2.8.9",
"@midwayjs/serverless-scf-trigger": "^2.8.9",
"koa": "^2.13.0",
"koa-bodyparser": "^4.2.1",
"koa-bodyparser": "^4.3.0",
"koa-router": "^9.4.0",
"supertest": "^4.0.2"
},
Expand Down
3 changes: 3 additions & 0 deletions packages-serverless/runtime-engine/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ export interface Runtime extends RuntimeExtension {
setOptions(RuntimeOptions): void;
getFunctionName(): string;
getFunctionServiceName(): string;
getRuntimeConfig(): any;
}

export interface LightRuntime extends Runtime {
invokeHandlerWrapper(context, invokeHandler);
asyncEvent(handler: handlerWrapper): (...args) => void;
getApplication(): any;
}

export interface IServerlessLogger {
Expand Down Expand Up @@ -120,4 +122,5 @@ export interface BootstrapOptions {
runtime?: Runtime;
initContext?: any;
isAppMode?: boolean;
runtimeConfig?: any;
}
1 change: 1 addition & 0 deletions packages-serverless/runtime-engine/src/lightRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ export abstract class ServerlessLightRuntime
}

abstract asyncEvent(handler): (...args) => void;
abstract getApplication(): any;
}
4 changes: 4 additions & 0 deletions packages-serverless/runtime-engine/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ export class ServerlessBaseRuntime extends EventEmitter implements Runtime {
return !!this.options.isAppMode;
}

getRuntimeConfig() {
return this.options?.runtimeConfig || {};
}

/**
* get function name in runtime
*/
Expand Down
1 change: 1 addition & 0 deletions packages-serverless/runtime-mock/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class MockRuntime {
: new BaseBootstrap({
runtime: this.options.runtime,
layers: this.options.layers,
runtimeConfig: this.options.runtimeConfig,
});
this.engine = this.bootstrap.getRuntimeEngine();
}
Expand Down
2 changes: 1 addition & 1 deletion packages-serverless/serverless-fc-starter/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class FCRuntime extends ServerlessLightRuntime {
ctx.body = result;
}

if (!ctx.response.explicitStatus) {
if (!ctx.response._explicitStatus) {
if (ctx.body === null || ctx.body === 'undefined') {
ctx.body = '';
ctx.type = 'text';
Expand Down
7 changes: 6 additions & 1 deletion packages-serverless/serverless-fc-trigger/src/apiGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ export class ApiGatewayTrigger extends FCBaseTrigger {
body: any;
}) => {
res.set(result.headers);
res.status(result.statusCode).send(result.body);
res.status(result.statusCode);
if (result.isBase64Encoded) {
res.send(Buffer.from(result.body, 'base64'));
} else {
res.send(result.body);
}
}
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class Application extends EventEmitter {
this.on('error', this.onerror);
return (req, res, respond) => {
// if (!this.listenerCount('error')) this.on('error', this.onerror);
res.statusCode = 404;
const onerror = err => ctx.onerror(err);
const ctx = this.createContext(req, res);
return respond(ctx).catch(onerror);
Expand Down
4 changes: 4 additions & 0 deletions packages-serverless/serverless-http-parser/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export const context = {
return this.request.path;
},

set path(value) {
this.request.path = value;
},

get query() {
return this.request.query;
},
Expand Down
4 changes: 4 additions & 0 deletions packages-serverless/serverless-http-parser/src/http/req.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export class HTTPRequest {
return this[EVENT].path;
}

set path(value) {
this[EVENT].path = value;
}

get pathParameters() {
return this[EVENT].pathParameters || {};
}
Expand Down
4 changes: 4 additions & 0 deletions packages-serverless/serverless-http-parser/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export const request = {
return this.req.path;
},

set path(value) {
this.req.path = value;
},

get method() {
return this.req.method;
},
Expand Down
6 changes: 3 additions & 3 deletions packages-serverless/serverless-http-parser/src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as only from 'only';
import * as vary from 'vary';

export const response = {
explicitStatus: null,
_explicitStatus: false,
_body: null,
res: null,
ctx: null,
Expand Down Expand Up @@ -62,7 +62,7 @@ export const response = {
}

// set the status
if (!this.explicitStatus) this.status = 200;
if (!this._explicitStatus) this.status = 200;

// set the content-type only if not yet set
const setType = !this.has('Content-Type');
Expand Down Expand Up @@ -247,7 +247,7 @@ export const response = {
set status(code) {
assert(Number.isInteger(code), 'status code must be a number');
assert(code >= 100 && code <= 999, `invalid status code: ${code}`);
this.explicitStatus = true;
this._explicitStatus = true;
this.res.statusCode = code;
if (this.body && statuses.empty[code]) this.body = null;
},
Expand Down
2 changes: 1 addition & 1 deletion packages-serverless/serverless-scf-starter/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class SCFRuntime extends ServerlessLightRuntime {
ctx.body = result;
}

if (!ctx.response.explicitStatus) {
if (!ctx.response._explicitStatus) {
if (ctx.body === null || ctx.body === 'undefined') {
ctx.body = '';
ctx.type = 'text';
Expand Down
7 changes: 6 additions & 1 deletion packages-serverless/serverless-scf-trigger/src/apiGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ export class ApiGatewayTrigger extends SCFBaseTrigger {
body: any;
}) => {
res.set(result.headers);
res.status(result.statusCode).send(result.body);
res.status(result.statusCode);
if (result.isBase64Encoded) {
res.send(Buffer.from(result.body, 'base64'));
} else {
res.send(result.body);
}
}
);
});
Expand Down
Loading