@pi-r/azure 0.6.1 → 0.6.3

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  ### @pi-r/azure
2
2
 
3
+ https://e-mc.readthedocs.io/en/latest/cloud/azure.html
4
+
3
5
  ### LICENSE
4
6
 
5
7
  MIT
package/client/index.js CHANGED
@@ -7,42 +7,51 @@ const cosmos = require("@azure/cosmos");
7
7
  const identity = require("@azure/identity");
8
8
  const types_1 = require("@e-mc/types");
9
9
  const util_1 = require("@e-mc/cloud/util");
10
- const Module = require("@e-mc/module");
11
10
  const Cloud = require("@e-mc/cloud");
12
11
  function validateStorage(credential) {
13
12
  const env = process.env;
14
- if (credential.accountName && credential.accountKey || credential.connectionString || credential.sharedAccessSignature || env.AZURE_TENANT_ID && env.AZURE_CLIENT_ID && env.AZURE_CLIENT_SECRET && (credential.accountName || env.AZURE_STORAGE_ACCOUNT)) {
13
+ if (credential.accountName && credential.accountKey || credential.connectionString || credential.sharedAccessSignature || env.AZURE_TENANT_ID && env.AZURE_CLIENT_ID && env.AZURE_CLIENT_SECRET) {
15
14
  return true;
16
15
  }
17
- return Module.enabled("process.env.apply" /* KEY_NAME.PROCESS_ENV_APPLY */) && !!((credential.connectionString = env.AZURE_STORAGE_CONNECTION_STRING) || (credential.sharedAccessSignature = env.AZURE_STORAGE_SAS_TOKEN) || (credential.accountName = env.AZURE_STORAGE_ACCOUNT) && (credential.accountKey = env.AZURE_STORAGE_KEY));
16
+ return Cloud.enabled("process.env.apply" /* KEY_NAME.PROCESS_ENV_APPLY */) && !!((credential.connectionString = env.AZURE_STORAGE_CONNECTION_STRING) || (credential.sharedAccessSignature = env.AZURE_STORAGE_SAS_TOKEN) || (credential.accountName = env.AZURE_STORAGE_ACCOUNT) && (credential.accountKey = env.AZURE_STORAGE_KEY));
18
17
  }
19
18
  exports.validateStorage = validateStorage;
20
19
  function validateDatabase(credential, data) {
21
- return !!(data.name && data.table && (credential.endpoint && credential.key || Module.enabled("process.env.apply" /* KEY_NAME.PROCESS_ENV_APPLY */) && (credential.endpoint = process.env.AZURE_COSMOS_ENDPOINT) && (credential.key = process.env.AZURE_COSMOS_KEY)));
20
+ return !!(data.name && data.table && (credential.endpoint && credential.key || credential.username && credential.password && (credential.tenantId && credential.clientId || (credential.tenantId = process.env.AZURE_TENANT_ID) && (credential.clientId = process.env.AZURE_CLIENT_ID)) || Cloud.enabled("process.env.apply" /* KEY_NAME.PROCESS_ENV_APPLY */) && (credential.endpoint = process.env.AZURE_COSMOS_ENDPOINT) && (credential.key = process.env.AZURE_COSMOS_KEY)));
22
21
  }
23
22
  exports.validateDatabase = validateDatabase;
24
23
  function createStorageClient(credential) {
25
- const { BlobServiceClient, StorageSharedKeyCredential } = storage;
26
- const { accountName, accountKey, connectionString, sharedAccessSignature } = credential;
24
+ let { accountName, accountKey, connectionString, sharedAccessSignature } = credential;
27
25
  if (connectionString) {
28
- credential.accountName || (credential.accountName = /AccountName=([^;]+);/.exec(connectionString)?.[1]);
29
- return BlobServiceClient.fromConnectionString(connectionString);
26
+ if (!accountName) {
27
+ credential.accountName = /AccountName=([^;]+);/.exec(connectionString)?.[1];
28
+ }
29
+ return storage.BlobServiceClient.fromConnectionString(connectionString);
30
30
  }
31
+ const url = `https://${accountName || process.env.AZURE_STORAGE_ACCOUNT}.blob.core.windows.net`;
31
32
  if (sharedAccessSignature) {
32
- credential.accountName || (credential.accountName = /^https:\/\/([a-z\d]+)\./.exec(sharedAccessSignature)?.[1]);
33
- return new BlobServiceClient(sharedAccessSignature);
34
- }
35
- let azureCredential;
36
- if (accountName && accountKey) {
37
- azureCredential = new StorageSharedKeyCredential(accountName, accountKey);
38
- }
39
- else {
40
- azureCredential = new identity.DefaultAzureCredential();
33
+ if (accountName) {
34
+ if (!Cloud.isURL(sharedAccessSignature)) {
35
+ sharedAccessSignature = url + (!sharedAccessSignature.startsWith('?') ? '?' : '') + sharedAccessSignature;
36
+ }
37
+ }
38
+ else {
39
+ credential.accountName || (credential.accountName = /^https:\/\/([a-z\d]+)\./.exec(sharedAccessSignature)?.[1]);
40
+ }
41
+ return new storage.BlobServiceClient(sharedAccessSignature);
41
42
  }
42
- return new BlobServiceClient(`https://${accountName || process.env.AZURE_STORAGE_ACCOUNT}.blob.core.windows.net`, azureCredential);
43
+ return new storage.BlobServiceClient(url, accountName && accountKey ? new storage.StorageSharedKeyCredential(accountName, accountKey) : new identity.DefaultAzureCredential());
43
44
  }
44
45
  exports.createStorageClient = createStorageClient;
45
46
  function createDatabaseClient(credential) {
47
+ if (credential.username && credential.password && credential.tenantId && credential.clientId) {
48
+ credential.aadCredentials = new identity.UsernamePasswordCredential(credential.username, credential.password, credential.tenantId, credential.clientId);
49
+ credential = { ...credential };
50
+ delete credential.username;
51
+ delete credential.password;
52
+ delete credential.tenantId;
53
+ delete credential.clientId;
54
+ }
46
55
  return new cosmos.CosmosClient(credential);
47
56
  }
48
57
  exports.createDatabaseClient = createDatabaseClient;
@@ -102,14 +111,20 @@ async function setBucketWebsite(credential, bucket, options, service = 'azure')
102
111
  staticWebsite.indexDocument = path.basename(indexPage);
103
112
  }
