@pi-r/azure 0.3.0 → 0.6.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/client/index.js CHANGED
@@ -60,7 +60,7 @@ async function createBucketV2(credential, bucket, access, options, service = 'az
60
60
  break;
61
61
  }
62
62
  const containerClient = createStorageClient.call(this, credential).getContainerClient(bucket);
63
- const setAccessPolicy = () => 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 }));
63
+ 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 }));
64
64
  if (await containerClient.exists().catch(() => false)) {
65
65
  if (access) {
66
66
  await setAccessPolicy();
@@ -95,7 +95,7 @@ async function createBucketV2(credential, bucket, access, options, service = 'az
95
95
  });
96
96
  }
97
97
  exports.createBucketV2 = createBucketV2;
98
- function setBucketWebsite(credential, bucket, options, service = 'azure') {
98
+ async function setBucketWebsite(credential, bucket, options, service = 'azure') {
99
99
  const staticWebsite = { enabled: true };
100
100
  const { indexPage, indexPath, errorPath } = options;
101
101
  if ((0, types_1.isString)(indexPage)) {
@@ -118,7 +118,7 @@ function setBucketWebsite(credential, bucket, options, service = 'azure') {
118
118
  });
119
119
  }
120
120
  exports.setBucketWebsite = setBucketWebsite;
121
- function deleteObjects(credential, Bucket, service) {
121
+ async function deleteObjects(credential, Bucket, service) {
122
122
  return deleteObjectsV2.call(this, credential, Bucket, true, service);
123
123
  }
124
124
  exports.deleteObjects = deleteObjects;
@@ -129,7 +129,7 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
129
129
  const tasks = [];
130
130
  let fileCount = 0;
131
131
  for await (const blob of containerClient.listBlobsFlat({ includeUncommitedBlobs: true })) {
132
- if (!recursive && blob.name.indexOf('/') !== -1) {
132
+ if (!recursive && blob.name.includes('/')) {
133
133
  continue;
134
134
  }
135
135
  tasks.push(containerClient.deleteBlob(blob.name, { versionId: blob.versionId }).catch(() => { --fileCount; }));
@@ -163,23 +163,22 @@ async function executeBatchQuery(credential, batch, sessionKey) {
163
163
  const closeClient = () => client?.dispose();
164
164
  for (let i = 0; i < length; ++i) {
165
165
  const item = batch[i];
166
- const { service, name, table, id, query, storedProcedureId, partitionKey, params, limit = 0, update, ignoreCache } = item;
166
+ const { service, name, table, id, query, storedProcedureId, partitionKey = null, params, limit = 0, update, ignoreCache } = item;
167
167
  if (!name || !table) {
168
168
  closeClient();
169
169
  throw (0, util_1.formatError)(item, name ? "Missing database table" /* ERR_DB.TABLE */ : "Missing database name" /* ERR_DB.NAME */);
170
170
  }
171
171
  const renewCache = ignoreCache === 0;
172
172
  const getCache = (value) => {
173
- if (ignoreCache === 1) {
174
- return;
173
+ if (ignoreCache !== 1) {
174
+ cacheValue.renewCache = renewCache;
175
+ return this.getQueryResult(service, credential, value, cacheValue);
175
176
  }
176
- cacheValue.renewCache = renewCache;
177
- return this.getQueryResult(service, credential, value, cacheValue);
178
177
  };
179
178
  let rows, queryString = caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache ? name + '_' + table + '_' : '';
180
179
  if (storedProcedureId && params) {
181
180
  const { options } = item;
182
- if (queryString && (rows = getCache(queryString += (partitionKey || '') + '_' + storedProcedureId + Module.asString(params, true) + Module.asString(options, true)))) {
181
+ if (queryString && (rows = getCache(queryString += Module.asString(partitionKey, true) + '_' + storedProcedureId + Module.asString(params, true) + Module.asString(options, true)))) {
183
182
  result[i] = rows;
184
183
  continue;
185
184
  }
@@ -191,7 +190,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
191
190
  else if (id) {
192
191
  const { options } = item;
193
192
  if (queryString) {
194
- queryString += (partitionKey || '') + '_' + id + Module.asString(options, true);
193
+ queryString += Module.asString(partitionKey, true) + '_' + id + Module.asString(options, true);
195
194
  if (!update && (rows = getCache(queryString))) {
196
195
  result[i] = rows;
197
196
  continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/azure",
3
- "version": "0.3.0",
3
+ "version": "0.6.0",
4
4
  "description": "Azure cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
@@ -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.6.0",
24
- "@e-mc/module": "^0.6.0",
25
- "@e-mc/types": "^0.6.0",
26
- "@azure/cosmos": "^3.17.3",
27
- "@azure/identity": "^3.2.4",
28
- "@azure/storage-blob": "^12.15.0"
23
+ "@e-mc/cloud": "^0.8.0",
24
+ "@e-mc/module": "^0.8.0",
25
+ "@e-mc/types": "^0.8.0",
26
+ "@azure/cosmos": "^4.0.0",
27
+ "@azure/identity": "^4.0.0",
28
+ "@azure/storage-blob": "^12.17.0"
29
29
  }
30
30
  }
package/upload/index.js CHANGED
@@ -15,7 +15,7 @@ function upload(credential, service = 'azure') {
15
15
  return async (data, callback) => {
16
16
  var _a, _b;
17
17
  const { bucket, localUri } = data;
18
- const { pathname = '', fileGroup, contentType, metadata, endpoint, admin = {}, overwrite, options } = data.upload;
18
+ const { pathname = '', fileGroup, contentType, metadata, tags, endpoint, admin = {}, overwrite, options } = data.upload;
19
19
  const containerClient = blobServiceClient.getContainerClient(bucket);
20
20
  let filename = data.upload.filename || path.basename(localUri), bucketKey;
21
21
  const cleanup = () => {
@@ -28,15 +28,29 @@ function upload(credential, service = 'azure') {
28
28
  cleanup();
29
29
  callback(err);
30
30
  };
31
+ const addLog = (err) => err instanceof Error && this.addLog(this.statusType.WARN, err, service, bucket);
32
+ const configBucket = admin.configBucket;
31
33
  if (!BUCKET_SESSION.has(bucket)) {
32
34
  const bucketAcl = admin.publicRead ? 'blob' : admin.acl;
33
- const response = BUCKET_RESPONSE[_a = bucketKey = getBucketKey(credential, bucket, bucketAcl)] || (BUCKET_RESPONSE[_a] = client_1.createBucketV2.call(this, credential, bucket, bucketAcl, admin.configBucket?.create));
35
+ const response = BUCKET_RESPONSE[_a = bucketKey = getBucketKey(credential, bucket, bucketAcl)] || (BUCKET_RESPONSE[_a] = client_1.createBucketV2.call(this, credential, bucket, bucketAcl, configBucket?.create));
34
36
  if (!await response) {
35
37
  errorResponse(null);
36
38
  return;
37
39
  }
38
40
  BUCKET_SESSION.add(bucket);
39
41
  }
42
+ let retentionPolicy = configBucket?.retentionPolicy;
43
+ if (retentionPolicy) {
44
+ if (!Array.isArray(retentionPolicy)) {
45
+ retentionPolicy = [retentionPolicy];
46
+ }
47
+ containerClient.setAccessPolicy('container', retentionPolicy)
48
+ .then(() => {
49
+ const { expiresOn, startsOn, permissions } = retentionPolicy[0].accessPolicy;
50
+ 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 });
51
+ })
52
+ .catch(err => addLog(err));
53
+ }
40
54
  if (!overwrite) {
41
55
  try {
42
56
  const current = filename;
@@ -69,7 +83,6 @@ function upload(credential, service = 'azure') {
69
83
  const Key = [filename];
70
84
  const Body = [data.buffer];
71
85
  const ContentType = [contentType];
72
- const addLog = (err) => err instanceof Error && this.addLog(this.statusType.WARN, err.message, service + ': ' + bucket);
73
86
  if (fileGroup) {
74
87
  for (const [content, ext, localFile] of fileGroup) {
75
88
  try {
@@ -91,8 +104,13 @@ function upload(credential, service = 'azure') {
91
104
  }
92
105
  const blobName = Module.joinPath(pathname, Key[i]);
93
106
  const params = { ...options, abortSignal: this.signal };
94
- if (first && metadata) {
95
- params.metadata = metadata;
107
+ if (first) {
108
+ if ((0, types_1.isPlainObject)(metadata)) {
109
+ params.metadata = metadata;
110
+ }
111
+ if ((0, types_1.isPlainObject)(tags)) {
112
+ params.tags = tags;
113
+ }
96
114
  }
97
115
  (_b = (params.blobHTTPHeaders || (params.blobHTTPHeaders = {}))).blobContentType || (_b.blobContentType = ContentType[i]);
98
116
  containerClient.getBlockBlobClient(blobName).upload(Body[i], Body[i].byteLength, params)