-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.js
More file actions
290 lines (271 loc) · 7.38 KB
/
adapter.js
File metadata and controls
290 lines (271 loc) · 7.38 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { crocks, HyperErr, isHyperErr, path, R } from './deps.js'
const { Async } = crocks
const { always, is, ifElse } = R
export const handleHyperErr = ifElse(
isHyperErr,
Async.Resolved,
Async.Rejected,
)
/**
* hyper63 adapter for the storage port
*
* This adapter uses the file system
* as the implementation details for
* the storage port.
*
* @typedef {Object} StorageInfo
* @property {string} bucket
* @property {string} object
*
* @typedef {Object} StorageObject
* @property {string} bucket
* @property {string} object
* @property {stream} stream
*
* @typedef {Object} Response
* @property {boolean} ok
* @property {string} [msg] - error message
*/
/**
* @param {string} path
* @returns {Object}
*/
export default function (root) {
if (!root) {
throw new Error(
'STORAGE: FS_Adapter: root directory required for this service!',
)
}
const open = Async.fromPromise(Deno.open.bind(Deno))
const mkdir = Async.fromPromise(Deno.mkdir.bind(Deno))
const rm = Async.fromPromise(Deno.remove.bind(Deno))
const rmdir = Async.fromPromise(Deno.remove.bind(Deno))
const stat = Async.fromPromise(Deno.stat.bind(Deno))
const resolvePathFromRoot = (...pieces) => path.resolve(path.join(root, ...pieces))
const checkRelativeParts = (path) =>
Async.of(path.includes('..'))
.chain((invalid) =>
!invalid ? Async.Resolved(path) : Async.Rejected(
HyperErr({
status: 400,
msg: 'cannot contain relative path parts',
}),
)
)
const checkBucketName = (name) =>
checkRelativeParts(name)
/**
* A bucket name also cannot contain slashes
*/
.map((name) => name.includes('/') || name.includes('\\'))
.chain((invalid) =>
!invalid ? Async.Resolved(name) : Async.Rejected(
HyperErr({
status: 400,
msg: 'bucket name cannot contain slashes',
}),
)
)
const checkPathExists = (path, resource = 'bucket') =>
Async.of(path)
.chain(stat)
.bimap(
(err) => {
if (is(Deno.errors.NotFound)) {
return HyperErr({ status: 404, msg: `${resource} does not exist` })
}
return err
},
always(path),
)
const checkObjectPath = ({ bucket, object }) =>
Async.all([
// Bucket checks
Async.all([
checkBucketName(bucket),
checkPathExists(resolvePathFromRoot(bucket)),
]),
// Object checks
Async.all([
checkRelativeParts(object),
checkPathExists(resolvePathFromRoot(bucket, object), 'object'),
]),
])
.map(() => resolvePathFromRoot(bucket, object))
/**
* @param {string} name
* @returns {Promise<Response>}
*/
function makeBucket(name) {
return Async.of(name)
.chain(checkBucketName)
.map((name) => resolvePathFromRoot(name))
.chain((path) =>
checkPathExists(path).bichain(
() => Async.Resolved(path),
() =>
Async.Rejected(
HyperErr({ status: 409, msg: 'bucket already exists' }),
),
)
)
// Make the directory as the new bucket
.chain((dir) => mkdir(dir, { recursive: true }))
.map(always({ ok: true }))
.bichain(
handleHyperErr,
Async.Resolved,
)
.toPromise()
}
/**
* @param {string} name
* @returns {Promise<Response>}
*/
function removeBucket(name) {
return Async.of(name)
.chain(checkBucketName)
.chain((name) => checkPathExists(resolvePathFromRoot(name)))
// Delete all contents of directory, then directory
.chain((bucket) => rmdir(bucket, { recursive: true }))
.map(always({ ok: true }))
.bichain(
handleHyperErr,
Async.Resolved,
)
.toPromise()
}
/**
* @param {StorageObject}
* @returns {Promise<Response>}
*/
function putObject({ bucket, object, stream, useSignedUrl }) {
if (useSignedUrl) {
return Promise.resolve(
HyperErr({
status: 501,
msg: 'Not Implemented',
}),
)
}
return Async.all([
// Bucket checks
Async.all([
checkBucketName(bucket),
checkPathExists(resolvePathFromRoot(bucket)),
]),
// Object checks
Async.all([
checkRelativeParts(object),
]),
])
.map(() => resolvePathFromRoot(bucket, object))
/**
* Check if the sub-directory in the bucket exists,
* and create it if it does not
*/
.chain((p) =>
checkPathExists(path.dirname(p))
.bichain(
/**
* The directory does not exist within the bucket, so create it
*/
() => mkdir(path.dirname(p), { recursive: true }).map(() => p),
/**
* The directory does exist with the bucket, so simply noop
*/
() => Async.Resolved(p),
)
)
.chain((path) => {
return open(path, { create: true, write: true, truncate: true })
})
.chain(Async.fromPromise((file) => {
return stream.pipeTo(file.writable)
}))
.map(always({ ok: true }))
.bichain(
handleHyperErr,
Async.Resolved,
).toPromise()
}
/**
* @param {StorageInfo}
* @returns {Promise<Response>}
*/
function removeObject({ bucket, object }) {
return checkObjectPath({ bucket, object })
.map(() => resolvePathFromRoot(bucket, object))
.chain(rm)
.map(always({ ok: true }))
.bichain(
handleHyperErr,
Async.Resolved,
).toPromise()
}
/**
* @param {StorageInfo}
* @returns {Promise<stream>}
*/
function getObject({ bucket, object, useSignedUrl }) {
if (useSignedUrl) {
return Promise.resolve(
HyperErr({
status: 501,
msg: 'Not Implemented',
}),
)
}
return checkObjectPath({ bucket, object })
.chain((p) => open(p, { read: true, write: false }))
.map((file) => file.readable)
.bichain(
handleHyperErr,
Async.Resolved,
).toPromise()
}
function listObjects({ bucket, prefix = '' }) {
return Async.all([
checkBucketName(bucket),
checkPathExists(resolvePathFromRoot(bucket)),
])
.chain(() =>
/**
* At this point, we know the bucket exists, but the prefix may not,
* but that shouldn't result in an error
*
* So if the prefix does not exist we just return an empty list of objects
*/
checkPathExists(resolvePathFromRoot(bucket, prefix)).bichain(
() => Async.Resolved({ ok: true, objects: [] }),
Async.fromPromise(async (path) => {
const files = []
try {
for await (const dirEntry of Deno.readDir(path)) {
files.push(dirEntry.name)
}
return { ok: true, objects: files }
} catch (err) {
if (is(Deno.errors.NotFound)) {
return HyperErr({ status: 404, msg: `${resource} does not exist` })
}
throw err
}
}),
)
)
.bichain(
handleHyperErr,
Async.Resolved,
).toPromise()
}
return Object.freeze({
makeBucket,
removeBucket,
listBuckets: () => Promise.resolve(null),
putObject,
removeObject,
getObject,
listObjects,
})
}