From 5bf11adff33c5a46a89fca67ea701ae04a4516c9 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Tue, 14 Jan 2025 10:45:23 -0500 Subject: [PATCH] dev: add utility to measure bytes in a stream --- src/backend/src/util/streamutil.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/backend/src/util/streamutil.js b/src/backend/src/util/streamutil.js index a1ef28ffa..a6ba2aece 100644 --- a/src/backend/src/util/streamutil.js +++ b/src/backend/src/util/streamutil.js @@ -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,