Skip to content

Commit

Permalink
fix: support proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Jul 26, 2024
1 parent fcaf81a commit db4e6d9
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 13 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.5",
"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
29 changes: 24 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 @@ -427,8 +446,8 @@ const determineChannel = async ({config, version}: {config: Config; version?: st
}

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

0 comments on commit db4e6d9

Please sign in to comment.