-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert to Stream object and allow options to be passed in #72
base: master
Are you sure you want to change the base?
Conversation
Should not this be merged? |
Hm, sorry, but should not it be possible to pipe this to the next stage? |
It does seem that piping the output stream to a file creates corrupted output, for some reason. |
I inluded this pull request into one of my projects and it seems to perform just fine. This example seems rather to be wrong. Obviously you can't pipe json to a stream-writer that expects to get string or buffer to be streamed to. input.pipe(parse).pipe(fs.writeStream('xxx')) Working example (extended from the example coming in this pull request) const stream = require('stream');
const fs = require('fs');
const xmlStream = require('../');
class ConsoleLogStream extends stream.Writable {
_write(chunk, enc, next) {
console.log(chunk);
next();
}
}
const input = fs.createReadStream('json.xml');
const parse = new xmlStream({
element: 'media',
attributes: false,
output:'json',
preserve: {},
collect:['id']
});
const logStream = new ConsoleLogStream();
input.pipe(parse).pipe(logStream); |
Yeah, that works and should be merged IMO |
There is one issue I discovered with this pull-request. Once a file is streamed, the "finish" event is not triggered a the end of the pipe. Also if I inhearit from this class, _final won't be triggered. I assume that this is caused because those class inherit from the base class stream.Stream and not from Readable and Writeable. I will do my own implementation in Typescript by wrapping it somehow... |
I removed xml-stream and am using now directly node-expat. Turns out also node-expat does not emit an "end" event, which probably is the root cause here. So this my core typescript calls now: import stream = require("stream");
import expat = require("node-expat");
export class XmlParser extends stream.Transform {
private parser: expat.Parser;
private parsingLevel = 0;
constructor (config: any) {
super({objectMode: true});
this.parser = new expat.Parser('UTF-8')
this.parser.on('startElement', (name, attrs) => {
this.parsingLevel++;
});
this.parser.on("error", (error) => {
this.emit("error", error);
});
this.parser.on('endElement', (text) => {
this.parsingLevel--;
if(this.parsingLevel === 0) {
this.emit("end");
}
});
}
_transform(chunk, encoding, callback) {
this.parser.write(chunk, encoding, callback);
}
} |
BREAKING CHANGE
Closes #16
examples/json.js
)