@beauraines/node-helpers 2.14.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,9 +2,15 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
- ## [2.14.0](https://github.com/beauraines/node-helpers/compare/v2.7.1...v2.14.0) (2023-07-02)
5
+ ## [4.0.0](https://github.com/beauraines/node-helpers/compare/v2.7.1...v4.0.0) (2023-07-02)
6
6
 
7
7
 
8
+ ### ⚠ BREAKING CHANGES
9
+
10
+ * **azure:** - `generateBlobSignedUrl` is now asynchronous and must be called with `await`
11
+
12
+ Updates unit test to run async
13
+
8
14
  ### Features
9
15
 
10
16
  * adds download blob from azure helper ([08a1571](https://github.com/beauraines/node-helpers/commit/08a1571ab18b9eeeb1273e05b5abed95a7300457))
@@ -17,8 +23,12 @@ All notable changes to this project will be documented in this file. See [standa
17
23
 
18
24
  * **deps:** bump azure-devops-node-api from 12.0.0 to 12.1.0 ([#37](https://github.com/beauraines/node-helpers/issues/37)) ([b71396d](https://github.com/beauraines/node-helpers/commit/b71396d28d089a1f8919ad53363a4759f61b1e52))
19
25
  * **deps:** bump node-fetch from 2.6.11 to 2.6.12 ([786f458](https://github.com/beauraines/node-helpers/commit/786f458fbf0c03fd9462ab67db558c5646afb62e))
26
+ * **deps:** bump xml2js and @azure/core-http ([#46](https://github.com/beauraines/node-helpers/issues/46)) ([83b0c60](https://github.com/beauraines/node-helpers/commit/83b0c6077e70120a7e24519f7a03740578236969))
20
27
  * increment version number ([c42e3ae](https://github.com/beauraines/node-helpers/commit/c42e3aebf1373b8820b18ced1b88617bbfe69da6))
21
28
 
29
+
30
+ * **azure:** migrates remaining methods to @azure/storage blob ([#45](https://github.com/beauraines/node-helpers/issues/45)) ([0b11af0](https://github.com/beauraines/node-helpers/commit/0b11af00b2fb11b3e99a6190fd7848cd7563901c))
31
+
22
32
  ### [2.7.1](https://github.com/beauraines/node-helpers/compare/v2.7.0...v2.7.1) (2023-06-14)
23
33
 
24
34
  ## [2.7.0](https://github.com/beauraines/node-helpers/compare/v2.6.1...v2.7.0) (2023-06-14)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beauraines/node-helpers",
3
- "version": "2.14.0",
3
+ "version": "4.0.0",
4
4
  "description": "Collection of node helpers",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -16,7 +16,6 @@
16
16
  "@azure/storage-blob": "^12.14.0",
17
17
  "@azure/storage-queue": "^12.11.0",
18
18
  "azure-devops-node-api": "^12.0.0",
19
- "azure-storage": "^2.10.7",
20
19
  "dayjs": "^1.11.7",
21
20
  "node-fetch": "^2.6.7",
22
21
  "sparkly": "^5.0.0",
package/src/azure.js CHANGED
@@ -1,4 +1,3 @@
1
- const azure = require('azure-storage');
2
1
  const dayjs = require('dayjs')
3
2
  const fs = require('fs');
4
3
  const { streamToBuffer } = require('./helpers.js')
@@ -127,22 +126,26 @@ getStorageQueueSignedURL(queueUrl,options) {
127
126
  * @param {string} blobName The name of the blob to generate the token
128
127
  * @returns {string} the signed URL for the blob
129
128
  */
130
- //TODO migrate to @azure/storage-blob
131
- generateBlobSignedUrl(containerName, blobName) {
132
-
133
- const sharedAccessPolicy = {
134
- AccessPolicy: {
135
- Permissions: azure.BlobUtilities.SharedAccessPermissions.READ,
136
- Start: new Date(),
137
- Expiry: azure.date.minutesFromNow(this.tokenExpiry),
138
- },
139
- };
129
+ async generateBlobSignedUrl(containerName, blobName) {
130
+
131
+ const blobServiceClient = new BlobServiceClient(
132
+ this.host('blob',this.cloudName),
133
+ new StorageSharedKeyCredential(this.storageAccountName, this.storageAccountKey)
134
+ );
135
+ const containerClient = blobServiceClient.getContainerClient(containerName);
136
+ const blockBlobClient = containerClient.getBlockBlobClient(blobName);
137
+
138
+ const now = dayjs()
140
139
 
141
- const blobService = azure.createBlobService(this.storageAccountName, this.storageAccountKey, this.host('blob',this.cloudName));
142
- const sasToken = blobService.generateSharedAccessSignature(containerName, blobName, sharedAccessPolicy);
143
- const blobUrl = blobService.getUrl(containerName, blobName, sasToken);
140
+ const options = {
141
+ permissions: 'r',
142
+ startsOn: now.toDate(),
143
+ expiresOn: now.add(this.tokenExpiry,'minutes')
144
+ }
144
145
 
145
- return blobUrl;
146
+ const signedUrl = await blockBlobClient.generateSasUrl(options)
147
+ console.log(signedUrl)
148
+ return signedUrl;
146
149
  }
147
150
 
148
151
  /**
@@ -152,26 +155,35 @@ getStorageQueueSignedURL(queueUrl,options) {
152
155
  * @param {string} file The path the the local file to upload to the container
153
156
  * @returns {boolean} Success or failure to upload
154
157
  */
155
- //TODO migrate to @azure/storage-blob
156
- uploadBlobFromFile(containerName,file) {
157
- const blobService = azure.createBlobService(this.storageAccountName, this.storageAccountKey, this.host('blob',this.cloudName));
158
+ async uploadBlobFromFile(containerName,file) {
159
+ const blobServiceClient = new BlobServiceClient(
160
+ this.host('blob',this.cloudName),
161
+ new StorageSharedKeyCredential(this.storageAccountName, this.storageAccountKey)
162
+ );
163
+ const containerClient = blobServiceClient.getContainerClient(containerName);
164
+
158
165
  const options = {
159
- access: 'container'
166
+ access: 'container'
160
167
  };
161
168
 
162
169
  let blobName = path.basename(file)
163
- blobService.createBlockBlobFromLocalFile(containerName,blobName,file,options,function(error,response) {
164
- if( error) {
165
- console.error(error.message)
166
- return false
167
- } else {
168
- // TODO remove this from this function - separation of concerns, let the caller do the logging
169
- console.log(`${response.name} uploaded to ${response.container} container`)
170
- return true
171
- }
172
- });
170
+ const blockBlobClient = containerClient.getBlockBlobClient(blobName);
171
+ try {
172
+ // eslint-disable-next-line no-unused-vars
173
+ const response = await blockBlobClient.uploadFile(file,options)
174
+ // TODO remove this from this function - separation of concerns, let the caller do the logging
175
+ console.log(`${file} uploaded to ${containerName} container as ${blobName}`)
176
+ return true
177
+ } catch (error) {
178
+ console.error(error.message)
179
+ return false
180
+ }
181
+
182
+
183
+
173
184
  }
174
185
 
186
+
175
187
  /**
176
188
  * Downloads a blob to a local file
177
189
  *
package/src/azure.test.js CHANGED
@@ -46,7 +46,7 @@ describe('Azure Storage module', () => {
46
46
 
47
47
  })
48
48
 
49
- it.skip('should generate a signed URL for a blob', () => {
49
+ it.skip('should generate a signed URL for a blob', async () => {
50
50
  const account = "devstoreaccount1";
51
51
  const accountKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
52
52
  let containerName = 'node-helpers-testing'
@@ -56,7 +56,7 @@ describe('Azure Storage module', () => {
56
56
  const options = {
57
57
  tokenExpiry: 42
58
58
  }
59
- const signedUrl = azure.generateBlobSignedUrl(containerName,blobName,options)
59
+ const signedUrl = await azure.generateBlobSignedUrl(containerName,blobName,options)
60
60
  let url = new URL(signedUrl)
61
61
  const sasTokenParams = url.searchParams;
62
62