@pi-r/aws-lib 0.6.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 +7 -0
- package/README.md +7 -0
- package/index.js +100 -0
- package/package.json +26 -0
- package/types/index.d.ts +83 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2023 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
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getBucketKey = exports.checkBucketCannedACL = exports.setDatabaseEndpoint = exports.setEndpoint = exports.getPrivatePolicy = exports.getBucketPublicReadPolicy = exports.getPublicReadPolicy = exports.isDatabaseDefined = exports.isProviderChainDefined = exports.isSharedCredentialsDefined = exports.isEnvDefined = exports.isAccessDefined = void 0;
|
|
4
|
+
const types_1 = require("@e-mc/types");
|
|
5
|
+
function isAccessDefined(credential) {
|
|
6
|
+
return !!(credential.accessKeyId && credential.secretAccessKey);
|
|
7
|
+
}
|
|
8
|
+
exports.isAccessDefined = isAccessDefined;
|
|
9
|
+
function isEnvDefined() {
|
|
10
|
+
return !!(process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY);
|
|
11
|
+
}
|
|
12
|
+
exports.isEnvDefined = isEnvDefined;
|
|
13
|
+
function isSharedCredentialsDefined() {
|
|
14
|
+
return !!process.env.AWS_SDK_LOAD_CONFIG;
|
|
15
|
+
}
|
|
16
|
+
exports.isSharedCredentialsDefined = isSharedCredentialsDefined;
|
|
17
|
+
function isProviderChainDefined() {
|
|
18
|
+
return isSharedCredentialsDefined() || !!(process.env.AWS_WEB_IDENTITY_TOKEN_FILE || process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI || process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI);
|
|
19
|
+
}
|
|
20
|
+
exports.isProviderChainDefined = isProviderChainDefined;
|
|
21
|
+
function isDatabaseDefined(credential, data) {
|
|
22
|
+
return !!(data.table && (credential.region || credential.endpoint || process.env.AWS_REGION));
|
|
23
|
+
}
|
|
24
|
+
exports.isDatabaseDefined = isDatabaseDefined;
|
|
25
|
+
function getPublicReadPolicy(bucket, authenticated, write) {
|
|
26
|
+
const Action = ["s3:GetObject", "s3:GetObjectVersion"];
|
|
27
|
+
if (write) {
|
|
28
|
+
Action.push("s3:PutObject", "s3:DeleteObjectVersion");
|
|
29
|
+
}
|
|
30
|
+
return JSON.stringify({
|
|
31
|
+
"Version": "2012-10-17",
|
|
32
|
+
"Statement": [{
|
|
33
|
+
"Sid": (authenticated ? "AuthenticatedRead" : "PublicRead") + (write ? "Write" : ""),
|
|
34
|
+
"Effect": "Allow",
|
|
35
|
+
"Principal": authenticated ? { "AWS": "*" } : "*",
|
|
36
|
+
"Action": Action,
|
|
37
|
+
"Resource": [`arn:aws:s3:::${bucket}/*`]
|
|
38
|
+
}]
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
exports.getPublicReadPolicy = getPublicReadPolicy;
|
|
42
|
+
function getBucketPublicReadPolicy(bucket) {
|
|
43
|
+
return JSON.stringify({
|
|
44
|
+
"Version": "2012-10-17",
|
|
45
|
+
"Statement": [{
|
|
46
|
+
"Sid": "BucketPublicRead",
|
|
47
|
+
"Effect": "Allow",
|
|
48
|
+
"Principal": "*",
|
|
49
|
+
"Action": ["s3:ListBucket", "s3:ListBucketVersions", "s3:ListBucketMultipartUploads"],
|
|
50
|
+
"Resource": [`arn:aws:s3:::${bucket}`]
|
|
51
|
+
}]
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
exports.getBucketPublicReadPolicy = getBucketPublicReadPolicy;
|
|
55
|
+
function getPrivatePolicy(bucket) {
|
|
56
|
+
return JSON.stringify({
|
|
57
|
+
"Version": "2012-10-17",
|
|
58
|
+
"Statement": [{
|
|
59
|
+
"Sid": "Private",
|
|
60
|
+
"Effect": "Deny",
|
|
61
|
+
"Principal": "*",
|
|
62
|
+
"Action": "*",
|
|
63
|
+
"Resource": [`arn:aws:s3:::${bucket}`, `arn:aws:s3:::${bucket}/*`]
|
|
64
|
+
}]
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
exports.getPrivatePolicy = getPrivatePolicy;
|
|
68
|
+
function setEndpoint(config, instance, fallbackRegion) {
|
|
69
|
+
let { region, endpoint } = config;
|
|
70
|
+
if (!endpoint) {
|
|
71
|
+
const location = (0, types_1.isString)(region) ? region : process.env.AWS_REGION || fallbackRegion;
|
|
72
|
+
if (location) {
|
|
73
|
+
endpoint = `https://${instance}.${location}.amazonaws.com`;
|
|
74
|
+
config.endpoint = endpoint;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (!region) {
|
|
78
|
+
region = ((0, types_1.isString)(endpoint) && new RegExp(`\\b${instance}\\.([^.]+)\\.`, 'i').exec(endpoint)?.[1] || process.env.AWS_REGION || fallbackRegion)?.toLowerCase();
|
|
79
|
+
if (region && region !== "us-east-1" /* STRINGS.REGION */) {
|
|
80
|
+
config.region = region;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.setEndpoint = setEndpoint;
|
|
85
|
+
function setDatabaseEndpoint(config, fallbackRegion = "us-east-1" /* STRINGS.REGION */) {
|
|
86
|
+
setEndpoint(config, 'dynamodb', fallbackRegion);
|
|
87
|
+
}
|
|
88
|
+
exports.setDatabaseEndpoint = setDatabaseEndpoint;
|
|
89
|
+
function checkBucketCannedACL(value) {
|
|
90
|
+
switch (value) {
|
|
91
|
+
case "private" /* PERMISSIONS.PRIVATE */:
|
|
92
|
+
case "public-read" /* PERMISSIONS.PUBLIC_READ */:
|
|
93
|
+
case "public-read-write" /* PERMISSIONS.PUBLIC_READ_WRITE */:
|
|
94
|
+
case "authenticated-read" /* PERMISSIONS.AUTHENTICATED_READ */:
|
|
95
|
+
return value;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.checkBucketCannedACL = checkBucketCannedACL;
|
|
99
|
+
const getBucketKey = (credential, bucket, acl, service, sdk) => JSON.stringify(credential) + bucket + '_' + (acl || '') + service + sdk;
|
|
100
|
+
exports.getBucketKey = getBucketKey;
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pi-r/aws-lib",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "AWS cloud utility functions for E-mc.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "types/index.d.ts",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/anpham6/pi-r.git",
|
|
13
|
+
"directory": "src/cloud/aws-lib"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"squared",
|
|
17
|
+
"e-mc",
|
|
18
|
+
"squared-functions"
|
|
19
|
+
],
|
|
20
|
+
"author": "An Pham <anpham6@gmail.com>",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@e-mc/types": "^0.8.3"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { CloudDatabase } from '@e-mc/types/lib/cloud';
|
|
2
|
+
|
|
3
|
+
declare namespace AWS {
|
|
4
|
+
type BucketCannedACL = "authenticated-read" | "private" | "public-read" | "public-read-write";
|
|
5
|
+
type ObjectCannedACL = BucketCannedACL | "bucket-owner-full-control" | "bucket-owner-read";
|
|
6
|
+
|
|
7
|
+
interface EndpointConfig {
|
|
8
|
+
/**
|
|
9
|
+
* The region to send service requests to.
|
|
10
|
+
*/
|
|
11
|
+
region?: string | Provider<string>;
|
|
12
|
+
/**
|
|
13
|
+
* The endpoint URI to send requests to. The default endpoint is built from the configured region.
|
|
14
|
+
* The endpoint should be a string like 'https://{service}.{region}.amazonaws.com' or an Endpoint object.
|
|
15
|
+
*/
|
|
16
|
+
endpoint?: string | Endpoint;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface AccessConfig {
|
|
20
|
+
/**
|
|
21
|
+
* AWS access key ID.
|
|
22
|
+
*
|
|
23
|
+
* @deprecated
|
|
24
|
+
*/
|
|
25
|
+
accessKeyId?: string;
|
|
26
|
+
/**
|
|
27
|
+
* AWS secret access key.
|
|
28
|
+
*
|
|
29
|
+
* @deprecated
|
|
30
|
+
*/
|
|
31
|
+
secretAccessKey?: string;
|
|
32
|
+
/**
|
|
33
|
+
* AWS session token.
|
|
34
|
+
*
|
|
35
|
+
* @deprecated
|
|
36
|
+
*/
|
|
37
|
+
sessionToken?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class Endpoint {
|
|
41
|
+
/**
|
|
42
|
+
* Constructs a new endpoint given an endpoint URL.
|
|
43
|
+
*/
|
|
44
|
+
constructor(url: string);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The host portion of the endpoint including the port, e.g., example.com:80.
|
|
48
|
+
*/
|
|
49
|
+
host: string;
|
|
50
|
+
/**
|
|
51
|
+
* The host portion of the endpoint, e.g., example.com.
|
|
52
|
+
*/
|
|
53
|
+
hostname: string;
|
|
54
|
+
/**
|
|
55
|
+
* The full URL of the endpoint.
|
|
56
|
+
*/
|
|
57
|
+
href: string;
|
|
58
|
+
/**
|
|
59
|
+
* The port of the endpoint.
|
|
60
|
+
*/
|
|
61
|
+
port: number;
|
|
62
|
+
/**
|
|
63
|
+
* The protocol (http or https) of the endpoint URL.
|
|
64
|
+
*/
|
|
65
|
+
protocol: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type Provider<T> = () => Promise<T>;
|
|
69
|
+
|
|
70
|
+
function isAccessDefined(credential: AccessConfig): boolean;
|
|
71
|
+
function isEnvDefined(): boolean;
|
|
72
|
+
function isSharedCredentialsDefined(): boolean;
|
|
73
|
+
function isProviderChainDefined(): boolean;
|
|
74
|
+
function isDatabaseDefined(credential: unknown, data: CloudDatabase): boolean;
|
|
75
|
+
function getPublicReadPolicy(bucket: string, authenticated?: boolean, write?: boolean): string;
|
|
76
|
+
function getBucketPublicReadPolicy(bucket: string): string;
|
|
77
|
+
function getPrivatePolicy(bucket: string): string;
|
|
78
|
+
function setDatabaseEndpoint(config: EndpointConfig, fallbackRegion?: string): void;
|
|
79
|
+
function setEndpoint(config: EndpointConfig, instance: string, fallbackRegion?: string): void;
|
|
80
|
+
function getBucketKey(credential: unknown, bucket: string, acl: string | undefined, service: string, sdk: string): string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export = AWS;
|