-
Notifications
You must be signed in to change notification settings - Fork 43
chore(deps): migrate from azure-storage to @azure/storage-* SDK
#1410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,8 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Copyright (c) Microsoft Corporation and others. Licensed under the MIT license. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // SPDX-License-Identifier: MIT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const azure = require('azure-storage') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { QueueServiceClient } = require('@azure/storage-queue') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const logger = require('../logging/logger') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { promisify } = require('util') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class AzureStorageQueue { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| constructor(options) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -12,10 +11,9 @@ class AzureStorageQueue { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async initialize() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.queueService = azure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .createQueueService(this.options.connectionString) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .withFilter(new azure.LinearRetryPolicyFilter()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await promisify(this.queueService.createQueueIfNotExists).bind(this.queueService)(this.options.queueName) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.queueServiceClient = QueueServiceClient.fromConnectionString(this.options.connectionString) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.queueClient = this.queueServiceClient.getQueueClient(this.options.queueName) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await this.queueClient.createIfNotExists() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -25,7 +23,7 @@ class AzureStorageQueue { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param {string} message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async queue(message) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await promisify(this.queueService.createMessage).bind(this.queueService)(this.options.queueName, message) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await this.queueClient.sendMessage(message) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -34,35 +32,45 @@ class AzureStorageQueue { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Returns null if the queue is empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * If DQ count exceeds 5 the message will be deleted and the next message will be returned | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @returns {object} - { original: message, data: "JSON parsed, base64 decoded message" } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @returns {object} - { original: message, data: "JSON parsed message" } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async dequeue() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const message = await promisify(this.queueService.getMessage).bind(this.queueService)(this.options.queueName) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!message) return null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (message.dequeueCount <= 5) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { original: message, data: JSON.parse(Buffer.from(message.messageText, 'base64').toString('utf8')) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const response = await this.queueClient.receiveMessages({ numberOfMessages: 1 }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!response.receivedMessageItems || response.receivedMessageItems.length === 0) return null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const message = response.receivedMessageItems[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (message.dequeueCount <= 5) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| original: message, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data: JSON.parse(message.messageText) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await this.delete({ original: message }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this.dequeue() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+38
to
49
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const response = await this.queueClient.receiveMessages({ numberOfMessages: 1 }) | |
| if (!response.receivedMessageItems || response.receivedMessageItems.length === 0) return null | |
| const message = response.receivedMessageItems[0] | |
| if (message.dequeueCount <= 5) { | |
| return { | |
| original: message, | |
| data: JSON.parse(message.messageText) | |
| } | |
| } | |
| await this.delete({ original: message }) | |
| return this.dequeue() | |
| // Use a loop instead of recursion to avoid potential stack overflows when | |
| // many consecutive messages exceed the dequeueCount threshold. | |
| while (true) { | |
| const response = await this.queueClient.receiveMessages({ numberOfMessages: 1 }) | |
| if (!response.receivedMessageItems || response.receivedMessageItems.length === 0) return null | |
| const message = response.receivedMessageItems[0] | |
| if (message.dequeueCount <= 5) { | |
| return { | |
| original: message, | |
| data: JSON.parse(message.messageText) | |
| } | |
| } | |
| await this.delete({ original: message }) | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,7 @@ | ||
| // Copyright (c) Microsoft Corporation and others. Licensed under the MIT license. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| const azure = require('azure-storage') | ||
| const { promisify } = require('util') | ||
| const { BlobServiceClient } = require('@azure/storage-blob') | ||
| const Bottleneck = require('bottleneck').default | ||
| const limiter = new Bottleneck({ maxConcurrent: 1000 }) | ||
| const logger = require('../logging/logger') | ||
|
|
@@ -15,10 +14,9 @@ class AzBlobAttachmentStore { | |
| } | ||
|
|
||
| async initialize() { | ||
| this.blobService = azure | ||
| .createBlobService(this.options.connectionString) | ||
| .withFilter(new azure.LinearRetryPolicyFilter()) | ||
| return promisify(this.blobService.createContainerIfNotExists).bind(this.blobService)(this.containerName) | ||
| this.blobServiceClient = BlobServiceClient.fromConnectionString(this.options.connectionString) | ||
| this.containerClient = this.blobServiceClient.getContainerClient(this.containerName) | ||
| await this.containerClient.createIfNotExists() | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -32,15 +30,33 @@ class AzBlobAttachmentStore { | |
| try { | ||
| const name = 'attachment/' + key + '.json' | ||
| this.logger.info('2:1:1:notice_generate:get_single_file:start', { ts: new Date().toISOString(), file: key }) | ||
| const result = await promisify(this.blobService.getBlobToText).bind(this.blobService)(this.containerName, name) | ||
| const blobClient = this.containerClient.getBlobClient(name) | ||
| const downloadResponse = await blobClient.download() | ||
| const content = await this._streamToString(downloadResponse.readableStreamBody) | ||
| this.logger.info('2:1:1:notice_generate:get_single_file:end', { ts: new Date().toISOString(), file: key }) | ||
| return JSON.parse(result).attachment | ||
| return JSON.parse(content).attachment | ||
| } catch (error) { | ||
| if (error.statusCode === 404) return null | ||
| throw error | ||
| } | ||
| })() | ||
| } | ||
|
|
||
| /** | ||
| * Helper to convert a readable stream to a string | ||
| * | ||
| * @private | ||
| * @param {NodeJS.ReadableStream} readableStream - The stream to convert | ||
| * @returns {Promise<string>} The string content | ||
| */ | ||
| async _streamToString(readableStream) { | ||
| /** @type {Buffer[]} */ | ||
| const chunks = [] | ||
| for await (const chunk of readableStream) { | ||
| chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)) | ||
| } | ||
| return Buffer.concat(chunks).toString('utf8') | ||
| } | ||
|
Comment on lines
+52
to
+59
|
||
| } | ||
|
|
||
| module.exports = options => new AzBlobAttachmentStore(options) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old azure-storage SDK automatically base64-encoded queue messages, but the new @azure/storage-queue SDK does not. The code now directly parses message.messageText without base64 decoding. This is a breaking change if there are existing messages in the queue that were encoded with the old SDK, as they won't be decodable. Consider adding backward compatibility to handle both base64-encoded (old) and plain text (new) messages during the migration period, or ensure all queues are drained before deploying this change.
This issue also appears in the following locations of the same file: