-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsparql.js
More file actions
277 lines (245 loc) · 8.4 KB
/
sparql.js
File metadata and controls
277 lines (245 loc) · 8.4 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import httpContext from 'express-http-context';
import env from 'env-var';
import SPARQL from './sparql-tag';
import DigestFetch from "digest-fetch";
const LOG_SPARQL_QUERIES = process.env.LOG_SPARQL_QUERIES != undefined ? env.get('LOG_SPARQL_QUERIES').asBool() : env.get('LOG_SPARQL_ALL').asBool();
const LOG_SPARQL_UPDATES = process.env.LOG_SPARQL_UPDATES != undefined ? env.get('LOG_SPARQL_UPDATES').asBool() : env.get('LOG_SPARQL_ALL').asBool();
const DEBUG_AUTH_HEADERS = env.get('DEBUG_AUTH_HEADERS').asBool();
const MU_SPARQL_ENDPOINT = env.get('MU_SPARQL_ENDPOINT').default('http://database:8890/sparql').asString();
const RETRY = env.get('MU_QUERY_RETRY').default('false').asBool();
const RETRY_MAX_ATTEMPTS = env.get('MU_QUERY_RETRY_MAX_ATTEMPTS').default('5').asInt();
const RETRY_FOR_HTTP_STATUS_CODES = env.get('MU_QUERY_RETRY_FOR_HTTP_STATUS_CODES').default('').asArray();
const RETRY_FOR_CONNECTION_ERRORS = env.get('MU_QUERY_RETRY_FOR_CONNECTION_ERRORS').default('ECONNRESET,ETIMEDOUT,EAI_AGAIN').asArray();
const RETRY_TIMEOUT_INCREMENT_FACTOR = env.get('MU_QUERY_RETRY_TIMEOUT_INCREMENT_FACTOR').default('0.1').asFloat();
//==-- logic --==//
// executes a query (you can use the template syntax)
function query( queryString, extraHeaders = {}, connectionOptions = {} ) {
if (LOG_SPARQL_QUERIES) {
console.log(queryString);
}
return executeQuery(queryString, extraHeaders, connectionOptions);
};
// executes an update query
function update(queryString, extraHeaders = {}, connectionOptions = {}) {
if (LOG_SPARQL_UPDATES) {
console.log(queryString);
}
return executeQuery(queryString);
};
function defaultHeaders() {
const headers = new Headers();
headers.set("content-type", "application/x-www-form-urlencoded");
headers.set("Accept", "application/sparql-results+json");
if (httpContext.get("request")) {
headers.set(
"mu-session-id",
httpContext.get("request").get("mu-session-id")
);
headers.set("mu-call-id", httpContext.get("request").get("mu-call-id"));
}
return headers;
}
async function executeQuery(queryString, extraHeaders = {}, connectionOptions = {}, attempt = 0)
{
const sparqlEndpoint = connectionOptions.sparqlEndpoint ?? MU_SPARQL_ENDPOINT;
const headers = defaultHeaders();
for (const key of Object.keys(extraHeaders)) {
headers.append(key, extraHeaders[key]);
}
if (DEBUG_AUTH_HEADERS) {
const stringifiedHeaders = Array.from(headers.entries())
.filter(([key]) => key.startsWith("mu-"))
.map(([key, value]) => `${key}: ${value}`)
.join("\n");
console.log(`Headers set on SPARQL client: ${stringifiedHeaders}`);
}
try {
// note that URLSearchParams is used because it correctly encodes for form-urlencoded
const formData = new URLSearchParams();
formData.set("query", queryString);
headers.append("Content-Length", formData.toString().length.toString());
let response;
if (connectionOptions.authUser && connectionOptions.authPassword) {
const client = new DigestFetch(
connectionOptions.authUser,
connectionOptions.authPassword,
{ basic: connectionOptions.authType === "basic" }
);
response = await client.fetch(sparqlEndpoint, {
method: "POST",
body: formData.toString(),
headers,
});
} else {
response = await fetch(sparqlEndpoint, {
method: "POST",
body: formData.toString(),
headers,
});
}
updateResponseHeaders(response);
if (response.ok) {
return await maybeJSON(response);
} else {
throw new Error(`HTTP Error Response: ${response.status} ${response.statusText}`);
}
} catch (ex) {
if (mayRetry(ex, attempt, connectionOptions)) {
attempt += 1;
const sleepTime = nextAttemptTimeout(attempt);
console.log(`Sleeping ${sleepTime} ms before next attempt`);
await new Promise((r) => setTimeout(r, sleepTime));
return await executeRawQuery(
queryString,
extraHeaders,
connectionOptions,
attempt
);
} else {
console.log(`Failed Query:
${queryString}`);
throw ex;
}
}
}
function updateResponseHeaders(response){
// update the outgoing response headers with the headers received from the SPARQL endpoint
if (httpContext.get('response') && !httpContext.get('response').headersSent) {
// set mu-auth-allowed-groups on outgoing response
const allowedGroups = response.headers.get('mu-auth-allowed-groups');
if (allowedGroups) {
httpContext.get('response').setHeader('mu-auth-allowed-groups', allowedGroups);
if (DEBUG_AUTH_HEADERS) {
console.log(`Update mu-auth-allowed-groups to ${allowedGroups}`);
}
} else {
httpContext.get('response').removeHeader('mu-auth-allowed-groups');
if (DEBUG_AUTH_HEADERS) {
console.log('Remove mu-auth-allowed-groups');
}
}
// set mu-auth-used-groups on outgoing response
const usedGroups = response.headers.get('mu-auth-used-groups');
if (usedGroups) {
httpContext.get('response').setHeader('mu-auth-used-groups', usedGroups);
if (DEBUG_AUTH_HEADERS) {
console.log(`Update mu-auth-used-groups to ${usedGroups}`);
}
} else {
httpContext.get('response').removeHeader('mu-auth-used-groups');
if (DEBUG_AUTH_HEADERS) {
console.log('Remove mu-auth-used-groups');
}
}
}
}
async function maybeJSON(response) {
try {
return await response.json();
} catch (e) {
return null;
}
}
function mayRetry(
error,
attempt,
connectionOptions = {}
) {
console.log(
`Checking retry allowed for error: ${error} and attempt: ${attempt}`
);
let mayRetry = false;
if (!(RETRY || connectionOptions.mayRetry)) {
mayRetry = false;
} else if (attempt < RETRY_MAX_ATTEMPTS) {
if (error.code && RETRY_FOR_CONNECTION_ERRORS.includes(error.code)) {
mayRetry = true;
} else if ( error.httpStatus && RETRY_FOR_HTTP_STATUS_CODES.includes(`${error.httpStatus}`) ) {
mayRetry = true;
}
}
console.log(`Retry allowed? ${mayRetry}`);
return mayRetry;
}
function nextAttemptTimeout(attempt) {
// expected to be milliseconds
return Math.round(RETRY_TIMEOUT_INCREMENT_FACTOR * Math.exp(attempt + 10));
}
function sparqlEscapeString( value ){
return '"""' + value.replace(/[\\"]/g, function(match) { return '\\' + match; }) + '"""';
};
function sparqlEscapeUri( value ){
return '<' + value.replace(/[\\"<>]/g, function(match) { return '\\' + match; }) + '>';
};
function sparqlEscapeDecimal( value ){
return '"' + Number.parseFloat(value) + '"^^xsd:decimal';
};
function sparqlEscapeInt( value ){
return '"' + Number.parseInt(value) + '"^^xsd:integer';
};
function sparqlEscapeFloat( value ){
return '"' + Number.parseFloat(value) + '"^^xsd:float';
};
function sparqlEscapeDate( value ){
return '"' + new Date(value).toISOString().substring(0, 10) + '"^^xsd:date'; // only keep 'YYYY-MM-DD' portion of the string
};
function sparqlEscapeDateTime( value ){
return '"' + new Date(value).toISOString() + '"^^xsd:dateTime';
};
function sparqlEscapeBool( value ){
return value ? '"true"^^xsd:boolean' : '"false"^^xsd:boolean';
};
function sparqlEscape( value, type ){
switch(type) {
case 'string':
return sparqlEscapeString(value);
case 'uri':
return sparqlEscapeUri(value);
case 'bool':
return sparqlEscapeBool(value);
case 'decimal':
return sparqlEscapeDecimal(value);
case 'int':
return sparqlEscapeInt(value);
case 'float':
return sparqlEscapeFloat(value);
case 'date':
return sparqlEscapeDate(value);
case 'dateTime':
return sparqlEscapeDateTime(value);
default:
console.error(`WARN: Unknown escape type '${type}'. Escaping as string`);
return sparqlEscapeString(value);
}
}
//==-- exports --==//
const exports = {
SPARQL: SPARQL,
sparql: SPARQL,
query: query,
update: update,
sparqlEscape: sparqlEscape,
sparqlEscapeString: sparqlEscapeString,
sparqlEscapeUri: sparqlEscapeUri,
sparqlEscapeInt: sparqlEscapeInt,
sparqlEscapeFloat: sparqlEscapeFloat,
sparqlEscapeDate: sparqlEscapeDate,
sparqlEscapeDateTime: sparqlEscapeDateTime,
sparqlEscapeBool: sparqlEscapeBool
}
export default exports;
export {
SPARQL as SPARQL,
SPARQL as sparql,
query,
update,
sparqlEscape,
sparqlEscapeString,
sparqlEscapeUri,
sparqlEscapeDecimal,
sparqlEscapeInt,
sparqlEscapeFloat,
sparqlEscapeDate,
sparqlEscapeDateTime,
sparqlEscapeBool
};