lazy-sparql-result-reader 0.1.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +99 -0
- package/lazy_sparql_result_reader.d.ts +2 -2
- package/lazy_sparql_result_reader.js +78 -2
- package/lazy_sparql_result_reader_bg.wasm +0 -0
- package/package.json +7 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Ioannis Nezis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
lazy-sparql-result-reader
|
|
3
|
+
</h1>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
<a href="https://www.npmjs.com/package/lazy-sparql-result-reader">
|
|
7
|
+
<img alt="npm" src="https://img.shields.io/npm/v/lazy-sparql-result-reader" />
|
|
8
|
+
</a>
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
A fast SPARQL results parser for JavaScript and TypeScript, compiled from Rust via WebAssembly.
|
|
12
|
+
It reads streamed SPARQL query results and calls a callback for each parsed batch of bindings.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
- Processes streaming SPARQL results efficiently.
|
|
19
|
+
- Calls a JavaScript callback for each batch of parsed bindings.
|
|
20
|
+
- Written in Rust for speed and reliability.
|
|
21
|
+
- Fully compatible with TypeScript.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
## Usage Example
|
|
27
|
+
|
|
28
|
+
### 1. Install dependencies
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install lazy-sparql-result-reader vite @vitejs/plugin-wasm
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### 2. Configure Vite for WASM
|
|
35
|
+
|
|
36
|
+
Create or update vite.config.ts:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
import { defineConfig } from 'vite';
|
|
40
|
+
import wasm from '@vitejs/plugin-wasm';
|
|
41
|
+
|
|
42
|
+
export default defineConfig({
|
|
43
|
+
plugins: [wasm()],
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
This allows Vite to correctly load WebAssembly modules.
|
|
48
|
+
|
|
49
|
+
### 3. Use the parser in your app
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import init, { read } from "lazy-sparql-result-reader?init";
|
|
53
|
+
|
|
54
|
+
// Initialize the WASM module
|
|
55
|
+
await init();
|
|
56
|
+
|
|
57
|
+
fetch("https://qlever.dev/api/wikidata", {
|
|
58
|
+
method: 'POST',
|
|
59
|
+
headers: {
|
|
60
|
+
"Accept": "application/sparql-results+json",
|
|
61
|
+
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
|
62
|
+
},
|
|
63
|
+
body: new URLSearchParams({
|
|
64
|
+
query: "SELECT * {?s ?p ?o} LIMIT 1000",
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
.then(async response => {
|
|
68
|
+
if (!response.ok) {
|
|
69
|
+
throw new Error(`SPARQL request failed: ${response.status}`);
|
|
70
|
+
}
|
|
71
|
+
const stream = response.body; // ReadableStream of the SPARQL JSON results
|
|
72
|
+
if (!stream) throw new Error("Response has no body stream");
|
|
73
|
+
// Parse the streamed results with the WASM parser
|
|
74
|
+
await read(stream, 100, (bindings) => {
|
|
75
|
+
console.log("Received batch of bindings:", bindings);
|
|
76
|
+
});
|
|
77
|
+
})
|
|
78
|
+
.catch((err) => {
|
|
79
|
+
console.error("Error fetching or parsing SPARQL results:", err);
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Notes
|
|
84
|
+
|
|
85
|
+
- The **first callback invocation** contains the **SPARQL head**, i.e., the variable names in the result set.
|
|
86
|
+
- Subsequent callback invocations contain batches of bindings as they are parsed.
|
|
87
|
+
- `batch_size` controls how many bindings are buffered before each callback.
|
|
88
|
+
- Ensure your environment supports **ReadableStream** (modern browsers or Node.js >= 18).
|
|
89
|
+
|
|
90
|
+
This setup allows your JS/TS application to process **streaming SPARQL results** efficiently,
|
|
91
|
+
with immediate access to the head and incremental batches of bindings.
|
|
92
|
+
|
|
93
|
+
## License
|
|
94
|
+
|
|
95
|
+
This project is licensed under the **MIT** License.
|
|
96
|
+
|
|
97
|
+
You are free to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the software, under the conditions of the MIT License.
|
|
98
|
+
|
|
99
|
+
For full details, see the [LICENSE](./LICENSE) file.
|
|
@@ -11,12 +11,12 @@ export interface InitOutput {
|
|
|
11
11
|
readonly wasm_bindgen__convert__closures_____invoke__h53040b977ccf16ad: (a: number, b: number, c: any) => void;
|
|
12
12
|
readonly wasm_bindgen__closure__destroy__h9431bfc0c26898f5: (a: number, b: number) => void;
|
|
13
13
|
readonly wasm_bindgen__convert__closures_____invoke__h0204f368625abfd5: (a: number, b: number, c: any, d: any) => void;
|
|
14
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
15
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
14
16
|
readonly __wbindgen_exn_store: (a: number) => void;
|
|
15
17
|
readonly __externref_table_alloc: () => number;
|
|
16
18
|
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
17
19
|
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
18
|
-
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
19
|
-
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
20
20
|
readonly __wbindgen_start: () => void;
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -10,6 +10,71 @@ const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined')
|
|
|
10
10
|
? { register: () => {}, unregister: () => {} }
|
|
11
11
|
: new FinalizationRegistry(state => state.dtor(state.a, state.b));
|
|
12
12
|
|
|
13
|
+
function debugString(val) {
|
|
14
|
+
// primitive types
|
|
15
|
+
const type = typeof val;
|
|
16
|
+
if (type == 'number' || type == 'boolean' || val == null) {
|
|
17
|
+
return `${val}`;
|
|
18
|
+
}
|
|
19
|
+
if (type == 'string') {
|
|
20
|
+
return `"${val}"`;
|
|
21
|
+
}
|
|
22
|
+
if (type == 'symbol') {
|
|
23
|
+
const description = val.description;
|
|
24
|
+
if (description == null) {
|
|
25
|
+
return 'Symbol';
|
|
26
|
+
} else {
|
|
27
|
+
return `Symbol(${description})`;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (type == 'function') {
|
|
31
|
+
const name = val.name;
|
|
32
|
+
if (typeof name == 'string' && name.length > 0) {
|
|
33
|
+
return `Function(${name})`;
|
|
34
|
+
} else {
|
|
35
|
+
return 'Function';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// objects
|
|
39
|
+
if (Array.isArray(val)) {
|
|
40
|
+
const length = val.length;
|
|
41
|
+
let debug = '[';
|
|
42
|
+
if (length > 0) {
|
|
43
|
+
debug += debugString(val[0]);
|
|
44
|
+
}
|
|
45
|
+
for(let i = 1; i < length; i++) {
|
|
46
|
+
debug += ', ' + debugString(val[i]);
|
|
47
|
+
}
|
|
48
|
+
debug += ']';
|
|
49
|
+
return debug;
|
|
50
|
+
}
|
|
51
|
+
// Test for built-in
|
|
52
|
+
const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
|
|
53
|
+
let className;
|
|
54
|
+
if (builtInMatches && builtInMatches.length > 1) {
|
|
55
|
+
className = builtInMatches[1];
|
|
56
|
+
} else {
|
|
57
|
+
// Failed to match the standard '[object ClassName]'
|
|
58
|
+
return toString.call(val);
|
|
59
|
+
}
|
|
60
|
+
if (className == 'Object') {
|
|
61
|
+
// we're a user defined class or Object
|
|
62
|
+
// JSON.stringify avoids problems with cycles, and is generally much
|
|
63
|
+
// easier than looping through ownProperties of `val`.
|
|
64
|
+
try {
|
|
65
|
+
return 'Object(' + JSON.stringify(val) + ')';
|
|
66
|
+
} catch (_) {
|
|
67
|
+
return 'Object';
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// errors
|
|
71
|
+
if (val instanceof Error) {
|
|
72
|
+
return `${val.name}: ${val.message}\n${val.stack}`;
|
|
73
|
+
}
|
|
74
|
+
// TODO we could test for more things here, like `Set`s and `Map`s.
|
|
75
|
+
return className;
|
|
76
|
+
}
|
|
77
|
+
|
|
13
78
|
function getArrayU8FromWasm0(ptr, len) {
|
|
14
79
|
ptr = ptr >>> 0;
|
|
15
80
|
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
@@ -201,11 +266,22 @@ function __wbg_get_imports() {
|
|
|
201
266
|
const ret = Error(getStringFromWasm0(arg0, arg1));
|
|
202
267
|
return ret;
|
|
203
268
|
};
|
|
269
|
+
imports.wbg.__wbg_Number_2d1dcfcf4ec51736 = function(arg0) {
|
|
270
|
+
const ret = Number(arg0);
|
|
271
|
+
return ret;
|
|
272
|
+
};
|
|
204
273
|
imports.wbg.__wbg___wbindgen_boolean_get_dea25b33882b895b = function(arg0) {
|
|
205
274
|
const v = arg0;
|
|
206
275
|
const ret = typeof(v) === 'boolean' ? v : undefined;
|
|
207
276
|
return isLikeNone(ret) ? 0xFFFFFF : ret ? 1 : 0;
|
|
208
277
|
};
|
|
278
|
+
imports.wbg.__wbg___wbindgen_debug_string_adfb662ae34724b6 = function(arg0, arg1) {
|
|
279
|
+
const ret = debugString(arg1);
|
|
280
|
+
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
281
|
+
const len1 = WASM_VECTOR_LEN;
|
|
282
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
283
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
284
|
+
};
|
|
209
285
|
imports.wbg.__wbg___wbindgen_is_function_8d400b8b1af978cd = function(arg0) {
|
|
210
286
|
const ret = typeof(arg0) === 'function';
|
|
211
287
|
return ret;
|
|
@@ -379,8 +455,8 @@ function __wbg_get_imports() {
|
|
|
379
455
|
const ret = getStringFromWasm0(arg0, arg1);
|
|
380
456
|
return ret;
|
|
381
457
|
};
|
|
382
|
-
imports.wbg.
|
|
383
|
-
// Cast intrinsic for `Closure(Closure { dtor_idx:
|
|
458
|
+
imports.wbg.__wbindgen_cast_27d8f2a9ce018ee0 = function(arg0, arg1) {
|
|
459
|
+
// Cast intrinsic for `Closure(Closure { dtor_idx: 43, function: Function { arguments: [Externref], shim_idx: 44, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
|
|
384
460
|
const ret = makeMutClosure(arg0, arg1, wasm.wasm_bindgen__closure__destroy__h9431bfc0c26898f5, wasm_bindgen__convert__closures_____invoke__h53040b977ccf16ad);
|
|
385
461
|
return ret;
|
|
386
462
|
};
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lazy-sparql-result-reader",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"
|
|
4
|
+
"description": "A lazy sparql result reader",
|
|
5
|
+
"version": "1.0.1",
|
|
6
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/IoannisNezis/Lazy-SPARQL-result-reader"
|
|
10
|
+
},
|
|
5
11
|
"files": [
|
|
6
12
|
"lazy_sparql_result_reader_bg.wasm",
|
|
7
13
|
"lazy_sparql_result_reader.js",
|