1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.VirtualBuffer = void 0;
|
7 | class VirtualBuffer {
|
8 | constructor(readStream) {
|
9 | this._readStream = readStream;
|
10 | readStream.on('readable', () => {
|
11 | this._tryToMoveQueue();
|
12 | });
|
13 | readStream.on('error', error => {
|
14 | this._queue.forEach(e => e.reject(error));
|
15 | this._queue.length = 0;
|
16 | });
|
17 | readStream.on('end', () => {
|
18 | const error = new Error('stream ended');
|
19 | this._queue.forEach(e => e.reject(error));
|
20 | this._queue.length = 0;
|
21 | });
|
22 | this._queue = [];
|
23 | this.position = 0;
|
24 | }
|
25 | _tryToMoveQueue() {
|
26 | const queue = this._queue;
|
27 | while (queue.length > 0 && queue[0].tryToRead()) {
|
28 | queue.shift();
|
29 | }
|
30 | }
|
31 | async readChunk(length, label) {
|
32 | const promise = new Promise((resolve, reject) => {
|
33 | if (length === 0) {
|
34 | resolve(Buffer.alloc(0));
|
35 | } else {
|
36 | this._queue.push({
|
37 | reject,
|
38 | label,
|
39 | tryToRead: () => {
|
40 | const result = this._readStream.read(length);
|
41 | if (result !== null) {
|
42 | this.position += result.length;
|
43 | resolve(result);
|
44 | }
|
45 | return !!result;
|
46 | }
|
47 | });
|
48 | }
|
49 | });
|
50 | this._tryToMoveQueue();
|
51 | return promise;
|
52 | }
|
53 | }
|
54 | exports.VirtualBuffer = VirtualBuffer;
|
55 |
|
\ | No newline at end of file |