UNPKG

aws-lambda-event-handler

Version:
146 lines 6.78 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Lambda = void 0; const client_sqs_1 = require("@aws-sdk/client-sqs"); class Lambda { constructor() { this.sns = (topicArn, processSnsMessage) => { const fn = (event) => __awaiter(this, void 0, void 0, function* () { if (!Array.isArray(event.Records)) return; const record = event.Records[0]; if (record.EventSource !== 'aws:sns' || record.Sns.TopicArn !== topicArn) return; yield processSnsMessage(record.Sns); }); this.fns.push(fn); }; this.sqs = (queueArn, processSqsRecord) => { const fn = (event) => __awaiter(this, void 0, void 0, function* () { if (!Array.isArray(event.Records)) return; const records = event.Records; if (records[0].eventSource !== 'aws:sqs' || records[0].eventSourceARN !== queueArn) return; const fulfilledRecords = []; const errors = []; yield Promise.all(records.map((record) => processSqsRecord(record) .then(() => { fulfilledRecords.push({ Id: record.messageId, ReceiptHandle: record.receiptHandle }); }) .catch((err) => { errors.push(err); }))); if (errors.length) { if (fulfilledRecords.length) { yield this.sqsDeleteMessageBatch(queueArn, fulfilledRecords); } errors.forEach((err) => { console.error(err); }); throw new Error(`SQS Batch Failure: ${errors.length} of ${records.length} failed`); } }); this.fns.push(fn); }; this.sqsFifo = (queueArn, processSqsRecord) => { const fn = (event) => __awaiter(this, void 0, void 0, function* () { if (!Array.isArray(event.Records)) return; const records = event.Records; if (records[0].eventSource !== 'aws:sqs' || records[0].eventSourceARN !== queueArn) return; const fulfilledRecords = []; try { for (const record of records) { yield processSqsRecord(record); fulfilledRecords.push({ Id: record.messageId, ReceiptHandle: record.receiptHandle }); } } catch (err) { // catch error and delete fulfilled record(s) // throw error to retry failed and remaining record(s) if (fulfilledRecords.length) { yield this.sqsDeleteMessageBatch(queueArn, fulfilledRecords); } console.error(err); const failedRecordsCount = records.length - fulfilledRecords.length; throw new Error(`SQS FIFO Batch Failure: ${failedRecordsCount} of ${records.length} failed`); } }); this.fns.push(fn); }; this.msk = (mskArn, mskTopic, processMskRecord) => { const fn = (event) => __awaiter(this, void 0, void 0, function* () { if (event.eventSource !== 'aws:kafka' || event.eventSourceArn !== mskArn) return; if (!(event.records instanceof Object) || Array.isArray(event.records)) return; const records = []; for (const [, mskRecords] of Object.entries(event.records)) { mskRecords.forEach((mskRecord) => { if (mskRecord.topic === mskTopic) { records.push(mskRecord); } }); } for (const record of records) { yield processMskRecord(record); } }); this.fns.push(fn); }; this.scheduledEvent = (ruleArn, processScheduledEvent) => { const fn = (event) => __awaiter(this, void 0, void 0, function* () { if (event.source !== 'aws.events' || !event.resources.includes(ruleArn)) return; yield processScheduledEvent(); }); this.fns.push(fn); }; this.eventBridge = (resourceArn, processEventBridge) => { const fn = (event) => __awaiter(this, void 0, void 0, function* () { if (!Array.isArray(event.resources) || !event.resources.includes(resourceArn)) return; yield processEventBridge(event.detail); }); this.fns.push(fn); }; this.handler = (event) => __awaiter(this, void 0, void 0, function* () { for (const fn of this.fns) { const result = yield fn(event); if (result) { return result; } } }); this.sqsDeleteMessageBatch = (queueArn, fulfilledRecords) => __awaiter(this, void 0, void 0, function* () { const [, , , region, awsAccountId, queueName] = queueArn.split(':'); const queueUrl = `https://sqs.${region}.amazonaws.com/${awsAccountId}/${queueName}`; const sqs = new client_sqs_1.SQSClient({ region }); const command = new client_sqs_1.DeleteMessageBatchCommand({ QueueUrl: queueUrl, Entries: fulfilledRecords }); yield sqs.send(command); }); this.fns = []; } } exports.Lambda = Lambda; //# sourceMappingURL=index.js.map