@pi-r/azure 0.7.3 → 0.8.1

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
@@ -1,12 +1,23 @@
1
1
  "use strict";
2
- exports.CLOUD_DOWNLOAD_CHUNK = exports.CLOUD_UPLOAD_CHUNK = exports.CLOUD_UPLOAD_STREAM = exports.executeBatchQuery = exports.executeQuery = exports.deleteObjectsV2 = exports.deleteObjects = exports.setBucketWebsite = exports.createBucketV2 = exports.createBucket = exports.createDatabaseClient = exports.createStorageClient = exports.validateDatabase = exports.validateStorage = void 0;
2
+ exports.CLOUD_DOWNLOAD_CHUNK = exports.CLOUD_UPLOAD_CHUNK = exports.CLOUD_UPLOAD_STREAM = void 0;
3
+ exports.validateStorage = validateStorage;
4
+ exports.validateDatabase = validateDatabase;
5
+ exports.createStorageClient = createStorageClient;
6
+ exports.createDatabaseClient = createDatabaseClient;
7
+ exports.createBucket = createBucket;
8
+ exports.createBucketV2 = createBucketV2;
9
+ exports.setBucketWebsite = setBucketWebsite;
10
+ exports.deleteObjects = deleteObjects;
11
+ exports.deleteObjectsV2 = deleteObjectsV2;
12
+ exports.executeQuery = executeQuery;
13
+ exports.executeBatchQuery = executeBatchQuery;
3
14
  const path = require("path");
4
15
  const storage = require("@azure/storage-blob");
5
16
  const cosmos = require("@azure/cosmos");
6
17
  const identity = require("@azure/identity");
7
- const types_1 = require("@e-mc/types");
8
- const util_1 = require("@e-mc/cloud/util");
9
18
  const Cloud = require("@e-mc/cloud");
19
+ const util_1 = require("@e-mc/cloud/util");
20
+ const types_1 = require("@e-mc/types");
10
21
  function validateStorage(credential) {
11
22
  const env = process.env;
12
23
  if (credential.accountName && credential.accountKey || credential.connectionString || credential.sharedAccessSignature || env.AZURE_TENANT_ID && env.AZURE_CLIENT_ID && env.AZURE_CLIENT_SECRET) {
@@ -14,11 +25,9 @@ function validateStorage(credential) {
14
25
  }
15
26
  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));
16
27
  }
17
- exports.validateStorage = validateStorage;
18
28
  function validateDatabase(credential, data) {
19
29
  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)));
20
30
  }
