-
Notifications
You must be signed in to change notification settings - Fork 7
/
StreamClient.js
106 lines (103 loc) · 3.04 KB
/
StreamClient.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
import defaultFactory from '@rdfjs/data-model'
import isDataFactory from './lib/isDataFactory.js'
import SimpleClient from './SimpleClient.js'
import StreamQuery from './StreamQuery.js'
import StreamStore from './StreamStore.js'
/**
* The default client implementation based on {@link StreamQuery} and {@link StreamStore} parses SPARQL results into
* Readable streams of RDF/JS Quad objects (CONSTRUCT/DESCRIBE) or Readable streams of objects (SELECT). Graph Store
* read and write operations are handled using Readable streams.
*
* @extends SimpleClient
* @property {StreamQuery} query
* @property {StreamStore} store
*
* @example
* // read the height of the Eiffel Tower from Wikidata with a SELECT query
*
* import SparqlClient from 'sparql-http-client'
*
* const endpointUrl = 'https://query.wikidata.org/sparql'
* const query = `
* PREFIX wd: <http://www.wikidata.org/entity/>
* PREFIX p: <http://www.wikidata.org/prop/>
* PREFIX ps: <http://www.wikidata.org/prop/statement/>
* PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
*
* SELECT ?value WHERE {
* wd:Q243 p:P2048 ?height.
*
* ?height pq:P518 wd:Q24192182;
* ps:P2048 ?value .
* }`
*
* const client = new SparqlClient({ endpointUrl })
* const stream = client.query.select(query)
*
* stream.on('data', row => {
* for (const [key, value] of Object.entries(row)) {
* console.log(`${key}: ${value.value} (${value.termType})`)
* }
* })
*
* stream.on('error', err => {
* console.error(err)
* })
*
* @example
* // read all quads from a local triplestore using the Graph Store protocol
*
* import rdf from 'rdf-ext'
* import SparqlClient from 'sparql-http-client'
*
* const client = new SparqlClient({
* storeUrl: 'http://localhost:3030/test/data',
* factory: rdf
* })
*
* const stream = local.store.get(rdf.defaultGraph())
*
* stream.on('data', quad => {
* console.log(`${quad.subject} ${quad.predicate} ${quad.object}`)
* })
*/
class StreamClient extends SimpleClient {
/**
* @param {Object} options
* @param {string} [options.endpointUrl] SPARQL query endpoint URL
* @param {factory} [options.factory] RDF/JS factory
* @param {fetch} [options.fetch=nodeify-fetch] fetch implementation
* @param {Headers} [options.headers] headers sent with every request
* @param {string} [options.password] password used for basic authentication
* @param {string} [options.storeUrl] SPARQL Graph Store URL
* @param {string} [options.updateUrl] SPARQL update endpoint URL
* @param {string} [options.user] user used for basic authentication
*/
constructor ({
endpointUrl,
factory = defaultFactory,
fetch,
headers,
password,
storeUrl,
updateUrl,
user
}) {
super({
endpointUrl,
factory,
fetch,
headers,
password,
storeUrl,
updateUrl,
user,
Query: StreamQuery,
Store: StreamStore
})
if (!isDataFactory(this.factory)) {
throw new Error('the given factory doesn\'t implement the DataFactory interface')
}
}
}
export default StreamClient