UNPKG

@akadenia/azure-storage

Version:
205 lines (204 loc) 8.95 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.QueueStorage = void 0; const storage_queue_1 = require("@azure/storage-queue"); const identity_1 = require("@azure/identity"); /** * @class QueueStorage - A class that contains azure queue storage helpers * Supports both connection string and managed identity authentication */ class QueueStorage { constructor(connectionStringOrOptions) { if (typeof connectionStringOrOptions === "string") { if (!connectionStringOrOptions) { throw new Error("Connection string is required for Queue Storage operations"); } this.queueServiceClient = storage_queue_1.QueueServiceClient.fromConnectionString(connectionStringOrOptions); } else { if (!connectionStringOrOptions.accountName) { throw new Error("Account name is required when using managed identity"); } const accountUrl = `https://${connectionStringOrOptions.accountName}.queue.core.windows.net`; const credential = new identity_1.DefaultAzureCredential({ managedIdentityClientId: connectionStringOrOptions.managedIdentityClientId, }); this.queueServiceClient = new storage_queue_1.QueueServiceClient(accountUrl, credential); } } /** * Gets a QueueClient for a specific queue * @param {string} queueName - The name of the queue * @returns {QueueClient} - A QueueClient instance for the specified queue */ getQueueClient(queueName) { if (!queueName) { throw new Error("Queue name is required"); } return this.queueServiceClient.getQueueClient(queueName); } /** * Sends a message to the specified queue * @param {string} queueName - The name of the queue * @param {string | object} message - The message to send (string or object that will be JSON stringified) * @param {boolean} base64Encode - Whether to base64 encode the message (default: true) * @returns {Promise<QueueSendMessageResponse>} - The response from the queue operation * @throws {Error} If queue name or message is missing */ sendMessage(queueName_1, message_1) { return __awaiter(this, arguments, void 0, function* (queueName, message, base64Encode = true) { if (!queueName) { throw new Error("Queue name is required"); } if (!message) { throw new Error("Message is required"); } const queueClient = this.getQueueClient(queueName); const messageString = typeof message === "string" ? message : JSON.stringify(message); const messageToSend = base64Encode ? Buffer.from(messageString).toString("base64") : messageString; return yield queueClient.sendMessage(messageToSend); }); } /** * Receives messages from the specified queue * @param {string} queueName - The name of the queue * @param {number} maxMessages - Maximum number of messages to receive (1-32, default: 1) * @param {number} visibilityTimeout - Visibility timeout in seconds (default: 30) * @returns {Promise<any>} - The received messages * @throws {Error} If queue name is missing */ receiveMessages(queueName_1) { return __awaiter(this, arguments, void 0, function* (queueName, maxMessages = 1, visibilityTimeout = 30) { if (!queueName) { throw new Error("Queue name is required"); } const queueClient = this.getQueueClient(queueName); return yield queueClient.receiveMessages({ numberOfMessages: maxMessages, visibilityTimeout }); }); } /** * Deletes a message from the specified queue * @param {string} queueName - The name of the queue * @param {string} messageId - The message ID * @param {string} popReceipt - The pop receipt from when the message was received * @returns {Promise<void>} * @throws {Error} If queue name, message ID, or pop receipt is missing */ deleteMessage(queueName, messageId, popReceipt) { return __awaiter(this, void 0, void 0, function* () { if (!queueName) { throw new Error("Queue name is required"); } if (!messageId) { throw new Error("Message ID is required"); } if (!popReceipt) { throw new Error("Pop receipt is required"); } const queueClient = this.getQueueClient(queueName); yield queueClient.deleteMessage(messageId, popReceipt); }); } /** * Peeks messages from the specified queue without removing them * @param {string} queueName - The name of the queue * @param {number} maxMessages - Maximum number of messages to peek (1-32, default: 1) * @returns {Promise<any>} - The peeked messages * @throws {Error} If queue name is missing */ peekMessages(queueName_1) { return __awaiter(this, arguments, void 0, function* (queueName, maxMessages = 1) { if (!queueName) { throw new Error("Queue name is required"); } const queueClient = this.getQueueClient(queueName); return yield queueClient.peekMessages({ numberOfMessages: maxMessages }); }); } /** * Clears all messages from the specified queue * @param {string} queueName - The name of the queue * @returns {Promise<void>} * @throws {Error} If queue name is missing */ clearMessages(queueName) { return __awaiter(this, void 0, void 0, function* () { if (!queueName) { throw new Error("Queue name is required"); } const queueClient = this.getQueueClient(queueName); yield queueClient.clearMessages(); }); } /** * Creates a queue if it doesn't exist * @param {string} queueName - The name of the queue to create * @returns {Promise<void>} * @throws {Error} If queue name is missing */ createQueue(queueName) { return __awaiter(this, void 0, void 0, function* () { if (!queueName) { throw new Error("Queue name is required"); } const queueClient = this.getQueueClient(queueName); yield queueClient.createIfNotExists(); }); } /** * Deletes a queue * @param {string} queueName - The name of the queue to delete * @returns {Promise<void>} * @throws {Error} If queue name is missing */ deleteQueue(queueName) { return __awaiter(this, void 0, void 0, function* () { if (!queueName) { throw new Error("Queue name is required"); } const queueClient = this.getQueueClient(queueName); yield queueClient.delete(); }); } /** * Checks if a queue exists * @param {string} queueName - The name of the queue * @returns {Promise<boolean>} - True if the queue exists, false otherwise * @throws {Error} If queue name is missing */ queueExists(queueName) { return __awaiter(this, void 0, void 0, function* () { if (!queueName) { throw new Error("Queue name is required"); } const queueClient = this.getQueueClient(queueName); return yield queueClient.exists(); }); } /** * Gets the approximate number of messages in a queue * @param {string} queueName - The name of the queue * @returns {Promise<number>} - The approximate message count * @throws {Error} If queue name is missing */ getMessageCount(queueName) { return __awaiter(this, void 0, void 0, function* () { if (!queueName) { throw new Error("Queue name is required"); } const queueClient = this.getQueueClient(queueName); const properties = yield queueClient.getProperties(); return properties.approximateMessagesCount || 0; }); } } exports.QueueStorage = QueueStorage;