Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support proxies #880

Merged
merged 7 commits into from
Jul 29, 2024
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"debug": "^4.3.6",
"filesize": "^6.1.0",
"got": "^13",
"proxy-agent": "^6.4.0",
"semver": "^7.6.3",
"tar-fs": "^2.1.1",
"tty-table": "^4.2.3"
Expand Down
33 changes: 28 additions & 5 deletions src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import {Config, Interfaces, ux} from '@oclif/core'
import {green, yellow} from 'ansis'
import makeDebug from 'debug'
import fileSize from 'filesize'
import {got} from 'got'
import {HTTPError, got} from 'got'
import {Stats, existsSync} from 'node:fs'
import {mkdir, readFile, readdir, rm, stat, symlink, utimes, writeFile} from 'node:fs/promises'
import {basename, dirname, join} from 'node:path'
import {ProxyAgent} from 'proxy-agent'

import {Extractor} from './tar.js'
import {ls, wait} from './util.js'
Expand All @@ -17,6 +18,24 @@ const filesize = (n: number): string => {
return Number.parseFloat(num).toFixed(1) + ` ${suffix}`
}

async function httpGet<T>(url: string) {
debug(`[${url}] GET`)
return got
.get<T>(url, {
agent: {https: new ProxyAgent()},
})
.then((res) => {
debug(`[${url}] ${res.statusCode}`)
return res
})
.catch((error) => {
debug(`[${url}] ${error.response?.statusCode ?? error.code}`)
// constructing a new HTTPError here will produce a more actionable stack trace
debug(new HTTPError(error.response))
throw error
})
}

type Options = {
autoUpdate: boolean
channel?: string | undefined
Expand All @@ -38,7 +57,7 @@ export class Updater {
public async fetchVersionIndex(): Promise<VersionIndex> {
const newIndexUrl = this.config.s3Url(s3VersionIndexKey(this.config))
try {
const {body} = await got.get<VersionIndex>(newIndexUrl)
const {body} = await httpGet<VersionIndex>(newIndexUrl)
return typeof body === 'string' ? JSON.parse(body) : body
} catch {
throw new Error(`No version indices exist for ${this.config.name}.`)
Expand Down Expand Up @@ -295,7 +314,7 @@ const fetchManifest = async (s3Key: string, config: Config): Promise<Interfaces.
ux.action.status = 'fetching manifest'

const url = config.s3Url(s3Key)
const {body} = await got.get<Interfaces.S3Manifest | string>(url)
const {body} = await httpGet<Interfaces.S3Manifest | string>(url)
if (typeof body === 'string') {
return JSON.parse(body)
}
Expand Down Expand Up @@ -387,7 +406,11 @@ const downloadAndExtract = async (
version,
}),
)
const stream = got.stream(gzUrl)

debug(`Streaming ${gzUrl} to ${output}`)
const stream = got.stream(gzUrl, {
agent: {https: new ProxyAgent()},
})

stream.pause()

Expand Down Expand Up @@ -427,7 +450,7 @@ const determineChannel = async ({config, version}: {config: Config; version?: st
}

try {
const {body} = await got.get<{'dist-tags': Record<string, string>}>(
const {body} = await httpGet<{'dist-tags': Record<string, string>}>(
`${config.npmRegistry ?? 'https://registry.npmjs.org'}/${config.pjson.name}`,
)
const tags = body['dist-tags']
Expand Down
Loading