@pi-r/aws-v3 0.6.1 → 0.6.3

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  ### @pi-r/aws-v3
2
2
 
3
+ https://e-mc.readthedocs.io/en/latest/cloud/aws-v3.html
4
+
3
5
  ### LICENSE
4
6
 
5
7
  MIT
package/client/index.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.executeBatchQuery = exports.executeQuery = exports.deleteObjectsV2 = exports.deleteObjects = exports.setBucketWebsite = exports.setBucketPolicy = exports.createBucketV2 = exports.createBucket = exports.validateDatabase = exports.validateStorage = exports.createDatabaseClient = exports.createStorageClient = void 0;
4
4
  const Lib = require("@aws-sdk/lib-dynamodb");
5
5
  const Client = require("@aws-sdk/client-dynamodb");
6
+ const Providers = require("@aws-sdk/credential-providers");
6
7
  const aws_1 = require("@pi-r/aws");
7
8
  const util_1 = require("@e-mc/cloud/util");
8
9
  const types_1 = require("@e-mc/types");
@@ -14,11 +15,14 @@ async function setCannedAcl(S3, client, Bucket, ACL, service = 'aws-v3', recursi
14
15
  case 1:
15
16
  Policy = (0, aws_1.getBucketPublicReadPolicy)(Bucket);
16
17
  break;
17
- case 'private':
18
- Policy = (0, aws_1.getPrivatePolicy)(Bucket);
18
+ case 'public-read-write':
19
+ Policy = (0, aws_1.getPublicReadPolicy)(Bucket, false, true);
20
+ break;
21
+ case 'authenticated-read':
22
+ Policy = (0, aws_1.getPublicReadPolicy)(Bucket, true);
19
23
  break;
20
24
  default:
21
- Policy = (0, aws_1.getPublicReadPolicy)(Bucket, ACL === 'authenticated-read', ACL === 'public-read-write');
25
+ Policy = (0, aws_1.getPrivatePolicy)(Bucket);
22
26
  break;
23
27
  }
24
28
  return client.send(new S3.PutBucketPolicyCommand({ Bucket, Policy }))
@@ -54,16 +58,77 @@ function sanitizeCredentials(credential) {
54
58
  }
55
59
  return result;
56
60
  }
61
+ function fromProvider(credential) {
62
+ const from = credential.provider;
63
+ let credentials = credential.credentials;
64
+ if ((0, types_1.isPlainObject)(from)) {
65
+ if (from.http) {
66
+ credentials = Providers.fromHttp((0, types_1.isPlainObject)(from.http) ? from.http : {});
67
+ }
68
+ else if ((0, types_1.isPlainObject)(from.ini)) {
69
+ credentials = Providers.fromIni(from.ini);
70
+ }
71
+ else if ((0, types_1.isPlainObject)(from.cognitoIdentity)) {
72
+ credentials = Providers.fromCognitoIdentity(from.cognitoIdentity);
73
+ }
74
+ else if ((0, types_1.isPlainObject)(from.cognitoIdentityPool)) {
75
+ credentials = Providers.fromCognitoIdentityPool(from.cognitoIdentityPool);
76
+ }
77
+ else if ((0, types_1.isPlainObject)(from.temporaryCredentials)) {
78
+ credentials = Providers.fromTemporaryCredentials(from.temporaryCredentials);
79
+ }
80
+ else if ((0, types_1.isPlainObject)(from.webToken)) {
81
+ credentials = Providers.fromWebToken(from.webToken);
82
+ }
83
+ else if ((0, types_1.isPlainObject)(from.containerMetadata)) {
84
+ credentials = Providers.fromContainerMetadata(from.containerMetadata);
85
+ }
86
+ else if ((0, types_1.isPlainObject)(from.instanceMetadata)) {
87
+ credentials = Providers.fromInstanceMetadata(from.instanceMetadata);
88
+ }
89
+ else if ((0, types_1.isPlainObject)(from.process)) {
90
+ credentials = Providers.fromProcess(from.process);
91
+ }
92
+ else if ((0, types_1.isPlainObject)(from.tokenFile)) {
93
+ credentials = Providers.fromTokenFile(from.tokenFile);
94
+ }
95
+ else if ((0, types_1.isPlainObject)(from.sso)) {
96
+ credentials = Providers.fromSSO(from.sso);
97
+ }
98
+ else if (from.env && (0, aws_1.isEnvDefined)()) {
99
+ credentials = Providers.fromEnv();
100
+ }
101
+ else if (from.ini && (0, aws_1.isSharedCredentialsDefined)()) {
102
+ credentials = Providers.fromIni();
103
+ }
104
+ if ((0, types_1.isPlainObject)(credentials)) {
105
+ if (from.nodeProviderChain) {
106
+ credentials = Providers.fromNodeProviderChain((0, types_1.isPlainObject)(from.nodeProviderChain) ? { ...credentials, ...from.nodeProviderChain } : credentials);
107
+ }
108
+ credential.credentials = credentials;
109
+ }
110
+ delete credential.provider;
111
+ }
112
+ if ((0, types_1.isPlainObject)(credentials)) {
113
+ let expiration = credentials.expiration;
114
+ if (expiration) {
115
+ if ((0, types_1.isString)(expiration)) {
116
+ expiration = new Date(expiration);
117
+ credentials.expiration = expiration;
118
+ }
119
+ if (!(expiration instanceof Date && !isNaN(expiration.getTime()))) {
120
+ delete credentials.expiration;
121
+ }
122
+ }
123
+ }
124
+ return credential;
125
+ }
57
126
  const isNoSuchBucket = (err) => err instanceof Error && err.name === 'NoSuchBucket';
