(buffer, binaryPath, extractedBinary)
| 209 | } |
| 210 | |
| 211 | async function extractZip(buffer, binaryPath, extractedBinary) { |
| 212 | const binaryDir = path.dirname(binaryPath) |
| 213 | // Try fflate first (bundled dep) |
| 214 | let fflateError |
| 215 | try { |
| 216 | const { unzipSync } = require('fflate') |
| 217 | const unzipped = unzipSync(new Uint8Array(buffer)) |
| 218 | const key = findZipEntryKey(unzipped, extractedBinary) |
| 219 | if (!key) { |
| 220 | throw new Error(`Binary ${extractedBinary} not found in zip`) |
| 221 | } |
| 222 | writeFileSync(binaryPath, Buffer.from(unzipped[key])) |
| 223 | return |
| 224 | } catch (e) { |
| 225 | fflateError = e |
| 226 | } |
| 227 | |
| 228 | // Fallback: PowerShell Expand-Archive or unzip CLI |
| 229 | const tmpDir = path.join(binaryDir, '.tmp-download') |
| 230 | rmSync(tmpDir, { recursive: true, force: true }) |
| 231 | mkdirSync(tmpDir, { recursive: true }) |
| 232 | try { |
| 233 | const assetName = `archive.zip` |
| 234 | const archivePath = path.join(tmpDir, assetName) |
| 235 | writeFileSync(archivePath, buffer) |
| 236 | |
| 237 | let extracted = false |
| 238 | if (process.platform === 'win32') { |
| 239 | const psCmd = `Expand-Archive -Path '${archivePath.replace(/'/g, "''")}' -DestinationPath '${tmpDir.replace(/'/g, "''")}' -Force` |
| 240 | const psResult = spawnSync( |
| 241 | 'powershell.exe', |
| 242 | [ |
| 243 | '-NoProfile', |
| 244 | '-NonInteractive', |
| 245 | '-ExecutionPolicy', |
| 246 | 'Bypass', |
| 247 | '-Command', |
| 248 | psCmd, |
| 249 | ], |
| 250 | { stdio: 'pipe', windowsHide: true }, |
| 251 | ) |
| 252 | if (psResult.status === 0) { |
| 253 | extracted = true |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (!extracted) { |
| 258 | const result = spawnSync('unzip', ['-o', archivePath, '-d', tmpDir], { |
| 259 | stdio: 'pipe', |
| 260 | }) |
| 261 | if (result.status !== 0) { |
| 262 | const unzipErr = result.stderr?.toString().trim() || 'command not found' |
| 263 | const fflateMsg = |
| 264 | fflateError instanceof Error |
| 265 | ? fflateError.message |
| 266 | : String(fflateError) |
| 267 | throw new Error( |
| 268 | `zip extraction failed (fflate: ${fflateMsg}; unzip: ${unzipErr})`, |
no test coverage detected