@midwayjs/faas 3.5.3 → 3.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
- import { MidwayFaaSFramework } from './framework';
2
1
  import { MidwayDecoratorService } from '@midwayjs/core';
2
+ import { MidwayFaaSFramework } from './framework';
3
3
  export declare class FaaSConfiguration {
4
4
  framework: MidwayFaaSFramework;
5
5
  decoratorService: MidwayDecoratorService;
@@ -10,24 +10,24 @@ var __metadata = (this && this.__metadata) || function (k, v) {
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.FaaSConfiguration = void 0;
13
- const decorator_1 = require("@midwayjs/decorator");
14
- const framework_1 = require("./framework");
15
13
  const core_1 = require("@midwayjs/core");
14
+ const framework_1 = require("./framework");
16
15
  const DefaultConfig = require("./config.default");
17
16
  let FaaSConfiguration = class FaaSConfiguration {
18
17
  async init() {
19
- this.decoratorService.registerPropertyHandler(decorator_1.PLUGIN_KEY, (key, meta, target) => {
18
+ this.decoratorService.registerPropertyHandler(core_1.PLUGIN_KEY, (key, meta, target) => {
20
19
  var _a;
21
20
  return (((_a = target === null || target === void 0 ? void 0 : target[core_1.REQUEST_OBJ_CTX_KEY]) === null || _a === void 0 ? void 0 : _a[key]) ||
22
21
  this.framework.getApplication()[key]);
23
22
  });
24
- this.decoratorService.registerPropertyHandler(decorator_1.LOGGER_KEY, (key, meta, target) => {
23
+ this.decoratorService.registerPropertyHandler(core_1.LOGGER_KEY, (key, meta, target) => {
25
24
  var _a;
26
25
  return (((_a = target === null || target === void 0 ? void 0 : target[core_1.REQUEST_OBJ_CTX_KEY]) === null || _a === void 0 ? void 0 : _a['logger']) ||
27
26
  this.framework.getLogger());
28
27
  });
29
- this.decoratorService.registerParameterHandler(decorator_1.WEB_ROUTER_PARAM_KEY, options => {
30
- return (0, core_1.extractKoaLikeValue)(options.metadata.type, options.metadata.propertyData, options.originParamType)(options.originArgs[0], options.originArgs[1]);
28
+ this.decoratorService.registerParameterHandler(core_1.WEB_ROUTER_PARAM_KEY, options => {
29
+ var _a;
30
+ return (0, core_1.extractKoaLikeValue)(options.metadata.type, options.metadata.propertyData, options.originParamType, (_a = options.metadata) === null || _a === void 0 ? void 0 : _a.pipes)(options.originArgs[0], options.originArgs[1]);
31
31
  });
32
32
  }
33
33
  async onReady(container) { }
@@ -40,25 +40,25 @@ let FaaSConfiguration = class FaaSConfiguration {
40
40
  }
41
41
  };
42
42
  __decorate([
43
- (0, decorator_1.Inject)(),
43
+ (0, core_1.Inject)(),
44
44
  __metadata("design:type", framework_1.MidwayFaaSFramework)
45
45
  ], FaaSConfiguration.prototype, "framework", void 0);
46
46
  __decorate([
47
- (0, decorator_1.Inject)(),
47
+ (0, core_1.Inject)(),
48
48
  __metadata("design:type", core_1.MidwayDecoratorService)
49
49
  ], FaaSConfiguration.prototype, "decoratorService", void 0);
50
50
  __decorate([
51
- (0, decorator_1.ApplicationContext)(),
51
+ (0, core_1.ApplicationContext)(),
52
52
  __metadata("design:type", Object)
53
53
  ], FaaSConfiguration.prototype, "applicationContext", void 0);
54
54
  __decorate([
55
- (0, decorator_1.Init)(),
55
+ (0, core_1.Init)(),
56
56
  __metadata("design:type", Function),
57
57
  __metadata("design:paramtypes", []),
58
58
  __metadata("design:returntype", Promise)
59
59
  ], FaaSConfiguration.prototype, "init", null);
60
60
  FaaSConfiguration = __decorate([
61
- (0, decorator_1.Configuration)({
61
+ (0, core_1.Configuration)({
62
62
  namespace: 'faas',
63
63
  importConfigs: [
64
64
  {
@@ -32,7 +32,7 @@ export declare class MidwayFaaSFramework extends BaseFramework<Application, Cont
32
32
  * @param handlerMapping
33
33
  */
34
34
  handleInvokeWrapper(handlerMapping: string): (...args: any[]) => Promise<any>;
35
- getTriggerFunction(handlerMapping: string): (context: any, options: HandlerOptions) => Promise<any>;
35
+ getTriggerFunction(context: any, handlerMapping: string, options: HandlerOptions): Promise<any>;
36
36
  wrapHttpRequest(req: http.IncomingMessage | Record<string, any>, res?: http.ServerResponse): Promise<unknown>;
37
37
  /**
38
38
  * @deprecated
@@ -44,7 +44,7 @@ export declare class MidwayFaaSFramework extends BaseFramework<Application, Cont
44
44
  protected getFunctionHandler(ctx: any, args: any, target: any, method: any): string;
45
45
  createLogger(name: string, option?: LoggerOptions): import("@midwayjs/core").ILogger;
46
46
  getFrameworkName(): string;
47
- getServer(): http.Server;
47
+ getServer(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
48
48
  beforeStop(): Promise<void>;
49
49
  protected createHttpContext(req: any, res: any): Promise<unknown>;
50
50
  useMiddleware(middleware: CommonMiddlewareUnion<Context, NextFunction, undefined>): void;
package/dist/framework.js CHANGED
@@ -11,7 +11,6 @@ var __metadata = (this && this.__metadata) || function (k, v) {
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MidwayFaaSFramework = void 0;
13
13
  const core_1 = require("@midwayjs/core");
14
- const decorator_1 = require("@midwayjs/decorator");
15
14
  const simple_lock_1 = require("@midwayjs/simple-lock");
16
15
  const logger_1 = require("@midwayjs/logger");
17
16
  const serverless_http_parser_1 = require("@midwayjs/serverless-http-parser");
@@ -98,8 +97,8 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
98
97
  .createAnonymousContext()
99
98
  .requestContext.getAsync(serviceClass);
100
99
  },
101
- getTriggerFunction: (handlerMapping) => {
102
- return this.getTriggerFunction(handlerMapping);
100
+ getTriggerFunction: (context, handlerMapping, options) => {
101
+ return this.getTriggerFunction(context, handlerMapping, options);
103
102
  },
104
103
  });
105
104
  // hack use method
@@ -161,6 +160,15 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
161
160
  ...funOptions.controllerMiddleware,
162
161
  ...funOptions.middleware,
163
162
  async (ctx, next) => {
163
+ if (funOptions.controllerClz &&
164
+ typeof funOptions.method === 'string') {
165
+ const isPassed = await this.app
166
+ .getFramework()
167
+ .runGuard(ctx, funOptions.controllerClz, funOptions.method);
168
+ if (!isPassed) {
169
+ throw new core_1.httpError.ForbiddenError();
170
+ }
171
+ }
164
172
  if (isHttpFunction) {
165
173
  args = [ctx];
166
174
  }
@@ -184,115 +192,113 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
184
192
  return await composeMiddleware(context);
185
193
  };
186
194
  }
187
- getTriggerFunction(handlerMapping) {
195
+ async getTriggerFunction(context, handlerMapping, options) {
196
+ var _a, _b;
188
197
  let funOptions = this.funMappingStore.get(handlerMapping);
189
- return async (context, options) => {
190
- var _a, _b;
191
- const isHttpFunction = options.isHttpFunction;
192
- if (!funOptions && isHttpFunction) {
193
- funOptions = await this.serverlessFunctionService.getMatchedRouterInfo(context.path, context.method);
194
- if (funOptions) {
195
- const matchRes = core_1.PathToRegexpUtil.match(funOptions.fullUrlFlattenString)(context.path);
196
- context.req.pathParameters = matchRes['params'] || {};
197
- }
198
- }
199
- if (!funOptions) {
200
- throw new Error(`function handler = ${handlerMapping} not found`);
201
- }
202
- context = this.getContext(context);
203
- if ((_a = this.configurationOptions.applicationAdapter) === null || _a === void 0 ? void 0 : _a.runContextHook) {
204
- this.configurationOptions.applicationAdapter.runContextHook(context);
198
+ const isHttpFunction = options.isHttpFunction;
199
+ if (!funOptions && isHttpFunction) {
200
+ funOptions = await this.serverlessFunctionService.getMatchedRouterInfo(context.path, context.method);
201
+ if (funOptions) {
202
+ const matchRes = core_1.PathToRegexpUtil.match(funOptions.fullUrlFlattenString)(context.path);
203
+ context.req.pathParameters = matchRes['params'] || {};
205
204
  }
206
- const result = await (await this.applyMiddleware(async (ctx, next) => {
207
- const fn = await this.middlewareService.compose([
208
- ...(isHttpFunction
209
- ? this.httpMiddlewareManager
210
- : this.eventMiddlewareManager),
211
- ...funOptions.controllerMiddleware,
212
- ...funOptions.middleware,
213
- async (ctx, next) => {
214
- let args;
215
- if (isHttpFunction) {
216
- args = [ctx];
205
+ }
206
+ if (!funOptions) {
207
+ throw new Error(`function handler = ${handlerMapping} not found`);
208
+ }
209
+ context = this.getContext(context);
210
+ if ((_a = this.configurationOptions.applicationAdapter) === null || _a === void 0 ? void 0 : _a.runContextHook) {
211
+ this.configurationOptions.applicationAdapter.runContextHook(context);
212
+ }
213
+ const result = await (await this.applyMiddleware(async (ctx, next) => {
214
+ const fn = await this.middlewareService.compose([
215
+ ...(isHttpFunction
216
+ ? this.httpMiddlewareManager
217
+ : this.eventMiddlewareManager),
218
+ ...funOptions.controllerMiddleware,
219
+ ...funOptions.middleware,
220
+ async (ctx, next) => {
221
+ let args;
222
+ if (isHttpFunction) {
223
+ args = [ctx];
224
+ }
225
+ else {
226
+ args = [options.originEvent, options.originContext];
227
+ }
228
+ // invoke handler
229
+ const result = await this.invokeHandler(funOptions, ctx, args, isHttpFunction);
230
+ if (isHttpFunction && result !== undefined) {
231
+ if (result === null) {
232
+ // 这样设置可以绕过 koa 的 _explicitStatus 赋值机制
233
+ ctx.response._body = null;
217
234
  }
218
235
  else {
219
- args = [options.originEvent, options.originContext];
236
+ ctx.body = result;
220
237
  }
221
- // invoke handler
222
- const result = await this.invokeHandler(funOptions, ctx, args, isHttpFunction);
223
- if (isHttpFunction && result !== undefined) {
224
- if (result === null) {
225
- // 这样设置可以绕过 koa 的 _explicitStatus 赋值机制
226
- ctx.response._body = null;
227
- }
228
- else {
229
- ctx.body = result;
230
- }
231
- }
232
- // http 靠 ctx.body,否则会出现状态码不正确的问题
233
- if (!isHttpFunction) {
234
- return result;
235
- }
236
- },
237
- ], this.app);
238
- return await fn(ctx, next);
239
- }))(context);
240
- if (isHttpFunction) {
241
- if (!((_b = context.response) === null || _b === void 0 ? void 0 : _b._explicitStatus)) {
242
- if (context.body === null || context.body === 'undefined') {
243
- context.body = '';
244
- context.type = 'text';
245
- context.status = 204;
246
- }
247
- }
248
- let encoded = false;
249
- let data = context.body;
250
- if (typeof data === 'string') {
251
- if (!context.type) {
252
- context.type = 'text/plain';
253
238
  }
254
- context.body = data;
255
- }
256
- else if (isAnyArrayBuffer(data) || isUint8Array(data)) {
257
- encoded = true;
258
- if (!context.type) {
259
- context.type = 'application/octet-stream';
239
+ // http 靠 ctx.body,否则会出现状态码不正确的问题
240
+ if (!isHttpFunction) {
241
+ return result;
260
242
  }
261
- // data is reserved as buffer
262
- context.body = Buffer.from(data).toString('base64');
243
+ },
244
+ ], this.app);
245
+ return await fn(ctx, next);
246
+ }))(context);
247
+ if (isHttpFunction) {
248
+ if (!((_b = context.response) === null || _b === void 0 ? void 0 : _b._explicitStatus)) {
249
+ if (context.body === null || context.body === 'undefined') {
250
+ context.body = '';
251
+ context.type = 'text';
252
+ context.status = 204;
263
253
  }
264
- else if (typeof data === 'object') {
265
- if (!context.type) {
266
- context.type = 'application/json';
267
- }
268
- // set data to string
269
- context.body = data = JSON.stringify(data);
254
+ }
255
+ let encoded = false;
256
+ const data = context.body;
257
+ if (typeof data === 'string') {
258
+ if (!context.type) {
259
+ context.type = 'text/plain';
270
260
  }
271
- else {
272
- if (!context.type) {
273
- context.type = 'text/plain';
274
- }
275
- // set data to string
276
- context.body = data = data + '';
261
+ context.body = data;
262
+ }
263
+ else if (isAnyArrayBuffer(data) || isUint8Array(data)) {
264
+ encoded = true;
265
+ if (!context.type) {
266
+ context.type = 'application/octet-stream';
277
267
  }
278
- // middleware return value and will be got 204 status
279
- if (context.body === undefined &&
280
- !context.response._explicitStatus &&
281
- context._matchedRoute) {
282
- // 如果进了路由,重新赋值,防止 404
283
- context.body = undefined;
268
+ // data is reserved as buffer
269
+ context.body = Buffer.from(data).toString('base64');
270
+ }
271
+ else if (typeof data === 'object') {
272
+ if (!context.type) {
273
+ context.type = 'application/json';
284
274
  }
285
- return {
286
- isBase64Encoded: encoded,
287
- statusCode: context.status,
288
- headers: context.res.headers,
289
- body: context.body,
290
- };
275
+ // set data to string
276
+ context.body = JSON.stringify(data);
291
277
  }
292
278
  else {
293
- return result;
279
+ if (!context.type) {
280
+ context.type = 'text/plain';
281
+ }
282
+ // set data to string
283
+ context.body = data + '';
294
284
  }
295
- };
285
+ // middleware return value and will be got 204 status
286
+ if (context.body === undefined &&
287
+ !context.response._explicitStatus &&
288
+ context._matchedRoute) {
289
+ // 如果进了路由,重新赋值,防止 404
290
+ context.body = undefined;
291
+ }
292
+ return {
293
+ isBase64Encoded: encoded,
294
+ statusCode: context.status,
295
+ headers: context.res.headers,
296
+ body: context.body,
297
+ };
298
+ }
299
+ else {
300
+ return result;
301
+ }
296
302
  }
297
303
  async wrapHttpRequest(req, res) {
298
304
  const newReq = res ? new serverless_http_parser_1.HTTPRequest(req, res) : req;
@@ -344,18 +350,18 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
344
350
  if (isHttpFunction) {
345
351
  for (const routerRes of routerResponseData) {
346
352
  switch (routerRes.type) {
347
- case decorator_1.WEB_RESPONSE_HTTP_CODE:
353
+ case core_1.WEB_RESPONSE_HTTP_CODE:
348
354
  context.status = routerRes.code;
349
355
  break;
350
- case decorator_1.WEB_RESPONSE_HEADER:
356
+ case core_1.WEB_RESPONSE_HEADER:
351
357
  for (const key in (routerRes === null || routerRes === void 0 ? void 0 : routerRes.setHeaders) || {}) {
352
358
  context.set(key, routerRes.setHeaders[key]);
353
359
  }
354
360
  break;
355
- case decorator_1.WEB_RESPONSE_CONTENT_TYPE:
361
+ case core_1.WEB_RESPONSE_CONTENT_TYPE:
356
362
  context.type = routerRes.contentType;
357
363
  break;
358
- case decorator_1.WEB_RESPONSE_REDIRECT:
364
+ case core_1.WEB_RESPONSE_REDIRECT:
359
365
  context.status = routerRes.code;
360
366
  context.redirect(routerRes.url);
361
367
  return;
@@ -412,15 +418,15 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
412
418
  }
413
419
  };
414
420
  __decorate([
415
- (0, decorator_1.Inject)(),
421
+ (0, core_1.Inject)(),
416
422
  __metadata("design:type", core_1.MidwayEnvironmentService)
417
423
  ], MidwayFaaSFramework.prototype, "environmentService", void 0);
418
424
  __decorate([
419
- (0, decorator_1.Inject)(),
425
+ (0, core_1.Inject)(),
420
426
  __metadata("design:type", core_1.MidwayMiddlewareService)
421
427
  ], MidwayFaaSFramework.prototype, "middlewareService", void 0);
422
428
  MidwayFaaSFramework = __decorate([
423
- (0, decorator_1.Framework)()
429
+ (0, core_1.Framework)()
424
430
  ], MidwayFaaSFramework);
425
431
  exports.MidwayFaaSFramework = MidwayFaaSFramework;
426
432
  //# sourceMappingURL=framework.js.map
@@ -34,8 +34,8 @@ export declare type IMidwayFaaSApplication = IMidwayApplication<Context, {
34
34
  */
35
35
  getFunctionServiceName(): string;
36
36
  useEventMiddleware(middleware: CommonMiddlewareUnion<Context, NextFunction, undefined>): void;
37
- getEventMiddleware: ContextMiddlewareManager<Context, NextFunction, undefined>;
38
- getTriggerFunction(handler: string): (context: any, options: HandlerOptions) => Promise<any>;
37
+ getEventMiddleware(): ContextMiddlewareManager<Context, NextFunction, undefined>;
38
+ getTriggerFunction(context: any, handler: string, options: HandlerOptions): Promise<any>;
39
39
  getServerlessInstance<T>(serviceClass: T): Promise<T>;
40
40
  }> & ServerlessHttpApplication;
41
41
  export interface Application extends IMidwayFaaSApplication {
package/package.json CHANGED
@@ -1,20 +1,19 @@
1
1
  {
2
2
  "name": "@midwayjs/faas",
3
- "version": "3.5.3",
3
+ "version": "3.7.0",
4
4
  "main": "dist/index",
5
5
  "typings": "index.d.ts",
6
6
  "dependencies": {
7
- "@midwayjs/core": "^3.5.3",
8
- "@midwayjs/faas-typings": "^3.5.0",
7
+ "@midwayjs/core": "^3.7.0",
8
+ "@midwayjs/faas-typings": "^3.6.0",
9
9
  "@midwayjs/logger": "^2.15.0",
10
- "@midwayjs/serverless-http-parser": "^3.5.3",
10
+ "@midwayjs/serverless-http-parser": "^3.6.0",
11
11
  "@midwayjs/simple-lock": "^1.1.4"
12
12
  },
13
13
  "devDependencies": {
14
- "@midwayjs/decorator": "^3.4.11",
15
- "@midwayjs/mock": "^3.5.3",
16
- "@midwayjs/serverless-fc-starter": "^3.5.3",
17
- "@midwayjs/serverless-scf-starter": "^3.5.3",
14
+ "@midwayjs/mock": "^3.7.0",
15
+ "@midwayjs/serverless-fc-starter": "^3.7.0",
16
+ "@midwayjs/serverless-scf-starter": "^3.7.0",
18
17
  "mm": "3.2.0"
19
18
  },
20
19
  "engines": {
@@ -46,5 +45,5 @@
46
45
  "url": "git@github.com:midwayjs/midway.git"
47
46
  },
48
47
  "license": "MIT",
49
- "gitHead": "32356484664846984f6d3d65a3a75dea015e8dcc"
48
+ "gitHead": "99386083ee26b386fd508b9c892091c914e77535"
50
49
  }