(buffer, binaryPath, extractedBinary)
| 114 | } |
| 115 | |
| 116 | async function extractZip(buffer, binaryPath, extractedBinary) { |
| 117 | const dir = path.dirname(binaryPath) |
| 118 | let fflateError |
| 119 | try { |
| 120 | const { unzipSync } = require('fflate') |
| 121 | const unzipped = unzipSync(new Uint8Array(buffer)) |
| 122 | const key = findZipEntryKey(unzipped, extractedBinary) |
| 123 | if (!key) throw new Error(`Binary ${extractedBinary} not found in zip`) |
| 124 | writeFileSync(binaryPath, Buffer.from(unzipped[key])) |
| 125 | return |
| 126 | } catch (e) { fflateError = e } |
| 127 | const tmpDir = path.join(dir, '.tmp-download') |
| 128 | rmSync(tmpDir, { recursive: true, force: true }) |
| 129 | mkdirSync(tmpDir, { recursive: true }) |
| 130 | try { |
| 131 | const archivePath = path.join(tmpDir, 'archive.zip') |
| 132 | writeFileSync(archivePath, buffer) |
| 133 | let extracted = false |
| 134 | if (process.platform === 'win32') { |
| 135 | const psCmd = `Expand-Archive -Path '${archivePath.replace(/'/g, "''")}' -DestinationPath '${tmpDir.replace(/'/g, "''")}' -Force` |
| 136 | const r = spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command', psCmd], { stdio: 'pipe', windowsHide: true }) |
| 137 | if (r.status === 0) extracted = true |
| 138 | } |
| 139 | if (!extracted) { |
| 140 | const r = spawnSync('unzip', ['-o', archivePath, '-d', tmpDir], { stdio: 'pipe' }) |
| 141 | if (r.status !== 0) { |
| 142 | const unzipErr = r.stderr?.toString().trim() || 'command not found' |
| 143 | throw new Error(`zip extraction failed (fflate: ${fflateError instanceof Error ? fflateError.message : String(fflateError)}; unzip: ${unzipErr})`) |
| 144 | } |
| 145 | } |
| 146 | const srcBinary = path.join(tmpDir, extractedBinary) |
| 147 | if (!existsSync(srcBinary)) throw new Error(`Binary not found at expected path: ${srcBinary}`) |
| 148 | renameSync(srcBinary, binaryPath) |
| 149 | } finally { |
| 150 | rmSync(tmpDir, { recursive: true, force: true }) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | async function extractTarGz(buffer, binaryPath, extractedBinary, assetName) { |
| 155 | const dir = path.dirname(binaryPath) |
no test coverage detected