104
113
  if ((0, types_1.isString)(indexPath)) {
105
- staticWebsite.defaultIndexDocumentPath = Module.joinPath(bucket, indexPath);
114
+ staticWebsite.defaultIndexDocumentPath = Cloud.joinPath(bucket, indexPath);
106
115
  }
107
116
  if ((0, types_1.isString)(errorPath)) {
108
- staticWebsite.errorDocument404Path = Module.joinPath(bucket, errorPath);
117
+ staticWebsite.errorDocument404Path = Cloud.joinPath(bucket, errorPath);
109
118
  }
110
119
  return createStorageClient.call(this, credential).setProperties({ staticWebsite })
111
- .then(() => {
112
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket configured" /* VAL_CLOUD.CONFIGURE_BUCKET */, bucket], options, { ...Cloud.LOG_CLOUD_COMMAND });
120
+ .then(response => {
121
+ const { requestId, errorCode } = response;
122
+ if (errorCode) {
123
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket configured" /* VAL_CLOUD.CONFIGURE_BUCKET */ + ' with errors', bucket], (0, types_1.errorMessage)("Error code" /* ERR_MESSAGE.ERROR_CODE */, errorCode), { ...Cloud.LOG_CLOUD_WARN });
124
+ }
125
+ else {
126
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket configured" /* VAL_CLOUD.CONFIGURE_BUCKET */, bucket], requestId, { ...Cloud.LOG_CLOUD_COMMAND });
127
+ }
113
128
  return true;
114
129
  })
115
130
  .catch(err => {
@@ -129,15 +144,15 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
129
144
  const tasks = [];
130
145
  let fileCount = 0;
131
146
  for await (const blob of containerClient.listBlobsFlat({ includeUncommitedBlobs: true })) {
132
- if (!recursive && blob.name.includes('/')) {
133
- continue;
147
+ if (recursive || !blob.name.includes('/')) {
148
+ tasks.push(containerClient.deleteBlob(blob.name, { versionId: blob.versionId }).catch(() => {
149
+ --fileCount;
150
+ }));
134
151
  }
135
- tasks.push(containerClient.deleteBlob(blob.name, { versionId: blob.versionId }).catch(() => { --fileCount; }));
136
152
  }
137
153
  fileCount = tasks.length;
138
154
  return this.allSettled(tasks, ["Unable to delete blob" /* ERR_AZURE.DELETE_BLOB */, bucket], 64 /* LOG_TYPE.CLOUD */).then(() => {
139
- const files = fileCount + ' files';
140
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, [`Container emptied (${recursive ? 'recursive' : files})`, bucket], recursive ? files : '', { ...Cloud.LOG_CLOUD_COMMAND });
155
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ['Container emptied' + (recursive ? ' (recursive)' : ''), bucket], fileCount + ' files', { ...Cloud.LOG_CLOUD_COMMAND });
141
156
  });
142
157
  }
143
158
  catch (err) {
@@ -178,7 +193,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
178
193
  let rows, queryString = caching && ignoreCache !== true ? name + '_' + table + '_' : '';
179
194
  if (storedProcedureId && params) {
180
195
  const { options } = item;
181
- if (queryString && (rows = getCache(queryString += Module.asString(partitionKey, true) + '_' + storedProcedureId + Module.asString(params, true) + Module.asString(options, true)))) {
196
+ if (queryString && (rows = getCache(queryString += Cloud.asString(partitionKey, true) + '_' + storedProcedureId + Cloud.asString(params, true) + Cloud.asString(options, true)))) {
182
197
  result[i] = rows;
183
198
  continue;
184
199
  }
@@ -190,7 +205,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
190
205
  else if (id) {
191
206
  const { options } = item;
192
207
  if (queryString) {
193
- queryString += Module.asString(partitionKey, true) + '_' + id + Module.asString(options, true);
208
+ queryString += Cloud.asString(partitionKey, true) + '_' + id + Cloud.asString(options, true);
194
209
  if (!update && (rows = getCache(queryString))) {
195
210
  result[i] = rows;
196
211
  continue;
@@ -211,7 +226,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
211
226
  (options || (options = {})).maxItemCount = limit;
212
227
  }
213
228
  if (query || (0, types_1.isPlainObject)(options)) {
214
- if (queryString && (rows = getCache(queryString += (query ? Module.asString(query, true) : '') + Module.asString(options, true)))) {
229
+ if (queryString && (rows = getCache(queryString += (query ? Cloud.asString(query, true) : '') + Cloud.asString(options, true)))) {
215
230
  result[i] = rows;
216
231
  continue;
217
232
  }
package/download/index.js CHANGED
@@ -20,7 +20,7 @@ function download(credential, service = 'azure') {
20
20
  callback(null, value);
21
21
  const deleteObject = target.deleteObject;
22
22
  if (deleteObject) {
23
- blobClient.delete((0, types_1.isPlainObject)(deleteObject) && (deleteObject.abortSignal = this.signal) ? deleteObject : undefined)
23
+ blobClient.delete((0, types_1.isPlainObject)(deleteObject) ? (deleteObject.abortSignal = this.signal, deleteObject) : { abortSignal: this.signal })
24
24
  .then(() => this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Delete success" /* VAL_CLOUD.DELETE_FILE */, location, { ...Cloud.LOG_CLOUD_DELETE }))
25
25
  .catch((err) => {
26
26
  if (!(err instanceof Error && err.code === 'BlobNotFound')) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/azure",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "description": "Azure cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/anpham6/pi-r.git",
11
+ "url": "git+https://github.com/anpham6/pi-r.git",
12
12
  "directory": "src/cloud/azure"
13
13
  },
14
14
  "keywords": [
@@ -20,11 +20,11 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
22
  "dependencies": {
23
- "@e-mc/cloud": "^0.8.1",
24
- "@e-mc/module": "^0.8.1",
25
- "@e-mc/types": "^0.8.1",
23
+ "@e-mc/cloud": "^0.8.3",
24
+ "@e-mc/module": "^0.8.3",
25
+ "@e-mc/types": "^0.8.3",
26
26
  "@azure/cosmos": "^4.0.0",
27
- "@azure/identity": "^4.0.0",
27
+ "@azure/identity": "^4.0.1",
28
28
  "@azure/storage-blob": "^12.17.0"
29
29
  }
30
30
  }