21
- exports.validateDatabase = validateDatabase;
22
31
  function createStorageClient(credential) {
23
32
  let { accountName, accountKey, connectionString, sharedAccessSignature } = credential;
24
33
  if (connectionString) {
@@ -41,7 +50,6 @@ function createStorageClient(credential) {
41
50
  }
42
51
  return new storage.BlobServiceClient(url, accountName && accountKey ? new storage.StorageSharedKeyCredential(accountName, accountKey) : new identity.DefaultAzureCredential());
43
52
  }
44
- exports.createStorageClient = createStorageClient;
45
53
  function createDatabaseClient(credential) {
46
54
  if (credential.username && credential.password && credential.tenantId && credential.clientId) {
47
55
  credential.aadCredentials = new identity.UsernamePasswordCredential(credential.username, credential.password, credential.tenantId, credential.clientId);
@@ -53,11 +61,9 @@ function createDatabaseClient(credential) {
53
61
  }
54
62
  return new cosmos.CosmosClient(credential);
55
63
  }
56
- exports.createDatabaseClient = createDatabaseClient;
57
64
  async function createBucket(credential, bucket, publicRead, service = 'azure') {
58
65
  return createBucketV2.call(this, credential, bucket, publicRead ? 'blob' : 'container', undefined, service);
59
66
  }
60
- exports.createBucket = createBucket;
61
67
  async function createBucketV2(credential, bucket, access, options, service = 'azure') {
62
68
  switch (access) {
63
69
  case 'blob':
@@ -68,7 +74,11 @@ async function createBucketV2(credential, bucket, access, options, service = 'az
68
74
  break;
69
75
  }
70
76
  const containerClient = createStorageClient.call(this, credential).getContainerClient(bucket);
71
- const setAccessPolicy = async () => containerClient.setAccessPolicy(access).catch((err) => this.formatMessage(64, service, ["Unable to configure container", bucket], err, { ...Cloud.LOG_CLOUD_WARN }));
77
+ const setAccessPolicy = async () => {
78
+ return containerClient.setAccessPolicy(access).catch((err) => {
79
+ this.formatMessage(64, service, ["Unable to configure container", bucket], err, { ...Cloud.LOG_CLOUD_WARN });
80
+ });
81
+ };
72
82
  if (await containerClient.exists().catch(() => false)) {
73
83
  if (access) {
74
84
  await setAccessPolicy();
@@ -102,7 +112,6 @@ async function createBucketV2(credential, bucket, access, options, service = 'az
102
112
  return false;
103
113
  });
104
114
  }
105
- exports.createBucketV2 = createBucketV2;
106
115
  async function setBucketWebsite(credential, bucket, options, service = 'azure') {
107
116
  const staticWebsite = { enabled: true };
108
117
  const { indexPage, indexPath, errorPath } = options;
@@ -131,11 +140,9 @@ async function setBucketWebsite(credential, bucket, options, service = 'azure')
131
140
  return false;
132
141
  });
133
142
  }
134
- exports.setBucketWebsite = setBucketWebsite;
135
143
  async function deleteObjects(credential, Bucket, service) {
136
144
  return deleteObjectsV2.call(this, credential, Bucket, true, service);
137
145
  }
138
- exports.deleteObjects = deleteObjects;
139
146
  async function deleteObjectsV2(credential, bucket, recursive = true, service = 'azure') {
140
147
  const containerClient = createStorageClient.call(this, credential).getContainerClient(bucket);
141
148
  if (await containerClient.exists().catch(() => false)) {
@@ -160,11 +167,9 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
160
167
  }
161
168
  }
162
169
  }
163
- exports.deleteObjectsV2 = deleteObjectsV2;
164
170
  async function executeQuery(credential, data, sessionKey) {
165
171
  return (await executeBatchQuery.call(this, credential, [data], sessionKey))[0] || [];
166
172
  }
167
- exports.executeQuery = executeQuery;
168
173
  async function executeBatchQuery(credential, batch, sessionKey) {
169
174
  const length = batch.length;
170
175
  const result = new Array(length);
@@ -178,22 +183,16 @@ async function executeBatchQuery(credential, batch, sessionKey) {
178
183
  const closeClient = () => client?.dispose();
179
184
  for (let i = 0; i < length; ++i) {
180
185
  const item = batch[i];
181
- const { service, name, table, id, query, storedProcedureId, partitionKey = null, params, limit = 0, update, ignoreCache } = item;
186
+ const { service, name, table, id, query, storedProcedureId, partitionKey = null, params, limit = 0, ignoreCache } = item;
182
187
  if (!name || !table) {
183
188
  closeClient();
184
189
  throw (0, util_1.formatError)(item, name ? "Missing database table" : "Missing database name");
185
190
  }
186
- const getCache = (value) => {
187
- if (ignoreCache !== 1) {
188
- cacheValue.renewCache = ignoreCache === 0;
189
- return this.getQueryResult(service, credential, value, cacheValue);
190
- }
191
- };
192
191
  cacheValue.exclusiveOf = Array.isArray(ignoreCache) ? ignoreCache : undefined;
193
192
  let rows, queryString = caching && ignoreCache !== true ? name + '_' + table + '_' : '';
194
193
  if (storedProcedureId && params) {
195
194
  const { options } = item;
196
- if (queryString && (rows = getCache(queryString += (partitionKey ? Cloud.asString(partitionKey, true) : '') + '_' + storedProcedureId + '_' + Cloud.asString(params, true) + Cloud.asString(options, true)))) {
195
+ if (queryString && (rows = this.getCacheResult(service, credential, queryString += (partitionKey ? Cloud.asString(partitionKey, true) : '') + '_' + storedProcedureId + '_' + Cloud.asString(params, true) + Cloud.asString(options, true), cacheValue, ignoreCache))) {
197
196
  result[i] = rows;
198
197
  continue;
199
198
  }
@@ -203,10 +202,10 @@ async function executeBatchQuery(credential, batch, sessionKey) {
203
202
  }
204
203
  }
205
204
  else if (id) {
206
- const { options } = item;
205
+ const { update, options } = item;
207
206
  if (queryString) {
208
207
  queryString += (partitionKey ? Cloud.asString(partitionKey, true) : '') + '_' + id + '_' + Cloud.asString(options, true);
209
- if (!update && (rows = getCache(queryString))) {
208
+ if (!update && (rows = this.getCacheResult(service, credential, queryString, cacheValue, ignoreCache))) {
210
209
  result[i] = rows;
211
210
  continue;
212
211
  }
@@ -218,19 +217,25 @@ async function executeBatchQuery(credential, batch, sessionKey) {
218
217
  }
219
218
  }
220
219
  else {
221
- let { options } = item;
220
+ const { update, options = {} } = item;
222
221
  if (partitionKey) {
223
- (options ||= {}).partitionKey = partitionKey;
222
+ options.partitionKey = partitionKey;
224
223
  }
225
224
  if (limit > 0) {
226
- (options ||= {}).maxItemCount = limit;
225
+ options.maxItemCount = limit;
227
226
  }
228
- if (query || (0, types_1.isPlainObject)(options)) {
229
- if (queryString && (rows = getCache(queryString += (query ? Cloud.asString(query, true) : '') + Cloud.asString(options, true)))) {
230
- result[i] = rows;
231
- continue;
227
+ if (query || Object.keys(options).length > 0) {
228
+ if (queryString) {
229
+ queryString += (query ? Cloud.asString(query, true) : '') + Cloud.asString(options, true);
230
+ if (!update && (rows = this.getCacheResult(service, credential, queryString, cacheValue, ignoreCache))) {
231
+ result[i] = rows;
232
+ continue;
233
+ }
232
234
  }
233
235
  const items = createClient(name, table).items;
236
+ if (update) {
237
+ await items.upsert(update, { accessCondition: options.accessCondition, sessionToken: options.sessionToken, initialHeaders: options.initialHeaders, priorityLevel: options.priorityLevel });
238
+ }
234
239
  ({ resources: rows } = await (query ? items.query(query, options).fetchAll() : items.readAll(options).fetchAll()));
235
240
  }
236
241
  else {
@@ -243,7 +248,6 @@ async function executeBatchQuery(credential, batch, sessionKey) {
243
248
  closeClient();
244
249
  return result;
245
250
  }
246
- exports.executeBatchQuery = executeBatchQuery;
247
251
  exports.CLOUD_UPLOAD_STREAM = true;
248
252
  exports.CLOUD_UPLOAD_CHUNK = true;
249
253
  exports.CLOUD_DOWNLOAD_CHUNK = true;
package/download/index.js CHANGED
@@ -3,7 +3,7 @@ const types_1 = require("@e-mc/types");
3
3
  const path = require("path");
4
4
  const Cloud = require("@e-mc/cloud");
5
5
  const client_1 = require("@pi-r/azure");
6
- module.exports = function download(credential, service = 'azure') {
6
+ function download(credential, service = 'azure') {
7
7
  const blobServiceClient = client_1.createStorageClient.call(this, credential);
8
8
  return (data, callback) => {
9
9
  const { bucket: Bucket, download: target } = data;
@@ -16,7 +16,9 @@ module.exports = function download(credential, service = 'azure') {
16
16
  const downloadResponse = (err, url) => {
17
17
  callback(err, url);
18
18
  if (tempDir) {
19
- queueMicrotask(() => Cloud.removeDir(tempDir));
19
+ queueMicrotask(() => {
20
+ Cloud.removeDir(tempDir);
21
+ });
20
22
  }
21
23
  };
22
24
  const { flags = 0 } = target;
@@ -44,6 +46,9 @@ module.exports = function download(credential, service = 'azure') {
44
46
  });
45
47
  }
46
48
  })
47
- .catch((err) => downloadResponse(err));
49
+ .catch((err) => {
50
+ downloadResponse(err);
51
+ });
48
52
  };
49
- };
53
+ }
54
+ module.exports = download;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/azure",
3
- "version": "0.7.3",
3
+ "version": "0.8.1",
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.9.6",
24
- "@e-mc/module": "^0.9.6",
25
- "@e-mc/types": "^0.9.6",
23
+ "@e-mc/cloud": "^0.10.0",
24
+ "@e-mc/module": "^0.10.0",
25
+ "@e-mc/types": "^0.10.0",
26
26
  "@azure/cosmos": "^4.0.0",
27
- "@azure/identity": "^4.4.0",
27
+ "@azure/identity": "^4.4.1",
28
28
  "@azure/storage-blob": "^12.24.0"
29
29
  }
30
30
  }
package/types/index.d.ts CHANGED
@@ -11,7 +11,7 @@ export interface AzureStorageCredential {
11
11
  sharedAccessSignature?: string;
12
12
  }
13
13
 
14
- export interface AzureDatabaseQuery<T = FeedOptions | RequestOptions> extends CloudDatabase<string | SqlQuerySpec, T, PatchRequestBody, unknown[], AzureDatabaseCredential> {
14
+ export interface AzureDatabaseQuery<T = FeedOptions | RequestOptions, U = PatchRequestBody | PlainObject> extends CloudDatabase<string | SqlQuerySpec, T, U, unknown[], AzureDatabaseCredential> {
15
15
  source: "cloud";
16
16
  service: "azure" | "az";
17
17
  partitionKey?: PartitionKey;
package/upload/index.js CHANGED
@@ -2,14 +2,14 @@
2
2
  const path = require("path");
3
3
  const fs = require("fs");
4
4
  const stream = require("stream");
5
+ const Cloud = require("@e-mc/cloud");
5
6
  const util_1 = require("@e-mc/cloud/util");
6
7
  const types_1 = require("@e-mc/types");
7
- const Cloud = require("@e-mc/cloud");
8
8
  const client_1 = require("@pi-r/azure");
9
9
  const BUCKET_SESSION = new Set();
10
10
  const BUCKET_RESPONSE = {};
11
11
  const getBucketKey = (credential, bucket, acl = '') => Cloud.asString(credential, true) + bucket + '_' + acl;
12
- module.exports = function upload(credential, service = 'azure') {
12
+ function upload(credential, service = 'azure') {
13
13
  const blobServiceClient = client_1.createStorageClient.call(this, credential);
14
14
  return async (data, callback) => {
15
15
  const { bucket, localUri } = data;
@@ -26,7 +26,11 @@ module.exports = function upload(credential, service = 'azure') {
26
26
  cleanup();
27
27
  callback(err);
28
28
  };
29
- const addLog = (err) => err instanceof Error && this.addLog(this.statusType.WARN, err, service, bucket);
29
+ const addLog = (err) => {
30
+ if (err instanceof Error) {
31
+ this.addLog(this.statusType.WARN, err, service, bucket);
32
+ }
33
+ };
30
34
  const configBucket = admin.configBucket;
31
35
  if (!BUCKET_SESSION.has(bucket)) {
32
36
  const bucketAcl = admin.publicRead ? 'blob' : admin.acl;
@@ -44,7 +48,9 @@ module.exports = function upload(credential, service = 'azure') {
44
48
  }
45
49
  try {
46
50
  if (this.hasCoerce(service, 'options')) {
47
- retentionPolicy.forEach(policy => (0, types_1.coerceObject)(policy));
51
+ for (const policy of retentionPolicy) {
52
+ (0, types_1.coerceObject)(policy);
53
+ }
48
54
  }
49
55
  }
50
56
  catch {
@@ -54,7 +60,9 @@ module.exports = function upload(credential, service = 'azure') {
54
60
  const { expiresOn, startsOn, permissions } = retentionPolicy[0].accessPolicy;
55
61
  this.formatMessage(64, service, ["Bucket configured" + ' (retention policy)', bucket], expiresOn ? expiresOn.toLocaleString() + ' (expires)' : startsOn ? startsOn.toLocaleString() + ' (starts)' : permissions, { ...Cloud.LOG_CLOUD_COMMAND });
56
62
  })
57
- .catch((err) => addLog(err));
63
+ .catch((err) => {
64
+ addLog(err);
65
+ });
58
66
  }
59
67
  if (!overwrite) {
60
68
  try {
@@ -188,4 +196,5 @@ module.exports = function upload(credential, service = 'azure') {
188
196
  });
189
197
  }
190
198
  };
191
- };
199
+ }
200
+ module.exports = upload;