WritableStream.js 1016 B

1234567891011121314151617181920212223242526272829303132
  1. import { Parser } from "./Parser.js";
  2. /*
  3. * NOTE: If either of these two imports produces a type error,
  4. * please update your @types/node dependency!
  5. */
  6. import { Writable } from "stream";
  7. import { StringDecoder } from "string_decoder";
  8. // Following the example in https://nodejs.org/api/stream.html#stream_decoding_buffers_in_a_writable_stream
  9. function isBuffer(_chunk, encoding) {
  10. return encoding === "buffer";
  11. }
  12. /**
  13. * WritableStream makes the `Parser` interface available as a NodeJS stream.
  14. *
  15. * @see Parser
  16. */
  17. export class WritableStream extends Writable {
  18. constructor(cbs, options) {
  19. super({ decodeStrings: false });
  20. this._decoder = new StringDecoder();
  21. this._parser = new Parser(cbs, options);
  22. }
  23. _write(chunk, encoding, cb) {
  24. this._parser.write(isBuffer(chunk, encoding) ? this._decoder.write(chunk) : chunk);
  25. cb();
  26. }
  27. _final(cb) {
  28. this._parser.end(this._decoder.end());
  29. cb();
  30. }
  31. }
  32. //# sourceMappingURL=WritableStream.js.map