@pi-r/azure 0.5.0 → 0.5.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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2023 An Pham
1
+ Copyright 2023 Haruhi Suzumiya
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,5 +1,7 @@
1
- ### @pi-r/azure
1
+ # @pi-r/azure
2
2
 
3
- ### LICENSE
3
+ PEP 402 - Forever
4
+
5
+ ## LICENSE
4
6
 
5
7
  MIT
package/client/index.js CHANGED
@@ -14,11 +14,11 @@ function validateStorage(credential) {
14
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)) {
15
15
  return true;
16
16
  }
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));
17
+ return Module.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));
18
18
  }
19
19
  exports.validateStorage = validateStorage;
20
20
  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)));
21
+ return !!(data.name && data.table && (credential.endpoint && credential.key || Module.enabled("process.env.apply") && (credential.endpoint = process.env.AZURE_COSMOS_ENDPOINT) && (credential.key = process.env.AZURE_COSMOS_KEY)));
22
22
  }
23
23
  exports.validateDatabase = validateDatabase;
24
24
  function createStorageClient(credential) {
@@ -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 = () => containerClient.setAccessPolicy(access).catch(err => this.formatMessage(64, service, ["Unable to configure container", bucket], err, { ...Cloud.LOG_CLOUD_WARN }));
64
64
  if (await containerClient.exists().catch(() => false)) {
65
65
  if (access) {
66
66
  await setAccessPolicy();
@@ -76,10 +76,10 @@ async function createBucketV2(credential, bucket, access, options, service = 'az
76
76
  .then(response => {
77
77
  const { requestId, errorCode } = response;
78
78
  if (errorCode) {
79
- 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 });
79
+ this.formatMessage(64, service, ['Container created with errors', bucket], (0, types_1.errorMessage)("Error code", errorCode), { ...Cloud.LOG_CLOUD_WARN });
80
80
  }
81
81
  else {
82
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ['Container created', bucket], requestId, { ...Cloud.LOG_CLOUD_COMMAND });
82
+ this.formatMessage(64, service, ['Container created', bucket], requestId, { ...Cloud.LOG_CLOUD_COMMAND });
83
83
  }
84
84
  return true;
85
85
  })
@@ -90,7 +90,7 @@ async function createBucketV2(credential, bucket, access, options, service = 'az
90
90
  }
91
91
  return true;
92
92
  }
93
- this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Unable to create container" /* ERR_AZURE.CREATE_CONTAINER */, bucket], err, { ...Cloud.LOG_CLOUD_FAIL });
93
+ this.formatFail(64, service, ["Unable to create container", bucket], err, { ...Cloud.LOG_CLOUD_FAIL });
94
94
  return false;
95
95
  });
96
96
  }
@@ -109,11 +109,11 @@ function setBucketWebsite(credential, bucket, options, service = 'azure') {
109
109
  }
110
110
  return createStorageClient.call(this, credential).setProperties({ staticWebsite })
111
111
  .then(() => {
112
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket configured" /* VAL_CLOUD.CONFIGURE_BUCKET */, bucket], options, { ...Cloud.LOG_CLOUD_COMMAND });
112
+ this.formatMessage(64, service, ["Bucket configured", bucket], options, { ...Cloud.LOG_CLOUD_COMMAND });
113
113
  return true;
114
114
  })
115
115
  .catch(err => {
116
- this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Unable to configure bucket" /* ERR_CLOUD.CONFIGURE_BUCKET */, bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
116
+ this.formatFail(64, service, ["Unable to configure bucket", bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
117
117
  return false;
118
118
  });
119
119
  }
@@ -135,13 +135,13 @@ async function deleteObjectsV2(credential, bucket, recursive = true, service = '
135
135
  tasks.push(containerClient.deleteBlob(blob.name, { versionId: blob.versionId }).catch(() => { --fileCount; }));
136
136
  }
137
137
  fileCount = tasks.length;
138
- return this.allSettled(tasks, ["Unable to delete blob" /* ERR_AZURE.DELETE_BLOB */, bucket], 64 /* LOG_TYPE.CLOUD */).then(() => {
138
+ return this.allSettled(tasks, ["Unable to delete blob", bucket], 64).then(() => {
139
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 });
140
+ this.formatMessage(64, service, [`Container emptied (${recursive ? 'recursive' : files})`, bucket], recursive ? files : '', { ...Cloud.LOG_CLOUD_COMMAND });
141
141
  });
142
142
  }
