WritableStream.js 730 B

12345678910111213141516171819202122232425
  1. module.exports = Stream;
  2. var Parser = require("./Parser.js");
  3. var WritableStream = require("readable-stream").Writable;
  4. var StringDecoder = require("string_decoder").StringDecoder;
  5. var Buffer = require("buffer").Buffer;
  6. function Stream(cbs, options) {
  7. var parser = (this._parser = new Parser(cbs, options));
  8. var decoder = (this._decoder = new StringDecoder());
  9. WritableStream.call(this, { decodeStrings: false });
  10. this.once("finish", function() {
  11. parser.end(decoder.end());
  12. });
  13. }
  14. require("inherits")(Stream, WritableStream);
  15. Stream.prototype._write = function(chunk, encoding, cb) {
  16. if (chunk instanceof Buffer) chunk = this._decoder.write(chunk);
  17. this._parser.write(chunk);
  18. cb();
  19. };