@pi-r/aws 0.8.3 → 0.9.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/LICENSE CHANGED
@@ -1,7 +1,7 @@
1
- Copyright 2024 An Pham
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
-
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
-
7
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ Copyright 2024 An Pham
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -4,4 +4,4 @@ https://e-mc.readthedocs.io/en/latest/cloud/aws.html
4
4
 
5
5
  ### LICENSE
6
6
 
7
- MIT
7
+ MIT
package/client/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ICloud, IModule } from '@e-mc/types/lib';
2
- import type { BucketWebsiteOptions, CloudDatabase } from '@e-mc/types/lib/cloud';
2
+ import type { BucketWebsiteOptions, CloudDatabase, RecursiveAction } from '@e-mc/types/lib/cloud';
3
3
  import type { BatchQueryResult, QueryResult } from '@e-mc/types/lib/db';
4
4
 
5
5
  import type { BucketCannedACL } from '@pi-r/aws-lib';
@@ -7,9 +7,11 @@ import type { BucketCannedACL } from '@pi-r/aws-lib';
7
7
  import type { AWSDatabaseCredential, AWSDatabaseQuery, AWSStorageCredential, ConfigureBucketOptions } from '../types';
8
8
 
9
9
  import type { AttributeValue, DocumentClient } from 'aws-sdk/clients/dynamodb';
10
- import type { CreateBucketRequest } from 'aws-sdk/clients/s3';
10
+ import type { CreateBucketRequest, ListObjectsRequest as IListObjectsRequest } from 'aws-sdk/clients/s3';
11
11
 
12
12
  declare namespace AWS {
13
+ type ListObjectsRequest = IListObjectsRequest & RecursiveAction;
14
+
13
15
  function validateStorage(credential: AWSStorageCredential): boolean;
14
16
  function validateDatabase(credential: AWSDatabaseCredential, data: CloudDatabase): boolean;
15
17
  function createStorageClient(this: IModule, credential: AWSStorageCredential, service?: string, sdk?: string): boolean;
@@ -20,6 +22,7 @@ declare namespace AWS {
20
22
  function setBucketWebsite(this: IModule, credential: AWSStorageCredential, Bucket: string, options: BucketWebsiteOptions, service?: string, sdk?: string): Promise<boolean>;
21
23
  function deleteObjects(this: IModule, credential: AWSStorageCredential, Bucket: string, service?: string, sdk?: string): Promise<void>;
22
24
  function deleteObjectsV2(this: IModule, credential: AWSStorageCredential, Bucket: string, recursive?: boolean, service?: string, sdk?: string): Promise<void>;
25
+ function deleteObjectsV3(this: IModule, credential: AWSStorageCredential, Bucket: string, options?: ListObjectsRequest, service?: string, sdk?: string): Promise<void>;
23
26
  function executeQuery(this: ICloud, credential: AWSDatabaseCredential, data: AWSDatabaseQuery, sessionKey?: string): Promise<QueryResult>;
24
27
  function executeBatchQuery(this: ICloud, credential: AWSDatabaseCredential, batch: AWSDatabaseQuery[], sessionKey?: string): Promise<BatchQueryResult>;
25
28
  function parseAttributeValue(value: unknown): AttributeValue;
package/client/index.js CHANGED
@@ -11,14 +11,15 @@ exports.setBucketTagging = setBucketTagging;
11
11
  exports.setBucketWebsite = setBucketWebsite;
12
12
  exports.deleteObjects = deleteObjects;
13
13
  exports.deleteObjectsV2 = deleteObjectsV2;
14
+ exports.deleteObjectsV3 = deleteObjectsV3;
14
15
  exports.executeQuery = executeQuery;
15
16
  exports.executeBatchQuery = executeBatchQuery;
16
17
  exports.parseAttributeValue = parseAttributeValue;
17
18
  exports.isNoSuchBucket = isNoSuchBucket;
18
19
  const aws = require("aws-sdk");
19
20
  const Cloud = require("@e-mc/cloud");
20
- const util_1 = require("@e-mc/cloud/util");
21
21
  const types_1 = require("@e-mc/types");
22
+ const util_1 = require("@e-mc/cloud/util");
22
23
  const aws_lib_1 = require("@pi-r/aws-lib");
23
24
  const ACP_AUTHENTICATEDREAD = {
24
25
  Grants: [
@@ -127,7 +128,7 @@ function createDatabaseClient(credential) {
127
128
  }
128
129
  return new aws.DynamoDB.DocumentClient(options);
129
130
  }
130
- async function createBucket(credential, Bucket, publicRead, service = "aws", sdk = "aws-sdk/clients/s3") {
131
+ async function createBucket(credential, Bucket, publicRead, service, sdk) {
131
132
  return createBucketV2.call(this, credential, Bucket, publicRead ? "public-read" : undefined, undefined, service, sdk);
132
133
  }
133
134
  async function createBucketV2(credential, Bucket, ACL, options, service = "aws", sdk = "aws-sdk/clients/s3") {
@@ -209,7 +210,7 @@ async function setBucketTagging(credential, Bucket, options, service = "aws", sd
209
210
  return S3.deleteBucketTagging({ Bucket, ExpectedBucketOwner: options.ExpectedBucketOwner }).promise().then(command).catch((err) => error(err));
210
211
  }
211
212
  options.Bucket = Bucket;
212
- return S3.putBucketTagging(options).promise().then(command).catch((err) => error(err));
213
+ return S3.putBucketTagging(options).promise().then(command).catch(error);
213
214
  }
214
215
  async function setBucketWebsite(credential, Bucket, options, service = "aws", sdk = "aws-sdk/clients/s3") {
215
216
  const S3 = createStorageClient.call(this, credential, service, sdk);
@@ -233,12 +234,22 @@ async function setBucketWebsite(credential, Bucket, options, service = "aws", sd
233
234
  return false;
234
235
  });
235
236
  }
236
- async function deleteObjects(credential, Bucket, service = "aws", sdk = "aws-sdk/clients/s3") {
237
- return deleteObjectsV2.call(this, credential, Bucket, true, service, sdk);
237
+ async function deleteObjects(credential, Bucket, service, sdk, recursive = true) {
238
+ return deleteObjectsV2.call(this, credential, Bucket, recursive, service, sdk);
238
239
  }
239
- async function deleteObjectsV2(credential, Bucket, recursive = true, service = "aws", sdk = "aws-sdk/clients/s3") {
240
+ async function deleteObjectsV2(credential, Bucket, recursive = true, service, sdk) {
241
+ return deleteObjectsV3.call(this, credential, Bucket, { recursive, Bucket }, service, sdk);
242
+ }
243
+ async function deleteObjectsV3(credential, Bucket, options = {}, service = "aws", sdk = "aws-sdk/clients/s3") {
244
+ options = { ...options };
245
+ let recursive = true;
246
+ if ('recursive' in options) {
247
+ recursive = options.recursive;
248
+ delete options.recursive;
249
+ }
250
+ options.Bucket ||= Bucket;
240
251
  const S3 = createStorageClient.call(this, credential, service, sdk);
241
- return S3.listObjects({ Bucket }).promise()
252
+ return S3.listObjects(options).promise()
242
253
  .then(async ({ Contents }) => {
243
254
  if (Contents?.length) {
244
255
  let Objects = Contents.map(data => ({ Key: data.Key }));
package/download/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
- const types_1 = require("@e-mc/types");
3
2
  const Cloud = require("@e-mc/cloud");
3
+ const types_1 = require("@e-mc/types");
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/aws",
3
- "version": "0.8.3",
3
+ "version": "0.9.0",
4
4
  "description": "AWS V2 cloud functions for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "types": "client/index.d.ts",
@@ -14,17 +14,16 @@
14
14
  },
15
15
  "keywords": [
16
16
  "squared",
17
- "e-mc",
18
- "squared-functions"
17
+ "e-mc"
19
18
  ],
20
19
  "author": "An Pham <anpham6@gmail.com>",
21
20
  "license": "MIT",
22
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
23
22
  "dependencies": {
24
- "@e-mc/cloud": "^0.10.3",
25
- "@e-mc/module": "^0.10.3",
26
- "@e-mc/types": "^0.10.3",
27
- "@pi-r/aws-lib": "^0.8.3",
28
- "aws-sdk": "^2.1691.0"
23
+ "@e-mc/cloud": "^0.11.0",
24
+ "@e-mc/module": "^0.11.0",
25
+ "@e-mc/types": "^0.11.0",
26
+ "@pi-r/aws-lib": "^0.9.0",
27
+ "aws-sdk": "^2.1692.0"
29
28
  }
30
29
  }
package/types/index.d.ts CHANGED
@@ -1,29 +1,29 @@
1
- import type { CloudDatabase, CloudStorage } from '@e-mc/types/lib/cloud';
2
-
3
- import type { ConfigurationOptions } from 'aws-sdk/lib/core';
4
- import type { DocumentClient, Types } from 'aws-sdk/clients/dynamodb';
5
- import type { PutBucketAclRequest, PutBucketPolicyRequest, PutPublicAccessBlockRequest } from 'aws-sdk/clients/s3';
6
-
7
- export type ConfigureBucketOptions = PutBucketAclRequest | PutBucketPolicyRequest | PutPublicAccessBlockRequest;
8
- export type Key = DocumentClient.Key;
9
- export type GetItemInput = DocumentClient.GetItemInput;
10
- export type QueryInput = DocumentClient.QueryInput;
11
- export type UpdateItemInput = DocumentClient.UpdateItemInput;
12
- export type ScanInput = DocumentClient.ScanInput;
13
- export type BatchGetItemInput = DocumentClient.BatchGetItemInput;
14
- export type DynamoDBClientConfig = Types.ClientConfiguration & DocumentClient.DocumentClientOptions;
15
-
16
- export type AWSStorage = CloudStorage<AWSStorageCredential, "aws">;
17
-
18
- export interface AWSStorageCredential extends ConfigurationOptions {
19
- profile?: string;
20
- fromPath?: string;
21
- }
22
-
23
- export interface AWSDatabaseQuery extends CloudDatabase<QueryInput | Key[], Record<string, unknown>, UpdateItemInput, BatchGetItemInput | ScanInput, AWSDatabaseCredential> {
24
- source: "cloud";
25
- service: "aws";
26
- key?: string | Key;
27
- }
28
-
1
+ import type { CloudDatabase, CloudStorage } from '@e-mc/types/lib/cloud';
2
+
3
+ import type { ConfigurationOptions } from 'aws-sdk/lib/core';
4
+ import type { DocumentClient, Types } from 'aws-sdk/clients/dynamodb';
5
+ import type { PutBucketAclRequest, PutBucketPolicyRequest, PutPublicAccessBlockRequest } from 'aws-sdk/clients/s3';
6
+
7
+ export type ConfigureBucketOptions = PutBucketAclRequest | PutBucketPolicyRequest | PutPublicAccessBlockRequest;
8
+ export type Key = DocumentClient.Key;
9
+ export type GetItemInput = DocumentClient.GetItemInput;
10
+ export type QueryInput = DocumentClient.QueryInput;
11
+ export type UpdateItemInput = DocumentClient.UpdateItemInput;
12
+ export type ScanInput = DocumentClient.ScanInput;
13
+ export type BatchGetItemInput = DocumentClient.BatchGetItemInput;
14
+ export type DynamoDBClientConfig = Types.ClientConfiguration & DocumentClient.DocumentClientOptions;
15
+
16
+ export type AWSStorage = CloudStorage<AWSStorageCredential, "aws">;
17
+
18
+ export interface AWSStorageCredential extends ConfigurationOptions {
19
+ profile?: string;
20
+ fromPath?: string;
21
+ }
22
+
23
+ export interface AWSDatabaseQuery extends CloudDatabase<QueryInput | Key[], Record<string, unknown>, UpdateItemInput, BatchGetItemInput | ScanInput, AWSDatabaseCredential> {
24
+ source: "cloud";
25
+ service: "aws";
26
+ key?: string | Key;
27
+ }
28
+
29
29
  export interface AWSDatabaseCredential extends AWSStorageCredential, DynamoDBClientConfig {}
package/upload/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
- const path = require("path");
3
- const fs = require("fs");
4
- const stream = require("stream");
2
+ const path = require("node:path");
3
+ const fs = require("node:fs");
4
+ const stream = require("node:stream");
5
5
  const aws = require("aws-sdk");
6
- const crypto_1 = require("crypto");
6
+ const node_crypto_1 = require("node:crypto");
7
7
  const Cloud = require("@e-mc/cloud");
8
- const util_1 = require("@e-mc/cloud/util");
9
8
  const types_1 = require("@e-mc/types");
9
+ const util_1 = require("@e-mc/cloud/util");
10
10
  const aws_lib_1 = require("@pi-r/aws-lib");
11
11
  const client_1 = require("@pi-r/aws");
12
12
  const BUCKET_SESSION = new Set();
@@ -108,7 +108,7 @@ function upload(credential, service = "aws", sdk = "aws-sdk/clients/s3") {
108
108
  if (!overwrite) {
109
109
  const current = filename;
110
110
  const next = (0, util_1.generateFilename)(filename);
111
- let i = 0, exists;
111
+ let i = 0, exists = false;
112
112
  do {
113
113
  if (i > 0) {
114
114
  [filename, exists] = next(i);
@@ -120,7 +120,7 @@ function upload(credential, service = "aws", sdk = "aws-sdk/clients/s3") {
120
120
  .then(() => true)
121
121
  .catch((err) => {
122
122
  if (err instanceof Error && err.code !== 'NotFound') {
123
- filename = (0, crypto_1.randomUUID)() + path.extname(current);
123
+ filename = (0, node_crypto_1.randomUUID)() + path.extname(current);
124
124
  }
125
125
  return false;
126
126
  });