58
127
  function createStorageClient(credential, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
59
128
  try {
60
129
  sanitizeCredentials(credential);
61
- const expiration = credential.credentials?.expiration;
62
- if (expiration && !(expiration instanceof Date)) {
63
- delete credential.expiration;
64
- }
65
130
  const AWS = require(sdk);
66
- return [new AWS.S3Client(credential), AWS];
131
+ return [new AWS.S3Client(fromProvider(credential)), AWS];
67
132
  }
68
133
  catch (err) {
69
134
  this.checkPackage(err, sdk.split('/')[0], { passThrough: true });
@@ -72,18 +137,17 @@ function createStorageClient(credential, service = 'aws-v3', sdk = '@aws-sdk/cli
72
137
  }
73
138
  exports.createStorageClient = createStorageClient;
74
139
  function createDatabaseClient(credential) {
75
- return [Lib.DynamoDBDocumentClient.from(new Client.DynamoDBClient(credential), credential.translateConfig), Lib];
140
+ return [Lib.DynamoDBDocumentClient.from(new Client.DynamoDBClient(fromProvider(credential)), credential.translateConfig), Lib];
76
141
  }
77
142
  exports.createDatabaseClient = createDatabaseClient;
78
143
  function validateStorage(credential) {
79
144
  const credentials = sanitizeCredentials(credential);
80
145
  const profile = credential.profile;
81
- if (profile || !credentials && process.env.AWS_SDK_LOAD_CONFIG === '1') {
82
- const { fromIni } = require('@aws-sdk/credential-provider-ini');
83
- credential.credentials = fromIni({ profile });
146
+ if (profile || !credentials && !credential.provider && (0, aws_1.isSharedCredentialsDefined)()) {
147
+ credential.credentials = Providers.fromIni({ profile });
84
148
  return true;
85
149
  }
86
- return !!credentials && (typeof credentials === 'function' || (0, aws_1.isAccessDefined)(credentials) || (0, aws_1.isEnvDefined)());
150
+ return !!credentials && (typeof credentials === 'function' || (0, aws_1.isAccessDefined)(credentials)) || (0, types_1.isPlainObject)(credential.provider) || (0, aws_1.isEnvDefined)();
87
151
  }
88
152
  exports.validateStorage = validateStorage;
89
153
  function validateDatabase(credential, data) {
@@ -107,7 +171,7 @@ async function createBucketV2(credential, Bucket, ACL, options, service = 'aws-v
107
171
  .catch(async () => {
108
172
  const input = { ...options, Bucket };
109
173
  const region = credential.region;
110
- if (typeof region === 'string' && region !== 'us-east-1') {
174
+ if ((0, types_1.isString)(region) && region !== 'us-east-1') {
111
175
  input.CreateBucketConfiguration || (input.CreateBucketConfiguration = { LocationConstraint: region });
112
176
  }
113
177
  return client.send(new AWS.CreateBucketCommand(input))
@@ -247,7 +311,7 @@ async function executeBatchQuery(credential, batch, sessionKey) {
247
311
  }
248
312
  }
249
313
  const [db, AWS] = createClient();
250
- const Key = (0, types_1.isPlainObject)(key) ? key : { [key]: (0, aws_1.parseAttributeValue)(id) };
314
+ const Key = (0, types_1.isPlainObject)(key) ? key : { [key]: id };
251
315
  const command = { TableName, Key };
252
316
  if (update) {
253
317
  await db.send(new AWS.UpdateCommand({ ...command, ...update }));
@@ -261,13 +325,13 @@ async function executeBatchQuery(credential, batch, sessionKey) {
261
325
  if (TableName) {
262
326
  query.TableName = TableName;
263
327
  }
264
- if (limit > 0) {
265
- query.Limit = limit;
266
- }
267
328
  if (!query.TableName) {
268
329
  closeClient();
269
330
  throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
270
331
  }
332
+ if (limit > 0) {
333
+ query.Limit = limit;
334
+ }
271
335
  if (useCache && (rows = getCache(queryString = Module.asString(query, true)))) {
272
336
  result[i] = rows;
273
337
  continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/aws-v3",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "description": "AWS V3 cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "publishConfig": {
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/anpham6/pi-r.git",
11
+ "url": "git+https://github.com/anpham6/pi-r.git",
12
12
  "directory": "src/cloud/aws-v3"
13
13
  },
14
14
  "keywords": [
@@ -20,12 +20,13 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
22
  "dependencies": {
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"
23
+ "@e-mc/cloud": "^0.8.3",
24
+ "@e-mc/module": "^0.8.3",
25
+ "@e-mc/types": "^0.8.3",
26
+ "@pi-r/aws": "^0.6.3",
27
+ "@aws-sdk/client-dynamodb": "^3.501.0",
28
+ "@aws-sdk/client-s3": "^3.501.0",
29
+ "@aws-sdk/credential-providers": "^3.501.0",
30
+ "@aws-sdk/lib-dynamodb": "^3.501.0"
30
31
  }
31
32
  }
package/upload/index.js CHANGED
@@ -134,35 +134,37 @@ function upload(credential, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
134
134
  client.send(new AWS.PutObjectCommand(params), { abortSignal: this.signal })
135
135
  .then(() => {
136
136
  const url = endpoint ? Module.joinPath(endpoint, objectKey) : Module.joinPath(`https://${Bucket}.s3.${!credential.region || credential.region === 'us-east-1' ? 'us-east-1.' : ''}amazonaws.com`, objectKey);
137
- if (first) {
138
- if ((0, types_1.isPlainObject)(tags)) {
139
- const TagSet = [];
140
- for (const name in tags) {
141
- TagSet.push({ Key: name, Value: tags[name] });
142
- }
143
- client.send(new AWS.PutObjectTaggingCommand({ Bucket, Key: params.Key, ExpectedBucketOwner: params.ExpectedBucketOwner, Tagging: { TagSet }, RequestPayer: params.RequestPayer }), error => {
144
- if (!error) {
145
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags created" /* VAL_CLOUD.CREATE_TAG */, Bucket], params.Key, { ...Cloud.LOG_CLOUD_COMMAND });
146
- }
147
- else {
148
- addLog(error);
149
- }
150
- });
151
- }
152
- else if (tags === false) {
153
- client.send(new AWS.DeleteObjectTaggingCommand({ Bucket, Key: params.Key, ExpectedBucketOwner: params.ExpectedBucketOwner }), error => {
154
- if (!error) {
155
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags deleted" /* VAL_CLOUD.DELETE_TAG */, Bucket], params.Key, { ...Cloud.LOG_CLOUD_COMMAND });
156
- }
157
- else {
158
- addLog(error);
159
- }
160
- });
137
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Upload success" /* VAL_CLOUD.UPLOAD_FILE */, url, { ...Cloud.LOG_CLOUD_UPLOAD });
138
+ if (!first) {
139
+ return;
140
+ }
141
+ let length = -1;
142
+ if ((0, types_1.isPlainObject)(tags) && (length = Object.keys(tags).length) > 0) {
143
+ const TagSet = [];
144
+ for (const name in tags) {
145
+ TagSet.push({ Key: name, Value: tags[name] });
161
146
  }
162
- cleanup();
163
- callback(null, url);
147
+ client.send(new AWS.PutObjectTaggingCommand({ Bucket, Key: params.Key, ExpectedBucketOwner: params.ExpectedBucketOwner, Tagging: { TagSet }, RequestPayer: params.RequestPayer }), error => {
148
+ if (!error) {
149
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags created" /* VAL_CLOUD.CREATE_TAG */, Bucket], params.Key, { ...Cloud.LOG_CLOUD_COMMAND });
150
+ }
151
+ else {
152
+ addLog(error);
153
+ }
154
+ });
164
155
  }
165
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Upload success" /* VAL_CLOUD.UPLOAD_FILE */, url, { ...Cloud.LOG_CLOUD_UPLOAD });
156
+ else if (tags === false || length === 0) {
157
+ client.send(new AWS.DeleteObjectTaggingCommand({ Bucket, Key: params.Key, ExpectedBucketOwner: params.ExpectedBucketOwner }), error => {
158
+ if (!error) {
159
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags deleted" /* VAL_CLOUD.DELETE_TAG */, Bucket], params.Key, { ...Cloud.LOG_CLOUD_COMMAND });
160
+ }
161
+ else {
162
+ addLog(error);
163
+ }
164
+ });
165
+ }
166
+ cleanup();
167
+ callback(null, url);
166
168
  })
167
169
  .catch(err => {
168
170
  if (first) {