-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcontainerLogic.ts
More file actions
57 lines (52 loc) · 2.02 KB
/
containerLogic.ts
File metadata and controls
57 lines (52 loc) · 2.02 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
import { NamedNode, Statement, sym } from 'rdflib'
/**
* Container-related class
*/
export function createContainerLogic(store) {
function getContainerElements(containerNode: NamedNode): NamedNode[] {
return store
.statementsMatching(
containerNode,
sym('http://www.w3.org/ns/ldp#contains'),
undefined
)
.map((st: Statement) => st.object as NamedNode)
}
function isContainer(url: NamedNode) {
const nodeToString = url.value
return nodeToString.charAt(nodeToString.length - 1) === '/'
}
async function createContainer(url: string) {
const stringToNode = sym(url)
if (!isContainer(stringToNode)) {
throw new Error(`Not a container URL ${url}`)
}
// Copied from https://github.com/solidos/solid-crud-tests/blob/v3.1.0/test/surface/create-container.test.ts#L56-L64
const result = await store.fetcher._fetch(url, {
method: 'PUT',
headers: {
'Content-Type': 'text/turtle',
'If-None-Match': '*',
Link: '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"', // See https://github.com/solidos/node-solid-server/issues/1465
},
body: ' ', // work around https://github.com/michielbdejong/community-server/issues/4#issuecomment-776222863
})
// Treat 409 as idempotent success: another process/request already created the container.
if (result.status === 409) {
return
}
if (result.status.toString()[0] !== '2') {
throw new Error(`Not OK: got ${result.status} response while creating container at ${url}`)
}
}
async function getContainerMembers(containerUrl: NamedNode): Promise<NamedNode[]> {
await store.fetcher.load(containerUrl)
return getContainerElements(containerUrl)
}
return {
isContainer,
createContainer,
getContainerElements,
getContainerMembers
}
}