UNPKG

@message-queue-toolkit/sqs

Version:
60 lines 2.91 kB
import { SetQueueAttributesCommand, TagQueueCommand } from '@aws-sdk/client-sqs'; import { isProduction } from '@message-queue-toolkit/core'; import { assertQueue, deleteQueue, getQueueAttributes, resolveQueueUrlFromLocatorConfig, } from "./sqsUtils.js"; export async function deleteSqs(sqsClient, 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.queue.QueueName) { throw new Error('QueueName must be set for automatic deletion'); } await deleteQueue(sqsClient, creationConfig.queue.QueueName, deletionConfig.waitForConfirmation !== false); } export async function updateQueueAttributes(sqsClient, queueUrl, attributes = {}) { const command = new SetQueueAttributesCommand({ QueueUrl: queueUrl, Attributes: attributes, }); await sqsClient.send(command); } export async function updateQueueTags(sqsClient, queueUrl, tags = {}) { const command = new TagQueueCommand({ QueueUrl: queueUrl, Tags: tags, }); await sqsClient.send(command); } export async function initSqs(sqsClient, locatorConfig, creationConfig) { // reuse existing queue only if (locatorConfig) { const queueUrl = await resolveQueueUrlFromLocatorConfig(sqsClient, locatorConfig); const checkResult = await getQueueAttributes(sqsClient, queueUrl, ['QueueArn']); if (checkResult.error === 'not_found') { throw new Error(`Queue with queueUrl ${locatorConfig.queueUrl} does not exist.`); } const queueArn = checkResult.result?.attributes?.QueueArn; if (!queueArn) { throw new Error('Queue ARN was not set'); } const splitUrl = queueUrl.split('/'); // biome-ignore lint/style/noNonNullAssertion: <explanation> const queueName = splitUrl[splitUrl.length - 1]; return { queueArn, queueUrl, queueName }; } // create new queue if does not exist if (!creationConfig?.queue.QueueName) { throw new Error('queueConfig.QueueName is mandatory when locator is not provided'); } // create new queue const { queueUrl, queueArn } = await assertQueue(sqsClient, creationConfig.queue, { topicArnsWithPublishPermissionsPrefix: creationConfig.topicArnsWithPublishPermissionsPrefix, updateAttributesIfExists: creationConfig.updateAttributesIfExists, forceTagUpdate: creationConfig.forceTagUpdate, }); const queueName = creationConfig.queue.QueueName; return { queueUrl, queueArn, queueName }; } //# sourceMappingURL=sqsInitter.js.map