-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
117 lines (87 loc) · 2.97 KB
/
index.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
import rdf from 'rdf-ext'
import FilterStream from 'rdf-stream-filter'
import TripleToQuadTransform from 'rdf-transform-triple-to-quad'
import promiseToEvent from 'rdf-utils-stream/promiseToEvent.js'
import chunks from 'stream-chunks/chunks.js'
import handleResponse from './lib/handleResponse.js'
class WebStore {
constructor ({ factory = rdf } = {}) {
this.factory = factory
}
async _deleteGraph (graph) {
const res = await this.factory.fetch(graph.value, { method: 'DELETE' })
await handleResponse(res)
}
async _importGraph (graph, dataset, { truncate } = {}) {
const stream = new TripleToQuadTransform(graph, { factory: this.factory })
dataset.toStream().pipe(stream)
const res = await this.factory.fetch(graph.value, {
method: truncate ? 'PUT' : 'POST',
body: stream
})
const quadStream = await handleResponse(res)
if (quadStream) {
await chunks(quadStream)
}
}
async _import (stream, options) {
const dataset = await rdf.dataset().import(stream)
if (dataset.size === 0) {
return
}
const graph = [...dataset][0]?.graph
await this._importGraph(graph, dataset, options)
}
async _remove (stream) {
const remove = await rdf.dataset().import(stream)
// do nothing if there are no quads
if (remove.size === 0) {
return
}
const graph = [...remove][0]?.graph
const existing = await rdf.dataset().import(this.match(null, null, null, graph))
const updated = existing.difference(remove)
// don't update if there are no changes
if (updated.size === existing.size) {
return
}
return this._importGraph(graph, updated, { truncate: true })
}
async _removeMatches (subject, predicate, object, graph) {
const existing = await rdf.dataset().import(this.match(null, null, null, graph))
const remove = existing.match(subject, predicate, object)
// don't update if there are no changes
if (remove.size === 0) {
return
}
const updated = existing.difference(remove)
await this._importGraph(graph, updated, { truncate: true })
}
match (subject, predicate, object, graph) {
const stream = new TripleToQuadTransform(graph, { factory: this.factory })
Promise.resolve().then(async () => {
try {
const res = await this.factory.fetch(graph.value)
const quadStream = await handleResponse(res)
const filteredStream = new FilterStream(quadStream, subject, predicate, object)
filteredStream.pipe(stream)
} catch (err) {
stream.destroy(err)
}
})
return stream
}
import (stream, options) {
return promiseToEvent(this._import(stream, options))
}
remove (stream) {
return promiseToEvent(this._remove(stream))
}
removeMatches (subject, predicate, object, graph) {
return promiseToEvent(this._removeMatches(subject, predicate, object, graph))
}
deleteGraph (graph) {
return promiseToEvent(this._deleteGraph(graph))
}
}
export default WebStore