-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathldp-storage.js
More file actions
55 lines (48 loc) · 1.74 KB
/
ldp-storage.js
File metadata and controls
55 lines (48 loc) · 1.74 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
import { either, pipe as _, split, prop, when, identity, endsWith, replace, tryCatch } from 'ramda'
import resolvePathname from 'resolve-pathname'
import { option } from 'fp-ts'
const { some, none } = option
const noTrailingSlash = _(when(endsWith('/'), replace(/\/$/, '')))
export const getParentContainer = (/** @type {string} */ url) =>
tryCatch(
() => new URL(resolvePathname('./', noTrailingSlash(url))).href,
() => none,
)()
export const hasSpaceStorageType = (/** @type {string[]} */ linkHeaders) => {
const space = linkHeaders
?.map((l) => l.split(';'))
.filter(
([k, v]) =>
v?.trim() === 'rel="type"' &&
k?.trim().toLowerCase() === '<http://www.w3.org/ns/pim/space#Storage>'.toLowerCase(),
)
return space?.length > 0 || false
}
const fetchHead = async (url) => fetch(url, { method: 'HEAD', cache: 'no-store' })
const links = _(prop('headers'), (o) => o?.get('link') || '') // w.headers.get('link')
export const fetchLinkHeaders = async (/** @type {string} */ url) => {
if (typeof url !== 'string') {
return none
}
const res = fetchHead(url).catch(() => none)
if (typeof res.then !== 'function') {
return none
}
// @ts-ignore
return res.then(either(_(links, either(split(','), identity), some), none))
}
/** @returns {Promise<some | none>} */
export const container = async (/** @type {string} */ url) => {
const parent = getParentContainer(url)
if (!parent || parent === url) {
return none
}
return fetchLinkHeaders(parent).then(async (h) => {
if (!h) {
console.error('result is Nothing', parent)
return none
}
// console.debug(parent, h.value, hasSpaceStorageType(h.value))
return hasSpaceStorageType(h.value) ? parent : await container(parent)
})
}