@pi-r/aws-v3 0.5.0 → 0.6.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 +82 -34
- package/package.json +8 -8
- package/upload/index.js +5 -11
package/client/index.js
CHANGED
|
@@ -8,7 +8,7 @@ const util_1 = require("@e-mc/cloud/util");
|
|
|
8
8
|
const types_1 = require("@e-mc/types");
|
|
9
9
|
const Module = require("@e-mc/module");
|
|
10
10
|
const Cloud = require("@e-mc/cloud");
|
|
11
|
-
function setCannedAcl(S3, client, Bucket, ACL, service = 'aws-v3', recursive) {
|
|
11
|
+
async function setCannedAcl(S3, client, Bucket, ACL, service = 'aws-v3', recursive) {
|
|
12
12
|
let Policy;
|
|
13
13
|
switch (ACL) {
|
|
14
14
|
case 1:
|
|
@@ -28,7 +28,7 @@ function setCannedAcl(S3, client, Bucket, ACL, service = 'aws-v3', recursive) {
|
|
|
28
28
|
.catch((err) => {
|
|
29
29
|
if (!recursive && err instanceof Error) {
|
|
30
30
|
if (err.name === 'OperationAborted') {
|
|
31
|
-
setTimeout(() => setCannedAcl.call(this, S3, client, Bucket, ACL, service, true), 60000 /* TIME.m */);
|
|
31
|
+
setTimeout(async () => setCannedAcl.call(this, S3, client, Bucket, ACL, service, true), 60000 /* TIME.m */);
|
|
32
32
|
this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, [`Grant ${ACL} (delayed)`, err.Endpoint || Bucket], err, { ...Cloud.LOG_CLOUD_DELAYED });
|
|
33
33
|
}
|
|
34
34
|
else {
|
|
@@ -90,11 +90,11 @@ function validateDatabase(credential, data) {
|
|
|
90
90
|
return (0, aws_1.isDatabaseDefined)(credential, data) && validateStorage(credential);
|
|
91
91
|
}
|
|
92
92
|
exports.validateDatabase = validateDatabase;
|
|
93
|
-
function createBucket(credential, Bucket, publicRead, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
|
|
93
|
+
async function createBucket(credential, Bucket, publicRead, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
|
|
94
94
|
return createBucketV2.call(this, credential, Bucket, publicRead ? 'public-read' : undefined, undefined, service, sdk);
|
|
95
95
|
}
|
|
96
96
|
exports.createBucket = createBucket;
|
|
97
|
-
function createBucketV2(credential, Bucket, ACL, options, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
|
|
97
|
+
async function createBucketV2(credential, Bucket, ACL, options, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
|
|
98
98
|
ACL = ACL === 1 ? 1 : (0, aws_1.checkBucketCannedACL)(ACL);
|
|
99
99
|
const [client, AWS] = createStorageClient.call(this, credential, service, sdk);
|
|
100
100
|
return client.send(new AWS.HeadBucketCommand({ Bucket }))
|
|
@@ -104,7 +104,7 @@ function createBucketV2(credential, Bucket, ACL, options, service = 'aws-v3', sd
|
|
|
104
104
|
}
|
|
105
105
|
return true;
|
|
106
106
|
})
|
|
107
|
-
.catch(() => {
|
|
107
|
+
.catch(async () => {
|
|
108
108
|
const input = { ...options, Bucket };
|
|
109
109
|
const region = credential.region;
|
|
110
110
|
if (typeof region === 'string' && region !== 'us-east-1') {
|
|
@@ -134,7 +134,7 @@ function createBucketV2(credential, Bucket, ACL, options, service = 'aws-v3', sd
|
|
|
134
134
|
});
|
|
135
135
|
}
|
|
136
136
|
exports.createBucketV2 = createBucketV2;
|
|
137
|
-
function setBucketPolicy(credential, Bucket, options, service = 'aws', sdk = 'aws-sdk/clients/s3') {
|
|
137
|
+
async function setBucketPolicy(credential, Bucket, options, service = 'aws', sdk = 'aws-sdk/clients/s3') {
|
|
138
138
|
const [client, AWS] = createStorageClient.call(this, credential, service, sdk);
|
|
139
139
|
const block = 'PublicAccessBlockConfiguration' in options;
|
|
140
140
|
const policy = !block && 'Policy' in options && !('ACL' in options);
|
|
@@ -152,7 +152,7 @@ function setBucketPolicy(credential, Bucket, options, service = 'aws', sdk = 'aw
|
|
|
152
152
|
});
|
|
153
153
|
}
|
|
154
154
|
exports.setBucketPolicy = setBucketPolicy;
|
|
155
|
-
function setBucketWebsite(credential, Bucket, options, service = 'aws', sdk = 'aws-sdk/clients/s3') {
|
|
155
|
+
async function setBucketWebsite(credential, Bucket, options, service = 'aws', sdk = 'aws-sdk/clients/s3') {
|
|
156
156
|
const [client, AWS] = createStorageClient.call(this, credential, service, sdk);
|
|
157
157
|
const WebsiteConfiguration = {};
|
|
158
158
|
const { indexPage: Suffix, errorPage: Key } = options;
|
|
@@ -175,18 +175,18 @@ function setBucketWebsite(credential, Bucket, options, service = 'aws', sdk = 'a
|
|
|
175
175
|
});
|
|
176
176
|
}
|
|
177
177
|
exports.setBucketWebsite = setBucketWebsite;
|
|
178
|
-
function deleteObjects(credential, Bucket, service, sdk) {
|
|
178
|
+
async function deleteObjects(credential, Bucket, service, sdk) {
|
|
179
179
|
return deleteObjectsV2.call(this, credential, Bucket, true, service, sdk);
|
|
180
180
|
}
|
|
181
181
|
exports.deleteObjects = deleteObjects;
|
|
182
182
|
async function deleteObjectsV2(credential, Bucket, recursive = true, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
|
|
183
183
|
const [client, AWS] = createStorageClient.call(this, credential, service, sdk);
|
|
184
184
|
return client.send(new AWS.ListObjectsCommand({ Bucket }))
|
|
185
|
-
.then(({ Contents }) => {
|
|
185
|
+
.then(async ({ Contents }) => {
|
|
186
186
|
if (Contents?.length) {
|
|
187
187
|
let Objects = Contents.map(data => ({ Key: data.Key }));
|
|
188
188
|
if (!recursive) {
|
|
189
|
-
Objects = Objects.filter(value => value.Key.
|
|
189
|
+
Objects = Objects.filter(value => !value.Key.includes('/'));
|
|
190
190
|
}
|
|
191
191
|
return client.send(new AWS.DeleteObjectsCommand({ Bucket, Delete: { Objects } }))
|
|
192
192
|
.then(data => {
|
|
@@ -213,41 +213,42 @@ async function executeQuery(credential, data, sessionKey) {
|
|
|
213
213
|
}
|
|
214
214
|
exports.executeQuery = executeQuery;
|
|
215
215
|
async function executeBatchQuery(credential, batch, sessionKey) {
|
|
216
|
+
var _a;
|
|
216
217
|
const length = batch.length;
|
|
217
218
|
const result = new Array(length);
|
|
218
219
|
const caching = length > 0 && this.hasCache(batch[0].service, sessionKey);
|
|
219
220
|
const cacheValue = { value: this.valueOfKey(credential, 'cache'), sessionKey };
|
|
220
221
|
let client;
|
|
221
|
-
const createClient = () => client || (client = createDatabaseClient.call(this,
|
|
222
|
+
const createClient = () => client || (client = createDatabaseClient.call(this, credential));
|
|
222
223
|
const closeClient = () => client?.[0].destroy();
|
|
224
|
+
(0, aws_1.setDatabaseEndpoint)(credential);
|
|
223
225
|
for (let i = 0; i < length; ++i) {
|
|
224
226
|
const item = batch[i];
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
closeClient();
|
|
228
|
-
throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
|
|
229
|
-
}
|
|
230
|
-
(0, aws_1.setDatabaseEndpoint)(credential);
|
|
231
|
-
const renewCache = ignoreCache === 0;
|
|
227
|
+
let { service, table: TableName, id, query, partitionKey, key = partitionKey, limit = 0, update, ignoreCache } = item;
|
|
228
|
+
const useCache = caching && ignoreCache !== true;
|
|
232
229
|
const getCache = (value) => {
|
|
233
|
-
if (ignoreCache
|
|
234
|
-
|
|
230
|
+
if (ignoreCache !== 1) {
|
|
231
|
+
cacheValue.renewCache = ignoreCache === 0;
|
|
232
|
+
return this.getQueryResult(service, credential, value, cacheValue);
|
|
235
233
|
}
|
|
236
|
-
cacheValue.renewCache = renewCache;
|
|
237
|
-
return this.getQueryResult(service, credential, value, cacheValue);
|
|
238
234
|
};
|
|
239
|
-
|
|
235
|
+
cacheValue.exclusiveOf = Array.isArray(ignoreCache) ? ignoreCache : undefined;
|
|
236
|
+
let rows, queryString = '';
|
|
240
237
|
if (key && (id || (0, types_1.isPlainObject)(key))) {
|
|
241
|
-
if (
|
|
242
|
-
|
|
238
|
+
if (!TableName) {
|
|
239
|
+
closeClient();
|
|
240
|
+
throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
|
|
241
|
+
}
|
|
242
|
+
if (useCache) {
|
|
243
|
+
queryString = TableName + '_' + Module.asString(key, true) + (id !== undefined ? '_' + Module.asString(id, true) : '');
|
|
243
244
|
if (!update && (rows = getCache(queryString))) {
|
|
244
245
|
result[i] = rows;
|
|
245
246
|
continue;
|
|
246
247
|
}
|
|
247
248
|
}
|
|
248
249
|
const [db, AWS] = createClient();
|
|
249
|
-
const Key = (0, types_1.isPlainObject)(key) ? key : { [key]: id };
|
|
250
|
-
const command = { TableName
|
|
250
|
+
const Key = (0, types_1.isPlainObject)(key) ? key : { [key]: (0, aws_1.parseAttributeValue)(id) };
|
|
251
|
+
const command = { TableName, Key };
|
|
251
252
|
if (update) {
|
|
252
253
|
await db.send(new AWS.UpdateCommand({ ...command, ...update }));
|
|
253
254
|
}
|
|
@@ -257,18 +258,65 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
257
258
|
}
|
|
258
259
|
}
|
|
259
260
|
else if ((0, types_1.isPlainObject)(query)) {
|
|
260
|
-
if (
|
|
261
|
-
|
|
262
|
-
continue;
|
|
261
|
+
if (TableName) {
|
|
262
|
+
query.TableName = TableName;
|
|
263
263
|
}
|
|
264
|
-
query.TableName = table;
|
|
265
264
|
if (limit > 0) {
|
|
266
265
|
query.Limit = limit;
|
|
267
266
|
}
|
|
267
|
+
if (!query.TableName) {
|
|
268
|
+
closeClient();
|
|
269
|
+
throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
|
|
270
|
+
}
|
|
271
|
+
if (useCache && (rows = getCache(queryString = Module.asString(query, true)))) {
|
|
272
|
+
result[i] = rows;
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
const [db, AWS] = createClient();
|
|
276
|
+
const { Count, Items } = await db.send(new AWS.QueryCommand(query));
|
|
277
|
+
if (Count && Items) {
|
|
278
|
+
rows = Items;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
else if ((0, types_1.isArray)(query)) {
|
|
282
|
+
let params = (item.params || {});
|
|
283
|
+
if (!(0, types_1.isPlainObject)(params.RequestItems)) {
|
|
284
|
+
params.RequestItems = {};
|
|
285
|
+
}
|
|
286
|
+
TableName || (TableName = Object.keys(params.RequestItems)[0]);
|
|
287
|
+
if (!TableName) {
|
|
288
|
+
throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
|
|
289
|
+
}
|
|
290
|
+
// @ts-ignore
|
|
291
|
+
const Item = (_a = params.RequestItems)[TableName] || (_a[TableName] = {});
|
|
292
|
+
Item.Keys = query;
|
|
293
|
+
params = { RequestItems: { [TableName]: Item } };
|
|
294
|
+
if (useCache && (rows = getCache(queryString = Module.asString(params, true)))) {
|
|
295
|
+
result[i] = rows;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
const [db, AWS] = createClient();
|
|
299
|
+
const { Responses } = await db.send(new AWS.BatchGetCommand(params));
|
|
300
|
+
if (Responses) {
|
|
301
|
+
rows = Responses[TableName];
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
else if (TableName) {
|
|
305
|
+
let params = item.params;
|
|
306
|
+
if ((0, types_1.isPlainObject)(params)) {
|
|
307
|
+
params.TableName = TableName;
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
params = { TableName };
|
|
311
|
+
}
|
|
312
|
+
if (useCache && (rows = getCache(queryString = Module.asString(params, true)))) {
|
|
313
|
+
result[i] = rows;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
268
316
|
const [db, AWS] = createClient();
|
|
269
|
-
const
|
|
270
|
-
if (
|
|
271
|
-
rows =
|
|
317
|
+
const { Count, Items } = await db.send(new AWS.ScanCommand(params));
|
|
318
|
+
if (Count && Items) {
|
|
319
|
+
rows = Items;
|
|
272
320
|
}
|
|
273
321
|
}
|
|
274
322
|
else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/aws-v3",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "AWS V3 cloud functions for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -20,12 +20,12 @@
|
|
|
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
|
-
"@pi-r/aws": "^0.
|
|
27
|
-
"@aws-sdk/client-dynamodb": "^3.
|
|
28
|
-
"@aws-sdk/client-s3": "^3.
|
|
29
|
-
"@aws-sdk/lib-dynamodb": "^3.
|
|
23
|
+
"@e-mc/cloud": "^0.8.1",
|
|
24
|
+
"@e-mc/module": "^0.8.1",
|
|
25
|
+
"@e-mc/types": "^0.8.1",
|
|
26
|
+
"@pi-r/aws": "^0.6.1",
|
|
27
|
+
"@aws-sdk/client-dynamodb": "^3.485.0",
|
|
28
|
+
"@aws-sdk/client-s3": "^3.485.0",
|
|
29
|
+
"@aws-sdk/lib-dynamodb": "^3.485.0"
|
|
30
30
|
}
|
|
31
31
|
}
|
package/upload/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const path = require("path");
|
|
4
|
-
const fs = require("fs");
|
|
5
4
|
const stream = require("stream");
|
|
6
5
|
const util_1 = require("@e-mc/cloud/util");
|
|
7
6
|
const aws_1 = require("@pi-r/aws");
|
|
@@ -29,7 +28,7 @@ function upload(credential, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
|
|
|
29
28
|
callback(err);
|
|
30
29
|
return false;
|
|
31
30
|
};
|
|
32
|
-
const addLog = (err) => err instanceof Error && this.addLog(this.statusType.WARN, err
|
|
31
|
+
const addLog = (err) => err instanceof Error && this.addLog(this.statusType.WARN, err, service, Bucket);
|
|
33
32
|
const configBucket = admin.configBucket;
|
|
34
33
|
if (!BUCKET_SESSION.has(service + Bucket)) {
|
|
35
34
|
const bucketAcl = admin.publicRead ? 'public-read' : admin.acl;
|
|
@@ -80,15 +79,10 @@ function upload(credential, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
|
|
|
80
79
|
const Body = [data.buffer];
|
|
81
80
|
const ContentType = [contentType];
|
|
82
81
|
if (fileGroup) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
catch (err) {
|
|
89
|
-
addLog(err);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
82
|
+
const [key, body, type] = (0, util_1.createKeyAndBody)(filename, fileGroup, addLog);
|
|
83
|
+
Key.push(...key);
|
|
84
|
+
Body.push(...body);
|
|
85
|
+
ContentType.push(...type);
|
|
92
86
|
}
|
|
93
87
|
for (let i = 0; i < Key.length; ++i) {
|
|
94
88
|
const first = i === 0;
|