-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
63 lines (51 loc) · 1.66 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Readable } from 'stream';
export default make;
export type RangeSpec = [base: NativePointer, size: number];
function make(base: NativePointer, size: number): MemoryStream;
function make(ranges: RangeSpec[]): MemoryStream;
function make(baseOrRanges: NativePointer | RangeSpec[], size?: number) {
if (baseOrRanges instanceof NativePointer)
return new MemoryStream([[baseOrRanges, size as number]]);
else
return new MemoryStream(baseOrRanges);
}
class MemoryStream extends Readable {
#ranges: RangeSpec[];
constructor(ranges: RangeSpec[]) {
super({
highWaterMark: 4 * 1024 * 1024
});
this.#ranges = ranges;
if (ranges.length === 0)
this.push(null);
}
_read(size: number) {
let wantMoreData = true;
do {
const range = this.#ranges[0];
const address = range[0];
const remaining = range[1];
if (remaining === 0) {
this.#ranges.shift();
if (this.#ranges.length > 0) {
continue;
} else {
this.push(null);
return;
}
}
const n = Math.min(remaining, size);
range[0] = address.add(n);
range[1] -= n;
let chunk: ArrayBuffer;
try {
chunk = address.readByteArray(n) as ArrayBuffer;
} catch (e) {
this.emit('error', e);
this.push(null);
return;
}
wantMoreData = this.push(Buffer.from(chunk));
} while (wantMoreData);
}
}