@midwayjs/faas 3.4.0-beta.2 → 3.4.0-beta.5

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([
@@ -15,30 +15,25 @@ export declare class MidwayFaaSFramework extends BaseFramework<Application, Cont
15
15
  private server;
16
16
  private respond;
17
17
  private applicationAdapter;
18
- private webRouterService;
18
+ private serverlessFunctionService;
19
19
  protected httpMiddlewareManager: ContextMiddlewareManager<Context, unknown, unknown>;
20
20
  protected eventMiddlewareManager: ContextMiddlewareManager<Context, unknown, unknown>;
21
+ private legacyVersion;
22
+ private loadedFunction;
21
23
  environmentService: MidwayEnvironmentService;
22
24
  middlewareService: MidwayMiddlewareService<Context, any>;
23
25
  configure(options: IFaaSConfigurationOptions): any;
24
26
  isEnable(): boolean;
25
27
  applicationInitialize(options: IMidwayBootstrapOptions): Promise<void>;
26
28
  run(): Promise<void>;
29
+ loadFunction(): Promise<void>;
27
30
  getFrameworkType(): MidwayFrameworkType;
28
31
  /**
29
32
  * @deprecated
30
33
  * @param handlerMapping
31
34
  */
32
35
  handleInvokeWrapper(handlerMapping: string): (...args: any[]) => Promise<any>;
33
- getTriggerFunction(handlerMapping: string): (context: any, options: HandlerOptions) => Promise<{
34
- result: any;
35
- error: Error;
36
- } | {
37
- isBase64Encoded: boolean;
38
- statusCode: any;
39
- headers: any;
40
- body: any;
41
- }>;
36
+ getTriggerFunction(handlerMapping: string): (context: any, options: HandlerOptions) => Promise<any>;
42
37
  wrapHttpRequest(req: http.IncomingMessage, res?: http.ServerResponse): Promise<unknown>;
43
38
  /**
44
39
  * @deprecated
@@ -57,5 +52,4 @@ export declare class MidwayFaaSFramework extends BaseFramework<Application, Cont
57
52
  getEventMiddleware(): ContextMiddlewareManager<Context, NextFunction, undefined>;
58
53
  getAllHandlerNames(): string[];
59
54
  }
60
- export declare const createModuleServerlessFramework: (globalOption: Omit<IMidwayBootstrapOptions, 'applicationContext'> & IFaaSConfigurationOptions) => Promise<MidwayFaaSFramework>;
61
55
  //# sourceMappingURL=framework.d.ts.map
package/dist/framework.js CHANGED
@@ -9,12 +9,14 @@ 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");
16
16
  const logger_1 = require("@midwayjs/logger");
17
17
  const serverless_http_parser_1 = require("@midwayjs/serverless-http-parser");
18
+ const util_1 = require("util");
19
+ const { isAnyArrayBuffer, isUint8Array } = util_1.types;
18
20
  const LOCK_KEY = '_faas_starter_start_key';
19
21
  let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework {
20
22
  constructor() {
@@ -27,6 +29,8 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
27
29
  this.serverlessRoutes = [];
28
30
  this.httpMiddlewareManager = this.createMiddlewareManager();
29
31
  this.eventMiddlewareManager = this.createMiddlewareManager();
32
+ this.legacyVersion = false;
33
+ this.loadedFunction = false;
30
34
  }
31
35
  configure(options) {
32
36
  var _a;
@@ -49,6 +53,9 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
49
53
  }
50
54
  this.applicationAdapter =
51
55
  this.configurationOptions.applicationAdapter || {};
56
+ if (this.applicationAdapter.getApplication) {
57
+ this.legacyVersion = true;
58
+ }
52
59
  this.app =
53
60
  ((_b = (_a = this.applicationAdapter).getApplication) === null || _b === void 0 ? void 0 : _b.call(_a)) ||
54
61
  new serverless_http_parser_1.Application();
@@ -104,30 +111,34 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
104
111
  }
105
112
  }
106
113
  async run() {
107
- return this.lock.sureOnce(async () => {
108
- var _a;
109
- // set app keys
110
- this.app['keys'] = (_a = this.configService.getConfiguration('keys')) !== null && _a !== void 0 ? _a : '';
111
- // store all http function entry
112
- this.webRouterService = await this.applicationContext.getAsync(core_1.MidwayWebRouterService, [
113
- {
114
- includeFunctionRouter: true,
115
- },
116
- ]);
117
- const functionList = await this.webRouterService.getFlattenRouterTable();
118
- for (const funcInfo of functionList) {
119
- // store handler
120
- this.funMappingStore.set(funcInfo.funcHandlerName, funcInfo);
121
- if (funcInfo.url) {
122
- // store router
123
- this.serverlessRoutes.push({
124
- matchPattern: (0, core_1.pathToRegexp)(funcInfo.url, [], { end: false }),
125
- funcInfo: funcInfo,
126
- });
114
+ if (this.legacyVersion) {
115
+ return this.loadFunction();
116
+ }
117
+ }
118
+ async loadFunction() {
119
+ if (!this.loadedFunction) {
120
+ this.loadedFunction = true;
121
+ return this.lock.sureOnce(async () => {
122
+ var _a;
123
+ // set app keys
124
+ this.app['keys'] = (_a = this.configService.getConfiguration('keys')) !== null && _a !== void 0 ? _a : '';
125
+ // store all http function entry
126
+ this.serverlessFunctionService = await this.applicationContext.getAsync(core_1.MidwayServerlessFunctionService);
127
+ const functionList = await this.serverlessFunctionService.getFunctionList();
128
+ for (const funcInfo of functionList) {
129
+ // store handler
130
+ this.funMappingStore.set(funcInfo.funcHandlerName, funcInfo);
131
+ if (funcInfo.url) {
132
+ // store router
133
+ this.serverlessRoutes.push({
134
+ matchPattern: (0, core_1.pathToRegexp)(funcInfo.url, [], { end: false }),
135
+ funcInfo: funcInfo,
136
+ });
137
+ }
127
138
  }
128
- }
129
- this.respond = this.app.callback();
130
- }, LOCK_KEY);
139
+ this.respond = this.app.callback();
140
+ }, LOCK_KEY);
141
+ }
131
142
  }
132
143
  getFrameworkType() {
133
144
  return core_1.MidwayFrameworkType.FAAS;
@@ -249,13 +260,13 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
249
260
  }
250
261
  context.body = data;
251
262
  }
252
- else if (Buffer.isBuffer(data)) {
263
+ else if (isAnyArrayBuffer(data) || isUint8Array(data)) {
253
264
  encoded = true;
254
265
  if (!context.type) {
255
266
  context.type = 'application/octet-stream';
256
267
  }
257
268
  // data is reserved as buffer
258
- context.body = data.toString('base64');
269
+ context.body = Buffer.from(data).toString('base64');
259
270
  }
260
271
  else if (typeof data === 'object') {
261
272
  if (!context.type) {
@@ -322,36 +333,44 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
322
333
  return context;
323
334
  }
324
335
  async invokeHandler(routerInfo, context, args, isHttpFunction) {
325
- const funModule = await context.requestContext.getAsync(routerInfo.controllerId);
326
- const handlerName = this.getFunctionHandler(context, args, funModule, routerInfo.method) ||
327
- this.defaultHandlerMethod;
328
- if (funModule[handlerName]) {
329
- // invoke real method
330
- const result = await funModule[handlerName](...args);
331
- // implement response decorator
332
- const routerResponseData = routerInfo.responseMetadata;
333
- if (isHttpFunction) {
334
- for (const routerRes of routerResponseData) {
335
- switch (routerRes.type) {
336
- case decorator_1.WEB_RESPONSE_HTTP_CODE:
337
- context.status = routerRes.code;
338
- break;
339
- case decorator_1.WEB_RESPONSE_HEADER:
340
- for (const key in (routerRes === null || routerRes === void 0 ? void 0 : routerRes.setHeaders) || {}) {
341
- context.set(key, routerRes.setHeaders[key]);
342
- }
343
- break;
344
- case decorator_1.WEB_RESPONSE_CONTENT_TYPE:
345
- context.type = routerRes.contentType;
346
- break;
347
- case decorator_1.WEB_RESPONSE_REDIRECT:
348
- context.status = routerRes.code;
349
- context.redirect(routerRes.url);
350
- return;
336
+ if (typeof routerInfo.method !== 'string') {
337
+ if (!isHttpFunction) {
338
+ args.unshift(context);
339
+ }
340
+ return routerInfo.method(...args);
341
+ }
342
+ else {
343
+ const funModule = await context.requestContext.getAsync(routerInfo.controllerId);
344
+ const handlerName = this.getFunctionHandler(context, args, funModule, routerInfo.method) ||
345
+ this.defaultHandlerMethod;
346
+ if (funModule[handlerName]) {
347
+ // invoke real method
348
+ const result = await funModule[handlerName](...args);
349
+ // implement response decorator
350
+ const routerResponseData = routerInfo.responseMetadata;
351
+ if (isHttpFunction) {
352
+ for (const routerRes of routerResponseData) {
353
+ switch (routerRes.type) {
354
+ case decorator_1.WEB_RESPONSE_HTTP_CODE:
355
+ context.status = routerRes.code;
356
+ break;
357
+ case decorator_1.WEB_RESPONSE_HEADER:
358
+ for (const key in (routerRes === null || routerRes === void 0 ? void 0 : routerRes.setHeaders) || {}) {
359
+ context.set(key, routerRes.setHeaders[key]);
360
+ }
361
+ break;
362
+ case decorator_1.WEB_RESPONSE_CONTENT_TYPE:
363
+ context.type = routerRes.contentType;
364
+ break;
365
+ case decorator_1.WEB_RESPONSE_REDIRECT:
366
+ context.status = routerRes.code;
367
+ context.redirect(routerRes.url);
368
+ return;
369
+ }
351
370
  }
352
371
  }
372
+ return result;
353
373
  }
354
- return result;
355
374
  }
356
375
  }
357
376
  getFunctionHandler(ctx, args, target, method) {
@@ -404,13 +423,4 @@ MidwayFaaSFramework = __decorate([
404
423
  (0, decorator_1.Framework)()
405
424
  ], MidwayFaaSFramework);
406
425
  exports.MidwayFaaSFramework = MidwayFaaSFramework;
407
- const createModuleServerlessFramework = async (globalOption) => {
408
- const applicationContext = await (0, core_1.initializeGlobalApplicationContext)({
409
- ...globalOption,
410
- baseDir: '',
411
- appDir: '',
412
- });
413
- return applicationContext.get(MidwayFaaSFramework);
414
- };
415
- exports.createModuleServerlessFramework = createModuleServerlessFramework;
416
426
  //# 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;
@@ -61,6 +64,7 @@ export interface IWebMiddleware {
61
64
  }
62
65
  export interface ServerlessStarterOptions extends IMidwayBootstrapOptions {
63
66
  initializeMethodName?: string;
67
+ handlerName?: string;
64
68
  createAdapter?: () => Promise<{
65
69
  close(): any;
66
70
  createAppHook(app?: any): any;
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "@midwayjs/faas",
3
- "version": "3.4.0-beta.2",
3
+ "version": "3.4.0-beta.5",
4
4
  "main": "dist/index",
5
5
  "typings": "index.d.ts",
6
6
  "dependencies": {
7
- "@midwayjs/core": "^3.4.0-beta.2",
7
+ "@midwayjs/core": "^3.4.0-beta.5",
8
8
  "@midwayjs/faas-typings": "^3.3.5",
9
9
  "@midwayjs/logger": "^2.15.0",
10
- "@midwayjs/serverless-http-parser": "^3.4.0-beta.2",
10
+ "@midwayjs/serverless-http-parser": "^3.4.0-beta.5",
11
11
  "@midwayjs/simple-lock": "^1.1.4"
12
12
  },
13
13
  "devDependencies": {
14
- "@midwayjs/decorator": "^3.4.0-beta.2",
15
- "@midwayjs/mock": "^3.4.0-beta.2",
16
- "@midwayjs/serverless-fc-starter": "^3.4.0-beta.2",
17
- "@midwayjs/serverless-scf-starter": "^3.4.0-beta.2",
14
+ "@midwayjs/decorator": "^3.4.0-beta.5",
15
+ "@midwayjs/mock": "^3.4.0-beta.5",
16
+ "@midwayjs/serverless-fc-starter": "^3.4.0-beta.5",
17
+ "@midwayjs/serverless-scf-starter": "^3.4.0-beta.5",
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": "a61721d3946b30fd4cac183265edcd99b31ac72b"
49
+ "gitHead": "d84d1c77aed1fd973d002ab65cd0adeadb7924a6"
50
50
  }