UNPKG

@rewaa/event-broker

Version:

A broker for all the events that Rewaa will ever produce or consume

56 lines 2.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LambdaClient = void 0; const client_lambda_1 = require("@aws-sdk/client-lambda"); class LambdaClient { constructor(logger, config) { this.logger = logger; this.createQueueMappingForLambda = async (input) => { const params = { EventSourceArn: input.queueARN, FunctionName: input.functionName, BatchSize: input.batchSize, }; try { const { EventSourceMappings } = await this.getEventSourceMapping(input.queueARN); let functionName; if (EventSourceMappings === null || EventSourceMappings === void 0 ? void 0 : EventSourceMappings.length) { functionName = (EventSourceMappings[0].FunctionArn || "").split("function:")[1]; if (functionName && functionName !== input.functionName) { throw new Error(`Lambda Function ${functionName} is already listening for Queue ${input.queueARN}`); } } if (functionName === input.functionName) { return; } return await this.client.createEventSourceMapping(params); } catch (error) { this.logger.error(`Event Source Mapping Creation failed: Function: ${input.functionName} ${JSON.stringify(error)}`); if ((error === null || error === void 0 ? void 0 : error.name) === "ResourceConflictException") { this.logger.warn(`Event Source Mapping already exists: Function: ${input.functionName}`); return; } throw error; } }; this.getEventSourceMapping = async (queueARN) => { const params = { EventSourceArn: queueARN, }; try { return await this.client.listEventSourceMappings(params); } catch (error) { this.logger.error(`Failed to list Event Source Mapping for Queue: ${queueARN}`); throw error; } }; this.lambda = new client_lambda_1.Lambda(config); } get client() { return this.lambda; } } exports.LambdaClient = LambdaClient; //# sourceMappingURL=lambda.client.js.map