@midwayjs/faas 3.8.1 → 3.9.1-beta.1

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.
@@ -0,0 +1,2 @@
1
+ export declare const Event: () => ParameterDecorator;
2
+ //# sourceMappingURL=decorator.d.ts.map
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Event = void 0;
4
+ const core_1 = require("@midwayjs/core");
5
+ // 实现装饰器
6
+ const Event = () => {
7
+ return (0, core_1.createRequestParamDecorator)(ctx => {
8
+ return ctx.originEvent && ctx.originContext ? ctx.originEvent : ctx;
9
+ });
10
+ };
11
+ exports.Event = Event;
12
+ //# sourceMappingURL=decorator.js.map
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { Context, IFaaSConfigurationOptions, Application, NextFunction, HandlerOptions } from './interface';
2
+ import { Context, IFaaSConfigurationOptions, Application, NextFunction, HandlerOptions, HttpResponseFormat } from './interface';
3
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';
@@ -33,6 +33,7 @@ export declare class MidwayFaaSFramework extends BaseFramework<Application, Cont
33
33
  */
34
34
  handleInvokeWrapper(handlerMapping: string): (...args: any[]) => Promise<any>;
35
35
  invokeTriggerFunction(context: any, handlerMapping: string, options: HandlerOptions): Promise<any>;
36
+ formatHttpResponse(context: any): HttpResponseFormat;
36
37
  wrapHttpRequest(req: http.IncomingMessage | Record<string, any>, res?: http.ServerResponse): Promise<unknown>;
