@pi-r/aws 0.7.3 → 0.8.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 +38 -35
- package/download/index.js +3 -2
- package/package.json +6 -6
- package/types/index.d.ts +3 -3
- package/upload/index.js +13 -5
package/client/index.js
CHANGED
|
@@ -1,15 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
exports.CLOUD_UPLOAD_CHUNK = exports.CLOUD_UPLOAD_STREAM =
|
|
2
|
+
exports.CLOUD_UPLOAD_CHUNK = exports.CLOUD_UPLOAD_STREAM = void 0;
|
|
3
|
+
exports.validateStorage = validateStorage;
|
|
4
|
+
exports.validateDatabase = validateDatabase;
|
|
5
|
+
exports.createStorageClient = createStorageClient;
|
|
6
|
+
exports.createDatabaseClient = createDatabaseClient;
|
|
7
|
+
exports.createBucket = createBucket;
|
|
8
|
+
exports.createBucketV2 = createBucketV2;
|
|
9
|
+
exports.setBucketPolicy = setBucketPolicy;
|
|
10
|
+
exports.setBucketTagging = setBucketTagging;
|
|
11
|
+
exports.setBucketWebsite = setBucketWebsite;
|
|
12
|
+
exports.deleteObjects = deleteObjects;
|
|
13
|
+
exports.deleteObjectsV2 = deleteObjectsV2;
|
|
14
|
+
exports.executeQuery = executeQuery;
|
|
15
|
+
exports.executeBatchQuery = executeBatchQuery;
|
|
16
|
+
exports.parseAttributeValue = parseAttributeValue;
|
|
17
|
+
exports.isNoSuchBucket = isNoSuchBucket;
|
|
3
18
|
const aws = require("aws-sdk");
|
|
4
19
|
const Cloud = require("@e-mc/cloud");
|
|
5
20
|
const util_1 = require("@e-mc/cloud/util");
|
|
6
21
|
const types_1 = require("@e-mc/types");
|
|
7
22
|
const aws_lib_1 = require("@pi-r/aws-lib");
|
|
8
23
|
const ACP_AUTHENTICATEDREAD = {
|
|
9
|
-
Grants: [
|
|
24
|
+
Grants: [
|
|
25
|
+
{
|
|
10
26
|
Grantee: { Type: 'Group', URI: 'http://acs.amazonaws.com/groups/global/AuthenticatedUsers' },
|
|
11
27
|
Permission: 'READ'
|
|
12
|
-
}
|
|
28
|
+
}
|
|
29
|
+
]
|
|
13
30
|
};
|
|
14
31
|
async function setCannedAcl(S3, Bucket, ACL, service = "aws", recursive) {
|
|
15
32
|
if (service === "oci") {
|
|
@@ -21,7 +38,9 @@ async function setCannedAcl(S3, Bucket, ACL, service = "aws", recursive) {
|
|
|
21
38
|
}
|
|
22
39
|
else if (!recursive) {
|
|
23
40
|
if (err instanceof Error && err.code === 'OperationAborted') {
|
|
24
|
-
setTimeout(
|
|
41
|
+
setTimeout(() => {
|
|
42
|
+
void setCannedAcl.call(this, S3, Bucket, ACL, service, true);
|
|
43
|
+
}, 60000);
|
|
25
44
|
this.formatMessage(64, service, [`Grant ${ACL} (delayed)`, Bucket], err, { ...Cloud.LOG_CLOUD_DELAYED });
|
|
26
45
|
}
|
|
27
46
|
else {
|
|
@@ -55,16 +74,17 @@ async function setCannedAcl(S3, Bucket, ACL, service = "aws", recursive) {
|
|
|
55
74
|
break;
|
|
56
75
|
}
|
|
57
76
|
}
|
|
58
|
-
return promise.then(() =>
|
|
77
|
+
return promise.then(() => {
|
|
78
|
+
callback(null);
|
|
79
|
+
})
|
|
80
|
+
.catch(callback);
|
|
59
81
|
}
|
|
60
82
|
function validateStorage(credential) {
|
|
61
83
|
return !!((0, aws_lib_1.isAccessDefined)(credential) || (0, aws_lib_1.isEnvDefined)() || credential.fromPath || credential.profile || (0, aws_lib_1.isProviderChainDefined)());
|
|
62
84
|
}
|
|
63
|
-
exports.validateStorage = validateStorage;
|
|
64
85
|
function validateDatabase(credential, data) {
|
|
65
86
|
return (0, aws_lib_1.isDatabaseDefined)(credential, data) && validateStorage(credential);
|
|
66
87
|
}
|
|
67
|
-
exports.validateDatabase = validateDatabase;
|
|
68
88
|
function createStorageClient(credential, service = "aws", sdk = "aws-sdk/clients/s3") {
|
|
69
89
|
try {
|
|
70
90
|
if (service === "aws") {
|
|
@@ -86,7 +106,6 @@ function createStorageClient(credential, service = "aws", sdk = "aws-sdk/clients
|
|
|
86
106
|
throw err;
|
|
87
107
|
}
|
|
88
108
|
}
|
|
89
|
-
exports.createStorageClient = createStorageClient;
|
|
90
109
|
function createDatabaseClient(credential) {
|
|
91
110
|
const { profile, fromPath } = credential;
|
|
92
111
|
let options;
|
|
@@ -108,11 +127,9 @@ function createDatabaseClient(credential) {
|
|
|
108
127
|
}
|
|
109
128
|
return new aws.DynamoDB.DocumentClient(options);
|
|
110
129
|
}
|
|
111
|
-
exports.createDatabaseClient = createDatabaseClient;
|
|
112
130
|
async function createBucket(credential, Bucket, publicRead, service = "aws", sdk = "aws-sdk/clients/s3") {
|
|
113
131
|
return createBucketV2.call(this, credential, Bucket, publicRead ? "public-read" : undefined, undefined, service, sdk);
|
|
114
132
|
}
|
|
115
|
-
exports.createBucket = createBucket;
|
|
116
133
|
async function createBucketV2(credential, Bucket, ACL, options, service = "aws", sdk = "aws-sdk/clients/s3") {
|
|
117
134
|
ACL = ACL === 1 ? 1 : (0, aws_lib_1.checkBucketCannedACL)(ACL);
|
|
118
135
|
const S3 = createStorageClient.call(this, credential, service, sdk);
|
|
@@ -152,7 +169,6 @@ async function createBucketV2(credential, Bucket, ACL, options, service = "aws",
|
|
|
152
169
|
});
|
|
153
170
|
});
|
|
154
171
|
}
|
|
155
|
-
exports.createBucketV2 = createBucketV2;
|
|
156
172
|
async function setBucketPolicy(credential, Bucket, options, service = "aws", sdk = "aws-sdk/clients/s3") {
|
|
157
173
|
const ibm = service === "ibm";
|
|
158
174
|
const S3 = createStorageClient.call(this, credential, service, sdk);
|
|
@@ -167,13 +183,12 @@ async function setBucketPolicy(credential, Bucket, options, service = "aws", sdk
|
|
|
167
183
|
return true;
|
|
168
184
|
})
|
|
169
185
|
.catch((err) => {
|
|
170
|
-
if (!
|
|
186
|
+
if (!isNoSuchBucket(err)) {
|
|
171
187
|
this.formatFail(64, service, ["Unable to update bucket policy", Bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
|
|
172
188
|
}
|
|
173
189
|
return false;
|
|
174
190
|
});
|
|
175
191
|
}
|
|
176
|
-
exports.setBucketPolicy = setBucketPolicy;
|
|
177
192
|
async function setBucketTagging(credential, Bucket, options, service = "aws", sdk = "aws-sdk/clients/s3") {
|
|
178
193
|
if (!(0, types_1.isPlainObject)(options) || !Array.isArray(options.Tagging?.TagSet)) {
|
|
179
194
|
return false;
|
|
@@ -185,7 +200,7 @@ async function setBucketTagging(credential, Bucket, options, service = "aws", sd
|
|
|
185
200
|
return true;
|
|
186
201
|
};
|
|
187
202
|
const error = (err) => {
|
|
188
|
-
if (!
|
|
203
|
+
if (!isNoSuchBucket(err)) {
|
|
189
204
|
this.formatFail(64, service, ["Unable to update bucket tagging", Bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
|
|
190
205
|
}
|
|
191
206
|
return false;
|
|
@@ -196,7 +211,6 @@ async function setBucketTagging(credential, Bucket, options, service = "aws", sd
|
|
|
196
211
|
options.Bucket = Bucket;
|
|
197
212
|
return S3.putBucketTagging(options).promise().then(command).catch((err) => error(err));
|
|
198
213
|
}
|
|
199
|
-
exports.setBucketTagging = setBucketTagging;
|
|
200
214
|
async function setBucketWebsite(credential, Bucket, options, service = "aws", sdk = "aws-sdk/clients/s3") {
|
|
201
215
|
const S3 = createStorageClient.call(this, credential, service, sdk);
|
|
202
216
|
const WebsiteConfiguration = {};
|
|
@@ -213,17 +227,15 @@ async function setBucketWebsite(credential, Bucket, options, service = "aws", sd
|
|
|
213
227
|
return true;
|
|
214
228
|
})
|
|
215
229
|
.catch((err) => {
|
|
216
|
-
if (!
|
|
230
|
+
if (!isNoSuchBucket(err)) {
|
|
217
231
|
this.formatFail(64, service, ["Unable to set bucket website", Bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
|
|
218
232
|
}
|
|
219
233
|
return false;
|
|
220
234
|
});
|
|
221
235
|
}
|
|
222
|
-
exports.setBucketWebsite = setBucketWebsite;
|
|
223
236
|
async function deleteObjects(credential, Bucket, service = "aws", sdk = "aws-sdk/clients/s3") {
|
|
224
237
|
return deleteObjectsV2.call(this, credential, Bucket, true, service, sdk);
|
|
225
238
|
}
|
|
226
|
-
exports.deleteObjects = deleteObjects;
|
|
227
239
|
async function deleteObjectsV2(credential, Bucket, recursive = true, service = "aws", sdk = "aws-sdk/clients/s3") {
|
|
228
240
|
const S3 = createStorageClient.call(this, credential, service, sdk);
|
|
229
241
|
return S3.listObjects({ Bucket }).promise()
|
|
@@ -247,16 +259,14 @@ async function deleteObjectsV2(credential, Bucket, recursive = true, service = "
|
|
|
247
259
|
}
|
|
248
260
|
})
|
|
249
261
|
.catch((err) => {
|
|
250
|
-
if (!
|
|
262
|
+
if (!isNoSuchBucket(err)) {
|
|
251
263
|
this.formatFail(64, service, ["Unable to list bucket", Bucket], err, { ...Cloud.LOG_CLOUD_FAIL, fatal: false });
|
|
252
264
|
}
|
|
253
265
|
});
|
|
254
266
|
}
|
|
255
|
-
exports.deleteObjectsV2 = deleteObjectsV2;
|
|
256
267
|
async function executeQuery(credential, data, sessionKey) {
|
|
257
268
|
return (await executeBatchQuery.call(this, credential, [data], sessionKey))[0] || [];
|
|
258
269
|
}
|
|
259
|
-
exports.executeQuery = executeQuery;
|
|
260
270
|
async function executeBatchQuery(credential, batch, sessionKey) {
|
|
261
271
|
const length = batch.length;
|
|
262
272
|
const result = new Array(length);
|
|
@@ -269,12 +279,6 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
269
279
|
const item = batch[i];
|
|
270
280
|
let { service, table: TableName, id, query, key, limit = 0, update, ignoreCache } = item;
|
|
271
281
|
const useCache = caching && ignoreCache !== true;
|
|
272
|
-
const getCache = (value) => {
|
|
273
|
-
if (ignoreCache !== 1) {
|
|
274
|
-
cacheValue.renewCache = ignoreCache === 0;
|
|
275
|
-
return this.getQueryResult(service, credential, value, cacheValue);
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
282
|
cacheValue.exclusiveOf = Array.isArray(ignoreCache) ? ignoreCache : undefined;
|
|
279
283
|
let rows, queryString = '';
|
|
280
284
|
if (key && (id || (0, types_1.isPlainObject)(key))) {
|
|
@@ -283,7 +287,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
283
287
|
}
|
|
284
288
|
if (useCache) {
|
|
285
289
|
queryString = TableName + '_' + Cloud.asString(key, true) + (id !== undefined ? '_' + Cloud.asString(id, true) : '');
|
|
286
|
-
if (!update && (rows =
|
|
290
|
+
if (!update && (rows = this.getCacheResult(service, credential, queryString, cacheValue, ignoreCache))) {
|
|
287
291
|
result[i] = rows;
|
|
288
292
|
continue;
|
|
289
293
|
}
|
|
@@ -309,7 +313,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
309
313
|
if (limit > 0) {
|
|
310
314
|
query.Limit = limit;
|
|
311
315
|
}
|
|
312
|
-
if (useCache && (rows =
|
|
316
|
+
if (useCache && (rows = this.getCacheResult(service, credential, queryString = Cloud.asString(query, true), cacheValue, ignoreCache))) {
|
|
313
317
|
result[i] = rows;
|
|
314
318
|
continue;
|
|
315
319
|
}
|
|
@@ -330,7 +334,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
330
334
|
const Item = params.RequestItems[TableName] ||= {};
|
|
331
335
|
Item.Keys = query;
|
|
332
336
|
params = { RequestItems: { [TableName]: Item } };
|
|
333
|
-
if (useCache && (rows =
|
|
337
|
+
if (useCache && (rows = this.getCacheResult(service, credential, queryString = Cloud.asString(params, true), cacheValue, ignoreCache))) {
|
|
334
338
|
result[i] = rows;
|
|
335
339
|
continue;
|
|
336
340
|
}
|
|
@@ -347,7 +351,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
347
351
|
else {
|
|
348
352
|
params = { TableName };
|
|
349
353
|
}
|
|
350
|
-
if (useCache && (rows =
|
|
354
|
+
if (useCache && (rows = this.getCacheResult(service, credential, queryString = Cloud.asString(params, true), cacheValue, ignoreCache))) {
|
|
351
355
|
result[i] = rows;
|
|
352
356
|
continue;
|
|
353
357
|
}
|
|
@@ -363,7 +367,6 @@ async function executeBatchQuery(credential, batch, sessionKey) {
|
|
|
363
367
|
}
|
|
364
368
|
return result;
|
|
365
369
|
}
|
|
366
|
-
exports.executeBatchQuery = executeBatchQuery;
|
|
367
370
|
function parseAttributeValue(value) {
|
|
368
371
|
switch (typeof value) {
|
|
369
372
|
case 'string':
|
|
@@ -393,8 +396,8 @@ function parseAttributeValue(value) {
|
|
|
393
396
|
}
|
|
394
397
|
return { NULL: true };
|
|
395
398
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
+
function isNoSuchBucket(err) {
|
|
400
|
+
return err instanceof Error && err.code === 'NoSuchBucket';
|
|
401
|
+
}
|
|
399
402
|
exports.CLOUD_UPLOAD_STREAM = true;
|
|
400
403
|
exports.CLOUD_UPLOAD_CHUNK = true;
|
package/download/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
const types_1 = require("@e-mc/types");
|
|
3
3
|
const Cloud = require("@e-mc/cloud");
|
|
4
4
|
const client_1 = require("@pi-r/aws");
|
|
5
|
-
|
|
5
|
+
function download(credential, service = "aws", sdk = "aws-sdk/clients/s3") {
|
|
6
6
|
const s3 = client_1.createStorageClient.call(this, credential, service, sdk);
|
|
7
7
|
return (data, callback) => {
|
|
8
8
|
const { bucket: Bucket, download: target } = data;
|
|
@@ -35,4 +35,5 @@ module.exports = function download(credential, service = "aws", sdk = "aws-sdk/c
|
|
|
35
35
|
}
|
|
36
36
|
});
|
|
37
37
|
};
|
|
38
|
-
}
|
|
38
|
+
}
|
|
39
|
+
module.exports = download;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/aws",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "AWS V2 cloud functions for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"types": "client/index.d.ts",
|
|
@@ -21,10 +21,10 @@
|
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@e-mc/cloud": "^0.
|
|
25
|
-
"@e-mc/module": "^0.
|
|
26
|
-
"@e-mc/types": "^0.
|
|
27
|
-
"@pi-r/aws-lib": "^0.
|
|
28
|
-
"aws-sdk": "^2.
|
|
24
|
+
"@e-mc/cloud": "^0.10.0",
|
|
25
|
+
"@e-mc/module": "^0.10.0",
|
|
26
|
+
"@e-mc/types": "^0.10.0",
|
|
27
|
+
"@pi-r/aws-lib": "^0.8.0",
|
|
28
|
+
"aws-sdk": "^2.1668.0"
|
|
29
29
|
}
|
|
30
30
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import type { CloudDatabase, CloudStorage } from '@e-mc/types/lib/cloud';
|
|
2
2
|
|
|
3
3
|
import type { ConfigurationOptions } from 'aws-sdk/lib/core';
|
|
4
|
-
import type {
|
|
5
|
-
import type { DocumentClient } from 'aws-sdk/clients/dynamodb';
|
|
4
|
+
import type { DocumentClient, Types } from 'aws-sdk/clients/dynamodb';
|
|
6
5
|
import type { PutBucketAclRequest, PutBucketPolicyRequest, PutPublicAccessBlockRequest } from 'aws-sdk/clients/s3';
|
|
7
6
|
|
|
8
7
|
export type ConfigureBucketOptions = PutBucketAclRequest | PutBucketPolicyRequest | PutPublicAccessBlockRequest;
|
|
@@ -12,6 +11,7 @@ export type QueryInput = DocumentClient.QueryInput;
|
|
|
12
11
|
export type UpdateItemInput = DocumentClient.UpdateItemInput;
|
|
13
12
|
export type ScanInput = DocumentClient.ScanInput;
|
|
14
13
|
export type BatchGetItemInput = DocumentClient.BatchGetItemInput;
|
|
14
|
+
export type DynamoDBClientConfig = Types.ClientConfiguration & DocumentClient.DocumentClientOptions;
|
|
15
15
|
|
|
16
16
|
export type AWSStorage = CloudStorage<AWSStorageCredential, "aws">;
|
|
17
17
|
|
|
@@ -26,4 +26,4 @@ export interface AWSDatabaseQuery extends CloudDatabase<QueryInput | Key[], Reco
|
|
|
26
26
|
key?: string | Key;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
export interface AWSDatabaseCredential extends AWSStorageCredential,
|
|
29
|
+
export interface AWSDatabaseCredential extends AWSStorageCredential, DynamoDBClientConfig {}
|
package/upload/index.js
CHANGED
|
@@ -3,6 +3,7 @@ const path = require("path");
|
|
|
3
3
|
const fs = require("fs");
|
|
4
4
|
const stream = require("stream");
|
|
5
5
|
const aws = require("aws-sdk");
|
|
6
|
+
const crypto_1 = require("crypto");
|
|
6
7
|
const Cloud = require("@e-mc/cloud");
|
|
7
8
|
const util_1 = require("@e-mc/cloud/util");
|
|
8
9
|
const types_1 = require("@e-mc/types");
|
|
@@ -10,7 +11,7 @@ const aws_lib_1 = require("@pi-r/aws-lib");
|
|
|
10
11
|
const client_1 = require("@pi-r/aws");
|
|
11
12
|
const BUCKET_SESSION = new Set();
|
|
12
13
|
const BUCKET_RESPONSE = {};
|
|
13
|
-
|
|
14
|
+
function upload(credential, service = "aws", sdk = "aws-sdk/clients/s3") {
|
|
14
15
|
const s3 = client_1.createStorageClient.call(this, credential, service, sdk);
|
|
15
16
|
return async (data, callback) => {
|
|
16
17
|
const { bucket: Bucket, localUri } = data;
|
|
@@ -26,7 +27,11 @@ module.exports = function upload(credential, service = "aws", sdk = "aws-sdk/cli
|
|
|
26
27
|
cleanup();
|
|
27
28
|
callback(err);
|
|
28
29
|
};
|
|
29
|
-
const addLog = (err) =>
|
|
30
|
+
const addLog = (err) => {
|
|
31
|
+
if (err instanceof Error) {
|
|
32
|
+
this.addLog(this.statusType.WARN, err, service, Bucket);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
30
35
|
const configBucket = admin.configBucket;
|
|
31
36
|
if (!BUCKET_SESSION.has(service + Bucket)) {
|
|
32
37
|
const bucketAcl = admin.publicRead ? "public-read" : admin.acl;
|
|
@@ -40,7 +45,9 @@ module.exports = function upload(credential, service = "aws", sdk = "aws-sdk/cli
|
|
|
40
45
|
if (configBucket && service !== "oci") {
|
|
41
46
|
const { cors: CORSConfiguration, lifecycle: LifecycleConfiguration, retentionPolicy: DefaultRetention } = configBucket;
|
|
42
47
|
const ExpectedBucketOwner = options?.ExpectedBucketOwner;
|
|
43
|
-
const commandMessage = (feature, message) =>
|
|
48
|
+
const commandMessage = (feature, message) => {
|
|
49
|
+
this.formatMessage(64, service, ["Bucket configured" + ` (${feature})`, Bucket], message || ExpectedBucketOwner, { ...Cloud[message === 'delete' ? 'LOG_CLOUD_WARN' : 'LOG_CLOUD_COMMAND'] });
|
|
50
|
+
};
|
|
44
51
|
if (service !== "ibm") {
|
|
45
52
|
if ((0, types_1.isPlainObject)(DefaultRetention)) {
|
|
46
53
|
s3.putObjectLockConfiguration({ Bucket, ObjectLockConfiguration: { ObjectLockEnabled: 'Enabled', Rule: { DefaultRetention } }, ExpectedBucketOwner, RequestPayer: options?.RequestPayer }, err => {
|
|
@@ -113,7 +120,7 @@ module.exports = function upload(credential, service = "aws", sdk = "aws-sdk/cli
|
|
|
113
120
|
.then(() => true)
|
|
114
121
|
.catch((err) => {
|
|
115
122
|
if (err instanceof Error && err.code !== 'NotFound') {
|
|
116
|
-
filename = (0,
|
|
123
|
+
filename = (0, crypto_1.randomUUID)() + path.extname(current);
|
|
117
124
|
}
|
|
118
125
|
return false;
|
|
119
126
|
});
|
|
@@ -233,4 +240,5 @@ module.exports = function upload(credential, service = "aws", sdk = "aws-sdk/cli
|
|
|
233
240
|
});
|
|
234
241
|
}
|
|
235
242
|
};
|
|
236
|
-
}
|
|
243
|
+
}
|
|
244
|
+
module.exports = upload;
|