18
19
constructor(splitter: string | number | Buffer) {
21
>
if (typeof splitter === 'number') {
22
this.splitter = splitter;
23
this.spitterLen = 1;
25
>
const buf = Buffer.isBuffer(splitter) ? splitter : Buffer.from(splitter);
26
>
this.splitter = buf.length === 1 ? buf[0] : buf;
27
>
this.spitterLen = buf.length;
28
>
}
29
>
}
30
31
override _transform(chunk: Buffer, _encoding: string, callback: (error?: Error | null, data?: Buffer) => void): void {
33
>
this.buffer = chunk;
34
>
} else {
35
>
this.buffer = Buffer.concat([this.buffer, chunk]);
36
>
}
37
>
38
>
let offset = 0;
39
>
while (offset < this.buffer.length) {
40
>
const index = typeof this.splitter === 'number'
41
? this.buffer.indexOf(this.splitter, offset)
42
: binaryIndexOf(this.buffer, this.splitter, offset);
44
>
break;
45
>
}
46
>
47
>
this.push(this.buffer.slice(offset, index + this.spitterLen));
48
>
offset = index + this.spitterLen;
49
>
}
50
>
51
>
this.buffer = offset === this.buffer.length ? undefined : this.buffer.slice(offset);
52
>
callback();
53
>
}
54
55
override _flush(callback: (error?: Error | null, data?: Buffer) => void): void {
57
>
this.push(this.buffer);
58
>
}
59
>
60
>
callback();
61
>
}
62
}