@h4ad/serverless-adapter 2.1.0 → 2.3.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.
- package/README.md +40 -3
- package/lib/@types/huawei/huawei-api-gateway-event.d.ts +19 -0
- package/lib/@types/huawei/huawei-api-gateway-event.js +2 -0
- package/lib/@types/huawei/huawei-api-gateway-response.d.ts +7 -0
- package/lib/@types/huawei/huawei-api-gateway-response.js +2 -0
- package/lib/@types/huawei/huawei-context.d.ts +31 -0
- package/lib/@types/huawei/huawei-context.js +2 -0
- package/lib/@types/huawei/index.d.ts +3 -0
- package/lib/@types/huawei/index.js +15 -0
- package/lib/adapters/aws/api-gateway-v2.adapter.js +2 -2
- package/lib/adapters/huawei/huawei-api-gateway.adapter.d.ts +59 -0
- package/lib/adapters/huawei/huawei-api-gateway.adapter.js +113 -0
- package/lib/adapters/huawei/index.d.ts +1 -0
- package/lib/adapters/huawei/index.js +13 -0
- package/lib/frameworks/lazy/index.d.ts +1 -0
- package/lib/frameworks/lazy/index.js +13 -0
- package/lib/frameworks/lazy/lazy.framework.d.ts +51 -0
- package/lib/frameworks/lazy/lazy.framework.js +70 -0
- package/lib/resolvers/dummy/index.d.ts +1 -0
- package/lib/resolvers/dummy/index.js +13 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
<a href="#install">Install</a> |
|
|
7
7
|
<a href="#usage">Usage</a> |
|
|
8
8
|
<a href="#support">Support</a> |
|
|
9
|
+
<a href="#examples">Examples</a> |
|
|
9
10
|
<a href="#architecture">Architecture</a> |
|
|
10
11
|
<a href="#credits">Credits</a>
|
|
11
12
|
</p>
|
|
@@ -62,6 +63,10 @@ import { DefaultHandler } from '@h4ad/serverless-adapter/lib/handlers/default';
|
|
|
62
63
|
import { PromiseResolver } from '@h4ad/serverless-adapter/lib/resolvers/promise';
|
|
63
64
|
import app from './app';
|
|
64
65
|
|
|
66
|
+
// if you create your app asynchronously
|
|
67
|
+
// check the docs about the LazyFramework.
|
|
68
|
+
// ServerlessAdapter.new(null)
|
|
69
|
+
// .setFramework(new LazyFramework(new ExpressFramework(), async () => createAsyncApp()))
|
|
65
70
|
export const handler = ServerlessAdapter.new(app)
|
|
66
71
|
.setFramework(new ExpressFramework())
|
|
67
72
|
.setHandler(new DefaultHandler())
|
|
@@ -118,6 +123,9 @@ Currently, we support these frameworks:
|
|
|
118
123
|
- [Fastify](https://www.fastify.io/) by using ([FastifyFramework](src/frameworks/fastify/fastify.framework.ts))
|
|
119
124
|
- [Hapi](https://hapi.dev/) by using ([HapiFramework](src/frameworks/hapi/hapi.framework.ts))
|
|
120
125
|
- [Koa](https://koajs.com/) by using ([KoaFramework](src/frameworks/koa/koa.framework.ts))
|
|
126
|
+
- Async Initialization by using ([LazyFramework](src/frameworks/lazy/lazy.framework.ts))
|
|
127
|
+
- Use this framework to provide a way to create the instance of your app asynchronously.
|
|
128
|
+
- With him, you can create an instance of Express or Fastify asynchronously, [see the docs](src/frameworks/lazy/lazy.framework.ts).
|
|
121
129
|
|
|
122
130
|
We support these event sources:
|
|
123
131
|
|
|
@@ -140,7 +148,8 @@ We support these event sources:
|
|
|
140
148
|
using ([SQSAdapter](./src/adapters/aws/sqs.adapter.ts))
|
|
141
149
|
- Huawei
|
|
142
150
|
- [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.
|
|
143
|
-
- [Event Function](https://support.huaweicloud.com/intl/en-us/usermanual-functiongraph/functiongraph_01_1441.html):
|
|
151
|
+
- [Event Function](https://support.huaweicloud.com/intl/en-us/usermanual-functiongraph/functiongraph_01_1441.html):
|
|
152
|
+
- [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)).
|
|
144
153
|
- Azure
|
|
145
154
|
- [The support is coming soon.](https://github.com/H4ad/serverless-adapter/issues/3)
|
|
146
155
|
- Firebase
|
|
@@ -161,7 +170,7 @@ We support these handlers:
|
|
|
161
170
|
|
|
162
171
|
## Huawei
|
|
163
172
|
|
|
164
|
-
In Huawei,
|
|
173
|
+
In Huawei, we added support to FunctionGraphV2 with Http Function and Event Function.
|
|
165
174
|
|
|
166
175
|
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.
|
|
167
176
|
So, on implementation, this library will create an http server to listen on port 8000 and forward the request to your framework.
|
|
@@ -216,7 +225,35 @@ In the end, the structure of the zip file you upload looks like this:
|
|
|
216
225
|
|
|
217
226
|
### Huawei Event Function
|
|
218
227
|
|
|
219
|
-
|
|
228
|
+
With Http Function you need to use [HttpHuaweiHandler](src/handlers/huawei/http-huawei.handler.ts),
|
|
229
|
+
but with Event Function you should use [DefaultHandler](src/handlers/default/default.handler.ts).
|
|
230
|
+
|
|
231
|
+
So, to add support to Api Gateway you do the following:
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
import { ServerlessAdapter } from '@h4ad/serverless-adapter';
|
|
235
|
+
import { HuaweiApiGatewayAdapter } from '@h4ad/serverless-adapter/lib/adapters/huawei';
|
|
236
|
+
import { ExpressFramework } from '@h4ad/serverless-adapter/lib/frameworks/express';
|
|
237
|
+
import { DefaultHandler } from '@h4ad/serverless-adapter/lib/handlers/default';
|
|
238
|
+
import { CallbackResolver } from '@h4ad/serverless-adapter/lib/resolvers/callback';
|
|
239
|
+
import app from './app';
|
|
240
|
+
|
|
241
|
+
export const handler = ServerlessAdapter.new(app)
|
|
242
|
+
.setFramework(new ExpressFramework())
|
|
243
|
+
.setHandler(new DefaultHandler())
|
|
244
|
+
.setResolver(new CallbackResolver())
|
|
245
|
+
.addAdapter(new HuaweiApiGatewayAdapter())
|
|
246
|
+
.build();
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
#### One important thing
|
|
250
|
+
|
|
251
|
+
You must use the callback resolver because I couldn't get it to work with the PromiseResolver.
|
|
252
|
+
Maybe it's a bug in the library or something specific in Huawei, if you have a tip please create an issue.
|
|
253
|
+
|
|
254
|
+
# Examples
|
|
255
|
+
|
|
256
|
+
You can see some examples of how to use this library [here](https://github.com/H4ad/serverless-adapter-examples).
|
|
220
257
|
|
|
221
258
|
# Architecture
|
|
222
259
|
|
|
@@ -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,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,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
|
|
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
|
|
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,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);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lazy.framework';
|
|
@@ -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("./lazy.framework"), exports);
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IncomingMessage, ServerResponse } from 'http';
|
|
3
|
+
import { FrameworkContract } from '../../contracts';
|
|
4
|
+
import { ILogger } from '../../core';
|
|
5
|
+
/**
|
|
6
|
+
* The framework that asynchronously instantiates your application and forwards the request to the framework as quickly as possible.
|
|
7
|
+
*
|
|
8
|
+
* @example```typescript
|
|
9
|
+
* import express from 'express';
|
|
10
|
+
* import { ServerlessAdapter } from '@h4ad/serverless-adapter';
|
|
11
|
+
* import { ExpressFramework } from '@h4ad/serverless-adapter/lib/frameworks/express';
|
|
12
|
+
* import { LazyFramework } from '@h4ad/serverless-adapter/lib/frameworks/lazy';
|
|
13
|
+
*
|
|
14
|
+
* const expressFramework = new ExpressFramework();
|
|
15
|
+
* const factory = async () => {
|
|
16
|
+
* const app = express();
|
|
17
|
+
*
|
|
18
|
+
* // do some asynchronous stuffs like create the database;
|
|
19
|
+
* await new Promise(resolve => setTimeout(resolve, 100);
|
|
20
|
+
*
|
|
21
|
+
* return app;
|
|
22
|
+
* };
|
|
23
|
+
* const framework = new LazyFramework(expressFramework, factory);
|
|
24
|
+
*
|
|
25
|
+
* export const handler = ServerlessAdapter.new(null)
|
|
26
|
+
* .setFramework(framework)
|
|
27
|
+
* // set other configurations and then build
|
|
28
|
+
* .build();
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare class LazyFramework<TApp> implements FrameworkContract<null> {
|
|
32
|
+
protected readonly framework: FrameworkContract<TApp>;
|
|
33
|
+
protected readonly factory: () => Promise<TApp>;
|
|
34
|
+
protected readonly logger: ILogger;
|
|
35
|
+
/**
|
|
36
|
+
* Default Constructor
|
|
37
|
+
*/
|
|
38
|
+
constructor(framework: FrameworkContract<TApp>, factory: () => Promise<TApp>, logger?: ILogger);
|
|
39
|
+
/**
|
|
40
|
+
* The cached version of the app
|
|
41
|
+
*/
|
|
42
|
+
protected cachedApp?: TApp;
|
|
43
|
+
/**
|
|
44
|
+
* The delayed factory to create an instance of the app
|
|
45
|
+
*/
|
|
46
|
+
protected readonly delayedFactory: Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* @inheritDoc
|
|
49
|
+
*/
|
|
50
|
+
sendRequest(app: null, request: IncomingMessage, response: ServerResponse): void;
|
|
51
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region Imports
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.LazyFramework = void 0;
|
|
5
|
+
const core_1 = require("../../core");
|
|
6
|
+
//#endregion
|
|
7
|
+
/**
|
|
8
|
+
* The framework that asynchronously instantiates your application and forwards the request to the framework as quickly as possible.
|
|
9
|
+
*
|
|
10
|
+
* @example```typescript
|
|
11
|
+
* import express from 'express';
|
|
12
|
+
* import { ServerlessAdapter } from '@h4ad/serverless-adapter';
|
|
13
|
+
* import { ExpressFramework } from '@h4ad/serverless-adapter/lib/frameworks/express';
|
|
14
|
+
* import { LazyFramework } from '@h4ad/serverless-adapter/lib/frameworks/lazy';
|
|
15
|
+
*
|
|
16
|
+
* const expressFramework = new ExpressFramework();
|
|
17
|
+
* const factory = async () => {
|
|
18
|
+
* const app = express();
|
|
19
|
+
*
|
|
20
|
+
* // do some asynchronous stuffs like create the database;
|
|
21
|
+
* await new Promise(resolve => setTimeout(resolve, 100);
|
|
22
|
+
*
|
|
23
|
+
* return app;
|
|
24
|
+
* };
|
|
25
|
+
* const framework = new LazyFramework(expressFramework, factory);
|
|
26
|
+
*
|
|
27
|
+
* export const handler = ServerlessAdapter.new(null)
|
|
28
|
+
* .setFramework(framework)
|
|
29
|
+
* // set other configurations and then build
|
|
30
|
+
* .build();
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
class LazyFramework {
|
|
34
|
+
//#region Constructor
|
|
35
|
+
/**
|
|
36
|
+
* Default Constructor
|
|
37
|
+
*/
|
|
38
|
+
constructor(framework, factory, logger = (0, core_1.createDefaultLogger)()) {
|
|
39
|
+
this.framework = framework;
|
|
40
|
+
this.factory = factory;
|
|
41
|
+
this.logger = logger;
|
|
42
|
+
this.delayedFactory = Promise.resolve()
|
|
43
|
+
.then(() => factory())
|
|
44
|
+
.then(app => {
|
|
45
|
+
this.cachedApp = app;
|
|
46
|
+
})
|
|
47
|
+
.catch((error) => {
|
|
48
|
+
// deal with the error only when receive some request
|
|
49
|
+
// to be able to return some message to user
|
|
50
|
+
this.logger.error('SERVERLESS_ADAPTER:LAZY_FRAMEWORK: An error occours during the creation of your app.');
|
|
51
|
+
this.logger.error(error);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region Public Methods
|
|
56
|
+
/**
|
|
57
|
+
* @inheritDoc
|
|
58
|
+
*/
|
|
59
|
+
sendRequest(app, request, response) {
|
|
60
|
+
if (this.cachedApp)
|
|
61
|
+
return this.framework.sendRequest(this.cachedApp, request, response);
|
|
62
|
+
this.delayedFactory.then(() => {
|
|
63
|
+
if (!this.cachedApp) {
|
|
64
|
+
return response.emit('error', new Error('SERVERLESS_ADAPTER:LAZY_FRAMEWORK: The instance of the app returned by the factory is not valid, see the logs to learn more.'));
|
|
65
|
+
}
|
|
66
|
+
return this.framework.sendRequest(this.cachedApp, request, response);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.LazyFramework = LazyFramework;
|
|
@@ -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);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@h4ad/serverless-adapter",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.3.1",
|
|
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": [
|
|
@@ -90,6 +90,7 @@
|
|
|
90
90
|
"@types/koa": ">= 2.11.2",
|
|
91
91
|
"express": ">= 4.15.4",
|
|
92
92
|
"fastify": ">= 3.0.0",
|
|
93
|
+
"fastify-3": "npm:fastify@3.29.0",
|
|
93
94
|
"koa": ">= 2.5.1"
|
|
94
95
|
},
|
|
95
96
|
"peerDependenciesMeta": {
|
|
@@ -113,6 +114,9 @@
|
|
|
113
114
|
},
|
|
114
115
|
"@types/koa": {
|
|
115
116
|
"optional": true
|
|
117
|
+
},
|
|
118
|
+
"fastify-3": {
|
|
119
|
+
"optional": true
|
|
116
120
|
}
|
|
117
121
|
},
|
|
118
122
|
"config": {
|