Skip to content

Instantly share code, notes, and snippets.

@websharik
Last active May 4, 2024 14:20
Show Gist options
  • Save websharik/718eb81aaa7d9bf03b534522d8acbd2e to your computer and use it in GitHub Desktop.
Save websharik/718eb81aaa7d9bf03b534522d8acbd2e to your computer and use it in GitHub Desktop.
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