@h4ad/serverless-adapter 1.0.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/LICENSE +21 -0
- package/README.md +204 -0
- package/lib/@types/binary-settings.d.ts +23 -0
- package/lib/@types/binary-settings.js +2 -0
- package/lib/@types/headers.d.ts +24 -0
- package/lib/@types/headers.js +2 -0
- package/lib/@types/helpers.d.ts +6 -0
- package/lib/@types/helpers.js +2 -0
- package/lib/@types/index.d.ts +3 -0
- package/lib/@types/index.js +15 -0
- package/lib/@types/proxy-params.d.ts +8 -0
- package/lib/@types/proxy-params.js +2 -0
- package/lib/adapters/aws/alb.adapter.d.ts +58 -0
- package/lib/adapters/aws/alb.adapter.js +119 -0
- package/lib/adapters/aws/api-gateway-v1.adapter.d.ts +63 -0
- package/lib/adapters/aws/api-gateway-v1.adapter.js +126 -0
- package/lib/adapters/aws/api-gateway-v2.adapter.d.ts +61 -0
- package/lib/adapters/aws/api-gateway-v2.adapter.js +123 -0
- package/lib/adapters/aws/dynamodb.adapter.d.ts +62 -0
- package/lib/adapters/aws/dynamodb.adapter.js +81 -0
- package/lib/adapters/aws/event-bridge.adapter.d.ts +66 -0
- package/lib/adapters/aws/event-bridge.adapter.js +91 -0
- package/lib/adapters/aws/index.d.ts +8 -0
- package/lib/adapters/aws/index.js +20 -0
- package/lib/adapters/aws/lambda-edge.adapter.d.ts +179 -0
- package/lib/adapters/aws/lambda-edge.adapter.js +248 -0
- package/lib/adapters/aws/sns.adapter.d.ts +62 -0
- package/lib/adapters/aws/sns.adapter.js +81 -0
- package/lib/adapters/aws/sqs.adapter.d.ts +62 -0
- package/lib/adapters/aws/sqs.adapter.js +81 -0
- package/lib/adapters/index.d.ts +1 -0
- package/lib/adapters/index.js +13 -0
- package/lib/contracts/adapter.contract.d.ts +152 -0
- package/lib/contracts/adapter.contract.js +2 -0
- package/lib/contracts/framework.contract.d.ts +15 -0
- package/lib/contracts/framework.contract.js +3 -0
- package/lib/contracts/handler.contract.d.ts +18 -0
- package/lib/contracts/handler.contract.js +3 -0
- package/lib/contracts/index.d.ts +4 -0
- package/lib/contracts/index.js +16 -0
- package/lib/contracts/resolver.contract.d.ts +70 -0
- package/lib/contracts/resolver.contract.js +3 -0
- package/lib/core/constants.d.ts +4 -0
- package/lib/core/constants.js +10 -0
- package/lib/core/current-invoke.d.ts +37 -0
- package/lib/core/current-invoke.js +34 -0
- package/lib/core/event-body.d.ts +16 -0
- package/lib/core/event-body.js +24 -0
- package/lib/core/headers.d.ts +27 -0
- package/lib/core/headers.js +53 -0
- package/lib/core/index.d.ts +10 -0
- package/lib/core/index.js +22 -0
- package/lib/core/is-binary.d.ts +55 -0
- package/lib/core/is-binary.js +91 -0
- package/lib/core/logger.d.ts +38 -0
- package/lib/core/logger.js +51 -0
- package/lib/core/no-op.d.ts +4 -0
- package/lib/core/no-op.js +8 -0
- package/lib/core/optional.d.ts +17 -0
- package/lib/core/optional.js +25 -0
- package/lib/core/path.d.ts +28 -0
- package/lib/core/path.js +56 -0
- package/lib/core/stream.d.ts +8 -0
- package/lib/core/stream.js +39 -0
- package/lib/frameworks/express.framework.d.ts +13 -0
- package/lib/frameworks/express.framework.js +17 -0
- package/lib/frameworks/fastify.framework.d.ts +13 -0
- package/lib/frameworks/fastify.framework.js +18 -0
- package/lib/frameworks/hapi.framework.d.ts +13 -0
- package/lib/frameworks/hapi.framework.js +18 -0
- package/lib/frameworks/index.d.ts +4 -0
- package/lib/frameworks/index.js +16 -0
- package/lib/frameworks/koa.framework.d.ts +13 -0
- package/lib/frameworks/koa.framework.js +17 -0
- package/lib/handlers/base/base.handler.d.ts +28 -0
- package/lib/handlers/base/base.handler.js +52 -0
- package/lib/handlers/base/index.d.ts +1 -0
- package/lib/handlers/base/index.js +13 -0
- package/lib/handlers/default.handler.d.ts +85 -0
- package/lib/handlers/default.handler.js +183 -0
- package/lib/handlers/index.d.ts +2 -0
- package/lib/handlers/index.js +14 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +21 -0
- package/lib/network/index.d.ts +2 -0
- package/lib/network/index.js +14 -0
- package/lib/network/request.d.ts +15 -0
- package/lib/network/request.js +40 -0
- package/lib/network/response.d.ts +22 -0
- package/lib/network/response.js +104 -0
- package/lib/resolvers/aws-context.resolver.d.ts +21 -0
- package/lib/resolvers/aws-context.resolver.js +55 -0
- package/lib/resolvers/callback.resolver.d.ts +16 -0
- package/lib/resolvers/callback.resolver.js +40 -0
- package/lib/resolvers/index.d.ts +3 -0
- package/lib/resolvers/index.js +15 -0
- package/lib/resolvers/promise.resolver.d.ts +10 -0
- package/lib/resolvers/promise.resolver.js +38 -0
- package/lib/serverless-adapter.d.ts +114 -0
- package/lib/serverless-adapter.js +158 -0
- package/package.json +130 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region Imports
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.AwsContextResolver = void 0;
|
|
5
|
+
//#endregion
|
|
6
|
+
/**
|
|
7
|
+
* The class that implements the resolver by using the AWS Context object.
|
|
8
|
+
*
|
|
9
|
+
* @note To use this resolver, you MUST leave `{@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html callbackWaitsForEmptyEventLoop}` as true, otherwise, AWS will not wait for this resolver to resolve.
|
|
10
|
+
*
|
|
11
|
+
* ```md
|
|
12
|
+
* From the AWS Documentation, describing the functions used in this resolver:
|
|
13
|
+
* // Functions for compatibility with earlier Node.js Runtime v0.10.42
|
|
14
|
+
* // No longer documented, so they are deprecated, but they still work
|
|
15
|
+
* // as of the 12.x runtime, so they are not removed from the types.
|
|
16
|
+
* ```
|
|
17
|
+
* @deprecated
|
|
18
|
+
*/
|
|
19
|
+
class AwsContextResolver {
|
|
20
|
+
/**
|
|
21
|
+
* @inheritDoc
|
|
22
|
+
*/
|
|
23
|
+
createResolver({ context, event, log, respondWithErrors, adapter, }) {
|
|
24
|
+
if (!context) {
|
|
25
|
+
throw new Error('Could not figure out how to create the resolver because the "context" argument was not sent.');
|
|
26
|
+
}
|
|
27
|
+
if (!context.succeed) {
|
|
28
|
+
throw new Error('Could not figure out how to create the resolver because the "context" argument didn\'t have the "succeed" function.');
|
|
29
|
+
}
|
|
30
|
+
if (!context.fail) {
|
|
31
|
+
throw new Error('Could not figure out how to create the resolver because the "context" argument didn\'t have the "fail" function.');
|
|
32
|
+
}
|
|
33
|
+
const delegatedResolver = {
|
|
34
|
+
succeed: response => context.succeed(response),
|
|
35
|
+
fail: error => context.fail(error),
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
run: task => {
|
|
39
|
+
task()
|
|
40
|
+
.then(response => delegatedResolver.succeed(response))
|
|
41
|
+
.catch(error => {
|
|
42
|
+
log.error('SERVERLESS_ADAPTER:RESPOND_TO_EVENT_SOURCE_WITH_ERROR', error);
|
|
43
|
+
adapter.onErrorWhileForwarding({
|
|
44
|
+
delegatedResolver,
|
|
45
|
+
error,
|
|
46
|
+
log,
|
|
47
|
+
event,
|
|
48
|
+
respondWithErrors,
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.AwsContextResolver = AwsContextResolver;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Resolver, ResolverContract, ResolverProps } from '../contracts';
|
|
2
|
+
/**
|
|
3
|
+
* The default signature of the callback sent by serverless
|
|
4
|
+
*/
|
|
5
|
+
export declare type ServerlessCallback<TResponse> = (error: Error | null, success: TResponse | null) => void;
|
|
6
|
+
/**
|
|
7
|
+
* The class that implements the resolver using the callback function sent by serverless
|
|
8
|
+
*
|
|
9
|
+
* @note To use this resolver on AWS, you MUST leave `{@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html callbackWaitsForEmptyEventLoop}` as true, otherwise, AWS will not wait for this resolver to resolve.
|
|
10
|
+
*/
|
|
11
|
+
export declare class CallbackResolver<TEvent, TContext, TResponse> implements ResolverContract<TEvent, TContext, ServerlessCallback<any>, TResponse, void> {
|
|
12
|
+
/**
|
|
13
|
+
* @inheritDoc
|
|
14
|
+
*/
|
|
15
|
+
createResolver({ callback, event, log, respondWithErrors, adapter, }: ResolverProps<TEvent, TContext, ServerlessCallback<any>, TResponse>): Resolver<any, void>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region Imports
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.CallbackResolver = void 0;
|
|
5
|
+
/**
|
|
6
|
+
* The class that implements the resolver using the callback function sent by serverless
|
|
7
|
+
*
|
|
8
|
+
* @note To use this resolver on AWS, you MUST leave `{@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html callbackWaitsForEmptyEventLoop}` as true, otherwise, AWS will not wait for this resolver to resolve.
|
|
9
|
+
*/
|
|
10
|
+
class CallbackResolver {
|
|
11
|
+
/**
|
|
12
|
+
* @inheritDoc
|
|
13
|
+
*/
|
|
14
|
+
createResolver({ callback, event, log, respondWithErrors, adapter, }) {
|
|
15
|
+
if (!callback) {
|
|
16
|
+
throw new Error('Could not figure out how to create the resolver because the "callback" argument was not sent.');
|
|
17
|
+
}
|
|
18
|
+
const delegatedResolver = {
|
|
19
|
+
succeed: response => callback(null, response),
|
|
20
|
+
fail: error => callback(error, null),
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
run: task => {
|
|
24
|
+
task()
|
|
25
|
+
.then(response => delegatedResolver.succeed(response))
|
|
26
|
+
.catch(error => {
|
|
27
|
+
log.error('SERVERLESS_ADAPTER:RESPOND_TO_EVENT_SOURCE_WITH_ERROR', error);
|
|
28
|
+
adapter.onErrorWhileForwarding({
|
|
29
|
+
delegatedResolver,
|
|
30
|
+
error,
|
|
31
|
+
log,
|
|
32
|
+
event,
|
|
33
|
+
respondWithErrors,
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.CallbackResolver = CallbackResolver;
|
|
@@ -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("./aws-context.resolver"), exports);
|
|
14
|
+
__exportStar(require("./callback.resolver"), exports);
|
|
15
|
+
__exportStar(require("./promise.resolver"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Resolver, ResolverContract, ResolverProps } from '../contracts';
|
|
2
|
+
/**
|
|
3
|
+
* The class that implements the resolver using the promise object sent by this library
|
|
4
|
+
*/
|
|
5
|
+
export declare class PromiseResolver<TEvent, TContext, TCallback, TResponse, TReturn> implements ResolverContract<TEvent, TContext, TCallback, TResponse, Promise<any>> {
|
|
6
|
+
/**
|
|
7
|
+
* @inheritDoc
|
|
8
|
+
*/
|
|
9
|
+
createResolver({ event, log, respondWithErrors, adapter, }: ResolverProps<TEvent, TContext, TCallback, TResponse>): Resolver<TResponse, Promise<TReturn>>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region Imports
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.PromiseResolver = void 0;
|
|
5
|
+
//#endregion
|
|
6
|
+
/**
|
|
7
|
+
* The class that implements the resolver using the promise object sent by this library
|
|
8
|
+
*/
|
|
9
|
+
class PromiseResolver {
|
|
10
|
+
/**
|
|
11
|
+
* @inheritDoc
|
|
12
|
+
*/
|
|
13
|
+
createResolver({ event, log, respondWithErrors, adapter, }) {
|
|
14
|
+
return {
|
|
15
|
+
run: task => {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
const delegatedResolver = {
|
|
18
|
+
succeed: response => resolve(response),
|
|
19
|
+
fail: error => reject(error),
|
|
20
|
+
};
|
|
21
|
+
task()
|
|
22
|
+
.then(response => delegatedResolver.succeed(response))
|
|
23
|
+
.catch(error => {
|
|
24
|
+
log.error('SERVERLESS_ADAPTER:RESPOND_TO_EVENT_SOURCE_WITH_ERROR', error);
|
|
25
|
+
adapter.onErrorWhileForwarding({
|
|
26
|
+
delegatedResolver,
|
|
27
|
+
error,
|
|
28
|
+
log,
|
|
29
|
+
event,
|
|
30
|
+
respondWithErrors,
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.PromiseResolver = PromiseResolver;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { BinarySettings } from './@types';
|
|
2
|
+
import { AdapterContract, FrameworkContract, HandlerContract, ResolverContract, ServerlessHandler } from './contracts';
|
|
3
|
+
import { ILogger } from './core';
|
|
4
|
+
/**
|
|
5
|
+
* The class used to build the serverless handler.
|
|
6
|
+
*
|
|
7
|
+
* @example```typescript
|
|
8
|
+
* const app = express();
|
|
9
|
+
* export const handler = ServerlessAdapter.new(app)
|
|
10
|
+
* .setFramework(new ExpressFramework())
|
|
11
|
+
* .setHandler(new DefaultHandler())
|
|
12
|
+
* .setResolver(new PromiseResolver())
|
|
13
|
+
* .setRespondWithErrors(true)
|
|
14
|
+
* .addAdapter(new AlbAdapter())
|
|
15
|
+
* .addAdapter(new SQSAdapter())
|
|
16
|
+
* .addAdapter(new SNSAdapter())
|
|
17
|
+
* .build();
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class ServerlessAdapter<TApp, TEvent, TContext, TCallback, TResponse, TReturn> {
|
|
21
|
+
/**
|
|
22
|
+
* Default constructor
|
|
23
|
+
*/
|
|
24
|
+
private constructor();
|
|
25
|
+
/**
|
|
26
|
+
* The instance of the app (express, hapi, koa, etc...)
|
|
27
|
+
*/
|
|
28
|
+
protected app: TApp;
|
|
29
|
+
/**
|
|
30
|
+
* Settings for whether the response should be treated as binary or not
|
|
31
|
+
*
|
|
32
|
+
* @default `contentEncodings` and `contentTypes` are set with {@link DEFAULT_BINARY_ENCODINGS} and {@link DEFAULT_BINARY_ENCODINGS}, respectively.
|
|
33
|
+
*/
|
|
34
|
+
protected binarySettings: BinarySettings;
|
|
35
|
+
/**
|
|
36
|
+
* Indicates whether the error stack should be included in the response or not
|
|
37
|
+
*
|
|
38
|
+
* @note These errors will only be included when an error occurs while forwarding the event to the framework
|
|
39
|
+
* @default True when NODE_ENV is equal to `development`
|
|
40
|
+
*/
|
|
41
|
+
protected respondWithErrors: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* The instance of the logger service
|
|
44
|
+
*/
|
|
45
|
+
protected log: ILogger;
|
|
46
|
+
/**
|
|
47
|
+
* The list of adapters used to handle an event's request and response
|
|
48
|
+
*/
|
|
49
|
+
protected adapters: AdapterContract<TEvent, TContext, TResponse>[];
|
|
50
|
+
/**
|
|
51
|
+
* The framework that will process requests
|
|
52
|
+
*/
|
|
53
|
+
protected framework?: FrameworkContract<TApp>;
|
|
54
|
+
/**
|
|
55
|
+
* The resolver that aims to resolve the response to serverless and stop its execution when the request ends
|
|
56
|
+
*/
|
|
57
|
+
protected resolver?: ResolverContract<TEvent, TContext, TCallback, TResponse, TReturn>;
|
|
58
|
+
/**
|
|
59
|
+
* The handler that will get the event, context and callback and pass it to the adapter and framework
|
|
60
|
+
*/
|
|
61
|
+
protected handler?: HandlerContract<TApp, TEvent, TContext, TCallback, TResponse, TReturn>;
|
|
62
|
+
/**
|
|
63
|
+
* Creates a new instance of the builder with app (express, hapi, koa, etc...)
|
|
64
|
+
*
|
|
65
|
+
* @param app The instance of the app
|
|
66
|
+
*/
|
|
67
|
+
static new<TApp, TEvent, TContext = any, TCallback = any, TResponse = any, TReturn = any>(app: TApp): ServerlessAdapter<TApp, TEvent, TContext, TCallback, TResponse, TReturn>;
|
|
68
|
+
/**
|
|
69
|
+
* Defines the handler that will get the event, context and callback and pass it to the adapter and framework
|
|
70
|
+
*
|
|
71
|
+
* @param handler The implementation of the handler contract
|
|
72
|
+
*/
|
|
73
|
+
setHandler(handler: HandlerContract<TApp, TEvent, TContext, TCallback, TResponse, TReturn>): Omit<this, 'setHandler'>;
|
|
74
|
+
/**
|
|
75
|
+
* Defines the resolver that aims to resolve the response to serverless and stop its execution when the request ends
|
|
76
|
+
*
|
|
77
|
+
* @param resolver The implementation of the resolver contract
|
|
78
|
+
*/
|
|
79
|
+
setResolver(resolver: ResolverContract<TEvent, TContext, TCallback, TResponse, TReturn>): Omit<this, 'setResolver'>;
|
|
80
|
+
/**
|
|
81
|
+
* Defines the framework that will process requests
|
|
82
|
+
*
|
|
83
|
+
* @param framework The implementation of the framework contract
|
|
84
|
+
*/
|
|
85
|
+
setFramework(framework: FrameworkContract<TApp>): Omit<this, 'setFramework'>;
|
|
86
|
+
/**
|
|
87
|
+
* Defines the logger service used during the execution of the handler
|
|
88
|
+
*
|
|
89
|
+
* @param logger The implementation of the logger
|
|
90
|
+
*/
|
|
91
|
+
setLogger(logger: ILogger): Omit<this, 'setLogger'>;
|
|
92
|
+
/**
|
|
93
|
+
* Defines the binary settings for whether the response should be treated as binary or not
|
|
94
|
+
*
|
|
95
|
+
* @param binarySettings The binary settings
|
|
96
|
+
*/
|
|
97
|
+
setBinarySettings(binarySettings: BinarySettings): Omit<this, 'setBinarySettings'>;
|
|
98
|
+
/**
|
|
99
|
+
* Defines the responseWithErrors, a property that indicates whether the error stack should be included in the response or not
|
|
100
|
+
*
|
|
101
|
+
* @param respondWithErrors Should include or not the errors in response
|
|
102
|
+
*/
|
|
103
|
+
setRespondWithErrors(respondWithErrors: boolean): Omit<this, 'setRespondWithErrors'>;
|
|
104
|
+
/**
|
|
105
|
+
* Add an adapter to the adapters list to handle the event coming from any serverless event source
|
|
106
|
+
*
|
|
107
|
+
* @param adapter The implementation of the adapter contract
|
|
108
|
+
*/
|
|
109
|
+
addAdapter(adapter: AdapterContract<TEvent, TContext, TResponse>): Pick<this, 'addAdapter' | 'build'>;
|
|
110
|
+
/**
|
|
111
|
+
* The builder method that returns the handler function to be exported for serverless consumption
|
|
112
|
+
*/
|
|
113
|
+
build(): ServerlessHandler<TReturn>;
|
|
114
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region Imports
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.ServerlessAdapter = void 0;
|
|
5
|
+
const core_1 = require("./core");
|
|
6
|
+
//#endregion
|
|
7
|
+
/**
|
|
8
|
+
* The class used to build the serverless handler.
|
|
9
|
+
*
|
|
10
|
+
* @example```typescript
|
|
11
|
+
* const app = express();
|
|
12
|
+
* export const handler = ServerlessAdapter.new(app)
|
|
13
|
+
* .setFramework(new ExpressFramework())
|
|
14
|
+
* .setHandler(new DefaultHandler())
|
|
15
|
+
* .setResolver(new PromiseResolver())
|
|
16
|
+
* .setRespondWithErrors(true)
|
|
17
|
+
* .addAdapter(new AlbAdapter())
|
|
18
|
+
* .addAdapter(new SQSAdapter())
|
|
19
|
+
* .addAdapter(new SNSAdapter())
|
|
20
|
+
* .build();
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
class ServerlessAdapter {
|
|
24
|
+
//#region Constructor
|
|
25
|
+
/**
|
|
26
|
+
* Default constructor
|
|
27
|
+
*/
|
|
28
|
+
constructor(app) {
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region Protected Properties
|
|
31
|
+
/**
|
|
32
|
+
* Settings for whether the response should be treated as binary or not
|
|
33
|
+
*
|
|
34
|
+
* @default `contentEncodings` and `contentTypes` are set with {@link DEFAULT_BINARY_ENCODINGS} and {@link DEFAULT_BINARY_ENCODINGS}, respectively.
|
|
35
|
+
*/
|
|
36
|
+
this.binarySettings = {
|
|
37
|
+
contentEncodings: core_1.DEFAULT_BINARY_ENCODINGS,
|
|
38
|
+
contentTypes: core_1.DEFAULT_BINARY_ENCODINGS,
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Indicates whether the error stack should be included in the response or not
|
|
42
|
+
*
|
|
43
|
+
* @note These errors will only be included when an error occurs while forwarding the event to the framework
|
|
44
|
+
* @default True when NODE_ENV is equal to `development`
|
|
45
|
+
*/
|
|
46
|
+
this.respondWithErrors = process.env.NODE_ENV === 'development';
|
|
47
|
+
/**
|
|
48
|
+
* The instance of the logger service
|
|
49
|
+
*/
|
|
50
|
+
this.log = (0, core_1.createDefaultLogger)();
|
|
51
|
+
/**
|
|
52
|
+
* The list of adapters used to handle an event's request and response
|
|
53
|
+
*/
|
|
54
|
+
this.adapters = [];
|
|
55
|
+
this.app = app;
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region Static Methods
|
|
59
|
+
/**
|
|
60
|
+
* Creates a new instance of the builder with app (express, hapi, koa, etc...)
|
|
61
|
+
*
|
|
62
|
+
* @param app The instance of the app
|
|
63
|
+
*/
|
|
64
|
+
static new(app) {
|
|
65
|
+
return new ServerlessAdapter(app);
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region Builder Methods
|
|
69
|
+
/**
|
|
70
|
+
* Defines the handler that will get the event, context and callback and pass it to the adapter and framework
|
|
71
|
+
*
|
|
72
|
+
* @param handler The implementation of the handler contract
|
|
73
|
+
*/
|
|
74
|
+
setHandler(handler) {
|
|
75
|
+
if (this.handler)
|
|
76
|
+
throw new Error('SERVERLESS_ADAPTER: The handler should not set twice.');
|
|
77
|
+
this.handler = handler;
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Defines the resolver that aims to resolve the response to serverless and stop its execution when the request ends
|
|
82
|
+
*
|
|
83
|
+
* @param resolver The implementation of the resolver contract
|
|
84
|
+
*/
|
|
85
|
+
setResolver(resolver) {
|
|
86
|
+
if (this.resolver)
|
|
87
|
+
throw new Error('SERVERLESS_ADAPTER: The resolver should not set twice.');
|
|
88
|
+
this.resolver = resolver;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Defines the framework that will process requests
|
|
93
|
+
*
|
|
94
|
+
* @param framework The implementation of the framework contract
|
|
95
|
+
*/
|
|
96
|
+
setFramework(framework) {
|
|
97
|
+
if (this.framework) {
|
|
98
|
+
throw new Error('SERVERLESS_ADAPTER: The framework should not set twice.');
|
|
99
|
+
}
|
|
100
|
+
this.framework = framework;
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Defines the logger service used during the execution of the handler
|
|
105
|
+
*
|
|
106
|
+
* @param logger The implementation of the logger
|
|
107
|
+
*/
|
|
108
|
+
setLogger(logger) {
|
|
109
|
+
this.log = logger;
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Defines the binary settings for whether the response should be treated as binary or not
|
|
114
|
+
*
|
|
115
|
+
* @param binarySettings The binary settings
|
|
116
|
+
*/
|
|
117
|
+
setBinarySettings(binarySettings) {
|
|
118
|
+
this.binarySettings = binarySettings;
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Defines the responseWithErrors, a property that indicates whether the error stack should be included in the response or not
|
|
123
|
+
*
|
|
124
|
+
* @param respondWithErrors Should include or not the errors in response
|
|
125
|
+
*/
|
|
126
|
+
setRespondWithErrors(respondWithErrors) {
|
|
127
|
+
this.respondWithErrors = respondWithErrors;
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Add an adapter to the adapters list to handle the event coming from any serverless event source
|
|
132
|
+
*
|
|
133
|
+
* @param adapter The implementation of the adapter contract
|
|
134
|
+
*/
|
|
135
|
+
addAdapter(adapter) {
|
|
136
|
+
this.adapters.push(adapter);
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The builder method that returns the handler function to be exported for serverless consumption
|
|
141
|
+
*/
|
|
142
|
+
build() {
|
|
143
|
+
if (!this.resolver) {
|
|
144
|
+
throw new Error('SERVERLESS_ADAPTER: Is required to set a resolver before build.');
|
|
145
|
+
}
|
|
146
|
+
if (!this.framework) {
|
|
147
|
+
throw new Error('SERVERLESS_ADAPTER: Is required to set a framework before build.');
|
|
148
|
+
}
|
|
149
|
+
if (!this.handler) {
|
|
150
|
+
throw new Error('SERVERLESS_ADAPTER: Is required to set a handler before build.');
|
|
151
|
+
}
|
|
152
|
+
if (this.adapters.length === 0) {
|
|
153
|
+
throw new Error('SERVERLESS_ADAPTER: Is required to set at least one adapter.');
|
|
154
|
+
}
|
|
155
|
+
return this.handler.getHandler(this.app, this.framework, this.adapters, this.resolver, this.binarySettings, this.respondWithErrors, this.log);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
exports.ServerlessAdapter = ServerlessAdapter;
|
package/package.json
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@h4ad/serverless-adapter",
|
|
3
|
+
"version": "1.0.1",
|
|
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
|
+
"main": "./lib/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"lib/**/*"
|
|
8
|
+
],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"install:peers": "npx npm-install-peers",
|
|
11
|
+
"prepare": "husky install",
|
|
12
|
+
"build": "tsc -p tsconfig.build.json",
|
|
13
|
+
"clean": "rm -rf ./lib/",
|
|
14
|
+
"cm": "cz",
|
|
15
|
+
"coverage": "codecov",
|
|
16
|
+
"lint": "eslint ./src/ ./test/ --fix",
|
|
17
|
+
"semantic-release": "semantic-release",
|
|
18
|
+
"test:watch": "jest --watch",
|
|
19
|
+
"test": "jest --coverage",
|
|
20
|
+
"typecheck": "tsc --noEmit"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/H4ad/serverless-adapter.git"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"author": {
|
|
28
|
+
"name": "Vinícius Lourenço",
|
|
29
|
+
"email": "H4ad@users.noreply.github.com",
|
|
30
|
+
"url": "https://github.com/H4ad"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=12.0"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"aws",
|
|
37
|
+
"serverless",
|
|
38
|
+
"api gateway",
|
|
39
|
+
"sqs",
|
|
40
|
+
"sns",
|
|
41
|
+
"lambda-edge",
|
|
42
|
+
"alb",
|
|
43
|
+
"lambda",
|
|
44
|
+
"express",
|
|
45
|
+
"koa",
|
|
46
|
+
"hapi",
|
|
47
|
+
"fastify",
|
|
48
|
+
"node.js",
|
|
49
|
+
"http"
|
|
50
|
+
],
|
|
51
|
+
"bugs": {
|
|
52
|
+
"url": "https://github.com/H4ad/serverless-adapter/issues"
|
|
53
|
+
},
|
|
54
|
+
"homepage": "https://github.com/H4ad/serverless-adapter#readme",
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/aws-lambda": "^8.10.92",
|
|
57
|
+
"@types/body-parser": "^1.19.2",
|
|
58
|
+
"@types/jest": "^27.4.0",
|
|
59
|
+
"@types/node": "12.20.43",
|
|
60
|
+
"@typescript-eslint/eslint-plugin": "^5.12.1",
|
|
61
|
+
"@typescript-eslint/parser": "^5.12.1",
|
|
62
|
+
"body-parser": "^1.19.1",
|
|
63
|
+
"codecov": "^3.8.1",
|
|
64
|
+
"commitizen": "^4.2.4",
|
|
65
|
+
"cz-conventional-changelog": "^3.3.0",
|
|
66
|
+
"ejs": "^3.1.6",
|
|
67
|
+
"eslint": "^8.9.0",
|
|
68
|
+
"eslint-config-prettier": "^8.4.0",
|
|
69
|
+
"eslint-plugin-import": "^2.25.4",
|
|
70
|
+
"eslint-plugin-node": "^11.1.0",
|
|
71
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
72
|
+
"husky": "^6.0.0",
|
|
73
|
+
"jest": "^27.5.1",
|
|
74
|
+
"lint-staged": "^10.5.4",
|
|
75
|
+
"prettier": "^2.5.1",
|
|
76
|
+
"semantic-release": "^19.0.2",
|
|
77
|
+
"stream-mock": "^2.0.5",
|
|
78
|
+
"ts-jest": "^27.1.3",
|
|
79
|
+
"ts-node": "^10.4.0",
|
|
80
|
+
"typescript": "^4.5.5"
|
|
81
|
+
},
|
|
82
|
+
"peerDependencies": {
|
|
83
|
+
"@hapi/hapi": ">= 20.0.0",
|
|
84
|
+
"@types/express": ">= 4.15.4",
|
|
85
|
+
"@types/hapi": ">= 18.0.7",
|
|
86
|
+
"@types/koa": ">= 2.11.2",
|
|
87
|
+
"express": ">= 4.15.4",
|
|
88
|
+
"fastify": ">= 3.0.0",
|
|
89
|
+
"koa": ">= 2.5.1"
|
|
90
|
+
},
|
|
91
|
+
"peerDependenciesMeta": {
|
|
92
|
+
"@hapi/hapi": {
|
|
93
|
+
"optional": true
|
|
94
|
+
},
|
|
95
|
+
"express": {
|
|
96
|
+
"optional": true
|
|
97
|
+
},
|
|
98
|
+
"fastify": {
|
|
99
|
+
"optional": true
|
|
100
|
+
},
|
|
101
|
+
"koa": {
|
|
102
|
+
"optional": true
|
|
103
|
+
},
|
|
104
|
+
"@types/express": {
|
|
105
|
+
"optional": true
|
|
106
|
+
},
|
|
107
|
+
"@types/hapi": {
|
|
108
|
+
"optional": true
|
|
109
|
+
},
|
|
110
|
+
"@types/koa": {
|
|
111
|
+
"optional": true
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
"config": {
|
|
115
|
+
"commitizen": {
|
|
116
|
+
"path": "./node_modules/cz-conventional-changelog"
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
"lint-staged": {
|
|
120
|
+
"*.ts": "eslint --cache --cache-location .eslintcache --fix"
|
|
121
|
+
},
|
|
122
|
+
"release": {
|
|
123
|
+
"branches": [
|
|
124
|
+
"main"
|
|
125
|
+
]
|
|
126
|
+
},
|
|
127
|
+
"publishConfig": {
|
|
128
|
+
"access": "public"
|
|
129
|
+
}
|
|
130
|
+
}
|