Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"ink": "^7.0.0",
"react": "^19.2.5",
"strip-ansi": "^7.2.0",
"undici": "^7.27.2",
"zod": "^3.25.76"
},
"devDependencies": {
Expand Down
35 changes: 32 additions & 3 deletions src/menubar-installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { homedir, platform, tmpdir } from 'node:os'
import { join } from 'node:path'
import { pipeline } from 'node:stream/promises'
import { Readable } from 'node:stream'
import { ProxyAgent, fetch as undiciFetch } from 'undici'

import {
buildPersistentCodeburnLookupPath,
Expand All @@ -31,6 +32,34 @@ export type InstallResult = { installedPath: string; launched: boolean }
export type ReleaseAsset = { name: string; browser_download_url: string }
export type ReleaseResponse = { tag_name: string; assets: ReleaseAsset[] }
export type ResolvedAssets = { release: ReleaseResponse; zip: ReleaseAsset; checksum: ReleaseAsset }
type ProxyEnv = Partial<Record<'HTTPS_PROXY' | 'https_proxy' | 'HTTP_PROXY' | 'http_proxy' | 'NO_PROXY' | 'no_proxy', string>>
type FetchOptions = Parameters<typeof undiciFetch>[1]

export function resolveProxyUrlForUrl(url: string, env: ProxyEnv = process.env): string | undefined {
const target = new URL(url)
if (matchesNoProxy(target.hostname, env.NO_PROXY ?? env.no_proxy)) return undefined
if (target.protocol === 'https:') return env.HTTPS_PROXY ?? env.https_proxy ?? env.HTTP_PROXY ?? env.http_proxy
if (target.protocol === 'http:') return env.HTTP_PROXY ?? env.http_proxy
return undefined
}

function matchesNoProxy(hostname: string, noProxy?: string): boolean {
if (!noProxy) return false
const host = hostname.toLowerCase()
return noProxy.split(',').some(entry => {
const rule = entry.trim().toLowerCase().split(':')[0]
if (!rule) return false
if (rule === '*') return true
if (rule.startsWith('.')) return host === rule.slice(1) || host.endsWith(rule)
return host === rule || host.endsWith(`.${rule}`)
})
}

function fetchWithProxy(url: string, options: FetchOptions = {}) {
const proxyUrl = resolveProxyUrlForUrl(url)
const dispatcher = proxyUrl ? new ProxyAgent(proxyUrl) : undefined
return undiciFetch(url, dispatcher ? { ...options, dispatcher } : options)
}

export function resolveMenubarReleaseAssets(release: ReleaseResponse): ResolvedAssets {
const zip = release.assets.find(a => VERSIONED_ASSET_PATTERN.test(a.name))
Expand Down Expand Up @@ -102,7 +131,7 @@ async function sysProductVersion(): Promise<string> {
}

async function fetchLatestReleaseAssets(): Promise<ResolvedAssets> {
const response = await fetch(RELEASE_API, {
const response = await fetchWithProxy(RELEASE_API, {
headers: {
'User-Agent': 'codeburn-menubar-installer',
Accept: 'application/vnd.github+json',
Expand All @@ -116,7 +145,7 @@ async function fetchLatestReleaseAssets(): Promise<ResolvedAssets> {
}

async function verifyChecksum(archivePath: string, checksumUrl: string): Promise<void> {
const response = await fetch(checksumUrl, {
const response = await fetchWithProxy(checksumUrl, {
headers: { 'User-Agent': 'codeburn-menubar-installer' },
redirect: 'follow',
})
Expand All @@ -138,7 +167,7 @@ async function verifyChecksum(archivePath: string, checksumUrl: string): Promise
}

async function downloadToFile(url: string, destPath: string): Promise<void> {
const response = await fetch(url, {
const response = await fetchWithProxy(url, {
headers: { 'User-Agent': 'codeburn-menubar-installer' },
redirect: 'follow',
})
Expand Down
18 changes: 18 additions & 0 deletions tests/menubar-installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
resolveLatestMenubarReleaseAssets,
resolveMenubarReleaseAssets,
resolvePersistentCodeburnPathFromWhichOutput,
resolveProxyUrlForUrl,
type ReleaseResponse,
} from '../src/menubar-installer.js'

Expand Down Expand Up @@ -96,4 +97,21 @@ describe('resolveMenubarReleaseAssets', () => {
'/Users/me/.npm/_npx/abcd/node_modules/.bin/codeburn'
)).toThrow(/Install CodeBurn globally first/)
})

it('uses HTTPS proxy for GitHub HTTPS downloads', () => {
const proxyUrl = resolveProxyUrlForUrl('https://api.github.com/repos/getagentseal/codeburn/releases', {
HTTPS_PROXY: 'http://proxy.company.test:8080',
})

expect(proxyUrl).toBe('http://proxy.company.test:8080')
})

it('bypasses proxy when NO_PROXY matches the download host', () => {
const proxyUrl = resolveProxyUrlForUrl('https://api.github.com/repos/getagentseal/codeburn/releases', {
HTTPS_PROXY: 'http://proxy.company.test:8080',
NO_PROXY: '.github.com',
})

expect(proxyUrl).toBeUndefined()
})
})
Loading