143
143
  catch (err) {
144
- this.formatFail(64 /* LOG_TYPE.CLOUD */, service, ["Unable to list bucket" /* ERR_CLOUD.LIST_BUCKET */, bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
144
+ this.formatFail(64, service, ["Unable to list bucket", bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
145
145
  }
146
146
  }
147
147
  }
@@ -166,7 +166,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
166
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
- throw (0, util_1.formatError)(item, name ? "Missing database table" /* ERR_DB.TABLE */ : "Missing database name" /* ERR_DB.NAME */);
169
+ throw (0, util_1.formatError)(item, name ? "Missing database table" : "Missing database name");
170
170
  }
171
171
  const renewCache = ignoreCache === 0;
172
172
  const getCache = (value) => {
@@ -179,19 +179,19 @@ async function executeBatchQuery(credential, batch, sessionKey) {
179
179
  let rows, queryString = caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache ? name + '_' + table + '_' : '';
180
180
  if (storedProcedureId && params) {
181
181
  const { options } = item;
182
- if (queryString && (rows = getCache(queryString += Module.asString(partitionKey) + '_' + storedProcedureId + Module.asString(params, true) + Module.asString(options, true)))) {
182
+ if (queryString && (rows = getCache(queryString += Module.asString(partitionKey, true) + '_' + storedProcedureId + Module.asString(params, true) + Module.asString(options, true)))) {
183
183
  result[i] = rows;
184
184
  continue;
185
185
  }
186
186
  const { statusCode, resource } = await createClient(name, table).scripts.storedProcedure(storedProcedureId).execute(partitionKey, params, options);
187
- if (statusCode === 200 /* HTTP_STATUS.OK */) {
187
+ if (statusCode === 200) {
188
188
  rows = Array.isArray(resource) ? resource : [resource];
189
189
  }
190
190
  }
191
191
  else if (id) {
192
192
  const { options } = item;
193
193
  if (queryString) {
194
- queryString += Module.asString(partitionKey) + '_' + id + Module.asString(options, true);
194
+ queryString += Module.asString(partitionKey, true) + '_' + id + Module.asString(options, true);
195
195
  if (!update && (rows = getCache(queryString))) {
196
196
  result[i] = rows;
197
197
  continue;
@@ -199,7 +199,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
199
199
  }
200
200
  const db = createClient(name, table).item(id, partitionKey);
201
201
  const { statusCode, resource } = update ? await db.patch(update, options) : await db.read(options);
202
- if (statusCode === 200 /* HTTP_STATUS.OK */) {
202
+ if (statusCode === 200) {
203
203
  rows = [resource];
204
204
  }
205
205
  }
@@ -208,7 +208,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
208
208
  if (limit > 0) {
209
209
  (options || (options = {})).maxItemCount = limit;
210
210
  }
211
- if (queryString && (rows = getCache(queryString += Module.asString(query) + Module.asString(options, true)))) {
211
+ if (queryString && (rows = getCache(queryString += Module.asString(query, true) + Module.asString(options, true)))) {
212
212
  result[i] = rows;
213
213
  continue;
214
214
  }
@@ -216,7 +216,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
216
216
  }
217
217
  else {
218
218
  closeClient();
219
- throw (0, util_1.formatError)(item, "Missing database query" /* ERR_DB.QUERY */);
219
+ throw (0, util_1.formatError)(item, "Missing database query");
220
220
  }
221
221
  result[i] = this.setQueryResult(service, credential, queryString, rows, cacheValue);
222
222
  }
package/download/index.js CHANGED
@@ -1,9 +1,8 @@
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");
5
+ const client_1 = require("@pi-r/azure");
7
6
  function download(credential, service = 'azure') {
8
7
  const blobServiceClient = client_1.createStorageClient.call(this, credential);
9
8
  return (data, callback) => {
@@ -21,10 +20,10 @@ 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 : undefined)
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/azure",
3
- "version": "0.5.0",
3
+ "version": "0.5.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.7.0",
24
- "@e-mc/module": "^0.7.0",
25
- "@e-mc/types": "^0.7.0",
23
+ "@e-mc/cloud": "^0.7.3",
24
+ "@e-mc/module": "^0.7.3",
25
+ "@e-mc/types": "^0.7.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
  }
@@ -0,0 +1,24 @@
1
+ import type { CloudDatabase, CloudStorage } from '@e-mc/types/lib/cloud';
2
+
3
+ import type { CosmosClientOptions, FeedOptions, PartitionKey, PatchRequestBody, RequestOptions, SqlQuerySpec } from '@azure/cosmos';
4
+
5
+ export type AzureStorage = CloudStorage<AzureStorageCredential, "azure" | "az">;
6
+
7
+ export interface AzureStorageCredential {
8
+ accountName?: string;
9
+ accountKey?: string;
10
+ connectionString?: string;
11
+ sharedAccessSignature?: string;
12
+ }
13
+
14
+ export interface AzureDatabaseQuery<T = FeedOptions | RequestOptions> extends CloudDatabase<string | SqlQuerySpec, T, PatchRequestBody, unknown[], AzureDatabaseCredential> {
15
+ source: "cloud";
16
+ service: "azure" | "az";
17
+ partitionKey?: PartitionKey;
18
+ storedProcedureId?: string;
19
+ }
20
+
21
+ export interface AzureDatabaseCredential extends AuthValue, CosmosClientOptions {
22
+ tenantId?: string;
23
+ clientId?: string;
24
+ }
package/upload/index.js CHANGED
@@ -1,12 +1,11 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
2
  const path = require("path");
4
3
  const fs = require("fs");
5
4
  const util_1 = require("@e-mc/cloud/util");
6
5
  const types_1 = require("@e-mc/types");
7
6
  const Module = require("@e-mc/module");
8
7
  const Cloud = require("@e-mc/cloud");
9
- const client_1 = require("../client");
8
+ const client_1 = require("@pi-r/azure");
10
9
  const BUCKET_SESSION = new Set();
11
10
  const BUCKET_RESPONSE = {};
12
11
  const getBucketKey = (credential, bucket, acl = '') => Module.asString(credential, true) + bucket + '_' + acl;
@@ -47,7 +46,7 @@ function upload(credential, service = 'azure') {
47
46
  containerClient.setAccessPolicy('container', retentionPolicy)
48
47
  .then(() => {
49
48
  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 });
49
+ this.formatMessage(64, service, ["Bucket configured" + ' (retention policy)', bucket], expiresOn ? expiresOn.toLocaleString() + ' (expires)' : startsOn ? startsOn.toLocaleString() + ' (starts)' : permissions, { ...Cloud.LOG_CLOUD_COMMAND });
51
50
  })
52
51
  .catch(err => addLog(err));
53
52
  }
@@ -72,7 +71,7 @@ function upload(credential, service = 'azure') {
72
71
  }
73
72
  } while (exists && ++i);
74
73
  if (i > 0) {
75
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["File renamed" /* VAL_CLOUD.RENAME_FILE */, current], filename, { ...Cloud.LOG_CLOUD_WARN });
74
+ this.formatMessage(64, service, ["File renamed", current], filename, { ...Cloud.LOG_CLOUD_WARN });
76
75
  }
77
76
  }
78
77
  catch (err) {
@@ -117,7 +116,7 @@ function upload(credential, service = 'azure') {
117
116
  .then(result => {
118
117
  let requestUrl = result._response.request.url;
119
118
  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);
120
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Upload success" /* VAL_CLOUD.UPLOAD_FILE */, url, { ...Cloud.LOG_CLOUD_UPLOAD });
119
+ this.formatMessage(64, service, "Upload success", url, { ...Cloud.LOG_CLOUD_UPLOAD });
121
120
  if (first) {
122
121
  cleanup();
123
122
  callback(null, url);