37
38
  /**
38
39
  * @deprecated
package/dist/framework.js CHANGED
@@ -92,33 +92,8 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
92
92
  getEventMiddleware: () => {
93
93
  return this.getEventMiddleware();
94
94
  },
95
- getServerlessInstance: async (serviceClass, customContext = {}) => {
96
- const instance = new Proxy({}, {
97
- get: (target, prop) => {
98
- let funcInfo;
99
- if (typeof serviceClass === 'string') {
100
- funcInfo = this.funMappingStore.get(`${serviceClass}.${prop}`);
101
- }
102
- else {
103
- funcInfo = Array.from(this.funMappingStore.values()).find(item => {
104
- return (item.id === (0, core_1.getProviderUUId)(serviceClass) &&
105
- item.method === prop);
106
- });
107
- }
108
- if (funcInfo) {
109
- return async (...args) => {
110
- const context = this.app.createAnonymousContext({
111
- originContext: customContext,
112
- originEvent: args[0],
113
- });
114
- return this.invokeTriggerFunction(context, funcInfo.funcHandlerName, {
115
- isHttpFunction: false,
116
- });
117
- };
118
- }
119
- },
120
- });
121
- return instance;
95
+ getServerlessInstance: async () => {
96
+ throw new core_1.MidwayFeatureNotImplementedError('Please use it in by @midwayjs/mock in test.');
122
97
  },
123
98
  invokeTriggerFunction: (context, handlerMapping, options) => {
124
99
  return this.invokeTriggerFunction(context, handlerMapping, options);
@@ -216,7 +191,7 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
216
191
  };
217
192
  }
218
193
  async invokeTriggerFunction(context, handlerMapping, options) {
219
- var _a, _b;
194
+ var _a;
220
195
  let funOptions = this.funMappingStore.get(handlerMapping);
221
196
  const isHttpFunction = options.isHttpFunction;
222
197
  if (!funOptions && isHttpFunction) {
@@ -268,60 +243,66 @@ let MidwayFaaSFramework = class MidwayFaaSFramework extends core_1.BaseFramework
268
243
  return await fn(ctx, next);
269
244
  }))(context);
270
245
  if (isHttpFunction) {
271
- if (!((_b = context.response) === null || _b === void 0 ? void 0 : _b._explicitStatus)) {
272
- if (context.body === null || context.body === 'undefined') {
273
- context.body = '';
274
- context.type = 'text';
275
- context.status = 204;
276
- }
246
+ if (!options.isChunked) {
247
+ return this.formatHttpResponse(context);
277
248
  }
278
- let encoded = false;
279
- const data = context.body;
280
- if (typeof data === 'string') {
281
- if (!context.type) {
282
- context.type = 'text/plain';
283
- }
284
- context.body = data;
285
- }
286
- else if (isAnyArrayBuffer(data) || isUint8Array(data)) {
287
- encoded = true;
288
- if (!context.type) {
289
- context.type = 'application/octet-stream';
290
- }
291
- // data is reserved as buffer
292
- context.body = Buffer.from(data).toString('base64');
249
+ }
250
+ else {
251
+ return result;
252
+ }
253
+ }
254
+ formatHttpResponse(context) {
255
+ var _a;
256
+ if (!((_a = context.response) === null || _a === void 0 ? void 0 : _a._explicitStatus)) {
257
+ if (context.body === null || context.body === 'undefined') {
258
+ context.body = '';
259
+ context.type = 'text';
260
+ context.status = 204;
293
261
  }
294
- else if (typeof data === 'object') {
295
- if (!context.type) {
296
- context.type = 'application/json';
297
- }
298
- // set data to string
299
- context.body = JSON.stringify(data);
262
+ }
263
+ let encoded = false;
264
+ const data = context.body;
265
+ if (typeof data === 'string') {
266
+ if (!context.type) {
267
+ context.type = 'text/plain';
300
268
  }
301
- else {
302
- if (!context.type) {
303
- context.type = 'text/plain';
304
- }
305
- // set data to string
306
- context.body = data + '';
269
+ context.body = data;
270
+ }
271
+ else if (isAnyArrayBuffer(data) || isUint8Array(data)) {
272
+ encoded = true;
273
+ if (!context.type) {
274
+ context.type = 'application/octet-stream';
307
275
  }
308
- // middleware return value and will be got 204 status
309
- if (context.body === undefined &&
310
- !context.response._explicitStatus &&
311
- context._matchedRoute) {
312
- // 如果进了路由,重新赋值,防止 404
313
- context.body = undefined;
276
+ // data is reserved as buffer
277
+ context.body = Buffer.from(data).toString('base64');
278
+ }
279
+ else if (typeof data === 'object') {
280
+ if (!context.type) {
281
+ context.type = 'application/json';
314
282
  }
315
- return {
316
- isBase64Encoded: encoded,
317
- statusCode: context.status,
318
- headers: context.res.headers,
319
- body: context.body,
320
- };
283
+ // set data to string
284
+ context.body = JSON.stringify(data);
321
285
  }
322
286
  else {
323
- return result;
287
+ if (!context.type) {
288
+ context.type = 'text/plain';
289
+ }
290
+ // set data to string
291
+ context.body = data + '';
292
+ }
293
+ // middleware return value and will be got 204 status
294
+ if (context.body === undefined &&
295
+ !context.response._explicitStatus &&
296
+ context._matchedRoute) {
297
+ // 如果进了路由,重新赋值,防止 404
298
+ context.body = undefined;
324
299
  }
300
+ return {
301
+ isBase64Encoded: encoded,
302
+ statusCode: context.status,
303
+ headers: context.res.headers,
304
+ body: context.body,
305
+ };
325
306
  }
326
307
  async wrapHttpRequest(req, res) {
327
308
  const newReq = res ? new serverless_http_parser_1.HTTPRequest(req, res) : req;
package/dist/index.d.ts CHANGED
@@ -2,4 +2,5 @@ export * from './interface';
2
2
  export { MidwayFaaSFramework as Framework } from './framework';
3
3
  export { FaaSConfiguration as Configuration } from './configuration';
4
4
  export { AbstractBootstrapStarter } from './starter';
5
+ export * from './decorator';
5
6
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -22,4 +22,5 @@ var configuration_1 = require("./configuration");
22
22
  Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.FaaSConfiguration; } });
23
23
  var starter_1 = require("./starter");
24
24
  Object.defineProperty(exports, "AbstractBootstrapStarter", { enumerable: true, get: function () { return starter_1.AbstractBootstrapStarter; } });
25
+ __exportStar(require("./decorator"), exports);
25
26
  //# sourceMappingURL=index.js.map
@@ -13,10 +13,14 @@ export interface FaaSContext extends IMidwayContext<FaaSHTTPContext> {
13
13
  */
