UNPKG

@message-queue-toolkit/sns

Version:
144 lines 7.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.initSnsSqs = initSnsSqs; exports.deleteSnsSqs = deleteSnsSqs; exports.deleteSns = deleteSns; exports.initSns = initSns; const core_1 = require("@message-queue-toolkit/core"); const sqs_1 = require("@message-queue-toolkit/sqs"); const TopicTypes_1 = require("../types/TopicTypes"); const snsSubscriber_1 = require("./snsSubscriber"); const snsUtils_1 = require("./snsUtils"); // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation> async function initSnsSqs(sqsClient, snsClient, stsClient, locatorConfig, creationConfig, subscriptionConfig, extraParams) { if (!locatorConfig?.subscriptionArn) { if (!creationConfig?.topic && !locatorConfig?.topicArn && !locatorConfig?.topicName) { throw new Error('If locatorConfig.subscriptionArn is not specified, creationConfig.topic is mandatory in order to attempt to create missing topic and subscribe to it OR locatorConfig.name or locatorConfig.topicArn parameter is mandatory, to create subscription for existing topic.'); } if (!creationConfig?.queue) { throw new Error('If locatorConfig.subscriptionArn is not specified, creationConfig.queue parameter is mandatory, as there will be an attempt to create the missing queue'); } if (!creationConfig.queue.QueueName) { throw new Error('If locatorConfig.subscriptionArn is not specified, creationConfig.queue.QueueName parameter is mandatory, as there will be an attempt to create the missing queue'); } if (!subscriptionConfig) { throw new Error('If locatorConfig.subscriptionArn is not specified, subscriptionConfig parameter is mandatory, as there will be an attempt to create the missing subscription'); } const topicResolutionOptions = { ...locatorConfig, ...creationConfig.topic, }; const { subscriptionArn, topicArn, queueUrl } = await (0, snsSubscriber_1.subscribeToTopic)(sqsClient, snsClient, stsClient, creationConfig.queue, topicResolutionOptions, subscriptionConfig, { updateAttributesIfExists: creationConfig.updateAttributesIfExists, queueUrlsWithSubscribePermissionsPrefix: creationConfig.queueUrlsWithSubscribePermissionsPrefix, allowedSourceOwner: creationConfig.allowedSourceOwner, topicArnsWithPublishPermissionsPrefix: creationConfig.topicArnsWithPublishPermissionsPrefix, logger: extraParams?.logger, forceTagUpdate: creationConfig.forceTagUpdate, }); if (!subscriptionArn) { throw new Error('Failed to subscribe'); } return { subscriptionArn, topicArn, queueName: creationConfig.queue.QueueName, queueUrl, }; } if (!locatorConfig.queueUrl) { throw new Error('If locatorConfig.subscriptionArn is provided, you have to also provide locatorConfig.queueUrl'); } const checkPromises = []; // Check for existing resources, using the locators const subscriptionTopicArn = locatorConfig.topicArn ?? (await (0, snsUtils_1.getTopicArnByName)(snsClient, locatorConfig.topicName)); const topicPromise = (0, snsUtils_1.getTopicAttributes)(snsClient, subscriptionTopicArn); checkPromises.push(topicPromise); if (locatorConfig.queueUrl) { const queuePromise = (0, sqs_1.getQueueAttributes)(sqsClient, locatorConfig.queueUrl); checkPromises.push(queuePromise); } const [topicCheckResult, queueCheckResult] = await Promise.all(checkPromises); if (queueCheckResult?.error === 'not_found') { throw new Error(`Queue with queueUrl ${locatorConfig.queueUrl} does not exist.`); } if (topicCheckResult.error === 'not_found') { throw new Error(`Topic with topicArn ${locatorConfig.topicArn} does not exist.`); } let queueName; if (locatorConfig.queueUrl) { const splitUrl = locatorConfig.queueUrl.split('/'); queueName = splitUrl[splitUrl.length - 1]; } else { queueName = creationConfig.queue.QueueName; } return { subscriptionArn: locatorConfig.subscriptionArn, topicArn: subscriptionTopicArn, queueUrl: locatorConfig.queueUrl, queueName, }; } async function deleteSnsSqs(sqsClient, snsClient, stsClient, deletionConfig, queueConfiguration, topicConfiguration, subscriptionConfiguration, extraParams, topicLocator) { if (!deletionConfig.deleteIfExists) { return; } if ((0, core_1.isProduction)() && !deletionConfig.forceDeleteInProduction) { throw new Error('You are running autodeletion in production. This can and probably will cause a loss of data. If you are absolutely sure you want to do this, please set deletionConfig.forceDeleteInProduction to true'); } const { subscriptionArn } = await (0, snsSubscriber_1.subscribeToTopic)(sqsClient, snsClient, stsClient, queueConfiguration, topicConfiguration ?? topicLocator, subscriptionConfiguration, extraParams); if (!subscriptionArn) { throw new Error('subscriptionArn must be set for automatic deletion'); } await (0, sqs_1.deleteQueue)(sqsClient, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion queueConfiguration.QueueName, deletionConfig.waitForConfirmation !== false); if (topicConfiguration) { const topicName = (0, TopicTypes_1.isCreateTopicCommand)(topicConfiguration) ? topicConfiguration.Name : 'undefined'; if (!topicName) { throw new Error('Failed to resolve topic name'); } await (0, snsUtils_1.deleteTopic)(snsClient, stsClient, topicName); } await (0, snsUtils_1.deleteSubscription)(snsClient, subscriptionArn); } async function deleteSns(snsClient, stsClient, deletionConfig, creationConfig) { if (!deletionConfig.deleteIfExists) { return; } if ((0, core_1.isProduction)() && !deletionConfig.forceDeleteInProduction) { throw new Error('You are running autodeletion in production. This can and probably will cause a loss of data. If you are absolutely sure you want to do this, please set deletionConfig.forceDeleteInProduction to true'); } if (!creationConfig.topic?.Name) { throw new Error('topic.Name must be set for automatic deletion'); } await (0, snsUtils_1.deleteTopic)(snsClient, stsClient, creationConfig.topic.Name); } async function initSns(snsClient, stsClient, locatorConfig, creationConfig) { if (locatorConfig) { const topicArn = locatorConfig.topicArn ?? (await (0, snsUtils_1.getTopicArnByName)(snsClient, locatorConfig.topicName)); const checkResult = await (0, snsUtils_1.getTopicAttributes)(snsClient, topicArn); if (checkResult.error === 'not_found') { throw new Error(`Topic with topicArn ${locatorConfig.topicArn} does not exist.`); } return { topicArn, }; } // create new topic if it does not exist if (!creationConfig) { throw new Error('When locatorConfig for the topic is not specified, creationConfig of the topic is mandatory'); } const topicArn = await (0, snsUtils_1.assertTopic)(snsClient, stsClient, creationConfig.topic, { queueUrlsWithSubscribePermissionsPrefix: creationConfig.queueUrlsWithSubscribePermissionsPrefix, allowedSourceOwner: creationConfig.allowedSourceOwner, forceTagUpdate: creationConfig.forceTagUpdate, }); return { topicArn, }; } //# sourceMappingURL=snsInitter.js.map