@pi-r/azure 0.3.6 → 0.3.7

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
@@ -2,6 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  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
4
  const path = require("path");
5
+ const storage = require("@azure/storage-blob");
6
+ const cosmos = require("@azure/cosmos");
7
+ const identity = require("@azure/identity");
5
8
  const types_1 = require("@e-mc/types");
6
9
  const util_1 = require("@e-mc/cloud/util");
7
10
  const Module = require("@e-mc/module");
@@ -20,43 +23,28 @@ function validateDatabase(credential, data) {
20
23
  exports.validateDatabase = validateDatabase;
21
24
  function createStorageClient(credential) {
22
25
  var _a, _b;
23
- let packageName = '';
24
- try {
25
- const { BlobServiceClient, StorageSharedKeyCredential } = require(packageName = '@azure/storage-blob');
26
- const { accountName, accountKey, connectionString, sharedAccessSignature } = credential;
27
- if (connectionString) {
28
- credential.accountName || (credential.accountName = (_a = /AccountName=([^;]+);/.exec(connectionString)) === null || _a === void 0 ? void 0 : _a[1]);
29
- return BlobServiceClient.fromConnectionString(connectionString);
30
- }
31
- if (sharedAccessSignature) {
32
- credential.accountName || (credential.accountName = (_b = /^https:\/\/([a-z\d]+)\./.exec(sharedAccessSignature)) === null || _b === void 0 ? void 0 : _b[1]);
33
- return new BlobServiceClient(sharedAccessSignature);
34
- }
35
- let azureCredential;
36
- if (accountName && accountKey) {
37
- azureCredential = new StorageSharedKeyCredential(accountName, accountKey);
38
- }
39
- else {
40
- const { DefaultAzureCredential } = require(packageName = '@azure/identity');
41
- azureCredential = new DefaultAzureCredential();
42
- }
43
- return new BlobServiceClient(`https://${accountName || process.env.AZURE_STORAGE_ACCOUNT}.blob.core.windows.net`, azureCredential);
26
+ const { BlobServiceClient, StorageSharedKeyCredential } = storage;
27
+ const { accountName, accountKey, connectionString, sharedAccessSignature } = credential;
28
+ if (connectionString) {
29
+ credential.accountName || (credential.accountName = (_a = /AccountName=([^;]+);/.exec(connectionString)) === null || _a === void 0 ? void 0 : _a[1]);
30
+ return BlobServiceClient.fromConnectionString(connectionString);
44
31
  }
45
- catch (err) {
46
- this.checkPackage(err, packageName, { passThrough: true });
47
- throw err;
32
+ if (sharedAccessSignature) {
33
+ credential.accountName || (credential.accountName = (_b = /^https:\/\/([a-z\d]+)\./.exec(sharedAccessSignature)) === null || _b === void 0 ? void 0 : _b[1]);
34
+ return new BlobServiceClient(sharedAccessSignature);
48
35
  }
36
+ let azureCredential;
37
+ if (accountName && accountKey) {
38
+ azureCredential = new StorageSharedKeyCredential(accountName, accountKey);
39
+ }
40
+ else {
41
+ azureCredential = new identity.DefaultAzureCredential();
42
+ }
43
+ return new BlobServiceClient(`https://${accountName || process.env.AZURE_STORAGE_ACCOUNT}.blob.core.windows.net`, azureCredential);
49
44
  }
50
45
  exports.createStorageClient = createStorageClient;
51
46
  function createDatabaseClient(credential) {
52
- try {
53
- const { CosmosClient } = require('@azure/cosmos');
54
- return new CosmosClient(credential);
55
- }
56
- catch (err) {
57
- this.checkPackage(err, '@azure/cosmos', { passThrough: true });
58
- throw err;
59
- }
47
+ return new cosmos.CosmosClient(credential);
60
48
  }
61
49
  exports.createDatabaseClient = createDatabaseClient;
62
50
  async function createBucket(credential, bucket, publicRead, service = 'azure') {
@@ -87,11 +75,12 @@ async function createBucketV2(credential, bucket, access, options, service = 'az
87
75
  options.abortSignal = this.signal;
88
76
  return containerClient.create(options)
89
77
  .then(response => {
90
- if (response.errorCode) {
91
- this.formatMessage(64, service, ['Container created with errors', bucket], (0, types_1.errorMessage)("Error code", response.errorCode), { ...Cloud.LOG_CLOUD_WARN });
78
+ const { requestId, errorCode } = response;
79
+ if (errorCode) {
80
+ this.formatMessage(64, service, ['Container created with errors', bucket], (0, types_1.errorMessage)("Error code", errorCode), { ...Cloud.LOG_CLOUD_WARN });
92
81
  }
93
82
  else {
94
- this.formatMessage(64, service, ['Container created', bucket], response.requestId, { ...Cloud.LOG_CLOUD_COMMAND });
83
+ this.formatMessage(64, service, ['Container created', bucket], requestId, { ...Cloud.LOG_CLOUD_COMMAND });
95
84
  }
96
85
  return true;
97
86
  })
@@ -109,14 +98,15 @@ async function createBucketV2(credential, bucket, access, options, service = 'az
109
98
  exports.createBucketV2 = createBucketV2;
110
99
  function setBucketWebsite(credential, bucket, options, service = 'azure') {
111
100
  const staticWebsite = { enabled: true };
112
- if ((0, types_1.isString)(options.indexPage)) {
113
- staticWebsite.indexDocument = path.basename(options.indexPage);
101
+ const { indexPage, indexPath, errorPath } = options;
102
+ if ((0, types_1.isString)(indexPage)) {
103
+ staticWebsite.indexDocument = path.basename(indexPage);
114
104
  }
115
- if ((0, types_1.isString)(options.indexPath)) {
116
- staticWebsite.defaultIndexDocumentPath = Module.joinPath(bucket, options.indexPath);
105
+ if ((0, types_1.isString)(indexPath)) {
106
+ staticWebsite.defaultIndexDocumentPath = Module.joinPath(bucket, indexPath);
117
107
  }
118
- if ((0, types_1.isString)(options.errorPath)) {
119
- staticWebsite.errorDocument404Path = Module.joinPath(bucket, options.errorPath);
108
+ if ((0, types_1.isString)(errorPath)) {
109
+ staticWebsite.errorDocument404Path = Module.joinPath(bucket, errorPath);
120
110
  }
121
111
  return createStorageClient.call(this, credential).setProperties({ staticWebsite })
122
112
  .then(() => {
package/download/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
- const client_1 = require("@pi-r/azure");
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");
5
+ const client_1 = require("@pi-r/azure");
6
6
  function download(credential, service = 'azure') {
7
7
  const blobServiceClient = client_1.createStorageClient.call(this, credential);
8
8
  return (data, callback) => {
@@ -18,7 +18,7 @@ function download(credential, service = 'azure') {
18
18
  .then(value => {
19
19
  callback(null, value);
20
20
  const deleteObject = target.deleteObject;
21
- if (target.deleteObject) {
21
+ if (deleteObject) {
22
22
  blobClient.delete((0, types_1.isPlainObject)(deleteObject) && (deleteObject.abortSignal = this.signal) ? deleteObject : undefined)
23
23
  .then(() => this.formatMessage(64, service, "Delete success", location, { ...Cloud.LOG_CLOUD_DELETE }))
24
24
  .catch((err) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/azure",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
4
4
  "description": "Azure cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
package/upload/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  const path = require("path");
3
3
  const fs = require("fs");
4
- const client_1 = require("@pi-r/azure");
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");
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 = '') => Module.asString(credential, true) + bucket + '_' + acl;