@beauraines/node-helpers 2.8.0 → 2.10.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,18 +2,20 @@
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.8.0](https://github.com/beauraines/node-helpers/compare/v2.7.1...v2.8.0) (2023-07-01)
5
+ ## [2.10.0](https://github.com/beauraines/node-helpers/compare/v2.7.1...v2.10.0) (2023-07-01)
6
6
 
7
7
 
8
8
  ### Features
9
9
 
10
10
  * adds download blob from azure helper ([08a1571](https://github.com/beauraines/node-helpers/commit/08a1571ab18b9eeeb1273e05b5abed95a7300457))
11
+ * **azure:** adds list blob command ([#42](https://github.com/beauraines/node-helpers/issues/42)) ([e60219b](https://github.com/beauraines/node-helpers/commit/e60219bf75acea70defde417a07219a1508b7c61))
11
12
  * **azure:** new getBlob function ([876716b](https://github.com/beauraines/node-helpers/commit/876716b8256b090a2eadf45c1d03b097362b2eb2))
12
13
  * **helper:** adds file write helper ([9e727e9](https://github.com/beauraines/node-helpers/commit/9e727e9ffe51525d2a73fa66ea933c1804304729))
13
14
 
14
15
 
15
16
  ### Bug Fixes
16
17
 
18
+ * **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))
17
19
  * **deps:** bump node-fetch from 2.6.11 to 2.6.12 ([786f458](https://github.com/beauraines/node-helpers/commit/786f458fbf0c03fd9462ab67db558c5646afb62e))
18
20
 
19
21
  ### [2.7.1](https://github.com/beauraines/node-helpers/compare/v2.7.0...v2.7.1) (2023-06-14)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beauraines/node-helpers",
3
- "version": "2.8.0",
3
+ "version": "2.10.0",
4
4
  "description": "Collection of node helpers",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/azure.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const azure = require('azure-storage');
2
2
  const dayjs = require('dayjs')
3
+ const fs = require('fs');
3
4
  const { streamToBuffer } = require('./helpers.js')
4
5
  const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
5
6
  const { QueueClient } = require("@azure/storage-queue");
@@ -149,6 +150,7 @@ getStorageQueueSignedURL(queueUrl,options) {
149
150
  *
150
151
  * @param {string} containerName the container to which the file will be uploaded
151
152
  * @param {string} file The path the the local file to upload to the container
153
+ * @returns {boolean} Success or failure to upload
152
154
  */
153
155
  //TODO migrate to @azure/storage-blob
154
156
  uploadBlobFromFile(containerName,file) {
@@ -161,8 +163,11 @@ getStorageQueueSignedURL(queueUrl,options) {
161
163
  blobService.createBlockBlobFromLocalFile(containerName,blobName,file,options,function(error,response) {
162
164
  if( error) {
163
165
  console.error(error.message)
166
+ return false
164
167
  } else {
168
+ // TODO remove this from this function - separation of concerns, let the caller do the logging
165
169
  console.log(`${response.name} uploaded to ${response.container} container`)
170
+ return true
166
171
  }
167
172
  });
168
173
  }
@@ -183,8 +188,10 @@ getStorageQueueSignedURL(queueUrl,options) {
183
188
  const blobClient = containerClient.getBlobClient(blobName);
184
189
 
185
190
  const downloadBlockBlobResponse = await blobClient.download();
191
+ // TODO add a success and failure test
186
192
  let writer = fs.createWriteStream(file)
187
193
  downloadBlockBlobResponse.readableStreamBody.pipe(writer)
194
+ // TODO remove this from this function - separation of concerns, let the caller do the logging
188
195
  console.log(`${blobName} downloaded to ${file}`)
189
196
 
190
197
  }
@@ -217,6 +224,29 @@ getStorageQueueSignedURL(queueUrl,options) {
217
224
 
218
225
  }
219
226
 
227
+ /**
228
+ * Lists the blobs in a specified container, returning an array of the BlobItem object
229
+ *
230
+ * @param {string} containerName
231
+ * @returns Blob[]
232
+ */
233
+ async listBlobs(containerName) {
234
+ const blobServiceClient = new BlobServiceClient(
235
+ this.host('blob',this.cloudName),
236
+ new StorageSharedKeyCredential(this.storageAccountName, this.storageAccountKey)
237
+ );
238
+ const containerClient = blobServiceClient.getContainerClient(containerName);
239
+ let i = 1;
240
+ let blobs = []
241
+ for await (const blob of containerClient.listBlobsFlat()) {
242
+ blobs.push(blob)
243
+ }
244
+
245
+ return blobs
246
+
247
+ }
248
+
249
+
220
250
  }
221
251
 
222
252
  module.exports = AzureStorage
package/src/azure.test.js CHANGED
@@ -1,5 +1,9 @@
1
+ const dayjs = require('dayjs')
2
+ const { fileExists } = require('./helpers.js')
1
3
  const AzureStorage = require('./azure.js')
2
-
4
+ const os = require('os');
5
+ const path = require('path');
6
+
3
7
  describe('Azure Storage module', () => {
4
8
 
5
9
  it('should be created with default values', () => {
@@ -42,8 +46,75 @@ describe('Azure Storage module', () => {
42
46
 
43
47
  })
44
48
 
45
- it.todo('should generate a signed URL for a blob')
46
- it.todo('should upload a blob from a file')
49
+ it.skip('should generate a signed URL for a blob', () => {
50
+ const account = "devstoreaccount1";
51
+ const accountKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
52
+ let containerName = 'node-helpers-testing'
53
+ let blobName = 'package.json'
54
+
55
+ let azure = new AzureStorage(account,accountKey,{cloudName:'Azurite'})
56
+ const options = {
57
+ tokenExpiry: 42
58
+ }
59
+ const signedUrl = azure.generateBlobSignedUrl(containerName,blobName,options)
60
+ let url = new URL(signedUrl)
61
+ const sasTokenParams = url.searchParams;
62
+
63
+ expect(signedUrl).toContain(azure.host('blob','Azurite'))
64
+ expect(dayjs(sasTokenParams.get('st')).isBefore(dayjs()))
65
+ expect(dayjs(sasTokenParams.get('se')).isAfter(dayjs()))
66
+ expect(dayjs(sasTokenParams.get('st')).add(azure.tokenExpiry).isSame(dayjs(sasTokenParams.get('se'))))
67
+ expect(sasTokenParams.get('sp')).toBe('r') // Read only by default
68
+ })
69
+
70
+ it.skip('should upload a blob from a file',async () => {
71
+ const account = "devstoreaccount1";
72
+ const accountKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
73
+ let containerName = 'node-helpers-testing'
74
+ let file = 'package.json'
75
+ let azure = new AzureStorage(account,accountKey,{cloudName:'Azurite'})
76
+ let success = await azure.uploadBlobFromFile(containerName,file)
77
+ expect(success)
78
+ })
79
+
47
80
  it.todo('should send a message to the storage queue')
48
81
 
82
+ it.skip('should get a blob from azure storage', async () =>{
83
+ const account = "devstoreaccount1";
84
+ const accountKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
85
+ let containerName = 'node-helpers-testing'
86
+ let blobName = 'package.json'
87
+ let azure = new AzureStorage(account,accountKey,{cloudName:'Azurite'})
88
+ let file = await azure.getBlob(containerName,blobName)
89
+ file = JSON.parse(file)
90
+ expect(file.name).toBe("@beauraines/node-helpers")
91
+
92
+ })
93
+
94
+ it.skip('should download a blob to a file', async () => {
95
+ const account = "devstoreaccount1";
96
+ const accountKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
97
+ let containerName = 'node-helpers-testing'
98
+ let blobName = 'package.json'
99
+ let file = path.join(os.tmpdir(),`package.${dayjs().unix()}.json`)
100
+ let azure = new AzureStorage(account,accountKey,{cloudName:'Azurite'})
101
+ await azure.downloadBlobToFile(containerName,blobName,file)
102
+ expect(fileExists(file))
103
+ })
104
+
105
+
106
+
107
+
108
+ it.skip('should list blobs from azure storage', async () => {
109
+ const account = "devstoreaccount1";
110
+ const accountKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
111
+ let containerName = 'node-helpers-testing'
112
+ let blobName = 'package.json'
113
+ let azure = new AzureStorage(account,accountKey,{cloudName:'Azurite'})
114
+ let blobs = await azure.listBlobs(containerName)
115
+ expect(Array.isArray(blobs));
116
+ expect(blobs.length).toBeGreaterThan(0)
117
+ expect(blobs.filter(b => b.name == blobName).length).toBe(1)
118
+ })
119
+
49
120
  })