@pi-r/azure 0.6.3 → 0.6.6

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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2023 An Pham
1
+ Copyright 2024 An Pham
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
package/README.md CHANGED
@@ -1,7 +1,10 @@
1
- ### @pi-r/azure
1
+ # @pi-r/azure
2
2
 
3
- https://e-mc.readthedocs.io/en/latest/cloud/azure.html
3
+ ## Documentation
4
4
 
5
- ### LICENSE
5
+ - [E-mc](https://e-mc.readthedocs.io/en/latest/cloud/azure.html)
6
+ - [squared](https://squared.readthedocs.io)
7
+
8
+ ## LICENSE
6
9
 
7
10
  MIT
package/client/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
2
  exports.executeBatchQuery = exports.executeQuery = exports.deleteObjectsV2 = exports.deleteObjects = exports.setBucketWebsite = exports.createBucketV2 = exports.createBucket = exports.createDatabaseClient = exports.createStorageClient = exports.validateDatabase = exports.validateStorage = void 0;
4
3
  const path = require("path");
5
4
  const storage = require("@azure/storage-blob");
@@ -13,11 +12,11 @@ function validateStorage(credential) {
13
12
  if (credential.accountName && credential.accountKey || credential.connectionString || credential.sharedAccessSignature || env.AZURE_TENANT_ID && env.AZURE_CLIENT_ID && env.AZURE_CLIENT_SECRET) {
14
13
  return true;
15
14
  }
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));
15
+ return Cloud.enabled("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));
17
16
  }
18
17
  exports.validateStorage = validateStorage;
19
18
  function validateDatabase(credential, data) {
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)));
19
+ 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") && (credential.endpoint = process.env.AZURE_COSMOS_ENDPOINT) && (credential.key = process.env.AZURE_COSMOS_KEY)));
21
20
  }
22
21
  exports.validateDatabase = validateDatabase;
23
22
  function createStorageClient(credential) {
@@ -69,7 +68,7 @@ async function createBucketV2(credential, bucket, access, options, service = 'az
69
68
  break;
70
69
  }
71
70
  const containerClient = createStorageClient.call(this, credential).getContainerClient(bucket);
72
- const setAccessPolicy = async () => containerClient.setAccessPolicy(access).catch(err => this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Unable to configure container" /* ERR_AZURE.CONFIGURE_CONTAINER */, bucket], err, { ...Cloud.LOG_CLOUD_WARN }));
71
+ const setAccessPolicy = async () => containerClient.setAccessPolicy(access).catch(err => this.formatMessage(64, service, ["Unable to configure container", bucket], err, { ...Cloud.LOG_CLOUD_WARN }));
73
72
  if (await containerClient.exists().catch(() => false)) {
74
73
  if (access) {
75
74
  await setAccessPolicy();
@@ -85,10 +84,10 @@ async function createBucketV2(credential, bucket, access, options, service = 'az
85
84
  .then(response => {
86
85
  const { requestId, errorCode } = response;
87
86
  if (errorCode) {
88
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ['Container created with errors', bucket], (0, types_1.errorMessage)("Error code" /* ERR_MESSAGE.ERROR_CODE */, errorCode), { ...Cloud.LOG_CLOUD_WARN });
87
+ this.formatMessage(64, service, ['Container created with errors', bucket], (0, types_1.errorMessage)("Error code", errorCode), { ...Cloud.LOG_CLOUD_WARN });
89
88
  }
90
89
  else {
91
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ['Container created', bucket], requestId, { ...Cloud.LOG_CLOUD_COMMAND });
90
+ this.formatMessage(64, service, ['Container created', bucket], requestId, { ...Cloud.LOG_CLOUD_COMMAND });
92
91
  }
93
92
  return true;
94
93
  })
@@ -99,7 +98,7 @@ async function createBucketV2(credential, bucket, access, options, service = 'az
99
98
  }
100
99
  return true;
101
100
  }
102
- this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Unable to create container" /* ERR_AZURE.CREATE_CONTAINER */, bucket], err, { ...Cloud.LOG_CLOUD_FAIL });
101
+ this.formatFail(64, service, ["Unable to create container", bucket], err, { ...Cloud.LOG_CLOUD_FAIL });
103
102
  return false;
104
103
  });
105
104
  }
@@ -120,15 +119,15 @@ async function setBucketWebsite(credential, bucket, options, service = 'azure')
120
119
  .then(response => {
121
120
  const { requestId, errorCode } = response;
122
121
  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 });
122
+ this.formatMessage(64, service, ["Bucket configured" + ' with errors', bucket], (0, types_1.errorMessage)("Error code", errorCode), { ...Cloud.LOG_CLOUD_WARN });
124
123
  }
