@klarna/nest-lambda-microservice 0.1.0-beta.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/CHANGELOG.md +12 -0
- package/LICENSE +202 -0
- package/README.md +364 -0
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.js +17 -0
- package/dist/client/lambda-microservice.client.d.ts +21 -0
- package/dist/client/lambda-microservice.client.js +102 -0
- package/dist/ctx-host/index.d.ts +1 -0
- package/dist/ctx-host/index.js +17 -0
- package/dist/ctx-host/lambda.context.d.ts +10 -0
- package/dist/ctx-host/lambda.context.js +18 -0
- package/dist/decorators/index.d.ts +1 -0
- package/dist/decorators/index.js +17 -0
- package/dist/decorators/use-partial-pattern-match.d.ts +21 -0
- package/dist/decorators/use-partial-pattern-match.js +53 -0
- package/dist/errors/IncomingResponseError.d.ts +6 -0
- package/dist/errors/IncomingResponseError.js +25 -0
- package/dist/errors/NoConsumerError.d.ts +3 -0
- package/dist/errors/NoConsumerError.js +9 -0
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.js +18 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +24 -0
- package/dist/interfaces/index.d.ts +4 -0
- package/dist/interfaces/index.js +20 -0
- package/dist/interfaces/lambda-microservice.config.d.ts +9 -0
- package/dist/interfaces/lambda-microservice.config.js +2 -0
- package/dist/interfaces/lambda-microservice.packet.d.ts +35 -0
- package/dist/interfaces/lambda-microservice.packet.js +2 -0
- package/dist/interfaces/request.builder.d.ts +4 -0
- package/dist/interfaces/request.builder.js +2 -0
- package/dist/interfaces/response.builder.d.ts +3 -0
- package/dist/interfaces/response.builder.js +2 -0
- package/dist/lambda/event-mapper.d.ts +7 -0
- package/dist/lambda/event-mapper.js +54 -0
- package/dist/lambda/index.d.ts +2 -0
- package/dist/lambda/index.js +18 -0
- package/dist/lambda/integrations/apigateway.d.ts +5 -0
- package/dist/lambda/integrations/apigateway.js +26 -0
- package/dist/lambda/integrations/dynamodb.d.ts +3 -0
- package/dist/lambda/integrations/dynamodb.js +16 -0
- package/dist/lambda/integrations/eventbridge.d.ts +10 -0
- package/dist/lambda/integrations/eventbridge.js +13 -0
- package/dist/lambda/integrations/index.d.ts +7 -0
- package/dist/lambda/integrations/index.js +23 -0
- package/dist/lambda/integrations/kinesis.d.ts +3 -0
- package/dist/lambda/integrations/kinesis.js +16 -0
- package/dist/lambda/integrations/s3.d.ts +3 -0
- package/dist/lambda/integrations/s3.js +16 -0
- package/dist/lambda/integrations/sns.d.ts +4 -0
- package/dist/lambda/integrations/sns.js +26 -0
- package/dist/lambda/integrations/sqs.d.ts +5 -0
- package/dist/lambda/integrations/sqs.js +38 -0
- package/dist/record-builders/api-gateway-request.builder.d.ts +17 -0
- package/dist/record-builders/api-gateway-request.builder.js +43 -0
- package/dist/record-builders/api-gateway-response.builder.d.ts +6 -0
- package/dist/record-builders/api-gateway-response.builder.js +23 -0
- package/dist/record-builders/custom-request.builder.d.ts +10 -0
- package/dist/record-builders/custom-request.builder.js +22 -0
- package/dist/record-builders/custom-response.builder.d.ts +6 -0
- package/dist/record-builders/custom-response.builder.js +27 -0
- package/dist/record-builders/event-bridge-request.builder.d.ts +16 -0
- package/dist/record-builders/event-bridge-request.builder.js +29 -0
- package/dist/record-builders/event-bridge-response.builder.d.ts +6 -0
- package/dist/record-builders/event-bridge-response.builder.js +23 -0
- package/dist/record-builders/index.d.ts +12 -0
- package/dist/record-builders/index.js +28 -0
- package/dist/record-builders/s3-request.builder.d.ts +15 -0
- package/dist/record-builders/s3-request.builder.js +30 -0
- package/dist/record-builders/s3-response.builder.d.ts +6 -0
- package/dist/record-builders/s3-response.builder.js +19 -0
- package/dist/record-builders/sns-request.builder.d.ts +16 -0
- package/dist/record-builders/sns-request.builder.js +51 -0
- package/dist/record-builders/sns-response.builder.d.ts +6 -0
- package/dist/record-builders/sns-response.builder.js +19 -0
- package/dist/record-builders/sqs-request.builder.d.ts +16 -0
- package/dist/record-builders/sqs-request.builder.js +57 -0
- package/dist/record-builders/sqs-response.builder.d.ts +11 -0
- package/dist/record-builders/sqs-response.builder.js +24 -0
- package/dist/server/__mocks__/index.d.ts +1 -0
- package/dist/server/__mocks__/index.js +17 -0
- package/dist/server/__mocks__/lambda-microservice.broker.d.ts +11 -0
- package/dist/server/__mocks__/lambda-microservice.broker.js +37 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +18 -0
- package/dist/server/lambda-microservice.broker.d.ts +13 -0
- package/dist/server/lambda-microservice.broker.js +44 -0
- package/dist/server/lambda-microservice.server.d.ts +26 -0
- package/dist/server/lambda-microservice.server.js +144 -0
- package/dist/server/mocks.d.ts +1 -0
- package/dist/server/mocks.js +17 -0
- package/package.json +59 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LambdaMicroserviceClient = exports.ClientToken = void 0;
|
|
4
|
+
const microservices_1 = require("@nestjs/microservices");
|
|
5
|
+
const rxjs_1 = require("rxjs");
|
|
6
|
+
const uuid_1 = require("uuid");
|
|
7
|
+
const errors_1 = require("../errors");
|
|
8
|
+
const lambda_1 = require("../lambda");
|
|
9
|
+
exports.ClientToken = Symbol('LambdaMicroserviceClient');
|
|
10
|
+
class LambdaMicroserviceClient extends microservices_1.ClientProxy {
|
|
11
|
+
broker;
|
|
12
|
+
eventMapper = new lambda_1.EventMapper();
|
|
13
|
+
requestMap = new WeakMap();
|
|
14
|
+
isConnected = false;
|
|
15
|
+
constructor(options) {
|
|
16
|
+
super();
|
|
17
|
+
this.broker = options.broker;
|
|
18
|
+
this.initializeSerializer(options);
|
|
19
|
+
this.initializeDeserializer(options);
|
|
20
|
+
}
|
|
21
|
+
async close() {
|
|
22
|
+
if (this.isConnected) {
|
|
23
|
+
this.isConnected = false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async connect() {
|
|
27
|
+
if (!this.isConnected) {
|
|
28
|
+
this.isConnected = true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async processEvent(event, context) {
|
|
32
|
+
const request = this.eventMapper.mapEventToRequest(event, context);
|
|
33
|
+
const requests = Array.isArray(request) ? request : [request];
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
const merged$ = (0, rxjs_1.merge)(...requests.map((outgoingRequest) => {
|
|
36
|
+
this.requestMap.set(outgoingRequest.data, outgoingRequest);
|
|
37
|
+
return this.send(outgoingRequest.pattern, outgoingRequest.data)
|
|
38
|
+
.pipe((0, rxjs_1.catchError)((err) => (0, rxjs_1.of)(new errors_1.IncomingResponseError(outgoingRequest.id, err))))
|
|
39
|
+
.pipe((0, rxjs_1.map)((incomingResponse) => [
|
|
40
|
+
outgoingRequest,
|
|
41
|
+
incomingResponse,
|
|
42
|
+
]));
|
|
43
|
+
}));
|
|
44
|
+
merged$
|
|
45
|
+
.pipe((0, rxjs_1.reduce)((acc, responseTuple) => {
|
|
46
|
+
return [...acc, responseTuple];
|
|
47
|
+
}, []))
|
|
48
|
+
.subscribe((responseTuplesList) => {
|
|
49
|
+
try {
|
|
50
|
+
resolve(this.eventMapper.mapToLambdaResponse(event, responseTuplesList));
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
reject(error);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
publish(partialPacket, callbackFn) {
|
|
59
|
+
const packet = this.assignPacketId(partialPacket);
|
|
60
|
+
const producerUnsubscribeCallback = () => {
|
|
61
|
+
this.routingMap.delete(packet.id);
|
|
62
|
+
};
|
|
63
|
+
this.routingMap.set(packet.id, callbackFn);
|
|
64
|
+
try {
|
|
65
|
+
const context = this.requestMap.get(packet.data)?.context;
|
|
66
|
+
const serializedPacket = this.serializer.serialize(packet);
|
|
67
|
+
const responseCallback = this.createResponseCallback();
|
|
68
|
+
const responseStream$ = this.broker.sendRequest({ ...serializedPacket, context });
|
|
69
|
+
const responseSubscription = responseStream$.subscribe({ next: responseCallback });
|
|
70
|
+
return () => {
|
|
71
|
+
responseSubscription.unsubscribe();
|
|
72
|
+
producerUnsubscribeCallback();
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
callbackFn({ err });
|
|
77
|
+
}
|
|
78
|
+
return producerUnsubscribeCallback;
|
|
79
|
+
}
|
|
80
|
+
assignPacketId(packet) {
|
|
81
|
+
const incomingRequest = this.requestMap.get(packet.data);
|
|
82
|
+
const id = incomingRequest?.id ?? (0, uuid_1.v4)();
|
|
83
|
+
return Object.assign(packet, { id });
|
|
84
|
+
}
|
|
85
|
+
dispatchEvent(_packet) {
|
|
86
|
+
throw new Error('The dispatchEvent is not implemented in Lambda Microservice');
|
|
87
|
+
}
|
|
88
|
+
createResponseCallback() {
|
|
89
|
+
return async (incomingResponse) => {
|
|
90
|
+
const { id, err, response, isDisposed } = await this.deserializer.deserialize(incomingResponse);
|
|
91
|
+
const callbackFn = this.routingMap.get(id);
|
|
92
|
+
if (!callbackFn) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (isDisposed || err) {
|
|
96
|
+
return callbackFn({ err, response, isDisposed: true });
|
|
97
|
+
}
|
|
98
|
+
callbackFn({ err, response });
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.LambdaMicroserviceClient = LambdaMicroserviceClient;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lambda.context';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./lambda.context"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BaseRpcContext } from '@nestjs/microservices';
|
|
2
|
+
import { LoggerService } from '@nestjs/common/services/logger.service';
|
|
3
|
+
import { Context } from 'aws-lambda';
|
|
4
|
+
export type LambdaContextArgs = [context: Context];
|
|
5
|
+
export declare class LambdaContext<T extends LoggerService> extends BaseRpcContext<LambdaContextArgs> {
|
|
6
|
+
protected logger?: T;
|
|
7
|
+
getLambdaInvocationContext(): Context;
|
|
8
|
+
setLogger(logger: T): this;
|
|
9
|
+
getLogger(): T | void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LambdaContext = void 0;
|
|
4
|
+
const microservices_1 = require("@nestjs/microservices");
|
|
5
|
+
class LambdaContext extends microservices_1.BaseRpcContext {
|
|
6
|
+
logger;
|
|
7
|
+
getLambdaInvocationContext() {
|
|
8
|
+
return this.args[0];
|
|
9
|
+
}
|
|
10
|
+
setLogger(logger) {
|
|
11
|
+
this.logger = logger;
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
getLogger() {
|
|
15
|
+
return this.logger;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.LambdaContext = LambdaContext;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './use-partial-pattern-match';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./use-partial-pattern-match"), exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decorator that marks all controller methods decorated with @MessagePattern decorator as eligible for partial match
|
|
3
|
+
* @example The below snippet
|
|
4
|
+
* ```
|
|
5
|
+
* @Controller()
|
|
6
|
+
* class Controller {
|
|
7
|
+
* @MessagePattern({ foo: 'bar' }, { partialMatch: true })
|
|
8
|
+
* public method() {}
|
|
9
|
+
* }
|
|
10
|
+
* ```
|
|
11
|
+
* has same effects as
|
|
12
|
+
* ```
|
|
13
|
+
* @Controller()
|
|
14
|
+
* @UsePartialPatternMatch()
|
|
15
|
+
* class Controller {
|
|
16
|
+
* @MessagePattern({ foo: 'bar' })
|
|
17
|
+
* public method() {}
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function UsePartialPatternMatch(): ClassDecorator;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UsePartialPatternMatch = void 0;
|
|
4
|
+
const constants_1 = require("@nestjs/microservices/constants");
|
|
5
|
+
const server_1 = require("../server");
|
|
6
|
+
const getClassMethods = (target) => {
|
|
7
|
+
const ret = new Set();
|
|
8
|
+
function methods(obj) {
|
|
9
|
+
if (obj !== null && typeof obj === 'object') {
|
|
10
|
+
const ps = Object.getOwnPropertyNames(obj);
|
|
11
|
+
ps.forEach((p) => {
|
|
12
|
+
const value = Reflect.get(obj, p);
|
|
13
|
+
if (value instanceof Function && Reflect.hasMetadata(constants_1.PATTERN_METADATA, value)) {
|
|
14
|
+
ret.add(value);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
methods(Object.getPrototypeOf(obj));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (target !== null && typeof target === 'function' && 'prototype' in target) {
|
|
21
|
+
methods(target.prototype);
|
|
22
|
+
}
|
|
23
|
+
return Array.from(ret);
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Decorator that marks all controller methods decorated with @MessagePattern decorator as eligible for partial match
|
|
27
|
+
* @example The below snippet
|
|
28
|
+
* ```
|
|
29
|
+
* @Controller()
|
|
30
|
+
* class Controller {
|
|
31
|
+
* @MessagePattern({ foo: 'bar' }, { partialMatch: true })
|
|
32
|
+
* public method() {}
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
* has same effects as
|
|
36
|
+
* ```
|
|
37
|
+
* @Controller()
|
|
38
|
+
* @UsePartialPatternMatch()
|
|
39
|
+
* class Controller {
|
|
40
|
+
* @MessagePattern({ foo: 'bar' })
|
|
41
|
+
* public method() {}
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
function UsePartialPatternMatch() {
|
|
46
|
+
return (target) => {
|
|
47
|
+
for (const method of getClassMethods(target)) {
|
|
48
|
+
const extrasMetadata = Reflect.getMetadata(constants_1.PATTERN_EXTRAS_METADATA, method) ?? {};
|
|
49
|
+
Reflect.defineMetadata(constants_1.PATTERN_EXTRAS_METADATA, { [server_1.PartialMatchExtraProp]: true, ...extrasMetadata }, method);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
exports.UsePartialPatternMatch = UsePartialPatternMatch;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IncomingResponseError = void 0;
|
|
4
|
+
class IncomingResponseError extends Error {
|
|
5
|
+
requestId;
|
|
6
|
+
cause;
|
|
7
|
+
constructor(requestId, cause) {
|
|
8
|
+
super(`The server failed to process the request`);
|
|
9
|
+
this.requestId = requestId;
|
|
10
|
+
this.cause = cause;
|
|
11
|
+
this.message = `${this.message}, cause: ${this.trySerializeCause(cause)}`;
|
|
12
|
+
}
|
|
13
|
+
trySerializeCause(cause) {
|
|
14
|
+
try {
|
|
15
|
+
if (cause instanceof Error) {
|
|
16
|
+
return JSON.stringify(cause, Object.getOwnPropertyNames(cause));
|
|
17
|
+
}
|
|
18
|
+
return JSON.stringify(cause);
|
|
19
|
+
}
|
|
20
|
+
catch (_error) {
|
|
21
|
+
return `${cause}`;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.IncomingResponseError = IncomingResponseError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NoConsumerError = void 0;
|
|
4
|
+
class NoConsumerError extends Error {
|
|
5
|
+
constructor() {
|
|
6
|
+
super('No consumer is currently connected to the broker. The messages emitted by the client will be lost.');
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
exports.NoConsumerError = NoConsumerError;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./IncomingResponseError"), exports);
|
|
18
|
+
__exportStar(require("./NoConsumerError"), exports);
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./client"), exports);
|
|
18
|
+
__exportStar(require("./ctx-host"), exports);
|
|
19
|
+
__exportStar(require("./decorators"), exports);
|
|
20
|
+
__exportStar(require("./errors"), exports);
|
|
21
|
+
__exportStar(require("./lambda"), exports);
|
|
22
|
+
__exportStar(require("./interfaces"), exports);
|
|
23
|
+
__exportStar(require("./record-builders"), exports);
|
|
24
|
+
__exportStar(require("./server"), exports);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./lambda-microservice.config"), exports);
|
|
18
|
+
__exportStar(require("./lambda-microservice.packet"), exports);
|
|
19
|
+
__exportStar(require("./request.builder"), exports);
|
|
20
|
+
__exportStar(require("./response.builder"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { CustomClientOptions } from '@nestjs/microservices';
|
|
2
|
+
import { LambdaMicroserviceClient } from '../client';
|
|
3
|
+
import { LambdaMicroserviceBroker } from '../server';
|
|
4
|
+
export interface LambdaMicroserviceOptions extends CustomClientOptions {
|
|
5
|
+
customClass: typeof LambdaMicroserviceClient;
|
|
6
|
+
options: {
|
|
7
|
+
broker: LambdaMicroserviceBroker;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ReadPacket, WritePacket, PacketId } from '@nestjs/microservices';
|
|
2
|
+
import { Context, SNSMessage, SQSRecord, S3EventRecord, EventBridgeEvent, APIGatewayEvent, APIGatewayProxyEventV2 } from 'aws-lambda';
|
|
3
|
+
import { IncomingResponseError } from '../errors';
|
|
4
|
+
export interface ContextAware {
|
|
5
|
+
context: Context;
|
|
6
|
+
}
|
|
7
|
+
export type OutgoingCustomRequest = ReadPacket & PacketId & ContextAware;
|
|
8
|
+
export type IncomingCustomRequest = ReadPacket & PacketId & ContextAware;
|
|
9
|
+
export type OutgoingCustomResponse = WritePacket & PacketId;
|
|
10
|
+
export type IncomingCustomResponse = WritePacket & PacketId;
|
|
11
|
+
export type OutgoingSqsRequest = ReadPacket<SQSRecord> & PacketId & ContextAware;
|
|
12
|
+
export type IncomingSqsRequest = ReadPacket<SQSRecord> & PacketId & ContextAware;
|
|
13
|
+
export type OutgoingSqsResponse = WritePacket<void> & PacketId;
|
|
14
|
+
export type IncomingSqsResponse = WritePacket<void> & PacketId;
|
|
15
|
+
export type OutgoingSnsRequest = ReadPacket<SNSMessage> & PacketId & ContextAware;
|
|
16
|
+
export type IncomingSnsRequest = ReadPacket<SNSMessage> & PacketId & ContextAware;
|
|
17
|
+
export type OutgoingSnsResponse = WritePacket<void> & PacketId;
|
|
18
|
+
export type IncomingSnsResponse = WritePacket<void> & PacketId;
|
|
19
|
+
export type OutgoingS3Request = ReadPacket<S3EventRecord> & PacketId & ContextAware;
|
|
20
|
+
export type IncomingS3Request = ReadPacket<S3EventRecord> & PacketId & ContextAware;
|
|
21
|
+
export type OutgoingS3Response = WritePacket<void> & PacketId;
|
|
22
|
+
export type IncomingS3Response = WritePacket<void> & PacketId;
|
|
23
|
+
export type OutgoingEventBridgeRequest = ReadPacket<EventBridgeEvent<string, unknown>> & PacketId & ContextAware;
|
|
24
|
+
export type IncomingEventBridgeRequest = ReadPacket<EventBridgeEvent<string, unknown>> & PacketId & ContextAware;
|
|
25
|
+
export type OutgoingEventBridgeResponse = WritePacket<void> & PacketId;
|
|
26
|
+
export type IncomingEventBridgeResponse = WritePacket<void> & PacketId;
|
|
27
|
+
export type OutgoingApiGatewayRequest = ReadPacket<APIGatewayEvent | APIGatewayProxyEventV2> & PacketId & ContextAware;
|
|
28
|
+
export type IncomingApiGatewayRequest = ReadPacket<APIGatewayEvent | APIGatewayProxyEventV2> & PacketId & ContextAware;
|
|
29
|
+
export type OutgoingApiGatewayResponse = WritePacket<void> & PacketId;
|
|
30
|
+
export type IncomingApiGatewayResponse = WritePacket<void> & PacketId;
|
|
31
|
+
export type OutgoingRequest = OutgoingCustomRequest | OutgoingSqsRequest | OutgoingSnsRequest;
|
|
32
|
+
export type IncomingRequest = IncomingCustomRequest | IncomingSqsRequest | IncomingSnsRequest;
|
|
33
|
+
export type OutgoingResponse = OutgoingCustomResponse | OutgoingSqsResponse | OutgoingSnsResponse;
|
|
34
|
+
export type IncomingResponse = IncomingCustomResponse | IncomingSqsResponse | IncomingSnsResponse;
|
|
35
|
+
export type ResponseTuple = [OutgoingRequest, unknown | IncomingResponseError];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Context } from 'aws-lambda';
|
|
2
|
+
import { OutgoingRequest, ResponseTuple } from '../interfaces';
|
|
3
|
+
export declare class EventMapper {
|
|
4
|
+
mapEventToRequest(event: unknown, context: Context): OutgoingRequest | OutgoingRequest[];
|
|
5
|
+
mapToLambdaResponse(event: unknown, responseTuples: ResponseTuple[]): unknown;
|
|
6
|
+
protected throwNotImplementedError(eventType: string): void;
|
|
7
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventMapper = void 0;
|
|
4
|
+
const microservices_1 = require("@nestjs/microservices");
|
|
5
|
+
const record_builders_1 = require("../record-builders");
|
|
6
|
+
const integrations_1 = require("./integrations");
|
|
7
|
+
class EventMapper {
|
|
8
|
+
mapEventToRequest(event, context) {
|
|
9
|
+
if ((0, integrations_1.isApiGatewayEvent)(event)) {
|
|
10
|
+
return new record_builders_1.ApiGatewayRequestBuilder(event, context).build();
|
|
11
|
+
}
|
|
12
|
+
if ((0, integrations_1.isEventBridgeEvent)(event)) {
|
|
13
|
+
return new record_builders_1.EventBridgeRequestBuilder(event, context).build();
|
|
14
|
+
}
|
|
15
|
+
if ((0, integrations_1.isS3Event)(event)) {
|
|
16
|
+
return event.Records.map((record) => {
|
|
17
|
+
return new record_builders_1.S3RequestBuilder(record, context).build();
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
if ((0, integrations_1.isSnsEvent)(event)) {
|
|
21
|
+
return event.Records.map((record) => {
|
|
22
|
+
return new record_builders_1.SnsRequestBuilder(record, context).build();
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
if ((0, integrations_1.isSqsEvent)(event)) {
|
|
26
|
+
return event.Records.map((record) => {
|
|
27
|
+
return new record_builders_1.SqsRequestBuilder(record, context).build();
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return new record_builders_1.CustomRequestBuilder(event, context).build();
|
|
31
|
+
}
|
|
32
|
+
mapToLambdaResponse(event, responseTuples) {
|
|
33
|
+
if ((0, integrations_1.isApiGatewayEvent)(event)) {
|
|
34
|
+
return new record_builders_1.ApiGatewayResponseBuilder(responseTuples).build();
|
|
35
|
+
}
|
|
36
|
+
if ((0, integrations_1.isEventBridgeEvent)(event)) {
|
|
37
|
+
return new record_builders_1.EventBridgeResponseBuilder(responseTuples).build();
|
|
38
|
+
}
|
|
39
|
+
if ((0, integrations_1.isS3Event)(event)) {
|
|
40
|
+
return new record_builders_1.S3ResponseBuilder(responseTuples).build();
|
|
41
|
+
}
|
|
42
|
+
if ((0, integrations_1.isSnsEvent)(event)) {
|
|
43
|
+
return new record_builders_1.SnsResponseBuilder(responseTuples).build();
|
|
44
|
+
}
|
|
45
|
+
if ((0, integrations_1.isSqsEvent)(event)) {
|
|
46
|
+
return new record_builders_1.SqsResponseBuilder(responseTuples).build();
|
|
47
|
+
}
|
|
48
|
+
return new record_builders_1.CustomResponseBuilder(responseTuples).build();
|
|
49
|
+
}
|
|
50
|
+
throwNotImplementedError(eventType) {
|
|
51
|
+
throw new microservices_1.RpcException(`Event source ${eventType} not implemented`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.EventMapper = EventMapper;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./integrations"), exports);
|
|
18
|
+
__exportStar(require("./event-mapper"), exports);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { APIGatewayEvent, APIGatewayProxyEventV2 } from 'aws-lambda';
|
|
2
|
+
export type AnyAPIGatewayEvent = APIGatewayEvent | APIGatewayProxyEventV2;
|
|
3
|
+
export declare const isApiGatewayEvent: (event: unknown) => event is AnyAPIGatewayEvent;
|
|
4
|
+
export declare const isApiGatewayEventV1: (event: unknown) => event is import("aws-lambda").APIGatewayProxyEvent;
|
|
5
|
+
export declare const isApiGatewayEventV2: (event: unknown) => event is APIGatewayProxyEventV2;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.isApiGatewayEventV2 = exports.isApiGatewayEventV1 = exports.isApiGatewayEvent = void 0;
|
|
5
|
+
const isApiGatewayEvent = (event) => {
|
|
6
|
+
return (0, exports.isApiGatewayEventV1)(event) || (0, exports.isApiGatewayEventV2)(event);
|
|
7
|
+
};
|
|
8
|
+
exports.isApiGatewayEvent = isApiGatewayEvent;
|
|
9
|
+
const isApiGatewayEventV1 = (event) => {
|
|
10
|
+
return (event !== null &&
|
|
11
|
+
typeof event === 'object' &&
|
|
12
|
+
'requestContext' in event &&
|
|
13
|
+
'httpMethod' in event &&
|
|
14
|
+
'path' in event &&
|
|
15
|
+
'isBase64Encoded' in event);
|
|
16
|
+
};
|
|
17
|
+
exports.isApiGatewayEventV1 = isApiGatewayEventV1;
|
|
18
|
+
const isApiGatewayEventV2 = (event) => {
|
|
19
|
+
return (event !== null &&
|
|
20
|
+
typeof event === 'object' &&
|
|
21
|
+
'requestContext' in event &&
|
|
22
|
+
'routeKey' in event &&
|
|
23
|
+
'rawPath' in event &&
|
|
24
|
+
'isBase64Encoded' in event);
|
|
25
|
+
};
|
|
26
|
+
exports.isApiGatewayEventV2 = isApiGatewayEventV2;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.isDynamoDbEvent = exports.isDynamoDbRecord = void 0;
|
|
5
|
+
const isDynamoDbRecord = (record) => {
|
|
6
|
+
return (record !== null && typeof record === 'object' && 'eventSource' in record && record?.eventSource === 'aws:dynamodb');
|
|
7
|
+
};
|
|
8
|
+
exports.isDynamoDbRecord = isDynamoDbRecord;
|
|
9
|
+
const isDynamoDbEvent = (event) => {
|
|
10
|
+
return (event !== null &&
|
|
11
|
+
typeof event === 'object' &&
|
|
12
|
+
'Records' in event &&
|
|
13
|
+
Array.isArray(event?.Records) &&
|
|
14
|
+
event.Records.every(exports.isDynamoDbRecord));
|
|
15
|
+
};
|
|
16
|
+
exports.isDynamoDbEvent = isDynamoDbEvent;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EventBridgeEvent } from 'aws-lambda';
|
|
2
|
+
export { EventBridgeEvent } from 'aws-lambda';
|
|
3
|
+
export type JSONValue = string | number | boolean | JSONObject | JSONArray | Record<never, never>;
|
|
4
|
+
interface JSONObject {
|
|
5
|
+
[x: string]: JSONValue;
|
|
6
|
+
}
|
|
7
|
+
type JSONArray = Array<JSONValue>;
|
|
8
|
+
export type AnyEventBridgeEvent = EventBridgeEvent<string, JSONValue>;
|
|
9
|
+
export type EventBridgeCronEvent = EventBridgeEvent<'Scheduled Event', Record<string, unknown>>;
|
|
10
|
+
export declare const isEventBridgeEvent: (event: unknown) => event is AnyEventBridgeEvent;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents.html
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.isEventBridgeEvent = void 0;
|
|
5
|
+
const isEventBridgeEvent = (event) => {
|
|
6
|
+
return (event !== null &&
|
|
7
|
+
typeof event === 'object' &&
|
|
8
|
+
'source' in event &&
|
|
9
|
+
typeof event.source === 'string' &&
|
|
10
|
+
'detail-type' in event &&
|
|
11
|
+
typeof event['detail-type'] === 'string');
|
|
12
|
+
};
|
|
13
|
+
exports.isEventBridgeEvent = isEventBridgeEvent;
|