@midwayjs/faas 3.3.6-beta.3 → 3.3.6-beta.4

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 } 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): (...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
@@ -26,6 +26,7 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
26
26
  this.isReplaceLogger = process.env['MIDWAY_SERVERLESS_REPLACE_LOGGER'] === 'true';
27
27
  this.developmentRun = false;
28
28
  this.serverlessRoutes = [];
29
+ this.httpMiddlewareManager = this.createMiddlewareManager();
29
30
  this.eventMiddlewareManager = this.createMiddlewareManager();
30
31
  }
31
32
  configure(options) {
@@ -157,11 +158,11 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
157
158
  else {
158
159
  context = this.getContext(await ((_b = this.applicationAdapter) === null || _b === void 0 ? void 0 : _b.runEventHook(...args)));
159
160
  }
160
- const globalMiddlewareFn = await this.applyMiddleware();
161
- const middlewareManager = new core_1.ContextMiddlewareManager();
162
- middlewareManager.insertLast(globalMiddlewareFn);
163
- middlewareManager.insertLast(async (ctx, next) => {
161
+ const result = await (await this.applyMiddleware(async (ctx, next) => {
164
162
  const fn = await this.middlewareService.compose([
163
+ ...(isHttpFunction
164
+ ? this.httpMiddlewareManager
165
+ : this.eventMiddlewareManager),
165
166
  ...funOptions.controllerMiddleware,
166
167
  ...funOptions.middleware,
167
168
  async (ctx, next) => {
@@ -177,9 +178,7 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
177
178
  },
178
179
  ], this.app);
179
180
  return await fn(ctx, next);
180
- });
181
- const composeMiddleware = await this.middlewareService.compose(middlewareManager, this.app);
182
- const result = await composeMiddleware(context);
181
+ }))(context);
183
182
  if (isLegacyMode) {
184
183
  return result;
185
184
  }
@@ -317,6 +316,15 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
317
316
  this.respond(req, res, resolve);
318
317
  });
319
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
+ }
320
328
  };
321
329
  __decorate([
322
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/faas",
3
- "version": "3.3.6-beta.3",
3
+ "version": "3.3.6-beta.4",
4
4
  "main": "dist/index",
5
5
  "typings": "index.d.ts",
6
6
  "dependencies": {