125
124
  else {
126
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket configured" /* VAL_CLOUD.CONFIGURE_BUCKET */, bucket], requestId, { ...Cloud.LOG_CLOUD_COMMAND });
125
+ this.formatMessage(64, service, ["Bucket configured", bucket], requestId, { ...Cloud.LOG_CLOUD_COMMAND });
127
126
  }
128
127
  return true;
129
128
  })
130
129
  .catch(err => {
131
- this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Unable to configure bucket" /* ERR_CLOUD.CONFIGURE_BUCKET */, bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
130
+ this.formatFail(64, service, ["Unable to configure bucket", bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
132
131
  return false;
133
132
  });
134
133
  }
@@ -151,12 +150,12 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
151
150
  }
152
151
  }
153
152
  fileCount = tasks.length;
154
- return this.allSettled(tasks, ["Unable to delete blob" /* ERR_AZURE.DELETE_BLOB */, bucket], 64 /* LOG_TYPE.CLOUD */).then(() => {
155
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ['Container emptied' + (recursive ? ' (recursive)' : ''), bucket], fileCount + ' files', { ...Cloud.LOG_CLOUD_COMMAND });
153
+ return this.allSettled(tasks, ["Unable to delete blob", bucket], 64).then(() => {
154
+ this.formatMessage(64, service, ['Container emptied' + (recursive ? ' (recursive)' : ''), bucket], fileCount + ' files', { ...Cloud.LOG_CLOUD_COMMAND });
156
155
  });
157
156
  }
158
157
  catch (err) {
159
- this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Unable to list bucket" /* ERR_CLOUD.LIST_BUCKET */, bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
158
+ this.formatFail(64, service, ["Unable to list bucket", bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
160
159
  }
161
160
  }
162
161
  }
@@ -181,7 +180,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
181
180
  const { service, name, table, id, query, storedProcedureId, partitionKey = null, params, limit = 0, update, ignoreCache } = item;
182
181
  if (!name || !table) {
183
182
  closeClient();
184
- throw (0, util_1.formatError)(item, name ? "Missing database table" /* ERR_DB.TABLE */ : "Missing database name" /* ERR_DB.NAME */);
183
+ throw (0, util_1.formatError)(item, name ? "Missing database table" : "Missing database name");
185
184
  }
186
185
  const getCache = (value) => {
187
186
  if (ignoreCache !== 1) {
@@ -198,7 +197,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
198
197
  continue;
199
198
  }
200
199
  const { statusCode, resource } = await createClient(name, table).scripts.storedProcedure(storedProcedureId).execute(partitionKey, params, options);
201
- if (statusCode === 200 /* HTTP_STATUS.OK */) {
200
+ if (statusCode === 200) {
202
201
  rows = Array.isArray(resource) ? resource : [resource];
203
202
  }
204
203
  }
@@ -213,7 +212,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
213
212
  }
214
213
  const db = createClient(name, table).item(id, partitionKey);
215
214
  const { statusCode, resource } = update ? await db.patch(update, options) : await db.read(options);
216
- if (statusCode === 200 /* HTTP_STATUS.OK */) {
215
+ if (statusCode === 200) {
217
216
  rows = [resource];
218
217
  }
219
218
  }
@@ -235,7 +234,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
235
234
  }
236
235
  else {
237
236
  closeClient();
238
- throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
237
+ throw (0, util_1.formatError)(item, "Missing database query");
239
238
  }
240
239
  }
241
240
  result[i] = this.setQueryResult(service, credential, queryString, rows, cacheValue);
package/download/index.js CHANGED
@@ -1,10 +1,9 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
2
  const types_1 = require("@e-mc/types");
4
3
  const Module = require("@e-mc/module");
5
4
  const Cloud = require("@e-mc/cloud");
6
- const client_1 = require("../client");
7
- function download(credential, service = 'azure') {
5
+ const client_1 = require("@pi-r/azure");
6
+ module.exports = function download(credential, service = 'azure') {
8
7
  const blobServiceClient = client_1.createStorageClient.call(this, credential);
9
8
  return (data, callback) => {
10
9
  const { bucket: Bucket, download: target } = data;
@@ -21,16 +20,14 @@ function download(credential, service = 'azure') {
21
20
  const deleteObject = target.deleteObject;
22
21
  if (deleteObject) {
23
22
  blobClient.delete((0, types_1.isPlainObject)(deleteObject) ? (deleteObject.abortSignal = this.signal, deleteObject) : { abortSignal: this.signal })
24
- .then(() => this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Delete success" /* VAL_CLOUD.DELETE_FILE */, location, { ...Cloud.LOG_CLOUD_DELETE }))
23
+ .then(() => this.formatMessage(64, service, "Delete success", location, { ...Cloud.LOG_CLOUD_DELETE }))
25
24
  .catch((err) => {
26
25
  if (!(err instanceof Error && err.code === 'BlobNotFound')) {
27
- this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Delete failed" /* ERR_CLOUD.DELETE_FAIL */, location], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: !!target.active });
26
+ this.formatFail(64, service, ["Delete failed", location], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: !!target.active });
28
27
  }
29
28
  });
30
29
  }
31
30
  })
32
31
  .catch(err => callback(err));
33
32
  };
34
- }
35
-
36
- module.exports = download;
33
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/azure",
3
- "version": "0.6.3",
3
+ "version": "0.6.6",
4
4
  "description": "Azure cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
