@pi-r/aws-v3 0.2.2 → 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 CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  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
+ const Lib = require("@aws-sdk/lib-dynamodb");
5
+ const Client = require("@aws-sdk/client-dynamodb");
4
6
  const aws_1 = require("@pi-r/aws");
5
7
  const util_1 = require("@e-mc/cloud/util");
6
8
  const types_1 = require("@e-mc/types");
@@ -70,22 +72,15 @@ function createStorageClient(credential, service = 'aws-v3', sdk = '@aws-sdk/cli
70
72
  }
71
73
  exports.createStorageClient = createStorageClient;
72
74
  function createDatabaseClient(credential) {
73
- try {
74
- const AWS = require('@aws-sdk/lib-dynamodb');
75
- const { DynamoDBClient } = require('@aws-sdk/client-dynamodb');
76
- return [AWS.DynamoDBDocumentClient.from(new DynamoDBClient(credential), credential.translateConfig), AWS];
77
- }
78
- catch (err) {
79
- this.checkPackage(err, '@aws-sdk', { passThrough: true });
80
- throw err;
81
- }
75
+ return [Lib.DynamoDBDocumentClient.from(new Client.DynamoDBClient(credential), credential.translateConfig), Lib];
82
76
  }
83
77
  exports.createDatabaseClient = createDatabaseClient;
