1 parent
b85759e27c
commit
7ee5a5bc48
6 files changed
+81
-154
No files matched your search
@@ -2,122 +2,90 @@
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
const copyWithProgress = async ({
|
||||
source,
|
||||
destination,
|
||||
jobLog,
|
||||
updateProgress,
|
||||
label,
|
||||
copyMethod = 'auto',
|
||||
bufferSizeMb = 16,
|
||||
maxRetries = 2,
|
||||
retryDelayMs = 1000,
|
||||
}) => {
|
||||
const sourceSize = fs.statSync(source).size;
|
||||
const sourceSizeMb = (sourceSize / 1024 / 1024).toFixed(1);
|
||||
const normalizedBufferSizeMb = Math.min(
|
||||
Math.max(Number.parseInt(bufferSizeMb, 10) || 16, 1),
|
||||
128,
|
||||
);
|
||||
const bufferSize = normalizedBufferSizeMb * 1024 * 1024;
|
||||
const MAX_RETRIES = 2;
|
||||
const RETRY_DELAY_MS = 1000;
|
||||
const PROGRESS_INTERVAL_MS = 250;
|
||||
|
||||
const report = (percent, message) => {
|
||||
const boundedPercent = Math.min(Math.max(percent, 0), 100);
|
||||
const sleep = (milliseconds) => new Promise((resolve) => {
|
||||
setTimeout(resolve, milliseconds);
|
||||
});
|
||||
|
||||
const copyWithProgress = async ({ source, destination, jobLog, updateProgress, label }) => {
|
||||
const sourceSize = fs.statSync(source).size;
|
||||
const sourceSizeMb = sourceSize / 1024 / 1024;
|
||||
let lastError;
|
||||
|
||||
const report = (progress, message) => {
|
||||
if (typeof updateProgress === 'function') {
|
||||
updateProgress({
|
||||
progress: boundedPercent,
|
||||
jobLog: message,
|
||||
});
|
||||
updateProgress({ progress, jobLog: message });
|
||||
}
|
||||
if (typeof jobLog === 'function') jobLog(`${message}\n`);
|
||||
};
|
||||
|
||||
const copyOnceWithStream = () => new Promise((resolve, reject) => {
|
||||
const startMs = Date.now();
|
||||
let copiedBytes = 0;
|
||||
let lastReportedPercent = -1;
|
||||
const rd = fs.createReadStream(source, { highWaterMark: bufferSize });
|
||||
const wr = fs.createWriteStream(destination, { highWaterMark: bufferSize });
|
||||
let settled = false;
|
||||
const fail = (error) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
rd.destroy();
|
||||
wr.destroy();
|
||||
reject(error);
|
||||
};
|
||||
rd.on('error', fail);
|
||||
wr.on('error', fail);
|
||||
wr.on('finish', () => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
resolve(((Date.now() - startMs) / 1000).toFixed(1));
|
||||
});
|
||||
rd.on('data', (chunk) => {
|
||||
copiedBytes += chunk.length;
|
||||
const percent = sourceSize === 0 ? 100 : Math.floor((copiedBytes / sourceSize) * 100);
|
||||
if (percent <= lastReportedPercent || percent >= 100) return;
|
||||
const elapsedSeconds = Math.max((Date.now() - startMs) / 1000, 0.1);
|
||||
report(
|
||||
percent,
|
||||
`[${label}] Progress: ${Math.min(percent, 100)}% `
|
||||
+ `(${(copiedBytes / 1024 / 1024).toFixed(1)}/${sourceSizeMb} MB, `
|
||||
+ `${(copiedBytes / 1024 / 1024 / elapsedSeconds).toFixed(1)} MB/s)`,
|
||||
);
|
||||
lastReportedPercent = percent;
|
||||
});
|
||||
rd.pipe(wr);
|
||||
});
|
||||
|
||||
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(
|
||||
percent,
|
||||
`[${label}] Progress: ${percent}% `
|
||||
+ `(${(copiedBytes / 1024 / 1024).toFixed(1)}/${sourceSizeMb} MB, `
|
||||
+ `${(copiedBytes / 1024 / 1024 / elapsedSeconds).toFixed(1)} MB/s)`,
|
||||
);
|
||||
}, 1000);
|
||||
fs.promises.copyFile(source, destination).then(() => {
|
||||
settled = true;
|
||||
clearInterval(progressTimer);
|
||||
resolve(((Date.now() - startMs) / 1000).toFixed(1));
|
||||
}).catch((error) => {
|
||||
settled = true;
|
||||
clearInterval(progressTimer);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
|
||||
const copyOnce = copyMethod === 'stream' ? copyOnceWithStream : copyOnceWithNative;
|
||||
|
||||
let lastError;
|
||||
for (let attempt = 0; attempt <= maxRetries; attempt += 1) {
|
||||
for (let attempt = 0; attempt <= MAX_RETRIES; attempt += 1) {
|
||||
if (attempt > 0) {
|
||||
report(0, `[${label}] Retrying copy (${attempt}/${maxRetries})...`);
|
||||
await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
|
||||
report(0, `[${label}] Retrying copy (${attempt}/${MAX_RETRIES})...`);
|
||||
await sleep(RETRY_DELAY_MS);
|
||||
}
|
||||
|
||||
try {
|
||||
if (fs.existsSync(destination)) fs.unlinkSync(destination);
|
||||
const elapsed = await copyOnce();
|
||||
|
||||
const startedAt = Date.now();
|
||||
let lastReportedPercent = -1;
|
||||
let previousBytes = 0;
|
||||
let previousAt = startedAt;
|
||||
let copying = true;
|
||||
|
||||
const progressTimer = setInterval(() => {
|
||||
if (!copying || !fs.existsSync(destination)) return;
|
||||
let copiedBytes;
|
||||
try {
|
||||
copiedBytes = fs.statSync(destination).size;
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') return;
|
||||
return;
|
||||
}
|
||||
const percent = sourceSize === 0
|
||||
? 99
|
||||
: Math.min(Math.floor((copiedBytes / sourceSize) * 100), 99);
|
||||
if (percent <= lastReportedPercent) return;
|
||||
|
||||
const now = Date.now();
|
||||
const intervalSeconds = Math.max((now - previousAt) / 1000, 0.001);
|
||||
const intervalSpeed = (copiedBytes - previousBytes) / 1024 / 1024 / intervalSeconds;
|
||||
report(
|
||||
percent,
|
||||
`[${label}] Progress: ${percent}% `
|
||||
+ `(${(copiedBytes / 1024 / 1024).toFixed(1)}/${sourceSizeMb.toFixed(1)} MB, `
|
||||
+ `${intervalSpeed.toFixed(1)} MB/s)`,
|
||||
);
|
||||
lastReportedPercent = percent;
|
||||
previousBytes = copiedBytes;
|
||||
previousAt = now;
|
||||
}, PROGRESS_INTERVAL_MS);
|
||||
|
||||
try {
|
||||
await fs.promises.copyFile(source, destination);
|
||||
} finally {
|
||||
copying = false;
|
||||
clearInterval(progressTimer);
|
||||
}
|
||||
|
||||
const destinationSize = fs.statSync(destination).size;
|
||||
if (destinationSize !== sourceSize) {
|
||||
throw new Error(
|
||||
`Copy size mismatch: expected ${sourceSize} bytes, got ${destinationSize}`,
|
||||
);
|
||||
}
|
||||
report(100, `[${label}] Done: ${sourceSizeMb} MB copied in ${elapsed}s`);
|
||||
|
||||
const elapsedSeconds = Math.max((Date.now() - startedAt) / 1000, 0.001);
|
||||
const averageSpeed = sourceSize / 1024 / 1024 / elapsedSeconds;
|
||||
report(
|
||||
100,
|
||||
`[${label}] Done: ${sourceSizeMb.toFixed(1)} MB copied in `
|
||||
+ `${elapsedSeconds.toFixed(1)}s (${averageSpeed.toFixed(1)} MB/s average)`,
|
||||
);
|
||||
return;
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
@@ -128,15 +96,14 @@ const copyWithProgress = async ({
|
||||
`${error.message}; could not remove partial copy: ${cleanupError.message}`,
|
||||
);
|
||||
}
|
||||
if (attempt === maxRetries) break;
|
||||
if (typeof jobLog === 'function') {
|
||||
jobLog(`[${label}] Copy attempt ${attempt + 1} failed: ${error.message}\n`);
|
||||
if (attempt < MAX_RETRIES && typeof jobLog === 'function') {
|
||||
jobLog(`[${label}] Copy attempt ${attempt + 1} failed: ${lastError.message}\n`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`[${label}] Copy failed after ${maxRetries + 1} attempts: ${lastError.message}`,
|
||||
`[${label}] Copy failed after ${MAX_RETRIES + 1} attempts: ${lastError.message}`,
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user