WritableStream.js 1.2 KB

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