forked from rdf-ext/sparql-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamQuery.js
More file actions
94 lines (75 loc) · 2.62 KB
/
StreamQuery.js
File metadata and controls
94 lines (75 loc) · 2.62 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
const rdf = require('@rdfjs/data-model')
const N3Parser = require('@rdfjs/parser-n3')
const checkResponse = require('./lib/checkResponse')
const RawQuery = require('./RawQuery')
const ResultParser = require('./ResultParser')
/**
* Extends RawQuery by wrapping response body streams as RDF/JS Streams
*/
class StreamQuery extends RawQuery {
/**
* @param {Object} init
* @param {Endpoint} init.endpoint
* @param {DataFactory} [init.factory=@rdfjs/data-model]
*/
constructor ({ endpoint, factory = rdf }) {
super({ endpoint })
/** @member {DataFactory} */
this.factory = factory
}
/**
* @param {string} query SPARQL ASK query
* @param {Object} [init]
* @param {HeadersInit} [init.headers] HTTP request headers
* @param {'get'|'postUrlencoded'|'postDirect'} [init.operation='get']
* @return {Promise<boolean>}
*/
async ask (query, { headers, operation } = {}) {
const res = await super.ask(query, { headers, operation })
await checkResponse(res)
const json = await res.json()
return json.boolean
}
/**
* @param {string} query SPARQL query
* @param {Object} [init]
* @param {HeadersInit} [init.headers] HTTP request headers
* @param {'get'|'postUrlencoded'|'postDirect'} [init.operation='get']
* @return {Promise<Stream>}
*/
async construct (query, { headers, operation } = {}) {
headers = new this.endpoint.fetch.Headers(headers)
if (!headers.has('accept')) {
headers.set('accept', 'application/n-triples, text/turtle')
}
const res = await super.construct(query, { headers, operation })
await checkResponse(res)
const parser = new N3Parser({ factory: this.factory })
return parser.import(res.body)
}
/**
* @param {string} query SPARQL query
* @param {Object} [init]
* @param {HeadersInit} [init.headers] HTTP request headers
* @param {'get'|'postUrlencoded'|'postDirect'} [init.operation='get']
* @return {Promise<Stream>}
*/
async select (query, { headers, operation } = {}) {
const res = await super.select(query, { headers, operation })
await checkResponse(res)
const parser = new ResultParser({ factory: this.factory })
return res.body.pipe(parser)
}
/**
* @param {string} query SPARQL query
* @param {Object} [init]
* @param {HeadersInit} [init.headers] HTTP request headers
* @param {'get'|'postUrlencoded'|'postDirect'} [init.operation='postUrlencoded']
* @return {Promise<void>}
*/
async update (query, { headers, operation } = {}) {
const res = await super.update(query, { headers, operation })
await checkResponse(res)
}
}
module.exports = StreamQuery