14
14
  export declare type FaaSMiddleware = ((context: Context, next: () => Promise<any>) => any) | string;
15
15
  export interface HandlerOptions {
16
- isHttpFunction: boolean;
16
+ isHttpFunction?: boolean;
17
+ isChunked?: boolean;
17
18
  }
18
19
  export declare type IMidwayFaaSApplication = IMidwayApplication<Context, {
19
20
  getInitializeContext(): any;
21
+ /**
22
+ * @deprecated use useMiddleware instead
23
+ */
20
24
  use(middleware: FaaSMiddleware): any;
21
25
  /**
22
26
  * @deprecated
@@ -36,7 +40,7 @@ export declare type IMidwayFaaSApplication = IMidwayApplication<Context, {
36
40
  invokeTriggerFunction(context: any, handler: string, options: HandlerOptions): Promise<any>;
37
41
  getServerlessInstance<T>(serviceClass: ObjectIdentifier | {
38
42
  new (...args: any[]): T;
39
- }, customContext?: any): Promise<T>;
43
+ }, customContext?: Record<string, any>): Promise<T>;
40
44
  }> & ServerlessHttpApplication;
41
45
  export interface Application extends IMidwayFaaSApplication {
42
46
  }
@@ -77,4 +81,10 @@ export interface ServerlessStarterOptions extends IMidwayBootstrapOptions {
77
81
  end(): any;
78
82
  };
79
83
  }
84
+ export interface HttpResponseFormat<T = unknown> {
85
+ isBase64Encoded: boolean;
86
+ statusCode: number;
87
+ headers: Record<string, string>;
88
+ body: T;
89
+ }
80
90
  //# sourceMappingURL=interface.d.ts.map
package/dist/starter.d.ts CHANGED
@@ -9,6 +9,9 @@ export declare abstract class AbstractBootstrapStarter {
9
9
  close(): Promise<void>;
10
10
  start(options?: ServerlessStarterOptions): any;
11
11
  initFramework(bootstrapOptions?: IMidwayBootstrapOptions): Promise<void>;
12
+ protected createDefaultMockHttpEvent(): void;
13
+ protected createDefaultMockEvent(): void;
14
+ protected createDefaultMockContext(): void;
12
15
  abstract onStart(): any;
13
16
  abstract onInit(...args: unknown[]): any;
14
17
  abstract onRequest(...args: unknown[]): any;
package/dist/starter.js CHANGED
@@ -22,6 +22,9 @@ class AbstractBootstrapStarter {
22
22
  const midwayFrameworkService = this.applicationContext.get(core_1.MidwayFrameworkService);
23
23
  this.framework = midwayFrameworkService.getMainFramework();
24
24
  }
25
+ createDefaultMockHttpEvent() { }
26
+ createDefaultMockEvent() { }
27
+ createDefaultMockContext() { }
25
28
  }
26
29
  exports.AbstractBootstrapStarter = AbstractBootstrapStarter;
27
30
  //# sourceMappingURL=starter.js.map
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@midwayjs/faas",
3
- "version": "3.8.1",
3
+ "version": "3.9.1-beta.1",
4
4
  "main": "dist/index",
5
5
  "typings": "index.d.ts",
6
6
  "dependencies": {
7
- "@midwayjs/core": "^3.8.0",
7
+ "@midwayjs/core": "^3.9.0",
8
8
  "@midwayjs/faas-typings": "^3.6.0",
9
9
  "@midwayjs/logger": "^2.15.0",
10
10
  "@midwayjs/serverless-http-parser": "^3.6.0",
11
11
  "@midwayjs/simple-lock": "^1.1.4"
12
12
  },
13
13
  "devDependencies": {
14
- "@midwayjs/mock": "^3.8.0",
15
- "@midwayjs/serverless-fc-starter": "^3.8.0",
14
+ "@midwayjs/mock": "^3.9.0",
15
+ "@midwayjs/serverless-fc-starter": "^3.9.0",
16
16
  "@midwayjs/serverless-scf-starter": "^3.7.0",
17
17
  "mm": "3.2.1"
18
18
  },