WritableStream.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_js_1 = require("./Parser.js");
  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_js_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;
  54. //# sourceMappingURL=WritableStream.js.map