|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Postinstall patch for gaxios v7.x to fix the Array.toString() bug. |
| 9 | + * |
| 10 | + * In gaxios v7.x, the _request method reads stream error bodies like: |
| 11 | + * const response = []; |
| 12 | + * for await (const chunk of data) { response.push(chunk); } |
| 13 | + * translatedResponse.data = response.toString(); |
| 14 | + * |
| 15 | + * Array.toString() joins elements with commas, corrupting the response body |
| 16 | + * at TCP chunk boundaries. This patch replaces `response.toString()` with |
| 17 | + * `response.join('')` which concatenates without commas. |
| 18 | + * |
| 19 | + * This is a known bug in gaxios v7.x that has not been fixed upstream as of v7.1.4. |
| 20 | + */ |
| 21 | + |
| 22 | +import { readFileSync, writeFileSync, existsSync } from 'node:fs'; |
| 23 | +import { join, dirname } from 'node:path'; |
| 24 | +import { fileURLToPath } from 'node:url'; |
| 25 | + |
| 26 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 27 | +const rootDir = join(__dirname, '..'); |
| 28 | + |
| 29 | +function patchFile(filePath) { |
| 30 | + if (!existsSync(filePath)) return false; |
| 31 | + |
| 32 | + const content = readFileSync(filePath, 'utf8'); |
| 33 | + const bugPattern = 'translatedResponse.data = response.toString()'; |
| 34 | + |
| 35 | + if (!content.includes(bugPattern)) return false; |
| 36 | + |
| 37 | + const patched = content.replace( |
| 38 | + bugPattern, |
| 39 | + "translatedResponse.data = response.join('')", |
| 40 | + ); |
| 41 | + writeFileSync(filePath, patched, 'utf8'); |
| 42 | + return true; |
| 43 | +} |
| 44 | + |
| 45 | +// Find and patch all gaxios v7 copies |
| 46 | +const gaxiosPaths = [ |
| 47 | + // packages/core workspace copy |
| 48 | + join(rootDir, 'packages/core/node_modules/gaxios/build/cjs/src/gaxios.js'), |
| 49 | + join(rootDir, 'packages/core/node_modules/gaxios/build/esm/src/gaxios.js'), |
| 50 | + // @google/genai nested copy (root) |
| 51 | + join( |
| 52 | + rootDir, |
| 53 | + 'node_modules/@google/genai/node_modules/gaxios/build/cjs/src/gaxios.js', |
| 54 | + ), |
| 55 | + join( |
| 56 | + rootDir, |
| 57 | + 'node_modules/@google/genai/node_modules/gaxios/build/esm/src/gaxios.js', |
| 58 | + ), |
| 59 | + // @google/genai nested copy (packages/core workspace) |
| 60 | + join( |
| 61 | + rootDir, |
| 62 | + 'packages/core/node_modules/@google/genai/node_modules/gaxios/build/cjs/src/gaxios.js', |
| 63 | + ), |
| 64 | + join( |
| 65 | + rootDir, |
| 66 | + 'packages/core/node_modules/@google/genai/node_modules/gaxios/build/esm/src/gaxios.js', |
| 67 | + ), |
| 68 | + // Top-level (if ever hoisted) |
| 69 | + join(rootDir, 'node_modules/gaxios/build/cjs/src/gaxios.js'), |
| 70 | + join(rootDir, 'node_modules/gaxios/build/esm/src/gaxios.js'), |
| 71 | + // Old gaxios layout (single build dir) |
| 72 | + join(rootDir, 'packages/core/node_modules/gaxios/build/src/gaxios.js'), |
| 73 | + join( |
| 74 | + rootDir, |
| 75 | + 'node_modules/@google/genai/node_modules/gaxios/build/src/gaxios.js', |
| 76 | + ), |
| 77 | + join(rootDir, 'node_modules/gaxios/build/src/gaxios.js'), |
| 78 | +]; |
| 79 | + |
| 80 | +let patchedCount = 0; |
| 81 | +for (const p of gaxiosPaths) { |
| 82 | + if (patchFile(p)) { |
| 83 | + patchedCount++; |
| 84 | + console.log(` ✓ Patched: ${p.replace(rootDir + '/', '')}`); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +if (patchedCount > 0) { |
| 89 | + console.log( |
| 90 | + `\n gaxios postinstall: patched ${patchedCount} file(s) — fixed Array.toString() stream corruption bug`, |
| 91 | + ); |
| 92 | +} else { |
| 93 | + console.log(' gaxios postinstall: no files needed patching'); |
| 94 | +} |
0 commit comments