UNPKG

2.26 kBJavaScriptView Raw
1"use strict";
2var __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})();
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.WritableStream = void 0;
19var 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 */
24var node_stream_1 = require("node:stream");
25var node_string_decoder_1 = require("node:string_decoder");
26// Following the example in https://nodejs.org/api/stream.html#stream_decoding_buffers_in_a_writable_stream
27function 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 */
35var 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 node_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, callback) {
44 this._parser.write(isBuffer(chunk, encoding) ? this._decoder.write(chunk) : chunk);
45 callback();
46 };
47 WritableStream.prototype._final = function (callback) {
48 this._parser.end(this._decoder.end());
49 callback();
50 };
51 return WritableStream;
52}(node_stream_1.Writable));
53exports.WritableStream = WritableStream;
54//# sourceMappingURL=WritableStream.js.map
\No newline at end of file