mirror of
https://github.com/nimbold/Firelink.git
synced 2026-08-23 01:16:47 +00:00
fix(build): harden engine downloads and refresh dependencies
- update Lucide, Vitest, compatible Cargo locks, and FFmpeg source pins - resume interrupted archives with HTTP range and checksum validation - cover complete, range-ignored, and unsatisfiable archive recovery paths
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
import fs from 'node:fs';
|
||||
import { Readable, Transform } from 'node:stream';
|
||||
import { pipeline } from 'node:stream/promises';
|
||||
|
||||
import { sha256 } from './engine-payload-integrity.js';
|
||||
|
||||
const DEFAULT_ATTEMPTS = 3;
|
||||
const DEFAULT_IDLE_TIMEOUT_MS = 120_000;
|
||||
const DEFAULT_RETRY_DELAYS_MS = [2_000, 5_000];
|
||||
const FILE_RESET_RETRY_DELAYS_MS = [100, 250, 500, 1_000, 2_000];
|
||||
|
||||
function parseContentRange(value) {
|
||||
const match = /^bytes (\d+)-(\d+)\/(\d+|\*)$/.exec(value || '');
|
||||
if (!match) return undefined;
|
||||
const start = Number(match[1]);
|
||||
const end = Number(match[2]);
|
||||
const total = match[3] === '*' ? undefined : Number(match[3]);
|
||||
if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || end < start) {
|
||||
return undefined;
|
||||
}
|
||||
if (total !== undefined && (!Number.isSafeInteger(total) || end >= total)) {
|
||||
return undefined;
|
||||
}
|
||||
return { start, end, total };
|
||||
}
|
||||
|
||||
function responseLength(response) {
|
||||
const value = response.headers.get('content-length');
|
||||
if (!value || !/^\d+$/.test(value)) return undefined;
|
||||
const length = Number(value);
|
||||
return Number.isSafeInteger(length) ? length : undefined;
|
||||
}
|
||||
|
||||
function createDownloadTimeout(idleTimeoutMs) {
|
||||
const controller = new AbortController();
|
||||
let timer;
|
||||
const refresh = () => {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => {
|
||||
controller.abort(new Error(`Download idle for ${idleTimeoutMs}ms`));
|
||||
}, idleTimeoutMs);
|
||||
};
|
||||
const dispose = () => clearTimeout(timer);
|
||||
refresh();
|
||||
return { signal: controller.signal, refresh, dispose };
|
||||
}
|
||||
|
||||
function archiveSize(archive) {
|
||||
try {
|
||||
return fs.statSync(archive).size;
|
||||
} catch (error) {
|
||||
if (error?.code === 'ENOENT') return 0;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function checksumMismatchError(name, expected, actual) {
|
||||
const error = new Error(`Archive checksum mismatch for ${name}. Expected ${expected}, got ${actual}`);
|
||||
error.code = 'ARCHIVE_CHECKSUM_MISMATCH';
|
||||
return error;
|
||||
}
|
||||
|
||||
function sleep(milliseconds) {
|
||||
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
||||
}
|
||||
|
||||
async function resetArchive(archive) {
|
||||
for (let attempt = 0; ; attempt += 1) {
|
||||
try {
|
||||
fs.rmSync(archive, { force: true });
|
||||
return;
|
||||
} catch (error) {
|
||||
const retryable = process.platform === 'win32'
|
||||
&& ['EACCES', 'EBUSY', 'EPERM'].includes(error?.code);
|
||||
if (!retryable || attempt >= FILE_RESET_RETRY_DELAYS_MS.length) throw error;
|
||||
await sleep(FILE_RESET_RETRY_DELAYS_MS[attempt]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Download and checksum an engine archive, resuming an interrupted response
|
||||
* when the provider honors HTTP range requests. A provider that ignores the
|
||||
* range is handled safely by replacing the partial file instead of appending
|
||||
* a second full archive to it.
|
||||
*/
|
||||
export async function downloadEngineArchive({
|
||||
name,
|
||||
url,
|
||||
archive,
|
||||
expectedSha256,
|
||||
attempts = DEFAULT_ATTEMPTS,
|
||||
idleTimeoutMs = DEFAULT_IDLE_TIMEOUT_MS,
|
||||
retryDelaysMs = DEFAULT_RETRY_DELAYS_MS,
|
||||
}) {
|
||||
let lastError;
|
||||
|
||||
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
||||
const partialSize = archiveSize(archive);
|
||||
if (partialSize > 0 && sha256(archive) === expectedSha256) return archive;
|
||||
const downloadTimeout = createDownloadTimeout(idleTimeoutMs);
|
||||
let resetForRetry = false;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
headers: partialSize > 0 ? { Range: `bytes=${partialSize}-` } : undefined,
|
||||
redirect: 'follow',
|
||||
signal: downloadTimeout.signal,
|
||||
});
|
||||
|
||||
if (response.status === 416 && partialSize > 0) {
|
||||
await response.body?.cancel();
|
||||
resetForRetry = true;
|
||||
throw new Error(`Retained partial archive range is not satisfiable for ${name}`);
|
||||
}
|
||||
|
||||
if (!response.ok || !response.body) {
|
||||
await response.body?.cancel();
|
||||
throw new Error(`Failed to download ${name}: HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const contentRange = response.status === 206
|
||||
? parseContentRange(response.headers.get('content-range'))
|
||||
: undefined;
|
||||
if (response.status === 206 && (!contentRange || contentRange.start !== partialSize)) {
|
||||
await response.body.cancel();
|
||||
throw new Error(`Invalid Content-Range while downloading ${name}`);
|
||||
}
|
||||
|
||||
const append = response.status === 206 && partialSize > 0;
|
||||
const expectedResponseLength = responseLength(response);
|
||||
if (!append && partialSize > 0) {
|
||||
// The provider ignored Range and returned the complete archive.
|
||||
await resetArchive(archive);
|
||||
}
|
||||
|
||||
await pipeline(
|
||||
Readable.fromWeb(response.body),
|
||||
new Transform({
|
||||
transform(chunk, encoding, callback) {
|
||||
downloadTimeout.refresh();
|
||||
callback(null, chunk, encoding);
|
||||
},
|
||||
}),
|
||||
fs.createWriteStream(archive, { flags: append ? 'a' : 'w' }),
|
||||
{ signal: downloadTimeout.signal },
|
||||
);
|
||||
|
||||
const finalSize = archiveSize(archive);
|
||||
const expectedFinalSize = contentRange?.total
|
||||
?? (expectedResponseLength === undefined
|
||||
? undefined
|
||||
: (append ? partialSize + expectedResponseLength : expectedResponseLength));
|
||||
if (expectedFinalSize !== undefined && finalSize !== expectedFinalSize) {
|
||||
throw new Error(
|
||||
`Incomplete archive for ${name}: expected ${expectedFinalSize} bytes, got ${finalSize}`,
|
||||
);
|
||||
}
|
||||
|
||||
const actual = sha256(archive);
|
||||
if (actual === expectedSha256) return archive;
|
||||
|
||||
resetForRetry = true;
|
||||
throw checksumMismatchError(name, expectedSha256, actual);
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
if (resetForRetry || error?.code === 'ARCHIVE_CHECKSUM_MISMATCH') {
|
||||
await resetArchive(archive);
|
||||
}
|
||||
if (attempt === attempts) {
|
||||
throw new Error(
|
||||
`Failed to download ${name} after ${attempts} attempts: ${
|
||||
error instanceof Error ? error.message : String(error)
|
||||
}`,
|
||||
{ cause: error },
|
||||
);
|
||||
}
|
||||
await new Promise(resolve => setTimeout(resolve, retryDelaysMs[attempt - 1] ?? 0));
|
||||
} finally {
|
||||
downloadTimeout.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError;
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash } from 'node:crypto';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { test } from 'node:test';
|
||||
|
||||
import { downloadEngineArchive } from './engine-download.js';
|
||||
|
||||
async function withMockFetch(mockFetch, callback) {
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = mockFetch;
|
||||
try {
|
||||
return await callback();
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
}
|
||||
|
||||
function makeBody(chunks, failure) {
|
||||
return new ReadableStream({
|
||||
start(controller) {
|
||||
for (const chunk of chunks) controller.enqueue(Buffer.from(chunk));
|
||||
if (failure) controller.error(failure);
|
||||
else controller.close();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function makeArchivePath() {
|
||||
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'firelink-engine-download-'));
|
||||
return {
|
||||
directory,
|
||||
archive: path.join(directory, 'engine.zip'),
|
||||
};
|
||||
}
|
||||
|
||||
function digest(value) {
|
||||
return createHash('sha256').update(value).digest('hex');
|
||||
}
|
||||
|
||||
test('resumes an interrupted archive response from the retained partial file', async () => {
|
||||
const { directory, archive } = makeArchivePath();
|
||||
const full = Buffer.from('complete archive payload');
|
||||
const prefix = full.subarray(0, 8);
|
||||
const middle = full.subarray(8, 15);
|
||||
const suffix = full.subarray(15);
|
||||
fs.writeFileSync(archive, Buffer.concat([prefix, middle]));
|
||||
const ranges = [];
|
||||
let calls = 0;
|
||||
|
||||
try {
|
||||
await withMockFetch(async (_url, options) => {
|
||||
calls += 1;
|
||||
ranges.push(options.headers?.Range);
|
||||
if (calls === 1) {
|
||||
return new Response(makeBody([], new Error('connection reset')), {
|
||||
status: 206,
|
||||
headers: {
|
||||
'Content-Length': String(suffix.length),
|
||||
'Content-Range': `bytes ${prefix.length + middle.length}-${full.length - 1}/${full.length}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
return new Response(makeBody([suffix]), {
|
||||
status: 206,
|
||||
headers: {
|
||||
'Content-Length': String(suffix.length),
|
||||
'Content-Range': `bytes ${prefix.length + middle.length}-${full.length - 1}/${full.length}`,
|
||||
},
|
||||
});
|
||||
}, async () => {
|
||||
await downloadEngineArchive({
|
||||
name: 'test',
|
||||
url: 'https://example.test/engine.zip',
|
||||
archive,
|
||||
expectedSha256: digest(full),
|
||||
attempts: 2,
|
||||
retryDelaysMs: [0],
|
||||
});
|
||||
});
|
||||
|
||||
assert.deepEqual(
|
||||
ranges,
|
||||
[`bytes=${prefix.length + middle.length}-`, `bytes=${prefix.length + middle.length}-`],
|
||||
);
|
||||
assert.deepEqual(fs.readFileSync(archive), full);
|
||||
} finally {
|
||||
fs.rmSync(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('replaces a partial archive when the provider ignores the Range request', async () => {
|
||||
const { directory, archive } = makeArchivePath();
|
||||
const full = Buffer.from('complete archive after range fallback');
|
||||
fs.writeFileSync(archive, Buffer.from('stale partial bytes'));
|
||||
let requestedRange;
|
||||
|
||||
try {
|
||||
await withMockFetch(async (_url, options) => {
|
||||
requestedRange = options.headers?.Range;
|
||||
return new Response(full, {
|
||||
status: 200,
|
||||
headers: { 'Content-Length': String(full.length) },
|
||||
});
|
||||
}, async () => {
|
||||
await downloadEngineArchive({
|
||||
name: 'test',
|
||||
url: 'https://example.test/engine.zip',
|
||||
archive,
|
||||
expectedSha256: digest(full),
|
||||
attempts: 1,
|
||||
});
|
||||
});
|
||||
|
||||
assert.equal(requestedRange, 'bytes=19-');
|
||||
assert.deepEqual(fs.readFileSync(archive), full);
|
||||
} finally {
|
||||
fs.rmSync(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('accepts a complete retained archive without issuing an unsatisfiable range', async () => {
|
||||
const { directory, archive } = makeArchivePath();
|
||||
const full = Buffer.from('complete archive retained after a late connection reset');
|
||||
fs.writeFileSync(archive, full);
|
||||
let calls = 0;
|
||||
|
||||
try {
|
||||
await withMockFetch(async () => {
|
||||
calls += 1;
|
||||
throw new Error('fetch should not be called for a complete retained archive');
|
||||
}, async () => {
|
||||
await downloadEngineArchive({
|
||||
name: 'test',
|
||||
url: 'https://example.test/engine.zip',
|
||||
archive,
|
||||
expectedSha256: digest(full),
|
||||
attempts: 1,
|
||||
});
|
||||
});
|
||||
|
||||
assert.equal(calls, 0);
|
||||
assert.deepEqual(fs.readFileSync(archive), full);
|
||||
} finally {
|
||||
fs.rmSync(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('restarts from zero after an unsatisfiable retained range', async () => {
|
||||
const { directory, archive } = makeArchivePath();
|
||||
const full = Buffer.from('complete archive after a stale range response');
|
||||
fs.writeFileSync(archive, Buffer.from('stale partial bytes'));
|
||||
const ranges = [];
|
||||
let calls = 0;
|
||||
|
||||
try {
|
||||
await withMockFetch(async (_url, options) => {
|
||||
calls += 1;
|
||||
ranges.push(options.headers?.Range);
|
||||
if (calls === 1) return new Response(null, { status: 416 });
|
||||
return new Response(full, {
|
||||
status: 200,
|
||||
headers: { 'Content-Length': String(full.length) },
|
||||
});
|
||||
}, async () => {
|
||||
await downloadEngineArchive({
|
||||
name: 'test',
|
||||
url: 'https://example.test/engine.zip',
|
||||
archive,
|
||||
expectedSha256: digest(full),
|
||||
attempts: 2,
|
||||
retryDelaysMs: [0],
|
||||
});
|
||||
});
|
||||
|
||||
assert.deepEqual(ranges, ['bytes=19-', undefined]);
|
||||
assert.deepEqual(fs.readFileSync(archive), full);
|
||||
} finally {
|
||||
fs.rmSync(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
@@ -1,11 +1,10 @@
|
||||
#!/usr/bin/env node
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { Readable, Transform } from 'node:stream';
|
||||
import { pipeline } from 'node:stream/promises';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { collectRegularFiles, sha256 } from './engine-payload-integrity.js';
|
||||
import { downloadEngineArchive } from './engine-download.js';
|
||||
import {
|
||||
promoteDirectory,
|
||||
recoverInterruptedPromotion,
|
||||
@@ -47,44 +46,6 @@ const payloadDestination = path.join(temporary, 'payload');
|
||||
fs.mkdirSync(payloadDestination, { recursive: true });
|
||||
const isWindows = target.includes('windows');
|
||||
const executableSuffix = isWindows ? '.exe' : '';
|
||||
const DOWNLOAD_ATTEMPTS = 3;
|
||||
const DOWNLOAD_IDLE_TIMEOUT_MS = 120_000;
|
||||
const DOWNLOAD_RETRY_DELAYS_MS = [2_000, 5_000];
|
||||
const FILE_LOCK_RETRY_DELAYS_MS = [100, 250, 500, 1_000, 2_000];
|
||||
|
||||
function sleep(milliseconds) {
|
||||
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
||||
}
|
||||
|
||||
async function removeFileWithRetry(file) {
|
||||
for (let attempt = 0; ; attempt += 1) {
|
||||
try {
|
||||
fs.rmSync(file, { force: true });
|
||||
return;
|
||||
} catch (error) {
|
||||
const retryable = process.platform === 'win32'
|
||||
&& ['EACCES', 'EBUSY', 'EPERM'].includes(error?.code);
|
||||
if (!retryable || attempt >= FILE_LOCK_RETRY_DELAYS_MS.length) {
|
||||
throw error;
|
||||
}
|
||||
await sleep(FILE_LOCK_RETRY_DELAYS_MS[attempt]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function createDownloadTimeout() {
|
||||
const controller = new AbortController();
|
||||
let timer;
|
||||
const refresh = () => {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => {
|
||||
controller.abort(new Error(`Download idle for ${DOWNLOAD_IDLE_TIMEOUT_MS}ms`));
|
||||
}, DOWNLOAD_IDLE_TIMEOUT_MS);
|
||||
};
|
||||
const dispose = () => clearTimeout(timer);
|
||||
refresh();
|
||||
return { signal: controller.signal, refresh, dispose };
|
||||
}
|
||||
|
||||
async function download(name, source) {
|
||||
const sourcePath = new URL(source.url).pathname;
|
||||
@@ -92,56 +53,12 @@ async function download(name, source) {
|
||||
temporary,
|
||||
`${name}${sourcePath.endsWith('.tar.xz') ? '.tar.xz' : '.zip'}`
|
||||
);
|
||||
|
||||
let lastError;
|
||||
for (let attempt = 1; attempt <= DOWNLOAD_ATTEMPTS; attempt += 1) {
|
||||
const downloadTimeout = createDownloadTimeout();
|
||||
try {
|
||||
const response = await fetch(source.url, {
|
||||
redirect: 'follow',
|
||||
signal: downloadTimeout.signal,
|
||||
});
|
||||
if (!response.ok || !response.body) {
|
||||
throw new Error(`Failed to download ${name}: HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
await pipeline(
|
||||
Readable.fromWeb(response.body),
|
||||
new Transform({
|
||||
transform(chunk, encoding, callback) {
|
||||
downloadTimeout.refresh();
|
||||
callback(null, chunk, encoding);
|
||||
},
|
||||
}),
|
||||
fs.createWriteStream(archive),
|
||||
{ signal: downloadTimeout.signal }
|
||||
);
|
||||
break;
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
await removeFileWithRetry(archive);
|
||||
if (attempt === DOWNLOAD_ATTEMPTS) {
|
||||
throw new Error(
|
||||
`Failed to download ${name} after ${DOWNLOAD_ATTEMPTS} attempts: ${
|
||||
error instanceof Error ? error.message : String(error)
|
||||
}`,
|
||||
{ cause: error }
|
||||
);
|
||||
}
|
||||
await sleep(DOWNLOAD_RETRY_DELAYS_MS[attempt - 1]);
|
||||
} finally {
|
||||
downloadTimeout.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
if (lastError && !fs.existsSync(archive)) {
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
const actual = sha256(archive);
|
||||
if (actual !== source.sha256) {
|
||||
throw new Error(`Archive checksum mismatch for ${name}. Expected ${source.sha256}, got ${actual}`);
|
||||
}
|
||||
await downloadEngineArchive({
|
||||
name,
|
||||
url: source.url,
|
||||
archive,
|
||||
expectedSha256: source.sha256,
|
||||
});
|
||||
const extracted = path.join(temporary, `${name}-extracted`);
|
||||
fs.mkdirSync(extracted);
|
||||
if (archive.endsWith('.zip') && process.platform !== 'win32') {
|
||||
|
||||
Reference in New Issue
Block a user