@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.
Files changed (101) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +204 -0
  3. package/lib/@types/binary-settings.d.ts +23 -0
  4. package/lib/@types/binary-settings.js +2 -0
  5. package/lib/@types/headers.d.ts +24 -0
  6. package/lib/@types/headers.js +2 -0
  7. package/lib/@types/helpers.d.ts +6 -0
  8. package/lib/@types/helpers.js +2 -0
  9. package/lib/@types/index.d.ts +3 -0
  10. package/lib/@types/index.js +15 -0
  11. package/lib/@types/proxy-params.d.ts +8 -0
  12. package/lib/@types/proxy-params.js +2 -0
  13. package/lib/adapters/aws/alb.adapter.d.ts +58 -0
  14. package/lib/adapters/aws/alb.adapter.js +119 -0
  15. package/lib/adapters/aws/api-gateway-v1.adapter.d.ts +63 -0
  16. package/lib/adapters/aws/api-gateway-v1.adapter.js +126 -0
  17. package/lib/adapters/aws/api-gateway-v2.adapter.d.ts +61 -0
  18. package/lib/adapters/aws/api-gateway-v2.adapter.js +123 -0
  19. package/lib/adapters/aws/dynamodb.adapter.d.ts +62 -0
  20. package/lib/adapters/aws/dynamodb.adapter.js +81 -0
  21. package/lib/adapters/aws/event-bridge.adapter.d.ts +66 -0
  22. package/lib/adapters/aws/event-bridge.adapter.js +91 -0
  23. package/lib/adapters/aws/index.d.ts +8 -0
  24. package/lib/adapters/aws/index.js +20 -0
  25. package/lib/adapters/aws/lambda-edge.adapter.d.ts +179 -0
  26. package/lib/adapters/aws/lambda-edge.adapter.js +248 -0
  27. package/lib/adapters/aws/sns.adapter.d.ts +62 -0
  28. package/lib/adapters/aws/sns.adapter.js +81 -0
  29. package/lib/adapters/aws/sqs.adapter.d.ts +62 -0
  30. package/lib/adapters/aws/sqs.adapter.js +81 -0
  31. package/lib/adapters/index.d.ts +1 -0
  32. package/lib/adapters/index.js +13 -0
  33. package/lib/contracts/adapter.contract.d.ts +152 -0
  34. package/lib/contracts/adapter.contract.js +2 -0
  35. package/lib/contracts/framework.contract.d.ts +15 -0
  36. package/lib/contracts/framework.contract.js +3 -0
  37. package/lib/contracts/handler.contract.d.ts +18 -0
  38. package/lib/contracts/handler.contract.js +3 -0
  39. package/lib/contracts/index.d.ts +4 -0
  40. package/lib/contracts/index.js +16 -0
  41. package/lib/contracts/resolver.contract.d.ts +70 -0
  42. package/lib/contracts/resolver.contract.js +3 -0
  43. package/lib/core/constants.d.ts +4 -0
  44. package/lib/core/constants.js +10 -0
  45. package/lib/core/current-invoke.d.ts +37 -0
  46. package/lib/core/current-invoke.js +34 -0
  47. package/lib/core/event-body.d.ts +16 -0
  48. package/lib/core/event-body.js +24 -0
  49. package/lib/core/headers.d.ts +27 -0
  50. package/lib/core/headers.js +53 -0
  51. package/lib/core/index.d.ts +10 -0
  52. package/lib/core/index.js +22 -0
  53. package/lib/core/is-binary.d.ts +55 -0
  54. package/lib/core/is-binary.js +91 -0
  55. package/lib/core/logger.d.ts +38 -0
  56. package/lib/core/logger.js +51 -0
  57. package/lib/core/no-op.d.ts +4 -0
  58. package/lib/core/no-op.js +8 -0
  59. package/lib/core/optional.d.ts +17 -0
  60. package/lib/core/optional.js +25 -0
  61. package/lib/core/path.d.ts +28 -0
  62. package/lib/core/path.js +56 -0
  63. package/lib/core/stream.d.ts +8 -0
  64. package/lib/core/stream.js +39 -0
  65. package/lib/frameworks/express.framework.d.ts +13 -0
  66. package/lib/frameworks/express.framework.js +17 -0
  67. package/lib/frameworks/fastify.framework.d.ts +13 -0
  68. package/lib/frameworks/fastify.framework.js +18 -0
  69. package/lib/frameworks/hapi.framework.d.ts +13 -0
  70. package/lib/frameworks/hapi.framework.js +18 -0
  71. package/lib/frameworks/index.d.ts +4 -0
  72. package/lib/frameworks/index.js +16 -0
  73. package/lib/frameworks/koa.framework.d.ts +13 -0
  74. package/lib/frameworks/koa.framework.js +17 -0
  75. package/lib/handlers/base/base.handler.d.ts +28 -0
  76. package/lib/handlers/base/base.handler.js +52 -0
  77. package/lib/handlers/base/index.d.ts +1 -0
  78. package/lib/handlers/base/index.js +13 -0
  79. package/lib/handlers/default.handler.d.ts +85 -0
  80. package/lib/handlers/default.handler.js +183 -0
  81. package/lib/handlers/index.d.ts +2 -0
  82. package/lib/handlers/index.js +14 -0
  83. package/lib/index.d.ts +9 -0
  84. package/lib/index.js +21 -0
  85. package/lib/network/index.d.ts +2 -0
  86. package/lib/network/index.js +14 -0
  87. package/lib/network/request.d.ts +15 -0
  88. package/lib/network/request.js +40 -0
  89. package/lib/network/response.d.ts +22 -0
  90. package/lib/network/response.js +104 -0
  91. package/lib/resolvers/aws-context.resolver.d.ts +21 -0
  92. package/lib/resolvers/aws-context.resolver.js +55 -0
  93. package/lib/resolvers/callback.resolver.d.ts +16 -0
  94. package/lib/resolvers/callback.resolver.js +40 -0
  95. package/lib/resolvers/index.d.ts +3 -0
  96. package/lib/resolvers/index.js +15 -0
  97. package/lib/resolvers/promise.resolver.d.ts +10 -0
  98. package/lib/resolvers/promise.resolver.js +38 -0
  99. package/lib/serverless-adapter.d.ts +114 -0
  100. package/lib/serverless-adapter.js +158 -0
  101. package/package.json +130 -0
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ //#region Imports
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.SQSAdapter = void 0;
5
+ const core_1 = require("../../core");
6
+ /**
7
+ * The adapter to handle requests from AWS SQS.
8
+ *
9
+ * The option of `responseWithErrors` is ignored by this adapter and we always call `resolver.fail` with the error.
10
+ *
11
+ * {@link https://docs.aws.amazon.com/pt_br/lambda/latest/dg/with-sqs.html Event Reference}
12
+ *
13
+ * @example```typescript
14
+ * const sqsForwardPath = '/your/route/sqs'; // default /sqs
15
+ * const sqsForwardMethod = 'POST'; // default POST
16
+ * const adapter = new SQSAdapter({ sqsForwardPath, sqsForwardMethod });
17
+ * ```
18
+ */
19
+ class SQSAdapter {
20
+ //#region Constructor
21
+ /**
22
+ * Default constructor
23
+ *
24
+ * @param options The options to customize the {@link SQSAdapter}
25
+ */
26
+ constructor(options) {
27
+ this.options = options;
28
+ }
29
+ //#endregion
30
+ //#region Public Methods
31
+ /**
32
+ * @inheritDoc
33
+ */
34
+ getAdapterName() {
35
+ return SQSAdapter.name;
36
+ }
37
+ /**
38
+ * @inheritDoc
39
+ */
40
+ canHandle(event) {
41
+ var _a;
42
+ const sqsEvent = event;
43
+ if (!Array.isArray(sqsEvent === null || sqsEvent === void 0 ? void 0 : sqsEvent.Records))
44
+ return false;
45
+ const eventSource = (_a = sqsEvent.Records[0]) === null || _a === void 0 ? void 0 : _a.eventSource;
46
+ return eventSource === 'aws:sqs';
47
+ }
48
+ /**
49
+ * @inheritDoc
50
+ */
51
+ getRequest(event) {
52
+ var _a, _b;
53
+ const path = (0, core_1.getDefaultIfUndefined)((_a = this.options) === null || _a === void 0 ? void 0 : _a.sqsForwardPath, '/sqs');
54
+ const method = (0, core_1.getDefaultIfUndefined)((_b = this.options) === null || _b === void 0 ? void 0 : _b.sqsForwardMethod, 'POST');
55
+ const [body, contentLength] = (0, core_1.getEventBodyAsBuffer)(JSON.stringify(event), false);
56
+ const headers = {
57
+ host: 'sqs.amazonaws.com',
58
+ 'content-type': 'application/json',
59
+ 'content-length': String(contentLength),
60
+ };
61
+ return {
62
+ method,
63
+ headers,
64
+ body,
65
+ path,
66
+ };
67
+ }
68
+ /**
69
+ * @inheritDoc
70
+ */
71
+ getResponse() {
72
+ return core_1.EmptyResponse;
73
+ }
74
+ /**
75
+ * @inheritDoc
76
+ */
77
+ onErrorWhileForwarding({ error, delegatedResolver, }) {
78
+ delegatedResolver.fail(error);
79
+ }
80
+ }
81
+ exports.SQSAdapter = SQSAdapter;
@@ -0,0 +1 @@
1
+ export * from './aws';
@@ -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("./aws"), exports);
@@ -0,0 +1,152 @@
1
+ /// <reference types="node" />
2
+ import { BothValueHeaders, SingleValueHeaders } from '../@types';
3
+ import { ILogger } from '../core';
4
+ import { ServerlessResponse } from '../network';
5
+ import { DelegatedResolver } from './resolver.contract';
6
+ /**
7
+ * The props of the method that verify if this handler should handle the event.
8
+ */
9
+ export interface CanHandleProps<TEvent, TContext> {
10
+ /**
11
+ * The event sent by the serverless
12
+ */
13
+ event: TEvent | unknown;
14
+ /**
15
+ * The context sent by the serverless
16
+ */
17
+ context: TContext | unknown;
18
+ /**
19
+ * The instance of the logger.
20
+ */
21
+ log: ILogger;
22
+ }
23
+ /**
24
+ * The request interface used to bridge any event source to the framework.
25
+ */
26
+ export interface AdapterRequest {
27
+ /**
28
+ * The HTTP Method to use to create the request to the framework
29
+ */
30
+ method: string;
31
+ /**
32
+ * The path to use to create the request to the framework
33
+ */
34
+ path: string;
35
+ /**
36
+ * The headers to use to create the request to the framework
37
+ */
38
+ headers: SingleValueHeaders;
39
+ /**
40
+ * The body as buffer to use to create the request to the framework
41
+ */
42
+ body: Buffer | undefined;
43
+ /**
44
+ * The remote address (client ip) to use to create the request to the framework
45
+ */
46
+ remoteAddress?: string;
47
+ /**
48
+ * The address of the event source (used in Lambda@edge)
49
+ */
50
+ host?: string;
51
+ /**
52
+ * The address of the event source (used in Lambda@edge)
53
+ */
54
+ hostname?: string;
55
+ }
56
+ /**
57
+ * The props of the method that get the response from the framework and transform it into a format that the event source can handle
58
+ */
59
+ export interface GetResponseAdapterProps<TEvent> {
60
+ /**
61
+ * The event sent by the serverless
62
+ */
63
+ event: TEvent;
64
+ /**
65
+ * The framework {@link ServerlessResponse response}.
66
+ *
67
+ * @note It can be optional, as this method can be used when an error occurs during the handling of the request by the framework.
68
+ */
69
+ response?: ServerlessResponse;
70
+ /**
71
+ * The framework response status code
72
+ */
73
+ statusCode: number;
74
+ /**
75
+ * The framework response body
76
+ */
77
+ body: string;
78
+ /**
79
+ * The framework response headers
80
+ */
81
+ headers: BothValueHeaders;
82
+ /**
83
+ * Indicates whether the response is base64 encoded or not
84
+ */
85
+ isBase64Encoded: boolean;
86
+ /**
87
+ * The instance of the logger
88
+ */
89
+ log: ILogger;
90
+ }
91
+ /**
92
+ * The props of the method that handle the response when an error occurs while forwarding the request to the framework
93
+ */
94
+ export interface OnErrorProps<TEvent, TResponse> {
95
+ /**
96
+ * The event sent by the serverless
97
+ */
98
+ event: TEvent;
99
+ /**
100
+ * The error throwed during forwarding
101
+ */
102
+ error: Error;
103
+ /**
104
+ * The instance of the resolver
105
+ */
106
+ delegatedResolver: DelegatedResolver<TResponse>;
107
+ /**
108
+ * Indicates whether to forward the (error.stack) or not to the client
109
+ */
110
+ respondWithErrors: boolean;
111
+ /**
112
+ * The instance of the logger
113
+ */
114
+ log: ILogger;
115
+ }
116
+ /**
117
+ * The interface that represents a contract between the adapter and the actual implementation of the adapter.
118
+ */
119
+ export interface AdapterContract<TEvent, TContext, TResponse> {
120
+ /**
121
+ * Get the adapter name
122
+ */
123
+ getAdapterName(): string;
124
+ /**
125
+ * Decide if this adapter can handle a request based in the event payload
126
+ *
127
+ * @param event The event sent by serverless
128
+ * @param context The context sent by the serverless
129
+ * @param log The instance of logger
130
+ */
131
+ canHandle(event: unknown, context: TContext, log: ILogger): boolean;
132
+ /**
133
+ * Maps the serverless payload to an actual request that a framework can handle
134
+ *
135
+ * @param event The event sent by serverless
136
+ * @param context The context sent by the serverless
137
+ * @param log The instance of logger
138
+ */
139
+ getRequest(event: TEvent, context: TContext, log: ILogger): AdapterRequest;
140
+ /**
141
+ * Maps the response of the framework to a payload that serverless can handle
142
+ *
143
+ * @param props The props sent by serverless
144
+ */
145
+ getResponse(props: GetResponseAdapterProps<TEvent>): TResponse;
146
+ /**
147
+ * When an error occurs while forwarding the request to the framework
148
+ *
149
+ * @note You must call resolver.fail or resolver.succeed when implementing this method.
150
+ */
151
+ onErrorWhileForwarding(props: OnErrorProps<TEvent, TResponse>): void;
152
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,15 @@
1
+ /// <reference types="node" />
2
+ import { IncomingMessage, ServerResponse } from 'http';
3
+ /**
4
+ * The interface that represents a contract between the framework and the framework implementation
5
+ */
6
+ export interface FrameworkContract<TApp> {
7
+ /**
8
+ * Send the request and response objects to the framework
9
+ *
10
+ * @param app
11
+ * @param request
12
+ * @param response
13
+ */
14
+ sendRequest(app: TApp, request: IncomingMessage, response: ServerResponse): void;
15
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ //#region Imports
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,18 @@
1
+ import { BinarySettings } from '../@types';
2
+ import { ILogger } from '../core';
3
+ import { AdapterContract } from './adapter.contract';
4
+ import { FrameworkContract } from './framework.contract';
5
+ import { ResolverContract } from './resolver.contract';
6
+ /**
7
+ * The function used to handle serverless requests
8
+ */
9
+ export declare type ServerlessHandler<TReturn> = (...args: any[]) => TReturn;
10
+ /**
11
+ * The interface that represents the contract between the handler and the real implementation
12
+ */
13
+ export interface HandlerContract<TApp, TEvent, TContext, TCallback, TResponse, TReturn> {
14
+ /**
15
+ * Get the handler that will handle serverless requests
16
+ */
17
+ getHandler(app: TApp, framework: FrameworkContract<TApp>, adapters: AdapterContract<TEvent, TContext, TResponse>[], resolverFactory: ResolverContract<TEvent, TContext, TCallback, TResponse, TReturn>, binarySettings: BinarySettings, respondWithErrors: boolean, log: ILogger): ServerlessHandler<TReturn>;
18
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ //#region Imports
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ export * from './adapter.contract';
2
+ export * from './framework.contract';
3
+ export * from './handler.contract';
4
+ export * from './resolver.contract';
@@ -0,0 +1,16 @@
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("./adapter.contract"), exports);
14
+ __exportStar(require("./framework.contract"), exports);
15
+ __exportStar(require("./handler.contract"), exports);
16
+ __exportStar(require("./resolver.contract"), exports);
@@ -0,0 +1,70 @@
1
+ import { ILogger } from '../core';
2
+ import { AdapterContract } from './adapter.contract';
3
+ /**
4
+ * The type that represents a resolver used to send the response, error or success, to the client
5
+ */
6
+ export declare type Resolver<TResponse, TReturn> = {
7
+ /**
8
+ * The method that will perform the task of forwarding the request to the framework and waiting for the promise to be resolved with the response
9
+ *
10
+ * @param task The task to be executed
11
+ */
12
+ run(task: () => Promise<TResponse>): TReturn;
13
+ };
14
+ /**
15
+ * The type that represents a delegate resolver that is passed to the adapter to handle what to do when an error occurs during forwarding.
16
+ */
17
+ export declare type DelegatedResolver<TResponse> = {
18
+ /**
19
+ * Send the success response to the client
20
+ *
21
+ * @param success The serverless response
22
+ */
23
+ succeed: (response: TResponse) => void;
24
+ /**
25
+ * Send the error response to the client
26
+ *
27
+ * @param error The error object
28
+ */
29
+ fail: (error: Error) => void;
30
+ };
31
+ /**
32
+ * The createResolver contract props
33
+ */
34
+ export declare type ResolverProps<TEvent, TContext, TCallback, TResponse> = {
35
+ /**
36
+ * The event sent by the serverless environment
37
+ */
38
+ event: TEvent;
39
+ /**
40
+ * Indicates whether to forward the (error.stack) or not to the client
41
+ */
42
+ respondWithErrors: boolean;
43
+ /**
44
+ * The instance of the logger
45
+ */
46
+ log: ILogger;
47
+ /**
48
+ * The instance of the adapter
49
+ */
50
+ adapter: AdapterContract<TEvent, TContext, TResponse>;
51
+ /**
52
+ * The context sent by serverless
53
+ */
54
+ context?: TContext;
55
+ /**
56
+ * The callback sent by serverless
57
+ */
58
+ callback?: TCallback;
59
+ };
60
+ /**
61
+ * The interface that represents the contract used to send the response to the client
62
+ */
63
+ export interface ResolverContract<TEvent, TContext, TCallback, TResponse, TReturn> {
64
+ /**
65
+ * Create the resolver based on the context, callback or promise
66
+ *
67
+ * @param props The props used to create the resolver
68
+ */
69
+ createResolver(props: ResolverProps<TEvent, TContext, TCallback, TResponse>): Resolver<TResponse, TReturn>;
70
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ //#region Imports
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ export declare const DEFAULT_BINARY_ENCODINGS: string[];
2
+ export declare const DEFAULT_BINARY_CONTENT_TYPES: (string | RegExp)[];
3
+ export declare type IEmptyResponse = {};
4
+ export declare const EmptyResponse: IEmptyResponse;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EmptyResponse = exports.DEFAULT_BINARY_CONTENT_TYPES = exports.DEFAULT_BINARY_ENCODINGS = void 0;
4
+ exports.DEFAULT_BINARY_ENCODINGS = ['gzip', 'deflate', 'br'];
5
+ exports.DEFAULT_BINARY_CONTENT_TYPES = [
6
+ new RegExp('^image/.*$'),
7
+ new RegExp('^video/.*$'),
8
+ 'application/pdf',
9
+ ];
10
+ exports.EmptyResponse = {};
@@ -0,0 +1,37 @@
1
+ /**
2
+ * The type that represents the object that handles the references to the event created by the serverless trigger or context created by the serverless environment.
3
+ */
4
+ export declare type CurrentInvoke<TEvent, TContext> = {
5
+ /**
6
+ * The event created by the serverless trigger
7
+ *
8
+ * @note It's only null when you call {@link getCurrentInvoke} outside this library's pipeline.
9
+ */
10
+ event: TEvent | null;
11
+ /**
12
+ * The context created by the serverless environment
13
+ *
14
+ * @note It's only null when you call {@link getCurrentInvoke} outside this library's pipeline.
15
+ */
16
+ context: TContext | null;
17
+ };
18
+ /**
19
+ * Get the reference to the event created by the serverless trigger or context created by the serverless environment.
20
+ *
21
+ * @example```typescript
22
+ * import type { ALBEvent, Context } from 'aws-lambda';
23
+ *
24
+ * // inside the method that handles the aws alb request.
25
+ * const { event, context } = getCurrentInvoke<ALBEvent, Context>();
26
+ * ```
27
+ */
28
+ export declare function getCurrentInvoke<TEvent = any, TContext = any>(): CurrentInvoke<TEvent, TContext>;
29
+ /**
30
+ * Method that saves to the event created by the serverless trigger or context created by the serverless environment.
31
+ *
32
+ * @note This method MUST NOT be called by you, this method MUST only be used internally in this library.
33
+ *
34
+ * @param event The event created by the serverless trigger
35
+ * @param context The context created by the serverless environment
36
+ */
37
+ export declare function setCurrentInvoke<TEvent = any, TContext = any>({ event, context, }: CurrentInvoke<TEvent, TContext>): void;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setCurrentInvoke = exports.getCurrentInvoke = void 0;
4
+ const currentInvoke = {
5
+ context: null,
6
+ event: null,
7
+ };
8
+ /**
9
+ * Get the reference to the event created by the serverless trigger or context created by the serverless environment.
10
+ *
11
+ * @example```typescript
12
+ * import type { ALBEvent, Context } from 'aws-lambda';
13
+ *
14
+ * // inside the method that handles the aws alb request.
15
+ * const { event, context } = getCurrentInvoke<ALBEvent, Context>();
16
+ * ```
17
+ */
18
+ function getCurrentInvoke() {
19
+ return currentInvoke;
20
+ }
21
+ exports.getCurrentInvoke = getCurrentInvoke;
22
+ /**
23
+ * Method that saves to the event created by the serverless trigger or context created by the serverless environment.
24
+ *
25
+ * @note This method MUST NOT be called by you, this method MUST only be used internally in this library.
26
+ *
27
+ * @param event The event created by the serverless trigger
28
+ * @param context The context created by the serverless environment
29
+ */
30
+ function setCurrentInvoke({ event, context, }) {
31
+ currentInvoke.event = event;
32
+ currentInvoke.context = context;
33
+ }
34
+ exports.setCurrentInvoke = setCurrentInvoke;
@@ -0,0 +1,16 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Get the event body as buffer from body string with content length
4
+ *
5
+ * @example```typescript
6
+ * const body = '{}';
7
+ * const [buffer, contentLength] = getEventBodyAsBuffer(body, false);
8
+ * console.log(buffer);
9
+ * // <Buffer 7b 7d>
10
+ * console.log(contentLength);
11
+ * // 2
12
+ *
13
+ * @param body The body string that can be encoded or not
14
+ * @param isBase64Encoded Tells if body string is encoded in base64
15
+ */
16
+ export declare function getEventBodyAsBuffer(body: string, isBase64Encoded: boolean): [body: Buffer, contentLength: number];
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEventBodyAsBuffer = void 0;
4
+ /**
5
+ * Get the event body as buffer from body string with content length
6
+ *
7
+ * @example```typescript
8
+ * const body = '{}';
9
+ * const [buffer, contentLength] = getEventBodyAsBuffer(body, false);
10
+ * console.log(buffer);
11
+ * // <Buffer 7b 7d>
12
+ * console.log(contentLength);
13
+ * // 2
14
+ *
15
+ * @param body The body string that can be encoded or not
16
+ * @param isBase64Encoded Tells if body string is encoded in base64
17
+ */
18
+ function getEventBodyAsBuffer(body, isBase64Encoded) {
19
+ const encoding = isBase64Encoded ? 'base64' : 'utf8';
20
+ const buffer = Buffer.from(body, encoding);
21
+ const contentLength = Buffer.byteLength(buffer, encoding);
22
+ return [buffer, contentLength];
23
+ }
24
+ exports.getEventBodyAsBuffer = getEventBodyAsBuffer;
@@ -0,0 +1,27 @@
1
+ import { BothValueHeaders } from '../@types';
2
+ /**
3
+ * Transform a header map and make sure the value is not an array
4
+ *
5
+ * @example```typescript
6
+ * const headers = { 'accept-encoding': 'gzip', 'accept-language': ['en-US', 'en;q=0.9'] };
7
+ * const flattenedHeaders = getFlattenedHeadersMap(headers, ',', true);
8
+ * console.log(flattenedHeaders);
9
+ * // { 'accept-encoding': 'gzip', 'accept-language': 'en-US,en;q=0.9' }
10
+ * ```
11
+ * @param headersMap The initial headers
12
+ * @param separator The separator used when we join the array of header's value
13
+ * @param lowerCaseKey Should put all keys in lowercase
14
+ */
15
+ export declare function getFlattenedHeadersMap(headersMap: BothValueHeaders, separator?: string, lowerCaseKey?: boolean): Record<string, string>;
16
+ /**
17
+ * Transforms a header map into a multi-value map header.
18
+ *
19
+ * @example```typescript
20
+ * const headers = { 'accept-encoding': 'gzip', 'connection': ['keep-alive'] };
21
+ * const multiValueHeaders = getMultiValueHeadersMap(headers);
22
+ * console.log(multiValueHeaders);
23
+ * // { 'accept-encoding': ['gzip'], 'connection': ['keep-alive'] }
24
+ * ```
25
+ * @param headersMap The initial headers
26
+ */
27
+ export declare function getMultiValueHeadersMap(headersMap: BothValueHeaders): Record<string, string[]>;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ //#region Imports
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.getMultiValueHeadersMap = exports.getFlattenedHeadersMap = void 0;
5
+ //#endregion
6
+ /**
7
+ * Transform a header map and make sure the value is not an array
8
+ *
9
+ * @example```typescript
10
+ * const headers = { 'accept-encoding': 'gzip', 'accept-language': ['en-US', 'en;q=0.9'] };
11
+ * const flattenedHeaders = getFlattenedHeadersMap(headers, ',', true);
12
+ * console.log(flattenedHeaders);
13
+ * // { 'accept-encoding': 'gzip', 'accept-language': 'en-US,en;q=0.9' }
14
+ * ```
15
+ * @param headersMap The initial headers
16
+ * @param separator The separator used when we join the array of header's value
17
+ * @param lowerCaseKey Should put all keys in lowercase
18
+ */
19
+ function getFlattenedHeadersMap(headersMap, separator = ',', lowerCaseKey = false) {
20
+ const commaDelimitedHeaders = {};
21
+ const headersMapEntries = Object.entries(headersMap);
22
+ for (const [headerKey, headerValue] of headersMapEntries) {
23
+ const newKey = lowerCaseKey ? headerKey.toLowerCase() : headerKey;
24
+ if (Array.isArray(headerValue))
25
+ commaDelimitedHeaders[newKey] = headerValue.join(separator);
26
+ else
27
+ commaDelimitedHeaders[newKey] = headerValue || '';
28
+ }
29
+ return commaDelimitedHeaders;
30
+ }
31
+ exports.getFlattenedHeadersMap = getFlattenedHeadersMap;
32
+ /**
33
+ * Transforms a header map into a multi-value map header.
34
+ *
35
+ * @example```typescript
36
+ * const headers = { 'accept-encoding': 'gzip', 'connection': ['keep-alive'] };
37
+ * const multiValueHeaders = getMultiValueHeadersMap(headers);
38
+ * console.log(multiValueHeaders);
39
+ * // { 'accept-encoding': ['gzip'], 'connection': ['keep-alive'] }
40
+ * ```
41
+ * @param headersMap The initial headers
42
+ */
43
+ function getMultiValueHeadersMap(headersMap) {
44
+ const multiValueHeaders = {};
45
+ const headersMapEntries = Object.entries(headersMap);
46
+ for (const [headerKey, headerValue] of headersMapEntries) {
47
+ multiValueHeaders[headerKey.toLowerCase()] = Array.isArray(headerValue)
48
+ ? headerValue.map(String)
49
+ : [String(headerValue)];
50
+ }
51
+ return multiValueHeaders;
52
+ }
53
+ exports.getMultiValueHeadersMap = getMultiValueHeadersMap;
@@ -0,0 +1,10 @@
1
+ export * from './constants';
2
+ export * from './current-invoke';
3
+ export * from './event-body';
4
+ export * from './headers';
5
+ export * from './is-binary';
6
+ export * from './logger';
7
+ export * from './no-op';
8
+ export * from './optional';
9
+ export * from './path';
10
+ export * from './stream';
@@ -0,0 +1,22 @@
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("./constants"), exports);
14
+ __exportStar(require("./current-invoke"), exports);
15
+ __exportStar(require("./event-body"), exports);
16
+ __exportStar(require("./headers"), exports);
17
+ __exportStar(require("./is-binary"), exports);
18
+ __exportStar(require("./logger"), exports);
19
+ __exportStar(require("./no-op"), exports);
20
+ __exportStar(require("./optional"), exports);
21
+ __exportStar(require("./path"), exports);
22
+ __exportStar(require("./stream"), exports);