From b85759e27c192ee32720ec3f2ed5143232f8b4b1 Mon Sep 17 00:00:00 2001 From: PickleRick Date: Mon, 21 Sep 2026 22:57:38 +0200 Subject: [PATCH] Save uncommitted changes --- FlowHelpers/1.0.0/copyWithProgress.js | 16 +++++++++++----- tests/quick-logic-test.js | 2 ++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/FlowHelpers/1.0.0/copyWithProgress.js b/FlowHelpers/1.0.0/copyWithProgress.js index 74705a9..9adde62 100644 --- a/FlowHelpers/1.0.0/copyWithProgress.js +++ b/FlowHelpers/1.0.0/copyWithProgress.js @@ -35,7 +35,7 @@ const copyWithProgress = async ({ const copyOnceWithStream = () => new Promise((resolve, reject) => { const startMs = Date.now(); let copiedBytes = 0; - let nextProgressPercent = 0; + let lastReportedPercent = -1; const rd = fs.createReadStream(source, { highWaterMark: bufferSize }); const wr = fs.createWriteStream(destination, { highWaterMark: bufferSize }); let settled = false; @@ -56,7 +56,7 @@ const copyWithProgress = async ({ rd.on('data', (chunk) => { copiedBytes += chunk.length; const percent = sourceSize === 0 ? 100 : Math.floor((copiedBytes / sourceSize) * 100); - if (percent < nextProgressPercent) return; + if (percent <= lastReportedPercent || percent >= 100) return; const elapsedSeconds = Math.max((Date.now() - startMs) / 1000, 0.1); report( percent, @@ -64,7 +64,7 @@ const copyWithProgress = async ({ + `(${(copiedBytes / 1024 / 1024).toFixed(1)}/${sourceSizeMb} MB, ` + `${(copiedBytes / 1024 / 1024 / elapsedSeconds).toFixed(1)} MB/s)`, ); - nextProgressPercent = (Math.floor(percent / 10) + 1) * 10; + lastReportedPercent = percent; }); rd.pipe(wr); }); @@ -72,13 +72,19 @@ const copyWithProgress = async ({ const copyOnceWithNative = () => new Promise((resolve, reject) => { const startMs = Date.now(); let settled = false; + let lastReportedPercent = -1; const progressTimer = setInterval(() => { if (settled || !fs.existsSync(destination)) return; const copiedBytes = fs.statSync(destination).size; const elapsedSeconds = Math.max((Date.now() - startMs) / 1000, 0.1); + const percent = sourceSize === 0 + ? 99 + : Math.min(Math.floor((copiedBytes / sourceSize) * 100), 99); + if (percent <= lastReportedPercent) return; + lastReportedPercent = percent; report( - Math.floor((copiedBytes / sourceSize) * 100), - `[${label}] Progress: ${Math.min(Math.floor((copiedBytes / sourceSize) * 100), 100)}% ` + percent, + `[${label}] Progress: ${percent}% ` + `(${(copiedBytes / 1024 / 1024).toFixed(1)}/${sourceSizeMb} MB, ` + `${(copiedBytes / 1024 / 1024 / elapsedSeconds).toFixed(1)} MB/s)`, ); diff --git a/tests/quick-logic-test.js b/tests/quick-logic-test.js index 05a3071..83165fc 100644 --- a/tests/quick-logic-test.js +++ b/tests/quick-logic-test.js @@ -227,6 +227,8 @@ const AUDIO_INPUTS = { ok(copyHelperCode.includes("copyMethod === 'stream'"), 'copy helper supports stream fallback'); ok(copyHelperCode.includes('bufferSizeMb = 16'), 'copy helper defaults stream buffer to 16 MB'); ok(copyHelperCode.includes('128'), 'copy helper caps stream buffer size'); + ok(copyHelperCode.includes('Math.min(Math.floor((copiedBytes / sourceSize) * 100), 99)'), 'native progress reserves 100% for completed copy'); + ok(copyHelperCode.includes('percent <= lastReportedPercent'), 'copy helper reports each progress percentage once'); // ── Summary ─────────────────────────────────────────────────────────────── console.log(`\n${'='.repeat(55)}`);