UNPKG

988 BJavaScriptView Raw
1import { PassThrough } from 'stream';
2import { writeTo } from './utils';
3
4/**
5 * Parse a `.corpus` file content, and returns an object containing queries and
6 * ids.
7 *
8 * @returns {Object}
9 */
10function ISTEXParseDotCorpus(data, feed) {
11 if (this.isLast()) {
12 return feed.close();
13 }
14 const metadata = this.ezs.metaString(data);
15 const statement = this.ezs.fromString(data);
16 const input = new PassThrough({ objectMode: true });
17 const output = input
18 .pipe(statement)
19 .on('data', (chunk) => {
20 feed.write({ ...metadata, ...chunk });
21 })
22 .on('error', (e) => {
23 feed.write(e);
24 });
25 const handle = new Promise(
26 (resolve, reject) => output.on('error', reject).on('end', resolve),
27 );
28 writeTo(input, metadata, () => {
29 input.end(() => {
30 handle.then(() => {
31 feed.end();
32 });
33 });
34 });
35}
36
37
38export default {
39 ISTEXParseDotCorpus,
40};