@pi-r/azure 0.2.3 → 0.5.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 +35 -45
- package/download/index.js +2 -2
- package/package.json +7 -7
- package/upload/index.js +24 -6
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(() => {
|
|
@@ -173,7 +163,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
173
163
|
const closeClient = () => client?.dispose();
|
|
174
164
|
for (let i = 0; i < length; ++i) {
|
|
175
165
|
const item = batch[i];
|
|
176
|
-
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;
|
|
177
167
|
if (!name || !table) {
|
|
178
168
|
closeClient();
|
|
179
169
|
throw (0, util_1.formatError)(item, name ? "Missing database table" /* ERR_DB.TABLE */ : "Missing database name" /* ERR_DB.NAME */);
|
|
@@ -189,7 +179,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
189
179
|
let rows, queryString = caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache ? name + '_' + table + '_' : '';
|
|
190
180
|
if (storedProcedureId && params) {
|
|
191
181
|
const { options } = item;
|
|
192
|
-
if (queryString && (rows = getCache(queryString += (partitionKey
|
|
182
|
+
if (queryString && (rows = getCache(queryString += Module.asString(partitionKey) + '_' + storedProcedureId + Module.asString(params, true) + Module.asString(options, true)))) {
|
|
193
183
|
result[i] = rows;
|
|
194
184
|
continue;
|
|
195
185
|
}
|
|
@@ -201,7 +191,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
201
191
|
else if (id) {
|
|
202
192
|
const { options } = item;
|
|
203
193
|
if (queryString) {
|
|
204
|
-
queryString += (partitionKey
|
|
194
|
+
queryString += Module.asString(partitionKey) + '_' + id + Module.asString(options, true);
|
|
205
195
|
if (!update && (rows = getCache(queryString))) {
|
|
206
196
|
result[i] = rows;
|
|
207
197
|
continue;
|
|
@@ -218,7 +208,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
218
208
|
if (limit > 0) {
|
|
219
209
|
(options || (options = {})).maxItemCount = limit;
|
|
220
210
|
}
|
|
221
|
-
if (queryString && (rows = getCache(queryString += Module.asString(query
|
|
211
|
+
if (queryString && (rows = getCache(queryString += Module.asString(query) + Module.asString(options, true)))) {
|
|
222
212
|
result[i] = rows;
|
|
223
213
|
continue;
|
|
224
214
|
}
|
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.5.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.
|
|
24
|
-
"@e-mc/module": "^0.
|
|
25
|
-
"@e-mc/types": "^0.
|
|
26
|
-
"@azure/cosmos": "^
|
|
27
|
-
"@azure/identity": "^
|
|
28
|
-
"@azure/storage-blob": "^12.
|
|
23
|
+
"@e-mc/cloud": "^0.7.0",
|
|
24
|
+
"@e-mc/module": "^0.7.0",
|
|
25
|
+
"@e-mc/types": "^0.7.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
|
@@ -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;
|
|
@@ -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.message, 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,
|
|
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
|
|
95
|
-
|
|
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)
|