1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.growBufferForAppendedData = exports.WritableStreamBuffer = exports.ReadableStreamBuffer = exports.NullStream = void 0;
|
4 | const stream_1 = require("stream");
|
5 | const DEFAULT_CHUNK_SIZE = 4;
|
6 | const DEFAULT_ALLOC_SIZE = 32;
|
7 | const DEFAULT_GROW_SIZE = 16;
|
8 | class NullStream extends stream_1.Writable {
|
9 | _write(chunk, encoding, callback) {
|
10 | callback();
|
11 | }
|
12 | }
|
13 | exports.NullStream = NullStream;
|
14 | class ReadableStreamBuffer extends stream_1.Readable {
|
15 | constructor(opts) {
|
16 | super(opts);
|
17 | this._size = 0;
|
18 | this._stopped = false;
|
19 | this.buffer = Buffer.alloc(opts && opts.allocSize ? opts.allocSize : DEFAULT_ALLOC_SIZE);
|
20 | this.chunkSize = opts && opts.chunkSize ? opts.chunkSize : DEFAULT_CHUNK_SIZE;
|
21 | this.growSize = opts && opts.growSize ? opts.growSize : DEFAULT_GROW_SIZE;
|
22 | }
|
23 | get size() {
|
24 | return this._size;
|
25 | }
|
26 | get stopped() {
|
27 | return this._stopped;
|
28 | }
|
29 | _read() {
|
30 | this._send();
|
31 | }
|
32 | feed(data, encoding = 'utf8') {
|
33 | if (this._stopped) {
|
34 | throw new Error('ReadableStreamBuffer is stopped. Can no longer feed.');
|
35 | }
|
36 | const datasize = typeof data === 'string' ? Buffer.byteLength(data) : data.length;
|
37 | this.buffer = growBufferForAppendedData(this.buffer, this._size, Math.ceil(datasize / this.growSize) * this.growSize);
|
38 | if (typeof data === 'string') {
|
39 | this.buffer.write(data, this._size, datasize, encoding);
|
40 | }
|
41 | else {
|
42 | this.buffer.copy(data, this._size, 0);
|
43 | }
|
44 | this._size += datasize;
|
45 | }
|
46 | stop() {
|
47 | if (this._stopped) {
|
48 | return;
|
49 | }
|
50 | this._stopped = true;
|
51 | if (this._size === 0) {
|
52 | this.push(null);
|
53 | }
|
54 | }
|
55 | _send() {
|
56 | const chunkSize = Math.min(this.chunkSize, this._size);
|
57 | let done = false;
|
58 | if (chunkSize > 0) {
|
59 | const chunk = Buffer.alloc(chunkSize);
|
60 | this.buffer.copy(chunk, 0, 0, chunkSize);
|
61 | done = !this.push(chunk);
|
62 | this.buffer.copy(this.buffer, 0, chunkSize, this._size);
|
63 | this._size -= chunkSize;
|
64 | }
|
65 | if (this._size === 0 && this._stopped) {
|
66 | this.push(null);
|
67 | }
|
68 | if (!done) {
|
69 | setTimeout(() => this._send(), 1);
|
70 | }
|
71 | }
|
72 | }
|
73 | exports.ReadableStreamBuffer = ReadableStreamBuffer;
|
74 | class WritableStreamBuffer extends stream_1.Writable {
|
75 | constructor(opts) {
|
76 | super(opts);
|
77 | this._size = 0;
|
78 | this.buffer = Buffer.alloc(opts && opts.allocSize ? opts.allocSize : DEFAULT_ALLOC_SIZE);
|
79 | this.growSize = opts && opts.growSize ? opts.growSize : DEFAULT_GROW_SIZE;
|
80 | }
|
81 | get size() {
|
82 | return this._size;
|
83 | }
|
84 | _write(chunk, encoding, callback) {
|
85 | this.buffer = growBufferForAppendedData(this.buffer, this._size, Math.ceil(chunk.length / this.growSize) * this.growSize);
|
86 | chunk.copy(this.buffer, this._size, 0);
|
87 | this._size += chunk.length;
|
88 | callback();
|
89 | }
|
90 | consume(bytes) {
|
91 | bytes = typeof bytes === 'number' ? bytes : this._size;
|
92 | const data = Buffer.alloc(bytes);
|
93 | this.buffer.copy(data, 0, 0, data.length);
|
94 | this.buffer.copy(this.buffer, 0, data.length);
|
95 | this._size -= data.length;
|
96 | return data;
|
97 | }
|
98 | }
|
99 | exports.WritableStreamBuffer = WritableStreamBuffer;
|
100 | function growBufferForAppendedData(buf, actualsize, appendsize) {
|
101 | if ((buf.length - actualsize) >= appendsize) {
|
102 | return buf;
|
103 | }
|
104 | const newbuffer = Buffer.alloc(buf.length + appendsize);
|
105 | buf.copy(newbuffer, 0, 0, actualsize);
|
106 | return newbuffer;
|
107 | }
|
108 | exports.growBufferForAppendedData = growBufferForAppendedData;
|