WritableStream.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.WritableStream = void 0;
  19. var Parser_1 = require("./Parser");
  20. /*
  21. * NOTE: If either of these two imports produces a type error,
  22. * please update your @types/node dependency!
  23. */
  24. var stream_1 = require("stream");
  25. var string_decoder_1 = require("string_decoder");
  26. // Following the example in https://nodejs.org/api/stream.html#stream_decoding_buffers_in_a_writable_stream
  27. function isBuffer(_chunk, encoding) {
  28. return encoding === "buffer";
  29. }
  30. /**
  31. * WritableStream makes the `Parser` interface available as a NodeJS stream.
  32. *
  33. * @see Parser
  34. */
  35. var WritableStream = /** @class */ (function (_super) {
  36. __extends(WritableStream, _super);
  37. function WritableStream(cbs, options) {
  38. var _this = _super.call(this, { decodeStrings: false }) || this;
  39. _this._decoder = new string_decoder_1.StringDecoder();
  40. _this._parser = new Parser_1.Parser(cbs, options);
  41. return _this;
  42. }
  43. WritableStream.prototype._write = function (chunk, encoding, cb) {
  44. this._parser.write(isBuffer(chunk, encoding) ? this._decoder.write(chunk) : chunk);
  45. cb();
  46. };
  47. WritableStream.prototype._final = function (cb) {
  48. this._parser.end(this._decoder.end());
  49. cb();
  50. };
  51. return WritableStream;
  52. }(stream_1.Writable));
  53. exports.WritableStream = WritableStream;