UNPKG

@message-queue-toolkit/sns

Version:
140 lines 7.53 kB
import { isProduction } from '@message-queue-toolkit/core'; import { resolveQueueUrlFromLocatorConfig, } from '@message-queue-toolkit/sqs'; import { deleteQueue, getQueueAttributes } from '@message-queue-toolkit/sqs'; import { isCreateTopicCommand } from "../types/TopicTypes.js"; import { subscribeToTopic } from "./snsSubscriber.js"; import { assertTopic, deleteSubscription, deleteTopic, getTopicArnByName, getTopicAttributes, } from "./snsUtils.js"; // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation> export 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 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, }; } const queueUrl = await resolveQueueUrlFromLocatorConfig(sqsClient, locatorConfig); const checkPromises = []; // Check for existing resources, using the locators const subscriptionTopicArn = locatorConfig.topicArn ?? (await getTopicArnByName(snsClient, locatorConfig.topicName)); const topicPromise = getTopicAttributes(snsClient, subscriptionTopicArn); checkPromises.push(topicPromise); const queuePromise = getQueueAttributes(sqsClient, queueUrl); checkPromises.push(queuePromise); const [topicCheckResult, queueCheckResult] = await Promise.all(checkPromises); if (queueCheckResult?.error === 'not_found') { throw new Error(`Queue with queueUrl ${queueUrl} does not exist.`); } if (topicCheckResult?.error === 'not_found') { throw new Error(`Topic with topicArn ${locatorConfig.topicArn} does not exist.`); } let queueName; if (queueUrl) { const splitUrl = queueUrl.split('/'); // biome-ignore lint/style/noNonNullAssertion: <explanation> queueName = splitUrl[splitUrl.length - 1]; } else { // biome-ignore lint/style/noNonNullAssertion: <explanation> queueName = creationConfig.queue.QueueName; } return { subscriptionArn: locatorConfig.subscriptionArn, topicArn: subscriptionTopicArn, queueUrl, queueName, }; } export async function deleteSnsSqs(sqsClient, snsClient, stsClient, deletionConfig, queueConfiguration, topicConfiguration, subscriptionConfiguration, extraParams, topicLocator) { if (!deletionConfig.deleteIfExists) { return; } if (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 subscribeToTopic(sqsClient, snsClient, stsClient, queueConfiguration, // biome-ignore lint/style/noNonNullAssertion: <explanation> topicConfiguration ?? topicLocator, subscriptionConfiguration, extraParams); if (!subscriptionArn) { throw new Error('subscriptionArn must be set for automatic deletion'); } await deleteQueue(sqsClient, // biome-ignore lint/style/noNonNullAssertion: <explanation> queueConfiguration.QueueName, deletionConfig.waitForConfirmation !== false); if (topicConfiguration) { const topicName = isCreateTopicCommand(topicConfiguration) ? topicConfiguration.Name : 'undefined'; if (!topicName) { throw new Error('Failed to resolve topic name'); } await deleteTopic(snsClient, stsClient, topicName); } await deleteSubscription(snsClient, subscriptionArn); } export async function deleteSns(snsClient, stsClient, deletionConfig, creationConfig) { if (!deletionConfig.deleteIfExists) { return; } if (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 deleteTopic(snsClient, stsClient, creationConfig.topic.Name); } export async function initSns(snsClient, stsClient, locatorConfig, creationConfig) { if (locatorConfig) { const topicArn = locatorConfig.topicArn ?? (await getTopicArnByName(snsClient, locatorConfig.topicName)); const checkResult = await 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'); } // biome-ignore lint/style/noNonNullAssertion: <explanation> const topicArn = await assertTopic(snsClient, stsClient, creationConfig.topic, { queueUrlsWithSubscribePermissionsPrefix: creationConfig.queueUrlsWithSubscribePermissionsPrefix, allowedSourceOwner: creationConfig.allowedSourceOwner, forceTagUpdate: creationConfig.forceTagUpdate, }); return { topicArn, }; } //# sourceMappingURL=snsInitter.js.map