@midwayjs/faas 3.4.0-beta.4 → 3.4.0-beta.7

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.
@@ -33,8 +33,10 @@ let FaaSConfiguration = class FaaSConfiguration {
33
33
  async onReady(container) { }
34
34
  async onServerReady() {
35
35
  if (!this.framework.isEnable()) {
36
+ // just in legacy local dev and test
36
37
  await this.framework.run();
37
38
  }
39
+ await this.framework.loadFunction();
38
40
  }
39
41
  };
40
42
  __decorate([
@@ -11,19 +11,21 @@ export declare class MidwayFaaSFramework extends BaseFramework<Application, Cont
11
11
  app: Application;
12
12
  private isReplaceLogger;
13
13
  private developmentRun;
14
- private serverlessRoutes;
15
14
  private server;
16
15
  private respond;
17
16
  private applicationAdapter;
18
17
  private serverlessFunctionService;
19
18
  protected httpMiddlewareManager: ContextMiddlewareManager<Context, unknown, unknown>;
20
19
  protected eventMiddlewareManager: ContextMiddlewareManager<Context, unknown, unknown>;
20
+ private legacyVersion;
21
+ private loadedFunction;
21
22
  environmentService: MidwayEnvironmentService;
22
23
  middlewareService: MidwayMiddlewareService<Context, any>;
23
24
  configure(options: IFaaSConfigurationOptions): any;
24
25
  isEnable(): boolean;
25
26
  applicationInitialize(options: IMidwayBootstrapOptions): Promise<void>;
26
27
  run(): Promise<void>;
28
+ loadFunction(): Promise<void>;
27
29
  getFrameworkType(): MidwayFrameworkType;
28
30
  /**
29
31
  * @deprecated
@@ -49,5 +51,4 @@ export declare class MidwayFaaSFramework extends BaseFramework<Application, Cont
49
51
  getEventMiddleware(): ContextMiddlewareManager<Context, NextFunction, undefined>;
50
52
  getAllHandlerNames(): string[];
51
53
  }
52
- export declare const createModuleServerlessFramework: (globalOption: Omit<IMidwayBootstrapOptions, 'applicationContext'> & IFaaSConfigurationOptions) => Promise<MidwayFaaSFramework>;
53
54
  //# sourceMappingURL=framework.d.ts.map
package/dist/framework.js CHANGED
@@ -9,7 +9,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.createModuleServerlessFramework = exports.MidwayFaaSFramework = void 0;
12
+ exports.MidwayFaaSFramework = void 0;
13
13
  const core_1 = require("@midwayjs/core");
14
14
  const decorator_1 = require("@midwayjs/decorator");
15
15
  const simple_lock_1 = require("@midwayjs/simple-lock");
@@ -26,9 +26,10 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
26
26
  this.lock = new simple_lock_1.default();
27
27
  this.isReplaceLogger = process.env['MIDWAY_SERVERLESS_REPLACE_LOGGER'] === 'true';
28
28
  this.developmentRun = false;
29
- this.serverlessRoutes = [];
30
29
  this.httpMiddlewareManager = this.createMiddlewareManager();
31
30
  this.eventMiddlewareManager = this.createMiddlewareManager();
31
+ this.legacyVersion = false;
32
+ this.loadedFunction = false;
32
33
  }
33
34
  configure(options) {
34
35
  var _a;
@@ -51,6 +52,9 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
51
52
  }
52
53
  this.applicationAdapter =
53
54
  this.configurationOptions.applicationAdapter || {};
55
+ if (this.applicationAdapter.getApplication) {
56
+ this.legacyVersion = true;
57
+ }
54
58
  this.app =
55
59
  ((_b = (_a = this.applicationAdapter).getApplication) === null || _b === void 0 ? void 0 : _b.call(_a)) ||
56
60
  new serverless_http_parser_1.Application();
@@ -106,26 +110,27 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
106
110
  }
107
111
  }
108
112
  async run() {
109
- return this.lock.sureOnce(async () => {
110
- var _a;
111
- // set app keys
112
- this.app['keys'] = (_a = this.configService.getConfiguration('keys')) !== null && _a !== void 0 ? _a : '';
113
- // store all http function entry
114
- this.serverlessFunctionService = await this.applicationContext.getAsync(core_1.MidwayServerlessFunctionService);
115
- const functionList = await this.serverlessFunctionService.getFunctionList();
116
- for (const funcInfo of functionList) {
117
- // store handler
118
- this.funMappingStore.set(funcInfo.funcHandlerName, funcInfo);
119
- if (funcInfo.url) {
120
- // store router
121
- this.serverlessRoutes.push({
122
- matchPattern: (0, core_1.pathToRegexp)(funcInfo.url, [], { end: false }),
123
- funcInfo: funcInfo,
124
- });
113
+ if (this.legacyVersion) {
114
+ return this.loadFunction();
115
+ }
116
+ }
117
+ async loadFunction() {
118
+ if (!this.loadedFunction) {
119
+ this.loadedFunction = true;
120
+ return this.lock.sureOnce(async () => {
121
+ var _a;
122
+ // set app keys
123
+ this.app['keys'] = (_a = this.configService.getConfiguration('keys')) !== null && _a !== void 0 ? _a : '';
124
+ // store all http function entry
125
+ this.serverlessFunctionService = await this.applicationContext.getAsync(core_1.MidwayServerlessFunctionService);
126
+ const functionList = await this.serverlessFunctionService.getFunctionList();
127
+ for (const funcInfo of functionList) {
128
+ // store handler
129
+ this.funMappingStore.set(funcInfo.funcHandlerName, funcInfo);
125
130
  }
126
- }
127
- this.respond = this.app.callback();
128
- }, LOCK_KEY);
131
+ this.respond = this.app.callback();
132
+ }, LOCK_KEY);
133
+ }
129
134
  }
130
135
  getFrameworkType() {
131
136
  return core_1.MidwayFrameworkType.FAAS;
@@ -185,12 +190,10 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
185
190
  var _a;
186
191
  const isHttpFunction = options.isHttpFunction;
187
192
  if (!funOptions && isHttpFunction) {
188
- for (const item of this.serverlessRoutes) {
189
- if (context.method === item.funcInfo['requestMethod'].toUpperCase() &&
190
- item.matchPattern.test(context.path)) {
191
- funOptions = item.funcInfo;
192
- break;
193
- }
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'] || {};
194
197
  }
195
198
  }
196
199
  if (!funOptions) {
@@ -320,36 +323,44 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
320
323
  return context;
321
324
  }
322
325
  async invokeHandler(routerInfo, context, args, isHttpFunction) {
323
- const funModule = await context.requestContext.getAsync(routerInfo.controllerId);
324
- const handlerName = this.getFunctionHandler(context, args, funModule, routerInfo.method) ||
325
- this.defaultHandlerMethod;
326
- if (funModule[handlerName]) {
327
- // invoke real method
328
- const result = await funModule[handlerName](...args);
329
- // implement response decorator
330
- const routerResponseData = routerInfo.responseMetadata;
331
- if (isHttpFunction) {
332
- for (const routerRes of routerResponseData) {
333
- switch (routerRes.type) {
334
- case decorator_1.WEB_RESPONSE_HTTP_CODE:
335
- context.status = routerRes.code;
336
- break;
337
- case decorator_1.WEB_RESPONSE_HEADER:
338
- for (const key in (routerRes === null || routerRes === void 0 ? void 0 : routerRes.setHeaders) || {}) {
339
- context.set(key, routerRes.setHeaders[key]);
340
- }
341
- break;
342
- case decorator_1.WEB_RESPONSE_CONTENT_TYPE:
343
- context.type = routerRes.contentType;
344
- break;
345
- case decorator_1.WEB_RESPONSE_REDIRECT:
346
- context.status = routerRes.code;
347
- context.redirect(routerRes.url);
348
- return;
326
+ if (typeof routerInfo.method !== 'string') {
327
+ if (!isHttpFunction) {
328
+ args.unshift(context);
329
+ }
330
+ return routerInfo.method(...args);
331
+ }
332
+ else {
333
+ const funModule = await context.requestContext.getAsync(routerInfo.controllerId);
334
+ const handlerName = this.getFunctionHandler(context, args, funModule, routerInfo.method) ||
335
+ this.defaultHandlerMethod;
336
+ if (funModule[handlerName]) {
337
+ // invoke real method
338
+ const result = await funModule[handlerName](...args);
339
+ // implement response decorator
340
+ const routerResponseData = routerInfo.responseMetadata;
341
+ if (isHttpFunction) {
342
+ for (const routerRes of routerResponseData) {
343
+ switch (routerRes.type) {
344
+ case decorator_1.WEB_RESPONSE_HTTP_CODE:
345
+ context.status = routerRes.code;
346
+ break;
347
+ case decorator_1.WEB_RESPONSE_HEADER:
348
+ for (const key in (routerRes === null || routerRes === void 0 ? void 0 : routerRes.setHeaders) || {}) {
349
+ context.set(key, routerRes.setHeaders[key]);
350
+ }
351
+ break;
352
+ case decorator_1.WEB_RESPONSE_CONTENT_TYPE:
353
+ context.type = routerRes.contentType;
354
+ break;
355
+ case decorator_1.WEB_RESPONSE_REDIRECT:
356
+ context.status = routerRes.code;
357
+ context.redirect(routerRes.url);
358
+ return;
359
+ }
349
360
  }
350
361
  }
362
+ return result;
351
363
  }
352
- return result;
353
364
  }
354
365
  }
355
366
  getFunctionHandler(ctx, args, target, method) {
@@ -402,13 +413,4 @@ MidwayFaaSFramework = __decorate([
402
413
  (0, decorator_1.Framework)()
403
414
  ], MidwayFaaSFramework);
404
415
  exports.MidwayFaaSFramework = MidwayFaaSFramework;
405
- const createModuleServerlessFramework = async (globalOption) => {
406
- const applicationContext = await (0, core_1.initializeGlobalApplicationContext)({
407
- ...globalOption,
408
- baseDir: '',
409
- appDir: '',
410
- });
411
- return applicationContext.get(MidwayFaaSFramework);
412
- };
413
- exports.createModuleServerlessFramework = createModuleServerlessFramework;
414
416
  //# sourceMappingURL=framework.js.map
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from './interface';
2
- export { MidwayFaaSFramework as Framework, createModuleServerlessFramework, } from './framework';
2
+ export { MidwayFaaSFramework as Framework } from './framework';
3
3
  export { FaaSConfiguration as Configuration } from './configuration';
4
4
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -14,11 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.Configuration = exports.createModuleServerlessFramework = exports.Framework = void 0;
17
+ exports.Configuration = exports.Framework = void 0;
18
18
  __exportStar(require("./interface"), exports);
19
19
  var framework_1 = require("./framework");
20
20
  Object.defineProperty(exports, "Framework", { enumerable: true, get: function () { return framework_1.MidwayFaaSFramework; } });
21
- Object.defineProperty(exports, "createModuleServerlessFramework", { enumerable: true, get: function () { return framework_1.createModuleServerlessFramework; } });
22
21
  var configuration_1 = require("./configuration");
23
22
  Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.FaaSConfiguration; } });
24
23
  //# sourceMappingURL=index.js.map
@@ -47,6 +47,9 @@ export interface IFaaSConfigurationOptions extends IConfigurationOptions {
47
47
  config?: object;
48
48
  initializeContext?: object;
49
49
  applicationAdapter?: {
50
+ /**
51
+ * @deprecated
52
+ */
50
53
  getApplication(): Application;
51
54
  getFunctionName(): string;
52
55
  getFunctionServiceName(): string;
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "@midwayjs/faas",
3
- "version": "3.4.0-beta.4",
3
+ "version": "3.4.0-beta.7",
4
4
  "main": "dist/index",
5
5
  "typings": "index.d.ts",
6
6
  "dependencies": {
7
- "@midwayjs/core": "^3.4.0-beta.4",
7
+ "@midwayjs/core": "^3.4.0-beta.7",
8
8
  "@midwayjs/faas-typings": "^3.3.5",
9
9
  "@midwayjs/logger": "^2.15.0",
10
- "@midwayjs/serverless-http-parser": "^3.4.0-beta.4",
10
+ "@midwayjs/serverless-http-parser": "^3.4.0-beta.7",
11
11
  "@midwayjs/simple-lock": "^1.1.4"
12
12
  },
13
13
  "devDependencies": {
14
- "@midwayjs/decorator": "^3.4.0-beta.4",
15
- "@midwayjs/mock": "^3.4.0-beta.4",
16
- "@midwayjs/serverless-fc-starter": "^3.4.0-beta.4",
17
- "@midwayjs/serverless-scf-starter": "^3.4.0-beta.4",
14
+ "@midwayjs/decorator": "^3.4.0-beta.7",
15
+ "@midwayjs/mock": "^3.4.0-beta.7",
16
+ "@midwayjs/serverless-fc-starter": "^3.4.0-beta.7",
17
+ "@midwayjs/serverless-scf-starter": "^3.4.0-beta.7",
18
18
  "mm": "3.2.0"
19
19
  },
20
20
  "engines": {
@@ -46,5 +46,5 @@
46
46
  "url": "git@github.com:midwayjs/midway.git"
47
47
  },
48
48
  "license": "MIT",
49
- "gitHead": "3b686101f7802f87d47b8c9d25561339d556e573"
49
+ "gitHead": "4d5cc59a7a33e49beeaf20fcfaf766438649959c"
50
50
  }