1 parent
68166ad7eb
commit
b85759e27c
2 files changed
+13
-5
No files matched your search
@@ -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)`,
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user