This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
generated from hyper63/adapter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.js
98 lines (93 loc) · 2.76 KB
/
mod.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { ApiFactory, AwsEndpointResolver, crocks, R, S3 } from "./deps.js";
import createAdapter from "./adapter.js";
import PORT_NAME from "./port_name.js";
const { Either } = crocks;
const { Left, Right, of } = Either;
const {
__,
assoc,
identity,
mergeRight,
isNil,
reject,
over,
lensProp,
defaultTo,
} = R;
export default (
bucketPrefix,
{ awsAccessKeyId, awsSecretKey, region } = {},
) => {
const setPrefixOn = (obj) => assoc("prefix", __, obj); // expects object
const setAwsCreds = (env) =>
mergeRight(
env,
reject(isNil, {
awsAccessKeyId,
awsSecretKey,
region,
}),
);
const setAwsRegion = (env) =>
mergeRight(
{ region: "us-east-1" },
env,
);
const createFactory = (env) =>
over(
lensProp("factory"),
() =>
(env.awsAccessKeyId && env.awsSecretKey && env.region)
/**
* Disable using Dualstack endpoints, so this adapter will use VPC Gateway endpoint when used within a VPC
* - For lib api, see https://github.com/cloudydeno/deno-aws_api/blob/3afef9fe3aaef842fd3a19245593494c3705a1dd/lib/client/endpoints.ts#L19
* - For Dualstack description https://docs.aws.amazon.com/AmazonS3/latest/userguide/dual-stack-endpoints.html#dual-stack-endpoints-description
*/
? new ApiFactory({
credentials: env,
endpointResolver: new AwsEndpointResolver({ useDualstack: false }),
})
: /**
* ApiFactory attempts to pull credentials from multiple environment places
* If not provided via constructor
* See https://github.com/cloudydeno/deno-aws_api/blob/2b8605516802c1b790a2b112c03b790feb3bf58f/lib/client/credentials.ts#L50
*/
new ApiFactory({
endpointResolver: new AwsEndpointResolver({
useDualstack: false,
}),
}),
env,
);
const setAws = (env) =>
over(
lensProp("aws"),
() => ({ factory: env.factory, s3: new S3(env.factory) }),
env,
);
return Object.freeze({
id: "s3",
port: PORT_NAME,
load: (prevLoad) =>
of(prevLoad) // credentials can be received from a composed plugin
.map(defaultTo({}))
.map(setAwsCreds)
.map(setAwsRegion)
.chain((env) =>
notIsNil(bucketPrefix)
.map(setPrefixOn(env))
)
.map(createFactory)
.map(setAws)
.either(
(e) => console.log("Error: In Load Method", e.message),
identity,
),
link: ({ prefix, aws }) => (_) => createAdapter(prefix, aws),
});
};
function notIsNil(s) {
return isNil(s)
? Left({ message: "S3 Prefix Name: can not be null or undefined!" })
: Right(s);
}