@@ -20,9 +20,9 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
22
  "dependencies": {
23
- "@e-mc/cloud": "^0.8.3",
24
- "@e-mc/module": "^0.8.3",
25
- "@e-mc/types": "^0.8.3",
23
+ "@e-mc/cloud": "^0.8.7",
24
+ "@e-mc/module": "^0.8.7",
25
+ "@e-mc/types": "^0.8.7",
26
26
  "@azure/cosmos": "^4.0.0",
27
27
  "@azure/identity": "^4.0.1",
28
28
  "@azure/storage-blob": "^12.17.0"
package/upload/index.js CHANGED
@@ -1,15 +1,14 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
2
  const path = require("path");
4
3
  const util_1 = require("@e-mc/cloud/util");
5
4
  const types_1 = require("@e-mc/types");
6
5
  const Module = require("@e-mc/module");
7
6
  const Cloud = require("@e-mc/cloud");
8
- const client_1 = require("../client");
7
+ const client_1 = require("@pi-r/azure");
9
8
  const BUCKET_SESSION = new Set();
10
9
  const BUCKET_RESPONSE = {};
11
10
  const getBucketKey = (credential, bucket, acl = '') => Module.asString(credential, true) + bucket + '_' + acl;
12
- function upload(credential, service = 'azure') {
11
+ module.exports = function upload(credential, service = 'azure') {
13
12
  const blobServiceClient = client_1.createStorageClient.call(this, credential);
14
13
  return async (data, callback) => {
15
14
  var _a, _b;
@@ -46,7 +45,7 @@ function upload(credential, service = 'azure') {
46
45
  containerClient.setAccessPolicy('container', retentionPolicy)
47
46
  .then(() => {
48
47
  const { expiresOn, startsOn, permissions } = retentionPolicy[0].accessPolicy;
49
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket configured" /* VAL_CLOUD.CONFIGURE_BUCKET */ + ' (retention policy)', bucket], expiresOn ? expiresOn.toLocaleString() + ' (expires)' : startsOn ? startsOn.toLocaleString() + ' (starts)' : permissions, { ...Cloud.LOG_CLOUD_COMMAND });
48
+ this.formatMessage(64, service, ["Bucket configured" + ' (retention policy)', bucket], expiresOn ? expiresOn.toLocaleString() + ' (expires)' : startsOn ? startsOn.toLocaleString() + ' (starts)' : permissions, { ...Cloud.LOG_CLOUD_COMMAND });
50
49
  })
51
50
  .catch(err => addLog(err));
52
51
  }
@@ -71,7 +70,7 @@ function upload(credential, service = 'azure') {
71
70
  }
72
71
  } while (exists && ++i);
73
72
  if (i > 0) {
74
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["File renamed" /* VAL_CLOUD.RENAME_FILE */, current], filename, { ...Cloud.LOG_CLOUD_WARN });
73
+ this.formatMessage(64, service, ["File renamed", current], filename, { ...Cloud.LOG_CLOUD_WARN });
75
74
  }
76
75
  }
77
76
  catch (err) {
@@ -111,7 +110,7 @@ function upload(credential, service = 'azure') {
111
110
  .then(result => {
112
111
  let requestUrl = result._response.request.url;
113
112
  const url = endpoint ? Module.joinPath(endpoint, blobName) : Module.isFile(requestUrl = decodeURIComponent(requestUrl), 'http/s') ? requestUrl : Module.joinPath(`https://${credential.accountName}.blob.core.windows.net`, bucket, blobName);
114
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Upload success" /* VAL_CLOUD.UPLOAD_FILE */, url, { ...Cloud.LOG_CLOUD_UPLOAD });
113
+ this.formatMessage(64, service, "Upload success", url, { ...Cloud.LOG_CLOUD_UPLOAD });
115
114
  if (first) {
116
115
  cleanup();
117
116
  callback(null, url);
@@ -127,6 +126,4 @@ function upload(credential, service = 'azure') {
127
126
  });
128
127
  }
129
128
  };
130
- }
131
-
132
- module.exports = upload;
129
+ };