Skip to content

Commit

Permalink
fix: support proxies (#880)
Browse files Browse the repository at this point in the history
* fix: support proxies

* chore(release): 4.4.13-dev.0 [skip ci]

* chore: remove forced error

* chore(release): 4.4.13-dev.1 [skip ci]

* fix: proxy-agent on stream too

* chore(release): 4.4.13-dev.2 [skip ci]

---------

Co-authored-by: svc-cli-bot <Svc_cli_bot@salesforce.com>
Co-authored-by: Cristian Dominguez <cdominguez@salesforce.com>
  • Loading branch information
3 people authored Jul 29, 2024
1 parent 30364f0 commit 5b7dace
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 6 deletions.
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

0 comments on commit 5b7dace

Please sign in to comment.