@message-queue-toolkit/sqs
Version:
SQS adapter for message-queue-toolkit
65 lines • 3.16 kB
JavaScript
import { SetQueueAttributesCommand, TagQueueCommand } from '@aws-sdk/client-sqs';
import { isProduction } from '@message-queue-toolkit/core';
import { assertQueue, deleteQueue, getQueueAttributes, resolveQueueUrlFromLocatorConfig, validateFifoQueueName, } 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, isFifoQueue) {
// 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: It's ok
const queueName = splitUrl[splitUrl.length - 1];
// Validate FIFO queue name consistency
if (isFifoQueue !== undefined) {
validateFifoQueueName(queueName, isFifoQueue);
}
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,
policyConfig: creationConfig.policyConfig,
}, isFifoQueue);
const queueName = creationConfig.queue.QueueName;
return { queueUrl, queueArn, queueName };
}
//# sourceMappingURL=sqsInitter.js.map