Last active
May 4, 2024 14:20
-
-
Save websharik/718eb81aaa7d9bf03b534522d8acbd2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Transform } from 'stream' | |
export class HighWaterMarkTransformer extends Transform { | |
highWaterMark: number | |
temp = new Uint8Array(0) | |
constructor(highWaterMark = 2048) { | |
super({ highWaterMark }) | |
this.highWaterMark = highWaterMark | |
} | |
_transform(chunk, encoding, callback) { | |
this.temp = Uint8Array.from([...this.temp, ...chunk]) | |
while (this.temp.length > this.highWaterMark) { | |
this.push(this.temp.slice(0, this.highWaterMark)) | |
this.temp = this.temp.slice(this.highWaterMark) | |
} | |
callback() | |
} | |
_flush(cb) { | |
this.push(this.temp) | |
return cb() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment