Debian Codegraph @

src/N3StreamParser.js

raw
1 // **N3StreamParser** parses a text stream into a quad stream.
2 import N3Parser from './N3Parser';
3 import { Transform } from 'readable-stream';
4
5 // ## Constructor
6 export default class N3StreamParser extends Transform {
7 constructor(options) {
8 super({ decodeStrings: true });
9 this._readableState.objectMode = true;
10
11 // Set up parser with dummy stream to obtain `data` and `end` callbacks
12 const parser = new N3Parser(options);
13 let onData, onEnd;
14 parser.parse({
15 on: (event, callback) => {
16 switch (event) {
17 case 'data': onData = callback; break;
18 case 'end': onEnd = callback; break;
19 }
20 },
21 },
22 // Handle quads by pushing them down the pipeline
23 (error, quad) => { error && this.emit('error', error) || quad && this.push(quad); },
24 // Emit prefixes through the `prefix` event
25 (prefix, uri) => { this.emit('prefix', prefix, uri); }
26 );
27
28 // Implement Transform methods through parser callbacks
29 this._transform = (chunk, encoding, done) => { onData(chunk); done(); };
30 this._flush = done => { onEnd(); done(); };
31 }
32
33 // ### Parses a stream of strings
34 import(stream) {
35 stream.on('data', chunk => { this.write(chunk); });
36 stream.on('end', () => { this.end(); });
37 stream.on('error', error => { this.emit('error', error); });
38 return this;
39 }
40 }