-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.js
213 lines (196 loc) · 6 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
213
import { crocks, HyperErr, R } from './deps.js'
import { Multi } from './lib/multi.js'
import { Namespaced } from './lib/namespaced.js'
import { checkName, handleHyperErr, minioClientSchema } from './lib/utils.js'
const { Async } = crocks
const { prop, map, always, identity } = 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
*/
/**
* @param {{ minio: any, bucketPrefix: string, region?: string, useNamespacedBucket: boolean }} config
* @returns hyper storage terminating adapter impl
*/
export default function (config) {
const { bucketPrefix, region, useNamespacedBucket, minio } = config
const lib = useNamespacedBucket
? Namespaced({ bucketPrefix, region })
: Multi({ bucketPrefix, region })
const client = minioClientSchema.parse({
makeBucket: lib.makeBucket(minio),
removeBucket: lib.removeBucket(minio),
bucketExists: lib.bucketExists(minio),
listBuckets: lib.listBuckets(minio),
putObject: lib.putObject(minio),
removeObject: lib.removeObject(minio),
removeObjects: lib.removeObjects(minio),
getObject: lib.getObject(minio),
getSignedUrl: lib.getSignedUrl(minio),
listObjects: lib.listObjects(minio),
})
/**
* Check the name of the bucket is valid and whether it exists or not
*/
const checkBucket = (name) =>
checkName(name)
.chain(() =>
client.bucketExists(name)
.chain((exists) =>
exists ? Async.Resolved(name) : Async.Rejected(
HyperErr({ status: 404, msg: 'bucket does not exist' }),
)
)
)
/**
* @param {string} name
* @returns {Promise<ResponseMsg>}
*/
function makeBucket(name) {
return checkName(name)
.chain(() => client.makeBucket(name))
.bichain(
handleHyperErr,
always(Async.Resolved({ ok: true })),
).toPromise()
}
/**
* @param {string} name
* @returns {Promise<ResponseMsg>}
*/
function removeBucket(name) {
return checkBucket(name)
.chain(() => client.listObjects({ bucket: name, prefix: '' }))
.chain((objects) => {
if (!objects.length) return Async.Resolved()
return client.removeObjects({ bucket: name, keys: objects.map((o) => o.name) })
})
.chain(() => client.removeBucket(name))
.bichain(
handleHyperErr,
always(Async.Resolved({ ok: true })),
).toPromise()
}
/**
* @returns {Promise<ResponseBuckets>}
*/
function listBuckets() {
return client.listBuckets()
.bichain(
handleHyperErr,
(bucketNamesArr) => Async.Resolved({ ok: true, buckets: bucketNamesArr }),
).toPromise()
}
/**
* @param {PutObjectArgs}
* @returns {Promise<ResponseOk>}
*/
function putObject({ bucket, object, stream, useSignedUrl }) {
return Async.all([checkBucket(bucket), checkName(object)])
.chain(() => Async.of({ bucket, object, stream, useSignedUrl }))
.chain(({ bucket, object, stream, useSignedUrl }) => {
if (!useSignedUrl) {
return Async.of(stream)
.chain((stream) => client.putObject({ bucket, key: object, body: stream }))
.map(always({ ok: true }))
}
return Async.of()
.chain(() => client.getSignedUrl({ bucket, key: object, method: 'PUT' }))
.map((url) => ({ ok: true, url }))
})
.bichain(
handleHyperErr,
Async.Resolved,
).toPromise()
}
/**
* @param {ObjectArgs}
* @returns {Promise<ResponseOk>}
*/
function removeObject({ bucket, object }) {
return checkBucket(bucket)
.chain(() => client.removeObject({ bucket, key: object }))
.bichain(
handleHyperErr,
always(Async.Resolved({ ok: true })),
).toPromise()
}
/**
* @param {ObjectArgs}
* @returns {Promise<{ ok: false, msg?: string, status?: number } | ReadableStream>}
*/
function getObject({ bucket, object, useSignedUrl }) {
return Async.all([checkBucket(bucket), checkName(object)])
.chain(() => Async.of({ bucket, object, useSignedUrl }))
.chain(({ bucket, object, useSignedUrl }) => {
if (!useSignedUrl) {
return client.getObject({ bucket, key: object }) // WebReadableStream, so no need to convert
}
/**
* Generating a signedUrl has no way of knowing whether or not
* the object actually exists.
*
* Since signedUrls already sort of break of boundary,
* we are deferring this responsibility for checking the signed url to the consumer
*/
return Async.of()
// expiration is 1 hour
.chain(() => client.getSignedUrl({ bucket, key: object, method: 'GET', expires: 10000 }))
.map((url) => ({ ok: true, url }))
})
.bichain(
handleHyperErr,
Async.Resolved,
).toPromise()
}
/**
* @param {ListObjectsArgs}
* @returns {Promise<ResponseObjects>}
*/
function listObjects({ bucket, prefix }) {
return Async.all([checkBucket(bucket), checkName(prefix)])
.chain(() => client.listObjects({ bucket, prefix }))
.bimap(
identity,
map(prop('name')),
).bichain(
handleHyperErr,
(objectNamesArr) => Async.Resolved({ ok: true, objects: objectNamesArr }),
).toPromise()
}
return Object.freeze({
makeBucket,
removeBucket,
listBuckets,
putObject,
removeObject,
getObject,
listObjects,
})
}