Skip to content

Commit 8997488

Browse files
authored
fix(cli): preserve proxy-agent named exports in ESM bundle (google-gemini#27145)
1 parent 6589cdf commit 8997488

4 files changed

Lines changed: 104 additions & 0 deletions

File tree

esbuild.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ const cliConfig = {
102102
plugins: createWasmPlugins(),
103103
alias: {
104104
'is-in-ci': path.resolve(__dirname, 'packages/cli/src/patches/is-in-ci.ts'),
105+
'https-proxy-agent': path.resolve(
106+
__dirname,
107+
'packages/cli/src/patches/https-proxy-agent.ts',
108+
),
109+
'http-proxy-agent': path.resolve(
110+
__dirname,
111+
'packages/cli/src/patches/http-proxy-agent.ts',
112+
),
105113
...commonAliases,
106114
},
107115
metafile: true,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// eslint-disable-next-line import/no-relative-packages
8+
export { HttpProxyAgent } from '../../../../node_modules/http-proxy-agent/dist/index.js';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// eslint-disable-next-line import/no-relative-packages
8+
export { HttpsProxyAgent } from '../../../../node_modules/https-proxy-agent/dist/index.js';
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import { describe, expect, it, vi } from 'vitest';
8+
vi.unmock('fs');
9+
vi.unmock('node:fs');
10+
import * as esbuild from 'esbuild';
11+
import path from 'node:path';
12+
import { mkdtempSync, writeFileSync, rmSync } from 'node:fs';
13+
import { tmpdir } from 'node:os';
14+
import { fileURLToPath, pathToFileURL } from 'node:url';
15+
16+
const __filename = fileURLToPath(import.meta.url);
17+
const __dirname = path.dirname(__filename);
18+
const projectRoot = path.resolve(__dirname, '../../');
19+
20+
describe('proxy-agent bundle shape', () => {
21+
it('preserves named constructors after ESM splitting', async () => {
22+
const tmpDir = mkdtempSync(path.join(tmpdir(), 'gemini-proxy-test-'));
23+
const entryFile = path.join(tmpDir, 'entry.ts');
24+
25+
// Create a minimal entry file that dynamically imports the proxy agents
26+
writeFileSync(
27+
entryFile,
28+
`
29+
export async function getAgents() {
30+
const httpsMod = await import('https-proxy-agent');
31+
const httpMod = await import('http-proxy-agent');
32+
return {
33+
https: httpsMod,
34+
http: httpMod,
35+
};
36+
}
37+
`,
38+
);
39+
40+
// Bundle with the exact same splitting config and aliases as cliConfig
41+
await esbuild.build({
42+
entryPoints: { gemini: entryFile },
43+
outdir: path.join(tmpDir, 'bundle'),
44+
bundle: true,
45+
splitting: true,
46+
format: 'esm',
47+
platform: 'node',
48+
outExtension: { '.js': '.mjs' },
49+
alias: {
50+
'https-proxy-agent': path.resolve(
51+
projectRoot,
52+
'packages/cli/src/patches/https-proxy-agent.ts',
53+
),
54+
'http-proxy-agent': path.resolve(
55+
projectRoot,
56+
'packages/cli/src/patches/http-proxy-agent.ts',
57+
),
58+
},
59+
});
60+
61+
// Import the bundled chunk
62+
const bundledEntryUrl = pathToFileURL(
63+
path.join(tmpDir, 'bundle/gemini.mjs'),
64+
).href;
65+
const { getAgents } = await import(bundledEntryUrl);
66+
67+
const { https, http } = await getAgents();
68+
69+
// Verify named exports exist
70+
expect(typeof https.HttpsProxyAgent).toBe('function');
71+
expect(typeof http.HttpProxyAgent).toBe('function');
72+
73+
// Verify they are constructable
74+
expect(() => new https.HttpsProxyAgent('http://127.0.0.1:9')).not.toThrow();
75+
expect(() => new http.HttpProxyAgent('http://127.0.0.1:9')).not.toThrow();
76+
77+
// Cleanup
78+
rmSync(tmpDir, { recursive: true, force: true });
79+
});
80+
});

0 commit comments

Comments
 (0)