|
| 1 | +import { QueryEngine } from "@comunica/query-sparql"; |
| 2 | +import { |
| 3 | + getDefaultSession, |
| 4 | + fetch as authFetch, |
| 5 | +} from "@inrupt/solid-client-authn-browser"; |
| 6 | + |
| 7 | +/** |
| 8 | + * A class wrapping a Comunica engine, used for all but login actions. |
| 9 | + */ |
| 10 | +class ComunicaEngineWrapper { |
| 11 | + |
| 12 | + _engine; |
| 13 | + _fetchSuccess; |
| 14 | + _fetchStatusNumber; |
| 15 | + _underlyingFetchFunction; |
| 16 | + |
| 17 | + constructor() { |
| 18 | + this._engine = new QueryEngine(); |
| 19 | + this._fetchSuccess = {}; |
| 20 | + this._fetchStatusNumber = {}; |
| 21 | + this._underlyingFetchFunction = undefined; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Resets the engine and all we maintained here about executed queries |
| 26 | + */ |
| 27 | + reset() { |
| 28 | + this._engine.invalidateHttpCache(); |
| 29 | + this._fetchSuccess = {}; |
| 30 | + this._fetchStatusNumber = {}; |
| 31 | + this._underlyingFetchFunction = undefined; |
| 32 | + } |
| 33 | + |
| 34 | + getFetchSuccess(arg) { |
| 35 | + return this._fetchSuccess[arg]; |
| 36 | + } |
| 37 | + |
| 38 | + getFetchStatusNumber(arg) { |
| 39 | + return this._fetchStatusNumber[arg]; |
| 40 | + } |
| 41 | + |
| 42 | + getUnderlyingFetchFunction() { |
| 43 | + return this._underlyingFetchFunction; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Executes one generic SPARQL query with the Comunica engine |
| 48 | + * |
| 49 | + * Support the following callback functions. Forward only the ones you need. |
| 50 | + * - "variables": will be called once with an array of variable names, in case of a SELECT query |
| 51 | + * - "bindings": will be called for every bindings combo, in case of a SELECT query |
| 52 | + * - "quads": will be called for every quad, in case of a CONSTRUCT query |
| 53 | + * - "boolean": will be called for the resulting boolean, in case of an ASK query |
| 54 | + * |
| 55 | + * @param {string} queryText - the SPARQL query text |
| 56 | + * @param {object} context - the context to provide to the Comunica engine |
| 57 | + * @param {object} callbacks - an object contains the callback functions you specify |
| 58 | + * @returns {void} when the query has finished |
| 59 | + */ |
| 60 | + async query(queryText, context, callbacks) { |
| 61 | + try { |
| 62 | + this._prepareQuery(context); |
| 63 | + let result = await this._engine.query(queryText, context); |
| 64 | + switch (result.resultType) { |
| 65 | + case 'bindings': |
| 66 | + const metadata = await result.metadata(); |
| 67 | + const variables = metadata.variables.map((val) => { |
| 68 | + return val.value; |
| 69 | + }); |
| 70 | + if (callbacks["variables"]) { |
| 71 | + callbacks["variables"](variables); |
| 72 | + } |
| 73 | + const bindingsStream = await result.execute(); |
| 74 | + await new Promise((resolve, reject) => { |
| 75 | + if (callbacks["bindings"]) { |
| 76 | + bindingsStream.on('data', (bindings) => { |
| 77 | + callbacks["bindings"](bindings); |
| 78 | + }); |
| 79 | + } |
| 80 | + bindingsStream.on('end', resolve); |
| 81 | + bindingsStream.on('error', reject); |
| 82 | + }); |
| 83 | + break; |
| 84 | + case 'quads': |
| 85 | + const quadStream = await result.execute(); |
| 86 | + await new Promise((resolve, reject) => { |
| 87 | + if (callbacks["quads"]) { |
| 88 | + quadStream.on('data', (quad) => { |
| 89 | + callbacks["quads"](quad); |
| 90 | + }); |
| 91 | + } |
| 92 | + quadStream.on('end', resolve); |
| 93 | + quadStream.on('error', reject); |
| 94 | + }); |
| 95 | + break; |
| 96 | + case 'boolean': |
| 97 | + const answer = await result.execute(); |
| 98 | + if (callbacks["boolean"]) { |
| 99 | + callbacks["boolean"](answer); |
| 100 | + } |
| 101 | + break; |
| 102 | + default: |
| 103 | + break; |
| 104 | + } |
| 105 | + } catch (error) { |
| 106 | + this.reset(); |
| 107 | + throw error; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Executes one SPARQL SELECT query with the Comunica engine |
| 113 | + * |
| 114 | + * @param {string} queryText - the SPARQL SELECT query text |
| 115 | + * @param {object} context - the context to provide to the Comunica engine |
| 116 | + * @returns {Promise <BindingsStream>} Promis to the bindings stream |
| 117 | + */ |
| 118 | + async queryBindings(queryText, context) { |
| 119 | + try { |
| 120 | + this._prepareQuery(context); |
| 121 | + return this._engine.queryBindings(queryText, context); |
| 122 | + } catch (error) { |
| 123 | + this.reset(); |
| 124 | + throw error; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Prepares a call to any engine's query function |
| 130 | + * |
| 131 | + * @param {object} context - the context that will be used |
| 132 | + */ |
| 133 | + _prepareQuery(context) { |
| 134 | + // avoid faulty fetch status for sources cached in Comunica |
| 135 | + for (const source of context.sources) { |
| 136 | + this._fetchSuccess[source] = true; |
| 137 | + } |
| 138 | + this._underlyingFetchFunction = fetch; |
| 139 | + if (getDefaultSession().info.isLoggedIn) { |
| 140 | + this._underlyingFetchFunction = authFetch; |
| 141 | + } |
| 142 | + context.fetch = ComunicaEngineWrapper._getWrappedFetchFunction(this._underlyingFetchFunction, this); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Returns a function that wraps the underlying fetch function and sets the fetch success information in member variables of _this. |
| 147 | + * |
| 148 | + * @param underlyingFetchFunction - the underlying fetch functin |
| 149 | + * @param {ComunicaEngineWrapper} _this - the calling ComunicaEngineWrapper object |
| 150 | + * @returns {Function} that function. |
| 151 | + */ |
| 152 | + static _getWrappedFetchFunction(underlyingFetchFunction, _this) { |
| 153 | + const wrappedFetchFunction = async (arg) => { |
| 154 | + try { |
| 155 | + const response = await underlyingFetchFunction(arg, { |
| 156 | + headers: { |
| 157 | + Accept: "application/n-quads,application/trig;q=0.9,text/turtle;q=0.8,application/n-triples;q=0.7,*/*;q=0.1" |
| 158 | + } |
| 159 | + }); |
| 160 | + _this._fetchSuccess[arg] = response.ok; |
| 161 | + _this._fetchStatusNumber[arg] = response.status; |
| 162 | + return response; |
| 163 | + } |
| 164 | + catch (error) { |
| 165 | + _this._fetchSuccess[arg] = false; |
| 166 | + throw error; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return wrappedFetchFunction; |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +const comunicaEngineWrapper = new ComunicaEngineWrapper(); |
| 175 | +export default comunicaEngineWrapper; |
0 commit comments