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