@h4ad/serverless-adapter 2.2.0 → 2.3.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.
package/README.md
CHANGED
|
@@ -63,6 +63,10 @@ import { DefaultHandler } from '@h4ad/serverless-adapter/lib/handlers/default';
|
|
|
63
63
|
import { PromiseResolver } from '@h4ad/serverless-adapter/lib/resolvers/promise';
|
|
64
64
|
import app from './app';
|
|
65
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()))
|
|
66
70
|
export const handler = ServerlessAdapter.new(app)
|
|
67
71
|
.setFramework(new ExpressFramework())
|
|
68
72
|
.setHandler(new DefaultHandler())
|
|
@@ -119,6 +123,9 @@ Currently, we support these frameworks:
|
|
|
119
123
|
- [Fastify](https://www.fastify.io/) by using ([FastifyFramework](src/frameworks/fastify/fastify.framework.ts))
|
|
120
124
|
- [Hapi](https://hapi.dev/) by using ([HapiFramework](src/frameworks/hapi/hapi.framework.ts))
|
|
121
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).
|
|
122
129
|
|
|
123
130
|
We support these event sources:
|
|
124
131
|
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@h4ad/serverless-adapter",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.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": [
|