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 pathadapter.js
212 lines (196 loc) · 5.26 KB
/
adapter.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { Buffer, crocks, R, readAll } from "./deps.js";
import * as lib from "./lib/s3.js";
import { hashBucketName, mapErr } from "./lib/utils.js";
const { Async } = crocks;
const { always, compose, prop, map, identity, path } = R;
/**
* @typedef {Object} PutObjectArgs
* @property {string} bucket
* @property {string} object
* @property {any} stream
*
* @typedef {Object} ObjectArgs
* @property {string} bucket
* @property {string} object
*
* @typedef {Object} ListObjectsArgs
* @property {string} bucket
* @property {string} [prefix]
*
* @typedef {Object} Msg
* @property {string} [msg]
*
* @typedef {Object} Buckets
* @property {string[]} buckets
*
* @typedef {Object} Objects
* @property {string[]} objects
*
* @typedef {Object} ResponseOk
* @property {boolean} ok
*
* @typedef {Msg & ResponseOk} ResponseMsg
* @typedef {Buckets & ResponseOk} ResponseBuckets
* @typedef {Objects & ResponseOk} ResponseObjects
*/
/**
* the hashed name is sha1 which is 40 characters + "hyper-storage-" makes 54,
* which falls within the acceptable range of s3 bucket name lengths (3-63)
*/
export const HYPER_BUCKET_PREFIX = "hyper-storage";
/**
* @param {{ s3: any, factory: any }} aws
* @returns
*/
export default function (bucketPrefix, aws) {
const { s3 } = aws;
const client = {
makeBucket: Async.fromPromise(lib.makeBucket(s3)),
removeBucket: Async.fromPromise(lib.removeBucket(s3)),
listBuckets: Async.fromPromise(lib.listBuckets(s3)),
putObject: Async.fromPromise(lib.putObject(s3)),
removeObject: Async.fromPromise(lib.removeObject(s3)),
getObject: Async.fromPromise(lib.getObject(s3)),
listObjects: Async.fromPromise(lib.listObjects(s3)),
};
/**
* @param {string} name
* @returns {Promise<ResponseMsg>}
*/
function makeBucket(name) {
return client.makeBucket(
`${HYPER_BUCKET_PREFIX}-${hashBucketName(bucketPrefix, name)}`,
)
.bimap(
mapErr,
identity,
).bimap(
(msg) => ({ ok: false, msg }),
always({ ok: true }),
).toPromise();
}
/**
* @param {string} name
* @returns {Promise<ResponseMsg>}
*/
function removeBucket(name) {
return client.removeBucket(
`${HYPER_BUCKET_PREFIX}-${hashBucketName(bucketPrefix, name)}`,
)
.bimap(
mapErr,
identity,
).bimap(
(msg) => ({ ok: false, msg }),
always({ ok: true }),
).toPromise();
}
/**
* @returns {Promise<ResponseBuckets>}
*
* TODO: Tyler. This was broken as part of work to hash
* bucket names sent to AWS. This was done
* to satisfy https://github.com/hyper63/hyper-adapter-s3/issues/4
*
* So for now, the names coming back will be the hashes generated. But
* this isn't currently used by the hyper app, so shouldn't break hyper proper
*
* Possible solution: on bucket creation, add a statically named txt file (_metadata.txt) that contains
* the actual name, then fetch that file, for each bucket, and extract the name
*/
function listBuckets() {
return client.listBuckets()
.bimap(
mapErr,
compose(
map(prop("Name")),
prop("Buckets"),
),
).bimap(
(msg) => ({ ok: false, msg }),
(bucketNamesArr) => ({ ok: true, buckets: bucketNamesArr }),
).toPromise();
}
/**
* @param {PutObjectArgs}
* @returns {Promise<ResponseOk>}
*/
async function putObject({ bucket, object, stream }) {
const arrBuffer = await readAll(stream);
return client.putObject({
bucket: `${HYPER_BUCKET_PREFIX}-${hashBucketName(bucketPrefix, bucket)}`,
key: object,
body: arrBuffer,
})
.bimap(
mapErr,
identity,
).bimap(
(msg) => ({ ok: false, msg }),
always({ ok: true }),
).toPromise();
}
/**
* @param {ObjectArgs}
* @returns {Promise<ResponseOk>}
*/
function removeObject({ bucket, object }) {
return client.removeObject({
bucket: `${HYPER_BUCKET_PREFIX}-${hashBucketName(bucketPrefix, bucket)}`,
key: object,
})
.bimap(
mapErr,
identity,
).bimap(
(msg) => ({ ok: false, msg }),
always({ ok: true }),
).toPromise();
}
/**
* @param {ObjectArgs}
* @returns {Promise<Buffer>}
*/
function getObject({ bucket, object }) {
return client.getObject({
bucket: `${HYPER_BUCKET_PREFIX}-${hashBucketName(bucketPrefix, bucket)}`,
key: object,
})
.bimap(
mapErr,
path(["Body", "buffer"]),
).bimap(
(msg) => ({ ok: false, msg }),
(arrayBuffer) => new Buffer(arrayBuffer),
).toPromise();
}
/**
* @param {ListObjectsArgs}
* @returns {Promise<ResponseObjects>}
*/
function listObjects({ bucket, prefix }) {
return client.listObjects({
bucket: `${HYPER_BUCKET_PREFIX}-${hashBucketName(bucketPrefix, bucket)}`,
prefix,
})
.bimap(
mapErr,
compose(
map(prop("Key")),
prop("Contents"),
),
).bimap(
(msg) => ({ ok: false, msg }),
(objectNamesArr) => ({ ok: true, objects: objectNamesArr }),
).toPromise();
}
return Object.freeze({
makeBucket,
removeBucket,
listBuckets,
putObject,
removeObject,
getObject,
listObjects,
});
}