@lde/pipeline 0.28.1 → 0.28.2
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/README.md
CHANGED
|
@@ -103,6 +103,15 @@ const executor = new SparqlConstructExecutor({
|
|
|
103
103
|
});
|
|
104
104
|
```
|
|
105
105
|
|
|
106
|
+
When querying endpoints that return line-oriented formats like N-Triples (e.g. QLever), enable `lineBuffer` to work around an [N3.js chunk-splitting bug](https://github.com/rdfjs/N3.js/issues/578) that causes intermittent parse errors on large responses:
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
const executor = new SparqlConstructExecutor({
|
|
110
|
+
query: 'CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }',
|
|
111
|
+
lineBuffer: true,
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
106
115
|
`Executor` is an interface, so you can implement your own for logic that's hard to express in pure SPARQL — for example, cleaning up messy date notations or converting locale-specific dates to ISO 8601. The decorator pattern lets you wrap a SPARQL executor and post-process its quad stream in TypeScript:
|
|
107
116
|
|
|
108
117
|
```typescript
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Dataset, Distribution } from '@lde/dataset';
|
|
2
2
|
import { SparqlEndpointFetcher } from 'fetch-sparql-endpoint';
|
|
3
3
|
import type { NamedNode, Quad } from '@rdfjs/types';
|
|
4
|
+
import { Transform } from 'node:stream';
|
|
4
5
|
/**
|
|
5
6
|
* An executor could not run because the dataset lacks a supported distribution.
|
|
6
7
|
*/
|
|
@@ -42,6 +43,17 @@ export interface SparqlConstructExecutorOptions {
|
|
|
42
43
|
* Optional custom SparqlEndpointFetcher instance.
|
|
43
44
|
*/
|
|
44
45
|
fetcher?: SparqlEndpointFetcher;
|
|
46
|
+
/**
|
|
47
|
+
* Buffer complete lines before passing them to the N3 parser.
|
|
48
|
+
*
|
|
49
|
+
* Works around an [N3.js bug](https://github.com/rdfjs/N3.js/issues/578)
|
|
50
|
+
* where language tags (e.g. `@nl-nl`) split across HTTP chunk boundaries
|
|
51
|
+
* cause parse errors. Enable this when querying endpoints that return
|
|
52
|
+
* line-oriented formats such as N-Triples (e.g. QLever).
|
|
53
|
+
*
|
|
54
|
+
* @default false
|
|
55
|
+
*/
|
|
56
|
+
lineBuffer?: boolean;
|
|
45
57
|
}
|
|
46
58
|
/**
|
|
47
59
|
* A streaming SPARQL CONSTRUCT executor.
|
|
@@ -75,6 +87,7 @@ export declare class SparqlConstructExecutor implements Executor {
|
|
|
75
87
|
private readonly preParsed?;
|
|
76
88
|
private readonly fetcher;
|
|
77
89
|
private readonly retries;
|
|
90
|
+
private readonly lineBuffer;
|
|
78
91
|
private readonly generator;
|
|
79
92
|
constructor(options: SparqlConstructExecutorOptions);
|
|
80
93
|
/**
|
|
@@ -86,6 +99,12 @@ export declare class SparqlConstructExecutor implements Executor {
|
|
|
86
99
|
* @returns AsyncIterable<Quad> stream of results.
|
|
87
100
|
*/
|
|
88
101
|
execute(dataset: Dataset, distribution: Distribution, options?: ExecuteOptions): Promise<AsyncIterable<Quad>>;
|
|
102
|
+
/**
|
|
103
|
+
* Fetch quads from the endpoint, optionally line-buffering the response
|
|
104
|
+
* stream before it reaches the N3 parser to work around
|
|
105
|
+
* {@link https://github.com/rdfjs/N3.js/issues/578 | N3.js#578}.
|
|
106
|
+
*/
|
|
107
|
+
private fetchQuads;
|
|
89
108
|
/**
|
|
90
109
|
* Create an executor from a query file.
|
|
91
110
|
*
|
|
@@ -98,4 +117,29 @@ export declare class SparqlConstructExecutor implements Executor {
|
|
|
98
117
|
* Read a SPARQL query from a file.
|
|
99
118
|
*/
|
|
100
119
|
export declare function readQueryFile(filename: string): Promise<string>;
|
|
120
|
+
/**
|
|
121
|
+
* Buffers incoming data until complete lines (`\n`-terminated) are available,
|
|
122
|
+
* then pushes them downstream as a single chunk.
|
|
123
|
+
*
|
|
124
|
+
* **Why this exists:** `fetch-sparql-endpoint` pipes the raw HTTP response
|
|
125
|
+
* stream directly into N3.js's `StreamParser`. N3.js has a bug
|
|
126
|
+
* ({@link https://github.com/rdfjs/N3.js/issues/578 | N3.js#578}) where
|
|
127
|
+
* tokens that straddle chunk boundaries — most commonly language tags like
|
|
128
|
+
* `@nl-nl` — cause spurious `Unexpected "-nl"` parse errors. The error is
|
|
129
|
+
* non-deterministic and typically surfaces only on responses larger than
|
|
130
|
+
* ~12 MB, because that is when HTTP chunking starts splitting mid-token.
|
|
131
|
+
*
|
|
132
|
+
* By ensuring each chunk passed to the parser ends on a line boundary, we
|
|
133
|
+
* prevent any N-Triples token from being split. Memory overhead is minimal:
|
|
134
|
+
* at most one partial line is buffered at a time.
|
|
135
|
+
*
|
|
136
|
+
* This transform can be removed once N3.js#578 is fixed upstream.
|
|
137
|
+
*
|
|
138
|
+
* @see https://github.com/rdfjs/N3.js/issues/578
|
|
139
|
+
*/
|
|
140
|
+
export declare class LineBufferTransform extends Transform {
|
|
141
|
+
private remainder;
|
|
142
|
+
_transform(chunk: Buffer, _encoding: string, callback: () => void): void;
|
|
143
|
+
_flush(callback: () => void): void;
|
|
144
|
+
}
|
|
101
145
|
//# sourceMappingURL=executor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../src/sparql/executor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,YAAY,EAAiB,MAAM,cAAc,CAAC;AACpE,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../src/sparql/executor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,YAAY,EAAiB,MAAM,cAAc,CAAC;AACpE,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,KAAK,EAAE,SAAS,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;IACG,OAAO,CACX,OAAO,EAAE,OAAO,EAChB,YAAY,EAAE,YAAY,EAC1B,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;IAwC/B;;;;OAIG;YACW,UAAU;IAmBxB;;;;;OAKG;WACiB,QAAQ,CAC1B,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,IAAI,CAAC,8BAA8B,EAAE,OAAO,CAAC,GACtD,OAAO,CAAC,uBAAuB,CAAC;CAIpC;AAED;;GAEG;AACH,wBAAsB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAErE;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,mBAAoB,SAAQ,SAAS;IAChD,OAAO,CAAC,SAAS,CAAM;IAEd,UAAU,CACjB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,IAAI;IAWb,MAAM,CAAC,QAAQ,EAAE,MAAM,IAAI;CAMrC"}
|
package/dist/sparql/executor.js
CHANGED
|
@@ -2,6 +2,8 @@ import { assertSafeIri } from '@lde/dataset';
|
|
|
2
2
|
import { SparqlEndpointFetcher } from 'fetch-sparql-endpoint';
|
|
3
3
|
import { readFile } from 'node:fs/promises';
|
|
4
4
|
import { resolve } from 'node:path';
|
|
5
|
+
import { Transform } from 'node:stream';
|
|
6
|
+
import { StreamParser } from 'n3';
|
|
5
7
|
import { Parser } from '@traqula/parser-sparql-1-1';
|
|
6
8
|
import { Generator } from '@traqula/generator-sparql-1-1';
|
|
7
9
|
import isNetworkError from 'is-network-error';
|
|
@@ -49,10 +51,12 @@ export class SparqlConstructExecutor {
|
|
|
49
51
|
preParsed;
|
|
50
52
|
fetcher;
|
|
51
53
|
retries;
|
|
54
|
+
lineBuffer;
|
|
52
55
|
generator = new Generator();
|
|
53
56
|
constructor(options) {
|
|
54
57
|
this.rawQuery = options.query;
|
|
55
58
|
this.retries = options.retries ?? 3;
|
|
59
|
+
this.lineBuffer = options.lineBuffer ?? false;
|
|
56
60
|
if (!options.query.includes('#subjectFilter#')) {
|
|
57
61
|
const parsed = new Parser().parse(options.query);
|
|
58
62
|
if (parsed.type !== 'query' || parsed.subType !== 'construct') {
|
|
@@ -98,11 +102,25 @@ export class SparqlConstructExecutor {
|
|
|
98
102
|
let query = this.generator.generate(ast);
|
|
99
103
|
assertSafeIri(dataset.iri.toString());
|
|
100
104
|
query = query.replaceAll('?dataset', `<${dataset.iri}>`);
|
|
101
|
-
return await pRetry(() => this.
|
|
105
|
+
return await pRetry(() => this.fetchQuads(endpoint.toString(), query), {
|
|
102
106
|
retries: this.retries,
|
|
103
107
|
shouldRetry: ({ error }) => isTransientError(error),
|
|
104
108
|
});
|
|
105
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Fetch quads from the endpoint, optionally line-buffering the response
|
|
112
|
+
* stream before it reaches the N3 parser to work around
|
|
113
|
+
* {@link https://github.com/rdfjs/N3.js/issues/578 | N3.js#578}.
|
|
114
|
+
*/
|
|
115
|
+
async fetchQuads(endpoint, query) {
|
|
116
|
+
if (!this.lineBuffer) {
|
|
117
|
+
return this.fetcher.fetchTriples(endpoint, query);
|
|
118
|
+
}
|
|
119
|
+
const [contentType, , responseStream] = await this.fetcher.fetchRawStream(endpoint, query, SparqlEndpointFetcher.CONTENTTYPE_TURTLE);
|
|
120
|
+
return responseStream
|
|
121
|
+
.pipe(new LineBufferTransform())
|
|
122
|
+
.pipe(new StreamParser({ format: contentType }));
|
|
123
|
+
}
|
|
106
124
|
/**
|
|
107
125
|
* Create an executor from a query file.
|
|
108
126
|
*
|
|
@@ -120,6 +138,44 @@ export class SparqlConstructExecutor {
|
|
|
120
138
|
export async function readQueryFile(filename) {
|
|
121
139
|
return (await readFile(resolve(filename))).toString();
|
|
122
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Buffers incoming data until complete lines (`\n`-terminated) are available,
|
|
143
|
+
* then pushes them downstream as a single chunk.
|
|
144
|
+
*
|
|
145
|
+
* **Why this exists:** `fetch-sparql-endpoint` pipes the raw HTTP response
|
|
146
|
+
* stream directly into N3.js's `StreamParser`. N3.js has a bug
|
|
147
|
+
* ({@link https://github.com/rdfjs/N3.js/issues/578 | N3.js#578}) where
|
|
148
|
+
* tokens that straddle chunk boundaries — most commonly language tags like
|
|
149
|
+
* `@nl-nl` — cause spurious `Unexpected "-nl"` parse errors. The error is
|
|
150
|
+
* non-deterministic and typically surfaces only on responses larger than
|
|
151
|
+
* ~12 MB, because that is when HTTP chunking starts splitting mid-token.
|
|
152
|
+
*
|
|
153
|
+
* By ensuring each chunk passed to the parser ends on a line boundary, we
|
|
154
|
+
* prevent any N-Triples token from being split. Memory overhead is minimal:
|
|
155
|
+
* at most one partial line is buffered at a time.
|
|
156
|
+
*
|
|
157
|
+
* This transform can be removed once N3.js#578 is fixed upstream.
|
|
158
|
+
*
|
|
159
|
+
* @see https://github.com/rdfjs/N3.js/issues/578
|
|
160
|
+
*/
|
|
161
|
+
export class LineBufferTransform extends Transform {
|
|
162
|
+
remainder = '';
|
|
163
|
+
_transform(chunk, _encoding, callback) {
|
|
164
|
+
const data = this.remainder + chunk.toString();
|
|
165
|
+
const lines = data.split('\n');
|
|
166
|
+
this.remainder = lines.pop() ?? '';
|
|
167
|
+
if (lines.length > 0) {
|
|
168
|
+
this.push(lines.join('\n') + '\n');
|
|
169
|
+
}
|
|
170
|
+
callback();
|
|
171
|
+
}
|
|
172
|
+
_flush(callback) {
|
|
173
|
+
if (this.remainder.length > 0) {
|
|
174
|
+
this.push(this.remainder);
|
|
175
|
+
}
|
|
176
|
+
callback();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
123
179
|
const transientStatusPattern = /HTTP status (\d+)/;
|
|
124
180
|
function isTransientError(error) {
|
|
125
181
|
if (isNetworkError(error))
|
package/dist/sparql/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { SparqlConstructExecutor, NotSupported, readQueryFile, type ExecuteOptions, type Executor, type SparqlConstructExecutorOptions, type VariableBindings, } from './executor.js';
|
|
1
|
+
export { SparqlConstructExecutor, LineBufferTransform, NotSupported, readQueryFile, type ExecuteOptions, type Executor, type SparqlConstructExecutorOptions, type VariableBindings, } from './executor.js';
|
|
2
2
|
export { SparqlItemSelector, type SparqlItemSelectorOptions, } from './selector.js';
|
|
3
3
|
export { injectValues } from './values.js';
|
|
4
4
|
export { withDefaultGraph } from './graph.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sparql/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,YAAY,EACZ,aAAa,EACb,KAAK,cAAc,EACnB,KAAK,QAAQ,EACb,KAAK,8BAA8B,EACnC,KAAK,gBAAgB,GACtB,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,kBAAkB,EAClB,KAAK,yBAAyB,GAC/B,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sparql/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,mBAAmB,EACnB,YAAY,EACZ,aAAa,EACb,KAAK,cAAc,EACnB,KAAK,QAAQ,EACb,KAAK,8BAA8B,EACnC,KAAK,gBAAgB,GACtB,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,kBAAkB,EAClB,KAAK,yBAAyB,GAC/B,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/sparql/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { SparqlConstructExecutor, NotSupported, readQueryFile, } from './executor.js';
|
|
1
|
+
export { SparqlConstructExecutor, LineBufferTransform, NotSupported, readQueryFile, } from './executor.js';
|
|
2
2
|
export { SparqlItemSelector, } from './selector.js';
|
|
3
3
|
export { injectValues } from './values.js';
|
|
4
4
|
export { withDefaultGraph } from './graph.js';
|