@pi-r/aws 0.6.1 → 0.6.2

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
2
2
 
3
+ https://e-mc.readthedocs.io/en/latest/cloud/aws.html
4
+
3
5
  ### LICENSE
4
6
 
5
7
  MIT
package/client/index.d.ts CHANGED
@@ -13,6 +13,8 @@ import type { CreateBucketRequest } from 'aws-sdk/clients/s3';
13
13
  declare namespace AWS {
14
14
  function isAccessDefined(credential: Pick<ConfigurationOptions, "accessKeyId" | "secretAccessKey" | "sessionToken">): boolean;
15
15
  function isEnvDefined(): boolean;
16
+ function isSharedCredentialsDefined(): boolean;
17
+ function isProviderChainDefined(): boolean;
16
18
  function isDatabaseDefined(credential: AWSDatabaseCredential, data: CloudDatabase): boolean;
17
19
  function getPublicReadPolicy(bucket: string, authenticated?: boolean, write?: boolean): string;
18
20
  function getBucketPublicReadPolicy(bucket: string): string;
package/client/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isNoSuchBucket = exports.getBucketKey = exports.parseAttributeValue = exports.writeMessageDefaultRetention = exports.checkBucketCannedACL = exports.setDatabaseEndpoint = exports.executeBatchQuery = exports.executeQuery = exports.deleteObjectsV2 = exports.deleteObjects = exports.setBucketWebsite = exports.setBucketPolicy = exports.createBucketV2 = exports.createBucket = exports.createDatabaseClient = exports.createStorageClient = exports.validateDatabase = exports.validateStorage = exports.getPrivatePolicy = exports.getBucketPublicReadPolicy = exports.getPublicReadPolicy = exports.isDatabaseDefined = exports.isEnvDefined = exports.isAccessDefined = void 0;
3
+ exports.isNoSuchBucket = exports.getBucketKey = exports.parseAttributeValue = exports.writeMessageDefaultRetention = exports.checkBucketCannedACL = exports.setDatabaseEndpoint = exports.executeBatchQuery = exports.executeQuery = exports.deleteObjectsV2 = exports.deleteObjects = exports.setBucketWebsite = exports.setBucketPolicy = exports.createBucketV2 = exports.createBucket = exports.createDatabaseClient = exports.createStorageClient = exports.validateDatabase = exports.validateStorage = exports.getPrivatePolicy = exports.getBucketPublicReadPolicy = exports.getPublicReadPolicy = exports.isDatabaseDefined = exports.isProviderChainDefined = exports.isEnvDefined = exports.isSharedCredentialsDefined = exports.isAccessDefined = void 0;
4
4
  const aws = require("aws-sdk");
5
5
  const types_1 = require("@e-mc/types");
6
6
  const util_1 = require("@e-mc/cloud/util");
@@ -62,12 +62,20 @@ function isAccessDefined(credential) {
62
62
  return !!(credential.accessKeyId && credential.secretAccessKey || credential.sessionToken);
63
63
  }
64
64
  exports.isAccessDefined = isAccessDefined;
65
+ function isSharedCredentialsDefined() {
66
+ return !!process.env.AWS_SDK_LOAD_CONFIG;
67
+ }
68
+ exports.isSharedCredentialsDefined = isSharedCredentialsDefined;
65
69
  function isEnvDefined() {
66
- return !!(process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY || process.env.AWS_SESSION_TOKEN);
70
+ return !!(process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY);
67
71
  }
68
72
  exports.isEnvDefined = isEnvDefined;
73
+ function isProviderChainDefined() {
74
+ return isSharedCredentialsDefined() || !!(process.env.AWS_WEB_IDENTITY_TOKEN_FILE || process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI || process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI);
75
+ }
76
+ exports.isProviderChainDefined = isProviderChainDefined;
69
77
  function isDatabaseDefined(credential, data) {
70
- return !!(data.table && (credential.region || credential.endpoint || process.env.AWS_DEFAULT_REGION));
78
+ return !!(data.table && (credential.region || credential.endpoint || process.env.AWS_DEFAULT_REGION || process.env.AWS_REGION));
71
79
  }
72
80
  exports.isDatabaseDefined = isDatabaseDefined;
73
81
  function getPublicReadPolicy(bucket, authenticated, write) {
@@ -114,7 +122,7 @@ function getPrivatePolicy(bucket) {
114
122
  }
115
123
  exports.getPrivatePolicy = getPrivatePolicy;
116
124
  function validateStorage(credential) {
117
- return !!(isAccessDefined(credential) || isEnvDefined() || credential.fromPath || credential.profile || process.env.AWS_SDK_LOAD_CONFIG === '1');
125
+ return !!(isAccessDefined(credential) || isEnvDefined() || credential.fromPath || credential.profile || isProviderChainDefined());
118
126
  }
119
127
  exports.validateStorage = validateStorage;
120
128
  function validateDatabase(credential, data) {
@@ -130,7 +138,7 @@ function createStorageClient(credential, service = 'aws', sdk = 'aws-sdk/clients
130
138
  client.config.loadFromPath(fromPath);
131
139
  return client;
132
140
  }
133
- if (profile || process.env.AWS_SDK_LOAD_CONFIG === '1' && !isAccessDefined(credential) && !isEnvDefined()) {
141
+ if (profile || isSharedCredentialsDefined() && !isAccessDefined(credential) && !isEnvDefined()) {
134
142
  credential = new aws.SharedIniFileCredentials({ profile });
135
143
  }
136
144
  return new aws.S3(credential);
@@ -149,7 +157,7 @@ function createDatabaseClient(credential) {
149
157
  if (fromPath) {
150
158
  aws.config.loadFromPath(fromPath);
151
159
  }
152
- else if (profile || process.env.AWS_SDK_LOAD_CONFIG === '1' && !isAccessDefined(credential) && !isEnvDefined()) {
160
+ else if (profile || isSharedCredentialsDefined() && !isAccessDefined(credential) && !isEnvDefined()) {
153
161
  options = new aws.SharedIniFileCredentials({ profile });
154
162
  }
155
163
  else {
@@ -330,12 +338,12 @@ async function executeBatchQuery(credential, batch, sessionKey) {
330
338
  if (TableName) {
331
339
  query.TableName = TableName;
332
340
  }
333
- if (limit > 0) {
334
- query.Limit = limit;
335
- }
336
341
  if (!TableName) {
337
342
  throw (0, util_1.formatError)(item, "Missing database table" /* ERR_DB.TABLE */);
338
343
  }
344
+ if (limit > 0) {
345
+ query.Limit = limit;
346
+ }
339
347
  if (useCache && (rows = getCache(queryString = Module.asString(query, true)))) {
340
348
  result[i] = rows;
341
349
  continue;
@@ -394,9 +402,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
394
402
  exports.executeBatchQuery = executeBatchQuery;
395
403
  function setDatabaseEndpoint(config) {
396
404
  let region = config.region;
397
- config.endpoint || (config.endpoint = `https://dynamodb.${(0, types_1.isString)(region) ? region : process.env.AWS_DEFAULT_REGION || 'us-east-1'}.amazonaws.com`);
398
- if (!region && (0, types_1.isString)(config.endpoint)) {
399
- region = (/dynamodb\.([^.]+)\./i.exec(config.endpoint)?.[1] || process.env.AWS_DEFAULT_REGION)?.toLowerCase();
405
+ const endpoint = config.endpoint || (config.endpoint = `https://dynamodb.${(0, types_1.isString)(region) ? region : process.env.AWS_DEFAULT_REGION || process.env.AWS_REGION || 'us-east-1'}.amazonaws.com`);
406
+ if (!region) {
407
+ region = ((0, types_1.isString)(endpoint) && /\bdynamodb\.([^.]+)\./i.exec(endpoint)?.[1] || process.env.AWS_DEFAULT_REGION || process.env.AWS_REGION)?.toLowerCase();
400
408
  if (region && region !== 'us-east-1') {
401
409
  config.region = region;
402
410
  }
@@ -425,7 +433,7 @@ function writeMessageDefaultRetention(bucket, retention, service = 'aws') {
425
433
  if (Mode) {
426
434
  status.push(status.length ? `(${Mode})` : Mode);
427
435
  }
428
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket configured" /* VAL_CLOUD.CONFIGURE_BUCKET */ + ' (retention policy)', bucket], status.join(' '), { ...Cloud.LOG_CLOUD_COMMAND });
436
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket configured" /* VAL_CLOUD.CONFIGURE_BUCKET */ + ' (Retention Policy)', bucket], status.join(' '), { ...Cloud.LOG_CLOUD_COMMAND });
429
437
  }
430
438
  exports.writeMessageDefaultRetention = writeMessageDefaultRetention;
431
439
  function parseAttributeValue(value) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/aws",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "AWS V2 cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "types": "client/index.d.ts",
@@ -21,9 +21,9 @@
21
21
  "license": "MIT",
22
22
  "homepage": "https://github.com/anpham6/pi-r#readme",
23
23
  "dependencies": {
24
- "@e-mc/cloud": "^0.8.1",
25
- "@e-mc/module": "^0.8.1",
26
- "@e-mc/types": "^0.8.1",
27
- "aws-sdk": "^2.1531.0"
24
+ "@e-mc/cloud": "^0.8.2",
25
+ "@e-mc/module": "^0.8.2",
26
+ "@e-mc/types": "^0.8.2",
27
+ "aws-sdk": "^2.1545.0"
28
28
  }
29
29
  }
package/types/index.d.ts CHANGED
@@ -3,7 +3,7 @@ import type { CloudDatabase } from '@e-mc/types/lib/cloud';
3
3
 
4
4
  import type { ConfigurationOptions } from 'aws-sdk/lib/core';
5
5
  import type { ServiceConfigurationOptions } from 'aws-sdk/lib/service';
6
- import type { Key, BatchGetItemInput, QueryInput, ScanInput, UpdateItemInput } from 'aws-sdk/clients/dynamodb';
6
+ import type { BatchGetItemInput, Key, QueryInput, ScanInput, UpdateItemInput } from 'aws-sdk/clients/dynamodb';
7
7
  import type { PutBucketAclRequest, PutBucketPolicyRequest, PutPublicAccessBlockRequest } from 'aws-sdk/clients/s3';
8
8
 
9
9
  export type BucketCannedACL = "authenticated-read" | "private" | "public-read" | "public-read-write";
package/upload/index.js CHANGED
@@ -36,7 +36,7 @@ function upload(credential, service = 'aws', sdk = 'aws-sdk/clients/s3') {
36
36
  }
37
37
  BUCKET_SESSION.add(service + Bucket);
38
38
  }
39
- if (service !== 'oci') {
39
+ if (service !== 'ibm' && service !== 'oci') {
40
40
  const DefaultRetention = configBucket?.retentionPolicy;
41
41
  if ((0, types_1.isPlainObject)(DefaultRetention)) {
42
42
  s3.putObjectLockConfiguration({ Bucket, ObjectLockConfiguration: { ObjectLockEnabled: 'Enabled', Rule: { DefaultRetention } }, ExpectedBucketOwner: options?.ExpectedBucketOwner, RequestPayer: options?.RequestPayer }, err => {
@@ -119,46 +119,49 @@ function upload(credential, service = 'aws', sdk = 'aws-sdk/clients/s3') {
119
119
  }
120
120
  }
121
121
  s3.upload(params, (err, result) => {
122
- if (!err) {
123
- const url = endpoint ? Module.joinPath(endpoint, result.Key) : result.Location;
124
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Upload success" /* VAL_CLOUD.UPLOAD_FILE */, url, { ...Cloud.LOG_CLOUD_UPLOAD });
122
+ if (err) {
125
123
  if (first) {
126
- if (service !== 'oci') {
127
- if ((0, types_1.isPlainObject)(tags)) {
128
- const TagSet = [];
129
- for (const name in tags) {
130
- TagSet.push({ Key: name, Value: tags[name] });
131
- }
132
- s3.putObjectTagging({ Bucket, Key: result.Key, Tagging: { TagSet }, ExpectedBucketOwner: params.ExpectedBucketOwner, RequestPayer: params.RequestPayer }, error => {
133
- if (!error) {
134
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags created" /* VAL_CLOUD.CREATE_TAG */, Bucket], result.Key, { ...Cloud.LOG_CLOUD_COMMAND });
135
- }
136
- else {
137
- addLog(error);
138
- }
139
- });
140
- }
141
- else if (tags === false) {
142
- s3.deleteObjectTagging({ Bucket, Key: result.Key, ExpectedBucketOwner: params.ExpectedBucketOwner }, error => {
143
- if (!error) {
144
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags deleted" /* VAL_CLOUD.DELETE_TAG */, Bucket], result.Key, { ...Cloud.LOG_CLOUD_COMMAND });
145
- }
146
- else {
147
- addLog(error);
148
- }
149
- });
150
- }
151
- }
152
- cleanup();
153
- callback(null, url);
124
+ errorResponse(err);
154
125
  }
126
+ else {
127
+ addLog(err);
128
+ }
129
+ return;
155
130
  }
156
- else if (first) {
157
- errorResponse(err);
131
+ const url = endpoint ? Module.joinPath(endpoint, result.Key) : result.Location;
132
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, "Upload success" /* VAL_CLOUD.UPLOAD_FILE */, url, { ...Cloud.LOG_CLOUD_UPLOAD });
133
+ if (!first) {
134
+ return;
158
135
  }
159
- else {
160
- addLog(err);
136
+ if (service !== 'oci') {
137
+ let length = -1;
138
+ if ((0, types_1.isPlainObject)(tags) && (length = Object.keys(tags).length) > 0) {
139
+ const TagSet = [];
140
+ for (const name in tags) {
141
+ TagSet.push({ Key: name, Value: tags[name] });
142
+ }
143
+ s3.putObjectTagging({ Bucket, Key: result.Key, Tagging: { TagSet }, ExpectedBucketOwner: params.ExpectedBucketOwner, RequestPayer: params.RequestPayer }, error => {
144
+ if (!error) {
145
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags created" /* VAL_CLOUD.CREATE_TAG */, Bucket], result.Key, { ...Cloud.LOG_CLOUD_COMMAND });
146
+ }
147
+ else {
148
+ addLog(error);
149
+ }
150
+ });
151
+ }
152
+ else if (tags === false || length === 0) {
153
+ s3.deleteObjectTagging({ Bucket, Key: result.Key, ExpectedBucketOwner: params.ExpectedBucketOwner }, error => {
154
+ if (!error) {
155
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags deleted" /* VAL_CLOUD.DELETE_TAG */, Bucket], result.Key, { ...Cloud.LOG_CLOUD_COMMAND });
156
+ }
157
+ else {
158
+ addLog(error);
159
+ }
160
+ });
161
+ }
161
162
  }
163
+ cleanup();
164
+ callback(null, url);
162
165
  });
163
166
  }
164
167
  };