@h4ad/serverless-adapter 2.0.2 → 2.2.0

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.
Files changed (31) hide show
  1. package/README.md +104 -5
  2. package/lib/@types/huawei/huawei-api-gateway-event.d.ts +19 -0
  3. package/lib/@types/huawei/huawei-api-gateway-event.js +2 -0
  4. package/lib/@types/huawei/huawei-api-gateway-response.d.ts +7 -0
  5. package/lib/@types/huawei/huawei-api-gateway-response.js +2 -0
  6. package/lib/@types/huawei/huawei-context.d.ts +31 -0
  7. package/lib/@types/huawei/huawei-context.js +2 -0
  8. package/lib/@types/huawei/index.d.ts +3 -0
  9. package/lib/@types/huawei/index.js +15 -0
  10. package/lib/adapters/aws/api-gateway-v2.adapter.js +2 -2
  11. package/lib/adapters/dummy/dummy.adapter.d.ts +27 -0
  12. package/lib/adapters/dummy/dummy.adapter.js +47 -0
  13. package/lib/adapters/dummy/index.d.ts +1 -0
  14. package/lib/adapters/dummy/index.js +13 -0
  15. package/lib/adapters/huawei/huawei-api-gateway.adapter.d.ts +59 -0
  16. package/lib/adapters/huawei/huawei-api-gateway.adapter.js +113 -0
  17. package/lib/adapters/huawei/index.d.ts +1 -0
  18. package/lib/adapters/huawei/index.js +13 -0
  19. package/lib/handlers/default/default.handler.d.ts +1 -2
  20. package/lib/handlers/default/default.handler.js +1 -2
  21. package/lib/handlers/huawei/http-huawei.handler.d.ts +49 -0
  22. package/lib/handlers/huawei/http-huawei.handler.js +82 -0
  23. package/lib/handlers/huawei/index.d.ts +1 -0
  24. package/lib/handlers/huawei/index.js +13 -0
  25. package/lib/resolvers/dummy/dummy.resolver.d.ts +10 -0
  26. package/lib/resolvers/dummy/dummy.resolver.js +19 -0
  27. package/lib/resolvers/dummy/index.d.ts +1 -0
  28. package/lib/resolvers/dummy/index.js +13 -0
  29. package/lib/serverless-adapter.d.ts +1 -1
  30. package/lib/serverless-adapter.js +2 -2
  31. package/package.json +6 -2
package/README.md CHANGED
@@ -6,6 +6,7 @@
6
6
  <a href="#install">Install</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
7
7
  <a href="#usage">Usage</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
8
8
  <a href="#support">Support</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
9
+ <a href="#examples">Examples</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
9
10
  <a href="#architecture">Architecture</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
10
11
  <a href="#credits">Credits</a>
11
12
  </p>
@@ -19,7 +20,7 @@
19
20
  [![Semantic Release][semantic-release-img]][semantic-release-url]
20
21
 
21
22
  Run REST APIs and other web applications using your existing Node.js application framework (Express, Koa, Hapi and
22
- Fastify), on top of AWS Lambda, Amazon API Gateway and many other event sources.
23
+ Fastify), on top of AWS Lambda, Huawei and many other clouds.
23
24
 
