dev: add utility to measure bytes in a stream

This commit is contained in:
KernelDeimos
2025-01-14 10:45:23 -05:00
parent 202e7f06e2
commit 5bf11adff3

View File

@@ -341,6 +341,27 @@ const size_limit_stream = (source, { limit }) => {
return stream;
}
class SizeMeasuringStream extends Transform {
constructor(options, probe) {
super(options);
this.probe = probe;
this.loaded = 0;
}
_transform(chunk, encoding, callback) {
this.loaded += chunk.length;
probe.amount = this.loaded;
this.push(chunk);
callback();
}
}
const size_measure_stream = (source, probe = {}) => {
const stream = new SizeMeasuringStream({}, probe);
source.pipe(stream);
return stream;
}
class StuckDetectorStream extends Transform {
constructor(options, {
timeout,