84
78
  function validateStorage(credential) {
85
79
  const credentials = sanitizeCredentials(credential);
86
- if (credential.profile || !credentials && process.env.AWS_SDK_LOAD_CONFIG === '1') {
80
+ const profile = credential.profile;
81
+ if (profile || !credentials && process.env.AWS_SDK_LOAD_CONFIG === '1') {
87
82
  const { fromIni } = require('@aws-sdk/credential-provider-ini');
88
- credential.credentials = fromIni({ profile: credential.profile });
83
+ credential.credentials = fromIni({ profile });
89
84
  return true;
90
85
  }
91
86
  return !!credentials && (typeof credentials === 'function' || (0, aws_1.isAccessDefined)(credentials) || (0, aws_1.isEnvDefined)());
@@ -112,8 +107,8 @@ function createBucketV2(credential, Bucket, ACL, options, service = 'aws-v3', sd
112
107
  .catch(() => {
113
108
  const input = { ...options, Bucket };
114
109
  const region = credential.region;
115
- if (!input.CreateBucketConfiguration && typeof region === 'string' && region !== 'us-east-1') {
116
- input.CreateBucketConfiguration = { LocationConstraint: region };
110
+ if (typeof region === 'string' && region !== 'us-east-1') {
111
+ input.CreateBucketConfiguration || (input.CreateBucketConfiguration = { LocationConstraint: region });
117
112
  }
118
113
  return client.send(new AWS.CreateBucketCommand(input))
119
114
  .then(async () => {
@@ -141,11 +136,12 @@ function createBucketV2(credential, Bucket, ACL, options, service = 'aws-v3', sd
141
136
  exports.createBucketV2 = createBucketV2;
142
137
  function setBucketPolicy(credential, Bucket, options, service = 'aws', sdk = 'aws-sdk/clients/s3') {
143
138
  const [client, AWS] = createStorageClient.call(this, credential, service, sdk);
144
- const policy = 'Policy' in options && !('ACL' in options);
139
+ const block = 'PublicAccessBlockConfiguration' in options;
140
+ const policy = !block && 'Policy' in options && !('ACL' in options);
145
141
  options.Bucket = Bucket;
146
- return (policy ? client.send(new AWS.PutBucketPolicyCommand(options)) : client.send(new AWS.PutBucketAclCommand(options)))
142
+ return (block ? client.send(new AWS.PutPublicAccessBlockCommand(options)) : policy ? client.send(new AWS.PutBucketPolicyCommand(options)) : client.send(new AWS.PutBucketAclCommand(options)))
147
143
  .then(() => {
148
- this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, [policy ? "Bucket policy configured" /* VAL_CLOUD.POLICY_BUCKET */ : "Bucket ACL configured" /* VAL_CLOUD.ACL_BUCKET */, Bucket], '', { ...Cloud.LOG_CLOUD_COMMAND });
144
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, [block ? "Bucket configured" /* VAL_CLOUD.CONFIGURE_BUCKET */ : policy ? "Bucket policy configured" /* VAL_CLOUD.POLICY_BUCKET */ : "Bucket ACL configured" /* VAL_CLOUD.ACL_BUCKET */, Bucket], '', { ...Cloud.LOG_CLOUD_COMMAND });
149
145
  return true;
150
146
  })
151
147
  .catch(err => {
@@ -159,11 +155,12 @@ exports.setBucketPolicy = setBucketPolicy;
159
155
  function setBucketWebsite(credential, Bucket, options, service = 'aws', sdk = 'aws-sdk/clients/s3') {
160
156
  const [client, AWS] = createStorageClient.call(this, credential, service, sdk);
161
157
  const WebsiteConfiguration = {};
162
- if ((0, types_1.isString)(options.indexPage)) {
163
- WebsiteConfiguration.IndexDocument = { Suffix: options.indexPage };
158
+ const { indexPage: Suffix, errorPage: Key } = options;
159
+ if ((0, types_1.isString)(Suffix)) {
160
+ WebsiteConfiguration.IndexDocument = { Suffix };
164
161
  }
165
- if ((0, types_1.isString)(options.errorPage)) {
166
- WebsiteConfiguration.ErrorDocument = { Key: options.errorPage };
162
+ if ((0, types_1.isString)(Key)) {
163
+ WebsiteConfiguration.ErrorDocument = { Key };
167
164
  }
168
165
  return client.send(new AWS.PutBucketWebsiteCommand({ Bucket, WebsiteConfiguration }))
169
166
  .then(() => {
@@ -193,8 +190,9 @@ async function deleteObjectsV2(credential, Bucket, recursive = true, service = '
193
190
  }
194
191
  return client.send(new AWS.DeleteObjectsCommand({ Bucket, Delete: { Objects } }))
195
192
  .then(data => {
196
- if ((0, types_1.isArray)(data.Deleted)) {
197
- const files = data.Deleted.length + ' files';
193
+ const Deleted = data.Deleted;
194
+ if ((0, types_1.isArray)(Deleted)) {
195
+ const files = Deleted.length + ' files';
198
196
  this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Bucket emptied" /* VAL_CLOUD.EMPTY_BUCKET */ + ` (${recursive ? 'recursive' : files})`, Bucket], recursive ? files : '', { ...Cloud.LOG_CLOUD_COMMAND });
199
197
  }
200
198
  })
@@ -253,9 +251,9 @@ async function executeBatchQuery(credential, batch, sessionKey) {
253
251
  if (update) {
254
252
  await db.send(new AWS.UpdateCommand({ ...command, ...update }));
255
253
  }
256
- const output = await db.send(new AWS.GetCommand(command));
257
- if (output.Item) {
258
- rows = [output.Item];
254
+ const Item = (await db.send(new AWS.GetCommand(command))).Item;
255
+ if (Item) {
256
+ rows = [Item];
259
257
  }
260
258
  }
261
259
  else if ((0, types_1.isPlainObject)(query)) {
package/download/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const client_1 = require("../client");
4
3
  const util_1 = require("@e-mc/cloud/util");
5
4
  const types_1 = require("@e-mc/types");
6
5
  const Module = require("@e-mc/module");
7
6
  const Cloud = require("@e-mc/cloud");
7
+ const client_1 = require("../client");
8
8
  function download(config, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
9
9
  const [client, AWS] = client_1.createStorageClient.call(this, config, service, sdk);
10
10
  return (data, callback) => {
@@ -16,9 +16,7 @@ function download(config, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
16
16
  }
17
17
  const location = Module.joinPath(Bucket, Key);
18
18
  let input = { Bucket, Key, VersionId: target.versionId };
19
- // @ts-ignore
20
- client
21
- .send(new AWS.GetObjectCommand(input), { abortSignal: this.signal })
19
+ client.send(new AWS.GetObjectCommand(input), { abortSignal: this.signal })
22
20
  .then(result => {
23
21
  (0, util_1.readableAsBuffer)(result.Body).then(buffer => callback(null, buffer)).catch(err => callback(err));
24
22
  const deleteObject = target.deleteObject;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/aws-v3",
3
- "version": "0.2.2",
3
+ "version": "0.5.0",
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.5.3",
24
- "@e-mc/module": "^0.5.3",
25
- "@e-mc/types": "^0.5.3",
26
- "@pi-r/aws": "^0.2.2",
27
- "@aws-sdk/client-dynamodb": "^3.335.0",
28
- "@aws-sdk/client-s3": "^3.335.0",
29
- "@aws-sdk/lib-dynamodb": "^3.335.0"
23
+ "@e-mc/cloud": "^0.7.0",
24
+ "@e-mc/module": "^0.7.0",
25
+ "@e-mc/types": "^0.7.0",
26
+ "@pi-r/aws": "^0.5.0",
27
+ "@aws-sdk/client-dynamodb": "^3.468.0",
28
+ "@aws-sdk/client-s3": "^3.468.0",
29
+ "@aws-sdk/lib-dynamodb": "^3.468.0"
30
30
  }
31
31
  }
package/upload/index.js CHANGED
@@ -3,12 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const path = require("path");
4
4
  const fs = require("fs");
5
5
  const stream = require("stream");
6
- const client_1 = require("../client");
7
6
  const util_1 = require("@e-mc/cloud/util");
8
7
  const aws_1 = require("@pi-r/aws");
9
8
  const types_1 = require("@e-mc/types");
10
9
  const Module = require("@e-mc/module");
11
10
  const Cloud = require("@e-mc/cloud");
11
+ const client_1 = require("../client");
12
12
  const BUCKET_SESSION = new Set();
13
13
  const BUCKET_RESPONSE = {};
14
14
  function upload(credential, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
@@ -16,7 +16,7 @@ function upload(credential, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
16
16
  return async (data, callback) => {
17
17
  var _a;
18
18
  const { bucket: Bucket, localUri } = data;
19
- const { pathname = '', fileGroup, contentType, metadata, endpoint, active, publicRead, acl, admin = {}, overwrite, options } = data.upload;
19
+ const { pathname = '', fileGroup, contentType, metadata, tags, endpoint, active, publicRead, acl, admin = {}, overwrite, options } = data.upload;
20
20
  let filename = data.upload.filename || path.basename(localUri), bucketKey;
21
21
  const cleanup = () => {
22
22
  BUCKET_SESSION.delete(service + Bucket);
@@ -29,15 +29,25 @@ function upload(credential, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
29
29
  callback(err);
30
30
  return false;
31
31
  };
32
+ const addLog = (err) => err instanceof Error && this.addLog(this.statusType.WARN, err.message, service + ': ' + Bucket);
33
+ const configBucket = admin.configBucket;
32
34
  if (!BUCKET_SESSION.has(service + Bucket)) {
33
35
  const bucketAcl = admin.publicRead ? 'public-read' : admin.acl;
34
- const response = BUCKET_RESPONSE[_a = bucketKey = (0, aws_1.getBucketKey)(credential, Bucket, bucketAcl, service, sdk)] || (BUCKET_RESPONSE[_a] = client_1.createBucketV2.call(this, credential, Bucket, bucketAcl, admin.configBucket?.create, service, sdk));
36
+ const response = BUCKET_RESPONSE[_a = bucketKey = (0, aws_1.getBucketKey)(credential, Bucket, bucketAcl, service, sdk)] || (BUCKET_RESPONSE[_a] = client_1.createBucketV2.call(this, credential, Bucket, bucketAcl, configBucket?.create, service, sdk));
35
37
  if (!await response) {
36
38
  errorResponse(null);
37
39
  return;
38
40
  }
39
41
  BUCKET_SESSION.add(service + Bucket);
40
42
  }
43
+ const DefaultRetention = configBucket?.retentionPolicy;
44
+ if ((0, types_1.isPlainObject)(DefaultRetention)) {
45
+ client.send(new AWS.PutObjectLockConfigurationCommand({ Bucket, ObjectLockConfiguration: { ObjectLockEnabled: 'Enabled', Rule: { DefaultRetention } }, ExpectedBucketOwner: options?.ExpectedBucketOwner, RequestPayer: options?.RequestPayer }))
46
+ .then(() => {
47
+ aws_1.writeMessageDefaultRetention.call(this, Bucket, DefaultRetention, service);
48
+ })
49
+ .catch(err => addLog(err));
50
+ }
41
51
  if (!overwrite) {
42
52
  const current = filename;
43
53
  const next = (0, util_1.generateFilename)(filename);
@@ -69,7 +79,6 @@ function upload(credential, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
69
79
  const Key = [filename];
70
80
  const Body = [data.buffer];
71
81
  const ContentType = [contentType];
72
- const addLog = (err) => err instanceof Error && this.addLog(this.statusType.WARN, err.message, service + ': ' + Bucket);
73
82
  if (fileGroup) {
74
83
  for (const [content, ext, localFile] of fileGroup) {
75
84
  try {
@@ -128,11 +137,34 @@ function upload(credential, service = 'aws-v3', sdk = '@aws-sdk/client-s3') {
128
137
  }
129
138
  continue;
130
139
  }
131
- // @ts-ignore
132
140
  client.send(new AWS.PutObjectCommand(params), { abortSignal: this.signal })
133
141
  .then(() => {
134
142
  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);
135
143
  if (first) {
144
+ if ((0, types_1.isPlainObject)(tags)) {
145
+ const TagSet = [];
146
+ for (const name in tags) {
147
+ TagSet.push({ Key: name, Value: tags[name] });
148
+ }
149
+ client.send(new AWS.PutObjectTaggingCommand({ Bucket, Key: params.Key, ExpectedBucketOwner: params.ExpectedBucketOwner, Tagging: { TagSet }, RequestPayer: params.RequestPayer }), error => {
150
+ if (!error) {
151
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags created" /* VAL_CLOUD.CREATE_TAG */, Bucket], params.Key, { ...Cloud.LOG_CLOUD_COMMAND });
152
+ }
153
+ else {
154
+ addLog(error);
155
+ }
156
+ });
157
+ }
158
+ else if (tags === false) {
159
+ client.send(new AWS.DeleteObjectTaggingCommand({ Bucket, Key: params.Key, ExpectedBucketOwner: params.ExpectedBucketOwner }), error => {
160
+ if (!error) {
161
+ this.formatMessage(64 /* LOG_TYPE.CLOUD */, service, ["Tags deleted" /* VAL_CLOUD.DELETE_TAG */, Bucket], params.Key, { ...Cloud.LOG_CLOUD_COMMAND });
162
+ }
163
+ else {
164
+ addLog(error);
165
+ }
166
+ });
167
+ }
136
168
  cleanup();
137
169
  callback(null, url);
138
170
  }