Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: production-grade download robustness improvements

Mariod57ac09f70fd
diff --git a/npm/scripts/postinstall.js b/npm/scripts/postinstall.js
index 586f3700..0ff04948 100644
--- a/npm/scripts/postinstall.js
+++ b/npm/scripts/postinstall.js
@@ -16,7 +16,18 @@ function getVersion() {
function detectPlatform() {
const platform = process.platform;
- const arch = process.arch === 'arm64' ? 'arm64' : 'amd64';
+
+ // Only support x64 and arm64
+ let arch;
+ if (process.arch === 'x64') {
+ arch = 'amd64';
+ } else if (process.arch === 'arm64') {
+ arch = 'arm64';
+ } else {
+ throw new Error(
+ `Unsupported architecture: ${process.arch}. ` + `Only x64 (amd64) and arm64 are supported.`
+ );
+ }
const osMap = {
darwin: 'darwin',
@@ -84,12 +95,20 @@ function fetchUrl(url, maxRedirects = 5) {
return;
}
- const redirectUrl = response.headers.location;
+ let redirectUrl = response.headers.location;
if (!redirectUrl) {
reject(new Error(`Redirect without location header from ${currentUrl}`));
return;
}
+ // Resolve relative URLs
+ try {
+ redirectUrl = new URL(redirectUrl, currentUrl).toString();
+ } catch (err) {
+ reject(new Error(`Invalid redirect URL from ${currentUrl}: ${redirectUrl}`));
+ return;
+ }
+
// Consume the response stream to avoid memory leaks
response.resume();
attemptFetch(redirectUrl, redirectsRemaining - 1);
@@ -161,10 +180,29 @@ async function downloadBinary(platform, version) {
const versionDir = path.join(binDir, version);
const binaryPath = path.join(versionDir, binaryName);
- // Skip if already exists
+ // Always verify existing binaries, even if they exist
+ // (guards against corrupted installs from previous failures)
if (fs.existsSync(binaryPath)) {
- console.log(`✓ Pinchtab binary already present: ${binaryPath}`);
- return;
+ try {
+ const checksums = await downloadChecksums(version);
+ if (checksums.has(binaryName)) {
+ const expectedHash = checksums.get(binaryName);
+ if (verifySHA256(binaryPath, expectedHash)) {
+ console.log(`✓ Pinchtab binary verified: ${binaryPath}`);
+ return;
+ } else {
+ console.warn(`⚠ Existing binary failed checksum, re-downloading...`);
+ fs.unlinkSync(binaryPath);
+ }
+ }
+ } catch (err) {
+ console.warn(`⚠ Could not verify existing binary, re-downloading...`);
+ try {
+ fs.unlinkSync(binaryPath);
+ } catch {
+ // ignore
+ }
+ }
}
// Fetch checksums
@@ -187,11 +225,14 @@ async function downloadBinary(platform, version) {
fs.mkdirSync(versionDir, { recursive: true });
}
- // Download binary
+ // Download to temp file first, then atomically rename to final path
+ // This prevents partial/corrupted files from being left behind
+ const tempPath = `${binaryPath}.tmp`;
+
return new Promise((resolve, reject) => {
console.log(`Downloading from ${downloadUrl}...`);
- const file = fs.createWriteStream(binaryPath);
+ const file = fs.createWriteStream(tempPath);
let redirectCount = 0;
const maxRedirects = 5;
@@ -201,18 +242,27 @@ async function downloadBinary(platform, version) {
// Handle redirects (301, 302, 307, 308)
if ([301, 302, 307, 308].includes(response.statusCode)) {
if (redirectCount >= maxRedirects) {
- fs.unlink(binaryPath, () => {});
+ fs.unlink(tempPath, () => {});
reject(new Error(`Too many redirects downloading ${downloadUrl}`));
return;
}
- const redirectUrl = response.headers.location;
+ let redirectUrl = response.headers.location;
if (!redirectUrl) {
- fs.unlink(binaryPath, () => {});
+ fs.unlink(tempPath, () => {});
reject(new Error(`Redirect without location header from ${url}`));
return;
}
+ // Resolve relative URLs
+ try {
+ redirectUrl = new URL(redirectUrl, url).toString();
+ } catch (err) {
+ fs.unlink(tempPath, () => {});
+ reject(new Error(`Invalid redirect URL from ${url}: ${redirectUrl}`));
+ return;
+ }
+
redirectCount++;
response.resume(); // Consume response stream
performDownload(redirectUrl);
@@ -220,7 +270,7 @@ async function downloadBinary(platform, version) {
}
if (response.statusCode !== 200) {
- fs.unlink(binaryPath, () => {});
+ fs.unlink(tempPath, () => {});
reject(new Error(`HTTP ${response.statusCode}: ${url}`));
return;
}
@@ -230,9 +280,9 @@ async function downloadBinary(platform, version) {
file.on('finish', () => {
file.close();
- // Verify checksum
- if (!verifySHA256(binaryPath, expectedHash)) {
- fs.unlink(binaryPath, () => {});
+ // Verify checksum before moving to final location
+ if (!verifySHA256(tempPath, expectedHash)) {
+ fs.unlink(tempPath, () => {});
reject(
new Error(
`Checksum verification failed for ${binaryName}\n` +
@@ -242,14 +292,29 @@ async function downloadBinary(platform, version) {
return;
}
+ // Atomically move temp file to final location
+ try {
+ fs.renameSync(tempPath, binaryPath);
+ } catch (err) {
+ fs.unlink(tempPath, () => {});
+ reject(new Error(`Failed to finalize binary: ${err.message}`));
+ return;
+ }
+
// Make executable
- fs.chmodSync(binaryPath, 0o755);
+ try {
+ fs.chmodSync(binaryPath, 0o755);
+ } catch (err) {
+ // On Windows, chmod may fail but binary may still be usable
+ console.warn(`⚠ Warning: could not set executable permissions: ${err.message}`);
+ }
+
console.log(`✓ Verified and installed: ${binaryPath}`);
resolve();
});
file.on('error', (err) => {
- fs.unlink(binaryPath, () => {});
+ fs.unlink(tempPath, () => {});
reject(err);
});
})
diff --git a/npm/src/download.ts b/npm/src/download.ts
index 26fe3c05..8441f85a 100644
--- a/npm/src/download.ts
+++ b/npm/src/download.ts
@@ -42,12 +42,20 @@ function fetchUrl(url: string, maxRedirects = 5): Promise<Buffer> {
return;
}
- const redirectUrl = response.headers.location;
+ let redirectUrl = response.headers.location;
if (!redirectUrl) {
reject(new Error(`Redirect without location header from ${currentUrl}`));
return;
}
+ // Resolve relative URLs
+ try {
+ redirectUrl = new URL(redirectUrl, currentUrl).toString();
+ } catch (err) {
+ reject(new Error(`Invalid redirect URL from ${currentUrl}: ${redirectUrl}`));
+ return;
+ }
+
// Consume the response stream to avoid memory leaks
response.resume();
attemptFetch(redirectUrl, redirectsRemaining - 1);
@@ -116,10 +124,29 @@ async function downloadBinary(platform: any, version: string): Promise<void> {
const binaryName = getBinaryName(platform);
const binaryPath = getBinaryPath(binaryName, version);
- // Skip if already exists
+ // Always verify existing binaries, even if they exist
+ // (guards against corrupted installs from previous failures)
if (fs.existsSync(binaryPath)) {
- console.log(`✓ Pinchtab binary already present: ${binaryPath}`);
- return;
+ try {
+ const checksums = await downloadChecksums(version);
+ if (checksums.has(binaryName)) {
+ const expectedHash = checksums.get(binaryName)!;
+ if (verifySHA256(binaryPath, expectedHash)) {
+ console.log(`✓ Pinchtab binary verified: ${binaryPath}`);
+ return;
+ } else {
+ console.warn(`⚠ Existing binary failed checksum, re-downloading...`);
+ fs.unlinkSync(binaryPath);
+ }
+ }
+ } catch (err) {
+ console.warn(`⚠ Could not verify existing binary, re-downloading...`);
+ try {
+ fs.unlinkSync(binaryPath);
+ } catch {
+ // ignore
+ }
+ }
}
// Fetch checksums
@@ -142,11 +169,14 @@ async function downloadBinary(platform: any, version: string): Promise<void> {
fs.mkdirSync(binDir, { recursive: true });
}
- // Download binary
+ // Download to temp file first, then atomically rename to final path
+ // This prevents partial/corrupted files from being left behind
+ const tempPath = `${binaryPath}.tmp`;
+
return new Promise((resolve, reject) => {
console.log(`Downloading from ${downloadUrl}...`);
- const file = fs.createWriteStream(binaryPath);
+ const file = fs.createWriteStream(tempPath);
let redirectCount = 0;
const maxRedirects = 5;
@@ -156,18 +186,27 @@ async function downloadBinary(platform: any, version: string): Promise<void> {
// Handle redirects (301, 302, 307, 308)
if ([301, 302, 307, 308].includes(response.statusCode || 0)) {
if (redirectCount >= maxRedirects) {
- fs.unlink(binaryPath, () => {});
+ fs.unlink(tempPath, () => {});
reject(new Error(`Too many redirects downloading ${downloadUrl}`));
return;
}
- const redirectUrl = response.headers.location;
+ let redirectUrl = response.headers.location;
if (!redirectUrl) {
- fs.unlink(binaryPath, () => {});
+ fs.unlink(tempPath, () => {});
reject(new Error(`Redirect without location header from ${url}`));
return;
}
+ // Resolve relative URLs
+ try {
+ redirectUrl = new URL(redirectUrl, url).toString();
+ } catch (err) {
+ fs.unlink(tempPath, () => {});
+ reject(new Error(`Invalid redirect URL from ${url}: ${redirectUrl}`));
+ return;
+ }
+
redirectCount++;
response.resume(); // Consume response stream
performDownload(redirectUrl);
@@ -175,7 +214,7 @@ async function downloadBinary(platform: any, version: string): Promise<void> {
}
if (response.statusCode !== 200) {
- fs.unlink(binaryPath, () => {});
+ fs.unlink(tempPath, () => {});
reject(new Error(`HTTP ${response.statusCode}: ${url}`));
return;
}
@@ -185,9 +224,9 @@ async function downloadBinary(platform: any, version: string): Promise<void> {
file.on('finish', () => {
file.close();
- // Verify checksum
- if (!verifySHA256(binaryPath, expectedHash)) {
- fs.unlink(binaryPath, () => {});
+ // Verify checksum before moving to final location
+ if (!verifySHA256(tempPath, expectedHash)) {
+ fs.unlink(tempPath, () => {});
reject(
new Error(
`Checksum verification failed for ${binaryName}. ` +
@@ -197,14 +236,31 @@ async function downloadBinary(platform: any, version: string): Promise<void> {
return;
}
+ // Atomically move temp file to final location
+ try {
+ fs.renameSync(tempPath, binaryPath);
+ } catch (err) {
+ fs.unlink(tempPath, () => {});
+ reject(new Error(`Failed to finalize binary: ${(err as Error).message}`));
+ return;
+ }
+
// Make executable
- fs.chmodSync(binaryPath, 0o755);
+ try {
+ fs.chmodSync(binaryPath, 0o755);
+ } catch (err) {
+ // On Windows, chmod may fail but binary may still be usable
+ console.warn(
+ `⚠ Warning: could not set executable permissions: ${(err as Error).message}`
+ );
+ }
+
console.log(`✓ Verified and installed: ${binaryPath}`);
resolve();
});
file.on('error', (err) => {
- fs.unlink(binaryPath, () => {});
+ fs.unlink(tempPath, () => {});
reject(err);
});
})
diff --git a/npm/src/platform.ts b/npm/src/platform.ts
index 9ff9a5e6..3085194c 100644
--- a/npm/src/platform.ts
+++ b/npm/src/platform.ts
@@ -7,7 +7,18 @@ export interface Platform {
export function detectPlatform(): Platform {
const platform = process.platform as any;
- const arch = process.arch === 'arm64' ? 'arm64' : 'amd64';
+
+ // Only support x64 (amd64) and arm64
+ let arch: 'amd64' | 'arm64';
+ if (process.arch === 'x64') {
+ arch = 'amd64';
+ } else if (process.arch === 'arm64') {
+ arch = 'arm64';
+ } else {
+ throw new Error(
+ `Unsupported architecture: ${process.arch}. ` + `Only x64 (amd64) and arm64 are supported.`
+ );
+ }
const osMap: Record<string, 'darwin' | 'linux' | 'windows'> = {
darwin: 'darwin',