24
25
  This library was a refactored version of [@vendia/serverless-express](https://github.com/vendia/serverless-express), I
25
26
  created a new way to interact and extend event sources by creating contracts to abstract the integrations between each
@@ -56,7 +57,7 @@ You can quickly use this library as follows:
56
57
 
57
58
  ```ts
58
59
  import { ServerlessAdapter } from '@h4ad/serverless-adapter';
59
- import { ApiGatewayV2Adapter } from '@h4ad/serverless-adapter/lib/adapters/aws';
60
+ import { ApiGatewayV2Adapter, AlbAdapter, SQSAdapter, SNSAdapter } from '@h4ad/serverless-adapter/lib/adapters/aws';
60
61
  import { ExpressFramework } from '@h4ad/serverless-adapter/lib/frameworks/express';
61
62
  import { DefaultHandler } from '@h4ad/serverless-adapter/lib/handlers/default';
62
63
  import { PromiseResolver } from '@h4ad/serverless-adapter/lib/resolvers/promise';
@@ -70,8 +71,6 @@ export const handler = ServerlessAdapter.new(app)
70
71
  .addAdapter(new SQSAdapter())
71
72
  .addAdapter(new SNSAdapter())
72
73
  .build();
73
-
74
- export { handler };
75
74
  ```
76
75
 
77
76
  Too fast? Ok, I can explain.
@@ -140,9 +139,15 @@ We support these event sources:
140
139
  using ([SNSAdapter](./src/adapters/aws/sns.adapter.ts))
141
140
  - [AWS SQS](https://docs.aws.amazon.com/pt_br/lambda/latest/dg/with-sqs.html) by
142
141
  using ([SQSAdapter](./src/adapters/aws/sqs.adapter.ts))
142
+ - Huawei
143
+ - [Http Function](https://support.huaweicloud.com/intl/en-us/usermanual-functiongraph/functiongraph_01_1442.html): Look [this section](#huawei-http-function) about Huawei support for Http Function.
144
+ - [Event Function](https://support.huaweicloud.com/intl/en-us/usermanual-functiongraph/functiongraph_01_1441.html):
145
+ - [Api Gateway](https://support.huaweicloud.com/intl/en-us/devg-functiongraph/functiongraph_02_0102.html#functiongraph_02_0102__li5178638110137) by using ([HuaweiApiGatewayAdapter](src/adapters/huawei/huawei-api-gateway.adapter.ts)).
143
146
  - Azure
147
+ - [The support is coming soon.](https://github.com/H4ad/serverless-adapter/issues/3)
148
+ - Firebase
144
149
  - The support is coming soon.
145
- - Huawei
150
+ - GCP
146
151
  - The support is coming soon.
147
152
 
148
153
  We support these resolvers:
@@ -154,6 +159,94 @@ We support these resolvers:
154
159
  We support these handlers:
155
160
 
156
161
  - Default by using ([DefaultHandler](src/handlers/default/default.handler.ts))
162
+ - Huawei by using ([HttpHuaweiHandler](src/handlers/huawei/http-huawei.handler.ts)) and ([EventHuaweiHandler](#huawei-event-function))
163
+
164
+ ## Huawei
165
+
166
+ In Huawei, we added support to FunctionGraphV2 with Http Function and Event Function.
167
+
168
+ The difference between Http Function and Event Function is that in Http Function you must expose port 8000 and Huawei will proxy Api Gateway requests to your application.
169
+ So, on implementation, this library will create an http server to listen on port 8000 and forward the request to your framework.
170
+
171
+ In Event Function, you will receive the event from event source in the same way you receive in AWS, an object with some structure, you can see the supported event sources [here](https://support.huaweicloud.com/intl/en-us/devg-functiongraph/functiongraph_02_0102.html).
172
+
173
+ ### Huawei Http Function
174
+
175
+ To integrate your app with Huawei FunctionGrapth with the Http Function type, you must do the following:
176
+
177
+ ```ts
178
+ import { ServerlessAdapter } from '@h4ad/serverless-adapter';
179
+ import { ExpressFramework } from '@h4ad/serverless-adapter/lib/frameworks/express';
180
+ import { HttpHuaweiHandler } from '@h4ad/serverless-adapter/lib/handlers/huawei';
181
+ import { DummyResolver } from '@h4ad/serverless-adapter/lib/resolvers/dummy';
182
+ import { DummyAdapter } from '@h4ad/serverless-adapter/lib/adapters/dummy';
183
+ import app from './app';
184
+
185
+ // instead exposing handler, you have the dispose function
186
+ // this dispose function is never called
187
+ // but you can to close the http server created with him
188
+ const dispose = ServerlessAdapter.new(app)
189
+ .setHandler(new HttpHuaweiHandler())
190
+ .setFramework(new ExpressFramework())
191
+ // dummy resolver and adapter is used because
192
+ // they are necessary in the core of the library to build
193
+ // but is optional to make huawei http function works.
194
+ .setResolver(new DummyResolver())
195
+ .addAdapter(new DummyAdapter())
196
+ .build();
197
+ ```
198
+
199
+ > You don't need to expose a variable called `handler` when you choose Http Function, you just need to call build to the library create a http server.
200
+
201
+ By the way of Huawei architecture in Http Function, they have no use for Resolvers and Adapters, so you need to use the dummy versions because the library requires it.
202
+
203
+ #### ONE IMPORTANT THING
204
+
205
+ You need to configure a file called `bootstrap` in the root of folder that you upload to Huawei, is like the file `Procfile` but for Huawei.
206
+
207
+ In my setup, I configure like:
208
+ ```
209
+ node /opt/function/code/index.js
210
+ ```
211
+
212
+ The path `/opt/function/code` is where your code is uploaded when you deploy something and `index.js` is the file that contains the `ServerlessAdapter`.
213
+
214
+ In the end, the structure of the zip file you upload looks like this:
215
+
216
+ - `bootstrap`
217
+ - `index.js`
218
+
219
+ ### Huawei Event Function
220
+
221
+ With Http Function you need to use [HttpHuaweiHandler](src/handlers/huawei/http-huawei.handler.ts),
222
+ but with Event Function you should use [DefaultHandler](src/handlers/default/default.handler.ts).
223
+
224
+ So, to add support to Api Gateway you do the following:
225
+
226
+ ```ts
227
+ import { ServerlessAdapter } from '@h4ad/serverless-adapter';
228
+ import { HuaweiApiGatewayAdapter } from '@h4ad/serverless-adapter/lib/adapters/huawei';
229
+ import { ExpressFramework } from '@h4ad/serverless-adapter/lib/frameworks/express';
230
+ import { DefaultHandler } from '@h4ad/serverless-adapter/lib/handlers/default';
231
+ import { CallbackResolver } from '@h4ad/serverless-adapter/lib/resolvers/callback';
232
+ import app from './app';
233
+
234
+ export const handler = ServerlessAdapter.new(app)
235
+ .setFramework(new ExpressFramework())
236
+ .setHandler(new DefaultHandler())
237
+ .setResolver(new CallbackResolver())
238
+ .addAdapter(new HuaweiApiGatewayAdapter())
239
+ .build();
240
+ ```
241
+
242
+ #### One important thing
243
+
244
+ You must use the callback resolver because I couldn't get it to work with the PromiseResolver.
245
+ Maybe it's a bug in the library or something specific in Huawei, if you have a tip please create an issue.
246
+
247
+ # Examples
248
+
249
+ You can see some examples of how to use this library [here](https://github.com/H4ad/serverless-adapter-examples).
157
250
 
158
251
  # Architecture
159
252
 
@@ -207,6 +300,12 @@ PR just to extend the library's functionality :)
207
300
  Honestly, I just refactored all the code that the @vendia team and many other contributors wrote, thanks so much to them
208
301
  for existing and giving us a brilliant library that is the core of my current company.
209
302
 
303
+ # Sponsors
304
+
305
+ | <a href="https://liga.facens.br/"><img height="50" src="https://mlogu6g7z5ex.i.optimole.com/yEwfkqo-4R0ttNtd/w:auto/h:auto/q:mauto/f:avif/http://liga.facens.br/wp-content/uploads/2020/03/logo-1.png" title="The LIGA logo" width="100"/></a> |
306
+ |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
307
+
308
+
210
309
  [build-img]:https://github.com/H4ad/serverless-adapter/actions/workflows/release.yml/badge.svg
211
310
 
212
311
  [build-url]:https://github.com/H4ad/serverless-adapter/actions/workflows/release.yml
@@ -0,0 +1,19 @@
1
+ import { BothValueHeaders } from '../index';
2
+ export interface HuaweiApiGatewayEvent {
3
+ body: string;
4
+ headers: BothValueHeaders;
5
+ httpMethod: string;
6
+ isBase64Encoded: boolean;
7
+ path: string;
8
+ pathParameters: HuaweiRequestPathParameters;
9
+ queryStringParameters: HuaweiRequestQueryStringParameters;
10
+ requestContext: HuaweiRequestContext;
11
+ [key: string]: any;
12
+ }
13
+ export declare type HuaweiRequestPathParameters = Record<string, string>;
14
+ export declare type HuaweiRequestQueryStringParameters = Record<string, string | string[]>;
15
+ export interface HuaweiRequestContext {
16
+ apiId: string;
17
+ requestId: string;
18
+ stage: string;
19
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ import { MultiValueHeaders } from '../headers';
2
+ export interface HuaweiApiGatewayResponse {
3
+ isBase64Encoded: boolean;
4
+ statusCode: number;
5
+ headers: MultiValueHeaders;
6
+ body: string;
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,31 @@
1
+ export declare type GetRequestIDSecondsReturn = string;
2
+ export declare type GetRemainingTimeInMilliSecondsReturn = number;
3
+ export declare type GetAccessKeyReturn = string;
4
+ export declare type GetSecretKeyReturn = string;
5
+ export declare type GetUserDataKeyParameter = string;
6
+ export declare type GetUserDataReturn = any;
7
+ export declare type GetFunctionNameReturn = string;
8
+ export declare type GetRunningTimeInSecondsReturn = number;
9
+ export declare type GetVersionReturn = string;
10
+ export declare type GetMemorySizeReturn = number;
11
+ export declare type GetCPUNumberReturn = number;
12
+ export declare type GetProjectIdReturn = number;
13
+ export declare type GetPackageReturn = string;
14
+ export declare type GetLoggerReturn = {
15
+ info(message: string): void;
16
+ };
17
+ export interface HuaweiContext {
18
+ getRequestID(): GetRequestIDSecondsReturn;
19
+ getRemainingTimeInMilliSeconds(): GetRemainingTimeInMilliSecondsReturn;
20
+ getAccessKey(): GetAccessKeyReturn;
21
+ getSecretKey(): GetSecretKeyReturn;
22
+ getUserData(key: GetUserDataKeyParameter): GetUserDataReturn;
23
+ getFunctionName(): GetFunctionNameReturn;
24
+ getRunningTimeInSeconds(): GetRunningTimeInSecondsReturn;
25
+ getVersion(): GetVersionReturn;
26
+ getMemorySize(): GetMemorySizeReturn;
27
+ getCPUNumber(): GetCPUNumberReturn;
28
+ getProjectID(): GetProjectIdReturn;
29
+ getPackage(): GetPackageReturn;
30
+ getLogger(): GetLoggerReturn;
31
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export * from './huawei-context';
2
+ export * from './huawei-api-gateway-event';
3
+ export * from './huawei-api-gateway-response';
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./huawei-context"), exports);
14
+ __exportStar(require("./huawei-api-gateway-event"), exports);
15
+ __exportStar(require("./huawei-api-gateway-response"), exports);
@@ -73,10 +73,10 @@ class ApiGatewayV2Adapter {
73
73
  const transferEncodingHeader = headers['transfer-encoding'];
74
74
  const hasTransferEncodingChunked = transferEncodingHeader && transferEncodingHeader.includes('chunked');
75
75
  if (hasTransferEncodingChunked) {
76
- throw new Error('chunked encoding in headers is not supported by API Gateway V1');
76
+ throw new Error('chunked encoding in headers is not supported by API Gateway V2');
77
77
  }
78
78
  if (response === null || response === void 0 ? void 0 : response.chunkedEncoding) {
79
- throw new Error('chunked encoding in response is not supported by API Gateway V1');
79
+ throw new Error('chunked encoding in response is not supported by API Gateway V2');
80
80
  }
81
81
  const cookies = multiValueHeaders['set-cookie'];
82
82
  if (headers)
@@ -0,0 +1,27 @@
1
+ import { AdapterContract, AdapterRequest, OnErrorProps } from '../../contracts';
2
+ import { IEmptyResponse } from '../../core';
3
+ /**
4
+ * The class that represents a dummy adapter that does nothing and can be used by the cloud that doesn't use adapters.
5
+ */
6
+ export declare class DummyAdapter implements AdapterContract<any, any, void> {
7
+ /**
8
+ * @inheritDoc
9
+ */
10
+ canHandle(): boolean;
11
+ /**
12
+ * @inheritDoc
13
+ */
14
+ getAdapterName(): string;
15
+ /**
16
+ * @inheritDoc
17
+ */
18
+ getRequest(): AdapterRequest;
19
+ /**
20
+ * @inheritDoc
21
+ */
22
+ getResponse(): IEmptyResponse;
23
+ /**
24
+ * @inheritDoc
25
+ */
26
+ onErrorWhileForwarding(props: OnErrorProps<any, void>): void;
27
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ //#region Imports
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.DummyAdapter = void 0;
5
+ const core_1 = require("../../core");
6
+ //#endregion
7
+ /**
8
+ * The class that represents a dummy adapter that does nothing and can be used by the cloud that doesn't use adapters.
9
+ */
10
+ class DummyAdapter {
11
+ /**
12
+ * @inheritDoc
13
+ */
14
+ canHandle() {
15
+ return true;
16
+ }
17
+ /**
18
+ * @inheritDoc
19
+ */
20
+ getAdapterName() {
21
+ return DummyAdapter.name;
22
+ }
23
+ /**
24
+ * @inheritDoc
25
+ */
26
+ getRequest() {
27
+ return {
28
+ method: 'POST',
29
+ body: void 0,
30
+ path: '/dummy',
31
+ headers: {},
32
+ };
33
+ }
34
+ /**
35
+ * @inheritDoc
36
+ */
37
+ getResponse() {
38
+ return core_1.EmptyResponse;
39
+ }
40
+ /**
41
+ * @inheritDoc
42
+ */
43
+ onErrorWhileForwarding(props) {
44
+ props.delegatedResolver.succeed();
45
+ }
46
+ }
47
+ exports.DummyAdapter = DummyAdapter;
@@ -0,0 +1 @@
1
+ export * from './dummy.adapter';
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./dummy.adapter"), exports);
@@ -0,0 +1,59 @@
1
+ import { HuaweiApiGatewayEvent, HuaweiContext } from '../../@types/huawei';
2
+ import { HuaweiApiGatewayResponse } from '../../@types/huawei/huawei-api-gateway-response';
3
+ import { AdapterContract, AdapterRequest, GetResponseAdapterProps, OnErrorProps } from '../../contracts';
4
+ /**
5
+ * The options to customize the {@link HuaweiApiGatewayAdapter}
6
+ */
7
+ export interface HuaweiApiGatewayOptions {
8
+ /**
9
+ * Strip base path for custom domains
10
+ *
11
+ * @default ''
12
+ */
13
+ stripBasePath?: string;
14
+ }
15
+ /**
16
+ * The adapter to handle requests from Huawei Api Gateway
17
+ *
18
+ * @example```typescript
19
+ * const stripBasePath = '/any/custom/base/path'; // default ''
20
+ * const adapter = new ApiGatewayAdapter({ stripBasePath });
21
+ * ```
22
+ *
23
+ * {@link https://support.huaweicloud.com/intl/en-us/devg-functiongraph/functiongraph_02_0102.html#functiongraph_02_0102__li5178638110137 Event Reference}
24
+ */
25
+ export declare class HuaweiApiGatewayAdapter implements AdapterContract<HuaweiApiGatewayEvent, HuaweiContext, HuaweiApiGatewayResponse> {
26
+ protected readonly options?: HuaweiApiGatewayOptions | undefined;
27
+ /**
28
+ * Default constructor
29
+ *
30
+ * @param options The options to customize the {@link HuaweiApiGatewayAdapter}
31
+ */
32
+ constructor(options?: HuaweiApiGatewayOptions | undefined);
33
+ /**
34
+ * @inheritDoc
35
+ */
36
+ getAdapterName(): string;
37
+ /**
38
+ * @inheritDoc
39
+ */
40
+ canHandle(event: unknown): event is HuaweiApiGatewayEvent;
41
+ /**
42
+ * @inheritDoc
43
+ */
44
+ getRequest(event: HuaweiApiGatewayEvent): AdapterRequest;
45
+ /**
46
+ * @inheritDoc
47
+ */
48
+ getResponse({ headers: responseHeaders, body, isBase64Encoded, statusCode, }: GetResponseAdapterProps<HuaweiApiGatewayEvent>): HuaweiApiGatewayResponse;
49
+ /**
50
+ * @inheritDoc
51
+ */
52
+ onErrorWhileForwarding({ error, delegatedResolver, respondWithErrors, event, log, }: OnErrorProps<HuaweiApiGatewayEvent, HuaweiApiGatewayResponse>): void;
53
+ /**
54
+ * Get path from event with query strings
55
+ *
56
+ * @param event The event sent by serverless
57
+ */
58
+ protected getPathFromEvent(event: HuaweiApiGatewayEvent): string;
59
+ }
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ //#region Imports
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.HuaweiApiGatewayAdapter = void 0;
5
+ const core_1 = require("../../core");
6
+ /**
7
+ * The adapter to handle requests from Huawei Api Gateway
8
+ *
9
+ * @example```typescript
10
+ * const stripBasePath = '/any/custom/base/path'; // default ''
11
+ * const adapter = new ApiGatewayAdapter({ stripBasePath });
12
+ * ```
13
+ *
14
+ * {@link https://support.huaweicloud.com/intl/en-us/devg-functiongraph/functiongraph_02_0102.html#functiongraph_02_0102__li5178638110137 Event Reference}
15
+ */
16
+ class HuaweiApiGatewayAdapter {
17
+ //#region Constructor
18
+ /**
19
+ * Default constructor
20
+ *
21
+ * @param options The options to customize the {@link HuaweiApiGatewayAdapter}
22
+ */
23
+ constructor(options) {
24
+ this.options = options;
25
+ }
26
+ //#endregion
27
+ //#region Public Methods
28
+ /**
29
+ * @inheritDoc
30
+ */
31
+ getAdapterName() {
32
+ return HuaweiApiGatewayAdapter.name;
33
+ }
34
+ /**
35
+ * @inheritDoc
36
+ */
37
+ canHandle(event) {
38
+ const apiGatewayEvent = event;
39
+ return !!(apiGatewayEvent &&
40
+ apiGatewayEvent.httpMethod &&
41
+ apiGatewayEvent.requestContext &&
42
+ apiGatewayEvent.requestContext.apiId &&
43
+ apiGatewayEvent.requestContext.stage &&
44
+ apiGatewayEvent.requestContext.requestId &&
45
+ // to avoid conflict with api gateway v1 of aws
46
+ !('multiValueQueryStringParameters' in apiGatewayEvent));
47
+ }
48
+ /**
49
+ * @inheritDoc
50
+ */
51
+ getRequest(event) {
52
+ const method = event.httpMethod;
53
+ const path = this.getPathFromEvent(event);
54
+ const headers = (0, core_1.getFlattenedHeadersMap)(event.headers, ',', true);
55
+ let body;
56
+ if (event.body) {
57
+ const [bufferBody, contentLength] = (0, core_1.getEventBodyAsBuffer)(event.body, event.isBase64Encoded);
58
+ body = bufferBody;
59
+ headers['content-length'] = String(contentLength);
60
+ }
61
+ const remoteAddress = headers['x-real-ip'];
62
+ return {
63
+ method,
64
+ headers,
65
+ body,
66
+ remoteAddress,
67
+ path,
68
+ };
69
+ }
70
+ /**
71
+ * @inheritDoc
72
+ */
73
+ getResponse({ headers: responseHeaders, body, isBase64Encoded, statusCode, }) {
74
+ const headers = (0, core_1.getMultiValueHeadersMap)(responseHeaders);
75
+ return {
76
+ statusCode,
77
+ body,
78
+ headers,
79
+ isBase64Encoded,
80
+ };
81
+ }
82
+ /**
83
+ * @inheritDoc
84
+ */
85
+ onErrorWhileForwarding({ error, delegatedResolver, respondWithErrors, event, log, }) {
86
+ const body = respondWithErrors ? error.stack : '';
87
+ const errorResponse = this.getResponse({
88
+ event,
89
+ statusCode: 500,
90
+ body: body || '',
91
+ headers: {},
92
+ isBase64Encoded: false,
93
+ log,
94
+ });
95
+ delegatedResolver.succeed(errorResponse);
96
+ }
97
+ //#endregion
98
+ //#region Protected Methods
99
+ /**
100
+ * Get path from event with query strings
101
+ *
102
+ * @param event The event sent by serverless
103
+ */
104
+ getPathFromEvent(event) {
105
+ var _a;
106
+ const stripBasePath = (0, core_1.getDefaultIfUndefined)((_a = this.options) === null || _a === void 0 ? void 0 : _a.stripBasePath, '');
107
+ const replaceRegex = new RegExp(`^${stripBasePath}`);
108
+ const path = event.path.replace(replaceRegex, '');
109
+ const queryParams = event.queryStringParameters;
110
+ return (0, core_1.getPathWithQueryStringParams)(path, queryParams);
111
+ }
112
+ }
113
+ exports.HuaweiApiGatewayAdapter = HuaweiApiGatewayAdapter;
@@ -0,0 +1 @@
1
+ export * from './huawei-api-gateway.adapter';
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./huawei-api-gateway.adapter"), exports);
@@ -1,7 +1,6 @@
1
1
  import { BinarySettings, SingleValueHeaders } from '../../@types';
2
2
  import { AdapterContract, AdapterRequest, FrameworkContract, ResolverContract, ServerlessHandler } from '../../contracts';
3
- import { ILogger } from '../../core';
4
- import { BaseHandler } from '../../core/base-handler';
3
+ import { BaseHandler, ILogger } from '../../core';
5
4
  import { ServerlessResponse } from '../../network';
6
5
  /**
7
6
  * The class that implements a default serverless handler consisting of a function with event, context and callback parameters respectively
@@ -16,13 +16,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.DefaultHandler = void 0;
17
17
  const util_1 = __importDefault(require("util"));
18
18
  const core_1 = require("../../core");
19
- const base_handler_1 = require("../../core/base-handler");
20
19
  const network_1 = require("../../network");
21
20
  //#endregion
22
21
  /**
23
22
  * The class that implements a default serverless handler consisting of a function with event, context and callback parameters respectively
24
23
  */
25
- class DefaultHandler extends base_handler_1.BaseHandler {
24
+ class DefaultHandler extends core_1.BaseHandler {
26
25
  //#region Public Methods
27
26
  /**
28
27
  * @inheritDoc
@@ -0,0 +1,49 @@
1
+ /// <reference types="node" />
2
+ import * as http from 'http';
3
+ import { RequestListener } from 'http';
4
+ import { BinarySettings } from '../../@types';
5
+ import { AdapterContract, FrameworkContract, HandlerContract, ResolverContract, ServerlessHandler } from '../../contracts';
6
+ import { ILogger } from '../../core';
7
+ /**
8
+ * The default port that huawei will proxy the request to your framework
9
+ *
10
+ * {@link https://support.huaweicloud.com/intl/en-us/ae-ad-1-usermanual-functiongraph/functiongraph_01_1442.html#functiongraph_01_1442__li194597302096 Reference}
11
+ */
12
+ export declare const DEFAULT_HUAWEI_LISTEN_PORT: number;
13
+ /**
14
+ * The options to customize {@link HttpHuaweiHandler}
15
+ */
16
+ export declare type HttpHuaweiHandlerOptions = {
17
+ /**
18
+ * @default {@link DEFAULT_HUAWEI_LISTEN_PORT}
19
+ */
20
+ port?: number;
21
+ /**
22
+ * The factory to create a http server to use to listen huawei requests
23
+ */
24
+ httpServerFactory?: (requestListener: RequestListener) => http.Server;
25
+ };
26
+ /**
27
+ * The class that implements a huawei serverless handler with http function that exposes a http server in specific port.
28
+ *
29
+ * In this Handler, you don't need to specific resolver and adapter, so you can use DummyAdapter and DummyResolver instead.
30
+ *
31
+ * @see https://support.huaweicloud.com/intl/en-us/ae-ad-1-usermanual-functiongraph/functiongraph_01_1442.html#functiongraph_01_1442__li194597302096
32
+ */
33
+ export declare class HttpHuaweiHandler<TApp> implements HandlerContract<TApp, void, void, void, void, Promise<void>> {
34
+ protected readonly options?: HttpHuaweiHandlerOptions | undefined;
35
+ /**
36
+ * Construtor padrão
37
+ */
38
+ constructor(options?: HttpHuaweiHandlerOptions | undefined);
39
+ /**
40
+ * @inheritDoc
41
+ */
42
+ getHandler(app: TApp, framework: FrameworkContract<TApp>, _: AdapterContract<void, void, void>[], __: ResolverContract<void, void, void, void, void>, binarySettings: BinarySettings, respondWithErrors: boolean, log: ILogger): ServerlessHandler<Promise<void>>;
43
+ /**
44
+ * The method that creates the instance of http server
45
+ *
46
+ * @param requestListener O método que lidará com as requisições recebidas
47
+ */
48
+ protected createHttpServer(requestListener: RequestListener): http.Server;
49
+ }
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ //#region Imports
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
6
+ }) : (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ o[k2] = m[k];
9
+ }));
10
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
11
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
12
+ }) : function(o, v) {
13
+ o["default"] = v;
14
+ });
15
+ var __importStar = (this && this.__importStar) || function (mod) {
16
+ if (mod && mod.__esModule) return mod;
17
+ var result = {};
18
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
19
+ __setModuleDefault(result, mod);
20
+ return result;
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.HttpHuaweiHandler = exports.DEFAULT_HUAWEI_LISTEN_PORT = void 0;
24
+ const http = __importStar(require("http"));
25
+ const core_1 = require("../../core");
26
+ //#endregion
27
+ /**
28
+ * The default port that huawei will proxy the request to your framework
29
+ *
30
+ * {@link https://support.huaweicloud.com/intl/en-us/ae-ad-1-usermanual-functiongraph/functiongraph_01_1442.html#functiongraph_01_1442__li194597302096 Reference}
31
+ */
32
+ exports.DEFAULT_HUAWEI_LISTEN_PORT = 8000;
33
+ /**
34
+ * The class that implements a huawei serverless handler with http function that exposes a http server in specific port.
35
+ *
36
+ * In this Handler, you don't need to specific resolver and adapter, so you can use DummyAdapter and DummyResolver instead.
37
+ *
38
+ * @see https://support.huaweicloud.com/intl/en-us/ae-ad-1-usermanual-functiongraph/functiongraph_01_1442.html#functiongraph_01_1442__li194597302096
39
+ */
40
+ class HttpHuaweiHandler {
41
+ //#region Constructor
42
+ /**
43
+ * Construtor padrão
44
+ */
45
+ constructor(options) {
46
+ this.options = options;
47
+ }
48
+ //#endregion
49
+ //#region Public Methods
50
+ /**
51
+ * @inheritDoc
52
+ */
53
+ getHandler(app, framework, _, __, binarySettings, respondWithErrors, log) {
54
+ var _a, _b;
55
+ const requestListener = (req, res) => {
56
+ framework.sendRequest(app, req, res);
57
+ };
58
+ const server = (0, core_1.getDefaultIfUndefined)((_a = this.options) === null || _a === void 0 ? void 0 : _a.httpServerFactory, this.createHttpServer.bind(this))(requestListener);
59
+ const port = (0, core_1.getDefaultIfUndefined)((_b = this.options) === null || _b === void 0 ? void 0 : _b.port, exports.DEFAULT_HUAWEI_LISTEN_PORT);
60
+ server.listen(port, () => {
61
+ log.debug('SERVERLESS_ADAPTER:PROXY: Server started.');
62
+ log.debug(`SERVERLESS_ADAPTER:PROXY: Listening the Huawei Requests in port ${port}`);
63
+ });
64
+ return () => {
65
+ log.debug('SERVERLESS_ADAPTER:PROXY: Disposing the server');
66
+ return new Promise((resolve, reject) => {
67
+ server.close(err => (err ? reject(err) : resolve()));
68
+ });
69
+ };
70
+ }
71
+ //#endregion
72
+ //#region Protected Methods
73
+ /**
74
+ * The method that creates the instance of http server
75
+ *
76
+ * @param requestListener O método que lidará com as requisições recebidas
77
+ */
78
+ createHttpServer(requestListener) {
79
+ return http.createServer(requestListener);
80
+ }
81
+ }
82
+ exports.HttpHuaweiHandler = HttpHuaweiHandler;
@@ -0,0 +1 @@
1
+ export * from './http-huawei.handler';
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./http-huawei.handler"), exports);
@@ -0,0 +1,10 @@
1
+ import { Resolver, ResolverContract } from '../../contracts';
2
+ /**
3
+ * The class that represents a dummy resolver that does nothing and can be used by the cloud that doesn't use resolvers.
4
+ */
5
+ export declare class DummyResolver implements ResolverContract<any, any, any, any, any> {
6
+ /**
7
+ * @inheritDoc
8
+ */
9
+ createResolver(): Resolver<any, void>;
10
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ //#region Imports
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.DummyResolver = void 0;
5
+ //#endregion
6
+ /**
7
+ * The class that represents a dummy resolver that does nothing and can be used by the cloud that doesn't use resolvers.
8
+ */
9
+ class DummyResolver {
10
+ /**
11
+ * @inheritDoc
12
+ */
13
+ createResolver() {
14
+ return {
15
+ run: () => Promise.resolve(),
16
+ };
17
+ }
18
+ }
19
+ exports.DummyResolver = DummyResolver;
@@ -0,0 +1 @@
1
+ export * from './dummy.resolver';
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./dummy.resolver"), exports);
@@ -29,7 +29,7 @@ export declare class ServerlessAdapter<TApp, TEvent, TContext, TCallback, TRespo
29
29
  /**
30
30
  * Settings for whether the response should be treated as binary or not
31
31
  *
32
- * @default `contentEncodings` and `contentTypes` are set with {@link DEFAULT_BINARY_ENCODINGS} and {@link DEFAULT_BINARY_ENCODINGS}, respectively.
32
+ * @default `contentEncodings` and `contentTypes` are set with {@link DEFAULT_BINARY_ENCODINGS} and {@link DEFAULT_BINARY_CONTENT_TYPES}, respectively.
33
33
  */
34
34
  protected binarySettings: BinarySettings;
35
35
  /**
@@ -31,11 +31,11 @@ class ServerlessAdapter {
31
31
  /**
32
32
  * Settings for whether the response should be treated as binary or not
33
33
  *
34
- * @default `contentEncodings` and `contentTypes` are set with {@link DEFAULT_BINARY_ENCODINGS} and {@link DEFAULT_BINARY_ENCODINGS}, respectively.
34
+ * @default `contentEncodings` and `contentTypes` are set with {@link DEFAULT_BINARY_ENCODINGS} and {@link DEFAULT_BINARY_CONTENT_TYPES}, respectively.
35
35
  */
36
36
  this.binarySettings = {
37
37
  contentEncodings: core_1.DEFAULT_BINARY_ENCODINGS,
38
- contentTypes: core_1.DEFAULT_BINARY_ENCODINGS,
38
+ contentTypes: core_1.DEFAULT_BINARY_CONTENT_TYPES,
39
39
  };
40
40
  /**
41
41
  * Indicates whether the error stack should be included in the response or not
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h4ad/serverless-adapter",
3
- "version": "2.0.2",
3
+ "version": "2.2.0",
4
4
  "description": "This library enables you to utilize any Cloud or Event Source to respond to web and API requests using your existing Node.js application framework.",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -46,7 +46,9 @@
46
46
  "hapi",
47
47
  "fastify",
48
48
  "node.js",
49
- "http"
49
+ "http",
50
+ "huawei",
51
+ "functiongraph"
50
52
  ],
51
53
  "bugs": {
52
54
  "url": "https://github.com/H4ad/serverless-adapter/issues"
@@ -57,6 +59,7 @@
57
59
  "@types/body-parser": "^1.19.2",
58
60
  "@types/jest": "^27.4.0",
59
61
  "@types/node": "12.20.43",
62
+ "@types/supertest": "^2.0.12",
60
63
  "@typescript-eslint/eslint-plugin": "^5.12.1",
61
64
  "@typescript-eslint/parser": "^5.12.1",
62
65
  "body-parser": "^1.19.1",
@@ -75,6 +78,7 @@
75
78
  "prettier": "^2.5.1",
76
79
  "semantic-release": "^19.0.2",
77
80
  "stream-mock": "^2.0.5",
81
+ "supertest": "^6.2.3",
78
82
  "ts-jest": "^27.1.3",
79
83
  "ts-node": "^10.4.0",
80
84
  "typescript": "^4.5.5"