@midwayjs/faas 3.3.6-beta.2 → 3.3.6-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.
@@ -1,34 +1,43 @@
1
1
  /// <reference types="node" />
2
- import { FaaSContext, IFaaSConfigurationOptions, IMidwayFaaSApplication, InvokeOptions } from './interface';
3
- import { BaseFramework, ContextMiddlewareManager, FunctionMiddleware, IMidwayBootstrapOptions, MidwayEnvironmentService, MidwayFrameworkType, MidwayMiddlewareService, RouterInfo } from '@midwayjs/core';
2
+ import { Context, IFaaSConfigurationOptions, Application, NextFunction } from './interface';
3
+ import { BaseFramework, CommonMiddlewareUnion, ContextMiddlewareManager, FunctionMiddleware, IMidwayBootstrapOptions, MidwayEnvironmentService, MidwayFrameworkType, MidwayMiddlewareService, RouterInfo } from '@midwayjs/core';
4
4
  import { LoggerOptions } from '@midwayjs/logger';
5
5
  import * as http from 'http';
6
- export declare class MidwayFaaSFramework extends BaseFramework<IMidwayFaaSApplication, FaaSContext, IFaaSConfigurationOptions> {
6
+ export declare class MidwayFaaSFramework extends BaseFramework<Application, Context, IFaaSConfigurationOptions> {
7
7
  protected defaultHandlerMethod: string;
8
8
  protected funMappingStore: Map<string, RouterInfo>;
9
9
  protected logger: any;
10
10
  private lock;
11
- app: IMidwayFaaSApplication;
11
+ app: Application;
12
12
  private isReplaceLogger;
13
13
  private developmentRun;
14
14
  private serverlessRoutes;
15
15
  private server;
16
16
  private respond;
17
17
  private applicationAdapter;
18
- protected eventMiddlewareManager: ContextMiddlewareManager<FaaSContext, unknown, unknown>;
18
+ protected httpMiddlewareManager: ContextMiddlewareManager<Context, unknown, unknown>;
19
+ protected eventMiddlewareManager: ContextMiddlewareManager<Context, unknown, unknown>;
19
20
  environmentService: MidwayEnvironmentService;
20
- middlewareService: MidwayMiddlewareService<FaaSContext, any>;
21
+ middlewareService: MidwayMiddlewareService<Context, any>;
21
22
  configure(options: IFaaSConfigurationOptions): any;
22
23
  isEnable(): boolean;
23
24
  applicationInitialize(options: IMidwayBootstrapOptions): Promise<void>;
24
25
  run(): Promise<void>;
25
26
  getFrameworkType(): MidwayFrameworkType;
26
- handleInvokeWrapper(handlerMapping: string, options?: InvokeOptions): (...args: any[]) => Promise<any>;
27
+ handleInvokeWrapper(handlerMapping: string): (...args: any[]) => Promise<{
28
+ result: any;
29
+ error: Error;
30
+ } | {
31
+ isBase64Encoded: boolean;
32
+ statusCode: any;
33
+ headers: any;
34
+ body: any;
35
+ }>;
27
36
  /**
28
37
  * @deprecated
29
38
  * @param middlewareId
30
39
  */
31
- generateMiddleware(middlewareId: string): Promise<FunctionMiddleware<FaaSContext, any>>;
40
+ generateMiddleware(middlewareId: string): Promise<FunctionMiddleware<Context, any>>;
32
41
  getContext(context?: any): any;
33
42
  private invokeHandler;
34
43
  protected getFunctionHandler(ctx: any, args: any, target: any, method: any): string;
@@ -36,6 +45,9 @@ export declare class MidwayFaaSFramework extends BaseFramework<IMidwayFaaSApplic
36
45
  getFrameworkName(): string;
37
46
  getServer(): http.Server;
38
47
  protected createHttpContext(req: any, res: any): Promise<unknown>;
48
+ useMiddleware(middleware: CommonMiddlewareUnion<Context, NextFunction, undefined>): void;
49
+ useEventMiddleware(middleware: CommonMiddlewareUnion<Context, NextFunction, undefined>): void;
50
+ getEventMiddleware(): ContextMiddlewareManager<Context, NextFunction, undefined>;
39
51
  }
40
52
  export declare const createModuleServerlessFramework: (globalOption: Omit<IMidwayBootstrapOptions, 'applicationContext'> & IFaaSConfigurationOptions) => Promise<MidwayFaaSFramework>;
41
53
  //# sourceMappingURL=framework.d.ts.map
package/dist/framework.js CHANGED
@@ -10,7 +10,6 @@ var __metadata = (this && this.__metadata) || function (k, v) {
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.createModuleServerlessFramework = exports.MidwayFaaSFramework = void 0;
13
- const interface_1 = require("./interface");
14
13
  const core_1 = require("@midwayjs/core");
15
14
  const decorator_1 = require("@midwayjs/decorator");
16
15
  const simple_lock_1 = require("@midwayjs/simple-lock");
@@ -27,6 +26,7 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
27
26
  this.isReplaceLogger = process.env['MIDWAY_SERVERLESS_REPLACE_LOGGER'] === 'true';
28
27
  this.developmentRun = false;
29
28
  this.serverlessRoutes = [];
29
+ this.httpMiddlewareManager = this.createMiddlewareManager();
30
30
  this.eventMiddlewareManager = this.createMiddlewareManager();
31
31
  }
32
32
  configure(options) {
@@ -44,13 +44,13 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
44
44
  return !this.developmentRun;
45
45
  }
46
46
  async applicationInitialize(options) {
47
- var _a, _b;
47
+ var _a, _b, _c;
48
48
  if (!this.logger) {
49
49
  this.logger = options.logger || logger_1.loggers.getLogger('appLogger');
50
50
  }
51
51
  this.applicationAdapter = this.configurationOptions.applicationAdapter;
52
52
  this.app =
53
- ((_a = this.applicationAdapter) === null || _a === void 0 ? void 0 : _a.getApplication()) ||
53
+ ((_b = (_a = this.applicationAdapter).getApplication) === null || _b === void 0 ? void 0 : _b.call(_a)) ||
54
54
  new serverless_http_parser_1.Application();
55
55
  this.defineApplicationProperties({
56
56
  /**
@@ -79,7 +79,7 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
79
79
  // hack use method
80
80
  this.app.originUse = this.app.use;
81
81
  this.app.use = this.app.useMiddleware;
82
- if ((_b = this.configurationOptions.applicationAdapter) === null || _b === void 0 ? void 0 : _b.runAppHook) {
82
+ if ((_c = this.configurationOptions.applicationAdapter) === null || _c === void 0 ? void 0 : _c.runAppHook) {
83
83
  this.configurationOptions.applicationAdapter.runAppHook(this.app);
84
84
  }
85
85
  }
@@ -122,7 +122,7 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
122
122
  getFrameworkType() {
123
123
  return core_1.MidwayFrameworkType.FAAS;
124
124
  }
125
- handleInvokeWrapper(handlerMapping, options) {
125
+ handleInvokeWrapper(handlerMapping) {
126
126
  let funOptions = this.funMappingStore.get(handlerMapping);
127
127
  return async (...args) => {
128
128
  var _a, _b;
@@ -130,9 +130,8 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
130
130
  throw new Error('first parameter must be function context');
131
131
  }
132
132
  const event = args[0];
133
- const isLegacyMode = !options;
134
- const isHttpFunction = (options === null || options === void 0 ? void 0 : options.triggerType) === interface_1.TRIGGER_TYPE_ENUM.HTTP ||
135
- event.constructor.name === 'IncomingMessage' ||
133
+ const isLegacyMode = event.originContext && event.originEvent;
134
+ const isHttpFunction = event.constructor.name === 'IncomingMessage' ||
136
135
  event.constructor.name === 'EventEmitter' ||
137
136
  !!(event.headers && event.get);
138
137
  if (!funOptions && isHttpFunction) {
@@ -159,11 +158,11 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
159
158
  else {
160
159
  context = this.getContext(await ((_b = this.applicationAdapter) === null || _b === void 0 ? void 0 : _b.runEventHook(...args)));
161
160
  }
162
- const globalMiddlewareFn = await this.applyMiddleware();
163
- const middlewareManager = new core_1.ContextMiddlewareManager();
164
- middlewareManager.insertLast(globalMiddlewareFn);
165
- middlewareManager.insertLast(async (ctx, next) => {
161
+ const result = await (await this.applyMiddleware(async (ctx, next) => {
166
162
  const fn = await this.middlewareService.compose([
163
+ ...(isHttpFunction
164
+ ? this.httpMiddlewareManager
165
+ : this.eventMiddlewareManager),
167
166
  ...funOptions.controllerMiddleware,
168
167
  ...funOptions.middleware,
169
168
  async (ctx, next) => {
@@ -179,9 +178,7 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
179
178
  },
180
179
  ], this.app);
181
180
  return await fn(ctx, next);
182
- });
183
- const composeMiddleware = await this.middlewareService.compose(middlewareManager, this.app);
184
- const result = await composeMiddleware(context);
181
+ }))(context);
185
182
  if (isLegacyMode) {
186
183
  return result;
187
184
  }
@@ -319,6 +316,15 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
319
316
  this.respond(req, res, resolve);
320
317
  });
321
318
  }
319
+ useMiddleware(middleware) {
320
+ this.httpMiddlewareManager.insertLast(middleware);
321
+ }
322
+ useEventMiddleware(middleware) {
323
+ this.eventMiddlewareManager.insertLast(middleware);
324
+ }
325
+ getEventMiddleware() {
326
+ return this.eventMiddlewareManager;
327
+ }
322
328
  };
323
329
  __decorate([
324
330
  (0, decorator_1.Inject)(),
@@ -1,4 +1,4 @@
1
- import { MidwayRequestContainer, IMidwayApplication, IConfigurationOptions, IMidwayContext, NextFunction as BaseNextFunction } from '@midwayjs/core';
1
+ import { MidwayRequestContainer, IMidwayApplication, IConfigurationOptions, IMidwayContext, NextFunction as BaseNextFunction, CommonMiddlewareUnion, ContextMiddlewareManager } from '@midwayjs/core';
2
2
  import { FaaSHTTPContext } from '@midwayjs/faas-typings';
3
3
  import { ILogger } from '@midwayjs/logger';
4
4
  import { Application as ServerlessHttpApplication } from '@midwayjs/serverless-http-parser';
@@ -11,8 +11,8 @@ export interface FaaSContext extends IMidwayContext<FaaSHTTPContext> {
11
11
  /**
12
12
  * @deprecated
13
13
  */
14
- export declare type FaaSMiddleware = ((context: FaaSContext, next: () => Promise<any>) => any) | string;
15
- export declare type IMidwayFaaSApplication = IMidwayApplication<FaaSContext, {
14
+ export declare type FaaSMiddleware = ((context: Context, next: () => Promise<any>) => any) | string;
15
+ export declare type IMidwayFaaSApplication = IMidwayApplication<Context, {
16
16
  getInitializeContext(): any;
17
17
  use(middleware: FaaSMiddleware): any;
18
18
  /**
@@ -28,7 +28,8 @@ export declare type IMidwayFaaSApplication = IMidwayApplication<FaaSContext, {
28
28
  * Get function service name in serverless environment
29
29
  */
30
30
  getFunctionServiceName(): string;
31
- useEventMiddleware(): void;
31
+ useEventMiddleware(middleware: CommonMiddlewareUnion<Context, NextFunction, undefined>): void;
32
+ getEventMiddleware: ContextMiddlewareManager<Context, NextFunction, undefined>;
32
33
  }> & ServerlessHttpApplication;
33
34
  export interface Application extends IMidwayFaaSApplication {
34
35
  }
@@ -39,10 +40,10 @@ export interface IFaaSConfigurationOptions extends IConfigurationOptions {
39
40
  config?: object;
40
41
  initializeContext?: object;
41
42
  applicationAdapter?: {
42
- getApplication(): IMidwayFaaSApplication;
43
+ getApplication(): Application;
43
44
  getFunctionName(): string;
44
45
  getFunctionServiceName(): string;
45
- runAppHook?(app: IMidwayFaaSApplication): void;
46
+ runAppHook?(app: Application): void;
46
47
  runEventHook?(...args: any[]): any | void;
47
48
  runRequestHook?(...args: any[]): any | void;
48
49
  };
@@ -53,11 +54,4 @@ export interface IFaaSConfigurationOptions extends IConfigurationOptions {
53
54
  export interface IWebMiddleware {
54
55
  resolve(): FaaSMiddleware;
55
56
  }
56
- export declare enum TRIGGER_TYPE_ENUM {
57
- HTTP = "http",
58
- Event = "event"
59
- }
60
- export interface InvokeOptions {
61
- triggerType: TRIGGER_TYPE_ENUM;
62
- }
63
57
  //# sourceMappingURL=interface.d.ts.map
package/dist/interface.js CHANGED
@@ -1,9 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TRIGGER_TYPE_ENUM = void 0;
4
- var TRIGGER_TYPE_ENUM;
5
- (function (TRIGGER_TYPE_ENUM) {
6
- TRIGGER_TYPE_ENUM["HTTP"] = "http";
7
- TRIGGER_TYPE_ENUM["Event"] = "event";
8
- })(TRIGGER_TYPE_ENUM = exports.TRIGGER_TYPE_ENUM || (exports.TRIGGER_TYPE_ENUM = {}));
9
3
  //# sourceMappingURL=interface.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/faas",
3
- "version": "3.3.6-beta.2",
3
+ "version": "3.3.6-beta.5",
4
4
  "main": "dist/index",
5
5
  "typings": "index.d.ts",
6
6
  "dependencies": {