@stone-js/aws-lambda-http-adapter 0.8.16 → 0.8.18
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/dist/index.js +132 -132
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { IntegrationError, Adapter, AdapterEventBuilder,
|
|
2
|
-
import { IncomingHttpEvent, HTTP_INTERNAL_SERVER_ERROR, BinaryFileResponse, OutgoingHttpResponse, isMultipart, getCharset, CookieCollection, isIpTrusted, getHostname, getProtocol, httpCoreBlueprint, getFilesUploads } from '@stone-js/http-core';
|
|
1
|
+
import { defaultLoggerResolver, IntegrationError, Adapter, AdapterEventBuilder, isNotEmpty, Logger, defaultKernelResolver, classDecoratorLegacyWrapper, addBlueprint } from '@stone-js/core';
|
|
3
2
|
import mime from 'mime';
|
|
4
3
|
import accepts from 'accepts';
|
|
5
4
|
import statuses from 'statuses';
|
|
5
|
+
import { HTTP_INTERNAL_SERVER_ERROR, IncomingHttpEvent, BinaryFileResponse, OutgoingHttpResponse, isMultipart, getCharset, CookieCollection, isIpTrusted, getHostname, getProtocol, httpCoreBlueprint, getFilesUploads } from '@stone-js/http-core';
|
|
6
6
|
import { cloneValue, deepMerge } from '@stone-js/config';
|
|
7
7
|
import { getString } from '@stone-js/env';
|
|
8
8
|
import { File } from '@stone-js/filesystem';
|
|
@@ -109,136 +109,6 @@ class RawHttpResponseWrapper {
|
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
/**
|
|
113
|
-
* Custom error for AWS Lambda adapter operations.
|
|
114
|
-
*/
|
|
115
|
-
class AwsLambdaHttpAdapterError extends IntegrationError {
|
|
116
|
-
constructor(message, options) {
|
|
117
|
-
super(message, options);
|
|
118
|
-
this.name = 'AwsLambdaHttpAdapterError';
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* AWS Lambda HTTP Adapter for Stone.js.
|
|
124
|
-
*
|
|
125
|
-
* The `AwsLambdaHttpAdapter` extends the functionality of the Stone.js `Adapter`
|
|
126
|
-
* to provide seamless integration with AWS Lambda for HTTP-based events. This adapter
|
|
127
|
-
* transforms incoming HTTP events from AWS Lambda into `IncomingHttpEvent` instances
|
|
128
|
-
* and produces a `RawHttpResponse` as output.
|
|
129
|
-
*
|
|
130
|
-
* This adapter simplifies the process of handling HTTP events within AWS Lambda
|
|
131
|
-
* while adhering to the Stone.js framework's event-driven architecture.
|
|
132
|
-
*
|
|
133
|
-
* @template AwsLambdaHttpEvent - The type of the raw HTTP event from AWS Lambda.
|
|
134
|
-
* @template RawHttpResponse - The type of the raw HTTP response to send back.
|
|
135
|
-
* @template AwsLambdaContext - The AWS Lambda execution context type.
|
|
136
|
-
* @template IncomingHttpEvent - The type of the processed incoming HTTP event.
|
|
137
|
-
* @template IncomingHttpEventOptions - Options used to create an incoming HTTP event.
|
|
138
|
-
* @template OutgoingHttpResponse - The type of the outgoing HTTP response after processing.
|
|
139
|
-
* @template AwsLambdaHttpAdapterContext - Context type specific to the HTTP adapter.
|
|
140
|
-
*
|
|
141
|
-
* @extends Adapter
|
|
142
|
-
*
|
|
143
|
-
* @example
|
|
144
|
-
* ```typescript
|
|
145
|
-
* import { AwsLambdaHttpAdapter } from '@stone-js/aws-lambda-http-adapter';
|
|
146
|
-
*
|
|
147
|
-
* const adapter = AwsLambdaHttpAdapter.create({...});
|
|
148
|
-
*
|
|
149
|
-
* const handler = await adapter.run();
|
|
150
|
-
*
|
|
151
|
-
* export { handler };
|
|
152
|
-
* ```
|
|
153
|
-
*
|
|
154
|
-
* @see {@link https://stone-js.com/docs Stone.js Documentation}
|
|
155
|
-
* @see {@link https://docs.aws.amazon.com/lambda/latest/dg/ AWS Lambda Documentation}
|
|
156
|
-
*/
|
|
157
|
-
class AwsLambdaHttpAdapter extends Adapter {
|
|
158
|
-
/**
|
|
159
|
-
* Creates an instance of the `AwsLambdaHttpAdapter`.
|
|
160
|
-
*
|
|
161
|
-
* @param blueprint - The application blueprint.
|
|
162
|
-
* @returns A new instance of `AwsLambdaHttpAdapter`.
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
* ```typescript
|
|
166
|
-
* const adapter = AwsLambdaHttpAdapter.create(blueprint);
|
|
167
|
-
* await adapter.run();
|
|
168
|
-
* ```
|
|
169
|
-
*/
|
|
170
|
-
static create(blueprint) {
|
|
171
|
-
return new this(blueprint);
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Executes the adapter and provides an AWS Lambda-compatible HTTP handler function.
|
|
175
|
-
*
|
|
176
|
-
* This method initializes the adapter and returns a handler function that can
|
|
177
|
-
* process HTTP events in AWS Lambda. It transforms raw events into `IncomingHttpEvent`
|
|
178
|
-
* instances and produces `RawHttpResponse` objects as output.
|
|
179
|
-
*
|
|
180
|
-
* @template ExecutionResultType - The type representing the AWS Lambda event handler function.
|
|
181
|
-
* @returns A promise resolving to the AWS Lambda HTTP handler function.
|
|
182
|
-
* @throws {AwsLambdaHttpAdapterError} If used outside the AWS Lambda environment.
|
|
183
|
-
*/
|
|
184
|
-
async run() {
|
|
185
|
-
await this.onStart();
|
|
186
|
-
const handler = async (rawEvent, executionContext) => {
|
|
187
|
-
return await this.eventListener(rawEvent, executionContext);
|
|
188
|
-
};
|
|
189
|
-
return handler;
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Initializes the adapter and validates its execution context.
|
|
193
|
-
*
|
|
194
|
-
* Ensures that the adapter is running in an AWS Lambda environment. Throws an error
|
|
195
|
-
* if it detects that the adapter is being used in an unsupported environment (e.g., a browser).
|
|
196
|
-
*
|
|
197
|
-
* @throws {AwsLambdaHttpAdapterError} If executed outside an AWS Lambda environment.
|
|
198
|
-
*/
|
|
199
|
-
async onStart() {
|
|
200
|
-
if (typeof window === 'object') {
|
|
201
|
-
throw new AwsLambdaHttpAdapterError('This `AwsLambdaHttpAdapter` must be used only in AWS Lambda context.');
|
|
202
|
-
}
|
|
203
|
-
await this.executeHooks('onStart');
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Processes an incoming AWS Lambda HTTP event.
|
|
207
|
-
*
|
|
208
|
-
* Converts a raw AWS Lambda HTTP event into an `IncomingHttpEvent`, processes it through
|
|
209
|
-
* the Stone.js pipeline, and generates a `RawHttpResponse` to send back.
|
|
210
|
-
*
|
|
211
|
-
* @param rawEvent - The raw HTTP event received from AWS Lambda.
|
|
212
|
-
* @param executionContext - The AWS Lambda execution context associated with the event.
|
|
213
|
-
* @returns A promise resolving to the processed `RawHttpResponse`.
|
|
214
|
-
*/
|
|
215
|
-
async eventListener(rawEvent, executionContext) {
|
|
216
|
-
const incomingEventBuilder = AdapterEventBuilder.create({
|
|
217
|
-
resolver: (options) => IncomingHttpEvent.create(options)
|
|
218
|
-
});
|
|
219
|
-
const rawResponseBuilder = AdapterEventBuilder.create({
|
|
220
|
-
resolver: (options) => RawHttpResponseWrapper.create(options)
|
|
221
|
-
});
|
|
222
|
-
const rawResponse = { statusCode: 500 };
|
|
223
|
-
const context = {
|
|
224
|
-
rawEvent,
|
|
225
|
-
rawResponse,
|
|
226
|
-
executionContext,
|
|
227
|
-
rawResponseBuilder,
|
|
228
|
-
incomingEventBuilder
|
|
229
|
-
};
|
|
230
|
-
try {
|
|
231
|
-
const eventHandler = this.resolveEventHandler();
|
|
232
|
-
await this.executeEventHandlerHooks('onInit', eventHandler);
|
|
233
|
-
return await this.sendEventThroughDestination(context, eventHandler);
|
|
234
|
-
}
|
|
235
|
-
catch (error) {
|
|
236
|
-
const rawResponseBuilder = await this.handleError(error, context);
|
|
237
|
-
return await this.buildRawResponse({ ...context, rawResponseBuilder });
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
112
|
/**
|
|
243
113
|
* Class representing an AwsLambdaHttpErrorHandler.
|
|
244
114
|
*/
|
|
@@ -453,6 +323,136 @@ function getRawBody(event) {
|
|
|
453
323
|
return event.body;
|
|
454
324
|
}
|
|
455
325
|
|
|
326
|
+
/**
|
|
327
|
+
* Custom error for AWS Lambda adapter operations.
|
|
328
|
+
*/
|
|
329
|
+
class AwsLambdaHttpAdapterError extends IntegrationError {
|
|
330
|
+
constructor(message, options) {
|
|
331
|
+
super(message, options);
|
|
332
|
+
this.name = 'AwsLambdaHttpAdapterError';
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* AWS Lambda HTTP Adapter for Stone.js.
|
|
338
|
+
*
|
|
339
|
+
* The `AwsLambdaHttpAdapter` extends the functionality of the Stone.js `Adapter`
|
|
340
|
+
* to provide seamless integration with AWS Lambda for HTTP-based events. This adapter
|
|
341
|
+
* transforms incoming HTTP events from AWS Lambda into `IncomingHttpEvent` instances
|
|
342
|
+
* and produces a `RawHttpResponse` as output.
|
|
343
|
+
*
|
|
344
|
+
* This adapter simplifies the process of handling HTTP events within AWS Lambda
|
|
345
|
+
* while adhering to the Stone.js framework's event-driven architecture.
|
|
346
|
+
*
|
|
347
|
+
* @template AwsLambdaHttpEvent - The type of the raw HTTP event from AWS Lambda.
|
|
348
|
+
* @template RawHttpResponse - The type of the raw HTTP response to send back.
|
|
349
|
+
* @template AwsLambdaContext - The AWS Lambda execution context type.
|
|
350
|
+
* @template IncomingHttpEvent - The type of the processed incoming HTTP event.
|
|
351
|
+
* @template IncomingHttpEventOptions - Options used to create an incoming HTTP event.
|
|
352
|
+
* @template OutgoingHttpResponse - The type of the outgoing HTTP response after processing.
|
|
353
|
+
* @template AwsLambdaHttpAdapterContext - Context type specific to the HTTP adapter.
|
|
354
|
+
*
|
|
355
|
+
* @extends Adapter
|
|
356
|
+
*
|
|
357
|
+
* @example
|
|
358
|
+
* ```typescript
|
|
359
|
+
* import { AwsLambdaHttpAdapter } from '@stone-js/aws-lambda-http-adapter';
|
|
360
|
+
*
|
|
361
|
+
* const adapter = AwsLambdaHttpAdapter.create({...});
|
|
362
|
+
*
|
|
363
|
+
* const handler = await adapter.run();
|
|
364
|
+
*
|
|
365
|
+
* export { handler };
|
|
366
|
+
* ```
|
|
367
|
+
*
|
|
368
|
+
* @see {@link https://stone-js.com/docs Stone.js Documentation}
|
|
369
|
+
* @see {@link https://docs.aws.amazon.com/lambda/latest/dg/ AWS Lambda Documentation}
|
|
370
|
+
*/
|
|
371
|
+
class AwsLambdaHttpAdapter extends Adapter {
|
|
372
|
+
/**
|
|
373
|
+
* Creates an instance of the `AwsLambdaHttpAdapter`.
|
|
374
|
+
*
|
|
375
|
+
* @param blueprint - The application blueprint.
|
|
376
|
+
* @returns A new instance of `AwsLambdaHttpAdapter`.
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* ```typescript
|
|
380
|
+
* const adapter = AwsLambdaHttpAdapter.create(blueprint);
|
|
381
|
+
* await adapter.run();
|
|
382
|
+
* ```
|
|
383
|
+
*/
|
|
384
|
+
static create(blueprint) {
|
|
385
|
+
return new this(blueprint);
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Executes the adapter and provides an AWS Lambda-compatible HTTP handler function.
|
|
389
|
+
*
|
|
390
|
+
* This method initializes the adapter and returns a handler function that can
|
|
391
|
+
* process HTTP events in AWS Lambda. It transforms raw events into `IncomingHttpEvent`
|
|
392
|
+
* instances and produces `RawHttpResponse` objects as output.
|
|
393
|
+
*
|
|
394
|
+
* @template ExecutionResultType - The type representing the AWS Lambda event handler function.
|
|
395
|
+
* @returns A promise resolving to the AWS Lambda HTTP handler function.
|
|
396
|
+
* @throws {AwsLambdaHttpAdapterError} If used outside the AWS Lambda environment.
|
|
397
|
+
*/
|
|
398
|
+
async run() {
|
|
399
|
+
await this.onStart();
|
|
400
|
+
const handler = async (rawEvent, executionContext) => {
|
|
401
|
+
return await this.eventListener(rawEvent, executionContext);
|
|
402
|
+
};
|
|
403
|
+
return handler;
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Initializes the adapter and validates its execution context.
|
|
407
|
+
*
|
|
408
|
+
* Ensures that the adapter is running in an AWS Lambda environment. Throws an error
|
|
409
|
+
* if it detects that the adapter is being used in an unsupported environment (e.g., a browser).
|
|
410
|
+
*
|
|
411
|
+
* @throws {AwsLambdaHttpAdapterError} If executed outside an AWS Lambda environment.
|
|
412
|
+
*/
|
|
413
|
+
async onStart() {
|
|
414
|
+
if (typeof window === 'object') {
|
|
415
|
+
throw new AwsLambdaHttpAdapterError('This `AwsLambdaHttpAdapter` must be used only in AWS Lambda context.');
|
|
416
|
+
}
|
|
417
|
+
await this.executeHooks('onStart');
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Processes an incoming AWS Lambda HTTP event.
|
|
421
|
+
*
|
|
422
|
+
* Converts a raw AWS Lambda HTTP event into an `IncomingHttpEvent`, processes it through
|
|
423
|
+
* the Stone.js pipeline, and generates a `RawHttpResponse` to send back.
|
|
424
|
+
*
|
|
425
|
+
* @param rawEvent - The raw HTTP event received from AWS Lambda.
|
|
426
|
+
* @param executionContext - The AWS Lambda execution context associated with the event.
|
|
427
|
+
* @returns A promise resolving to the processed `RawHttpResponse`.
|
|
428
|
+
*/
|
|
429
|
+
async eventListener(rawEvent, executionContext) {
|
|
430
|
+
const incomingEventBuilder = AdapterEventBuilder.create({
|
|
431
|
+
resolver: (options) => IncomingHttpEvent.create(options)
|
|
432
|
+
});
|
|
433
|
+
const rawResponseBuilder = AdapterEventBuilder.create({
|
|
434
|
+
resolver: (options) => RawHttpResponseWrapper.create(options)
|
|
435
|
+
});
|
|
436
|
+
const rawResponse = { statusCode: 500 };
|
|
437
|
+
const context = {
|
|
438
|
+
rawEvent,
|
|
439
|
+
rawResponse,
|
|
440
|
+
executionContext,
|
|
441
|
+
rawResponseBuilder,
|
|
442
|
+
incomingEventBuilder
|
|
443
|
+
};
|
|
444
|
+
try {
|
|
445
|
+
const eventHandler = this.resolveEventHandler();
|
|
446
|
+
await this.executeEventHandlerHooks('onInit', eventHandler);
|
|
447
|
+
return await this.sendEventThroughDestination(context, eventHandler);
|
|
448
|
+
}
|
|
449
|
+
catch (error) {
|
|
450
|
+
const rawResponseBuilder = await this.handleError(error, context);
|
|
451
|
+
return await this.buildRawResponse({ ...context, rawResponseBuilder });
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
456
|
/**
|
|
457
457
|
* Adapter resolver for AWS Lambda HTTP adapter.
|
|
458
458
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stone-js/aws-lambda-http-adapter",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.18",
|
|
4
4
|
"description": "Official AWS Lambda HTTP adapter for Stone.js. Run your Stone.js apps on AWS Lambda behind API Gateway with full Continuum lifecycle support.",
|
|
5
5
|
"author": "Mr. Stone <evensstone@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -50,10 +50,10 @@
|
|
|
50
50
|
"node": ">=18.17.0"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"@stone-js/core": "0.8.
|
|
54
|
-
"@stone-js/env": "0.8.
|
|
55
|
-
"@stone-js/filesystem": "0.8.
|
|
56
|
-
"@stone-js/http-core": "0.8.
|
|
53
|
+
"@stone-js/core": "0.8.18",
|
|
54
|
+
"@stone-js/env": "0.8.18",
|
|
55
|
+
"@stone-js/filesystem": "0.8.18",
|
|
56
|
+
"@stone-js/http-core": "0.8.18"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
59
|
"accepts": "^1.3.8",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"proxy-addr": "^2.0.7",
|
|
64
64
|
"statuses": "^2.0.1",
|
|
65
65
|
"type-is": "^2.0.1",
|
|
66
|
-
"@stone-js/config": "0.8.
|
|
66
|
+
"@stone-js/config": "0.8.18"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"@commitlint/cli": "^19.6.1",
|