From 5aee2fe4bf7426166b9beee5ec3f1defea228074 Mon Sep 17 00:00:00 2001 From: Spencer Kaiser Date: Tue, 25 Apr 2023 16:53:25 -0500 Subject: [PATCH 1/5] Adding a note about DNS to the generic HTTP error message --- sources/httpUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sources/httpUtils.ts b/sources/httpUtils.ts index e3a745c0f..b074a8526 100644 --- a/sources/httpUtils.ts +++ b/sources/httpUtils.ts @@ -22,7 +22,7 @@ export async function fetchUrlStream(url: string, options: RequestOptions = {}) }); request.on(`error`, err => { - reject(new Error(`Error when performing the request`)); + reject(new Error(`Error when performing the request; this may be due to a DNS error, try \`curl ${url}\` to help diagnose the issue`)); }); }); } From ad1f6a09b043ada0b71c827c7b13e51c26fbaf1f Mon Sep 17 00:00:00 2001 From: Spencer Kaiser Date: Thu, 27 Apr 2023 09:33:46 -0500 Subject: [PATCH 2/5] Adding troubleshooting section to README, updating error message, fixing style in utils file --- README.md | 10 ++++++++++ sources/httpUtils.ts | 38 +++++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index a68555daf..e589082b9 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,16 @@ network interaction. - `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY` are supported through [`node-proxy-agent`](https://github.com/TooTallNate/node-proxy-agent). +## Troubleshooting + +### Networking + +There are a wide variety of networking issues that can occur while running `corepack` commands. Things to check: + +- Make sure your network connection is active if you are trying to retrieve remote content (such as a version of `yarn` that is not installed locally) +- Make sure the host for your request can be resolved by your DNS; try using `curl [URL]` from your shell +- Check your proxy settings (see [Environment Variables](#environment-variables)) + ## Contributing See [`CONTRIBUTING.md`](./CONTRIBUTING.md). diff --git a/sources/httpUtils.ts b/sources/httpUtils.ts index b074a8526..18fe01921 100644 --- a/sources/httpUtils.ts +++ b/sources/httpUtils.ts @@ -2,9 +2,15 @@ import {UsageError} from 'clipanion'; import {RequestOptions} from 'https'; import {IncomingMessage} from 'http'; -export async function fetchUrlStream(url: string, options: RequestOptions = {}) { - if (process.env.COREPACK_ENABLE_NETWORK === `0`) - throw new UsageError(`Network access disabled by the environment; can't reach ${url}`); +export async function fetchUrlStream( + url: string, + options: RequestOptions = {}, +) { + if (process.env.COREPACK_ENABLE_NETWORK === `0`) { + throw new UsageError( + `Network access disabled by the environment; can't reach ${url}`, + ); + } const {default: https} = await import(`https`); @@ -13,16 +19,24 @@ export async function fetchUrlStream(url: string, options: RequestOptions = {}) const proxyAgent = new ProxyAgent(); return new Promise((resolve, reject) => { - const request = https.get(url, {...options, agent: proxyAgent}, response => { - const statusCode = response.statusCode ?? 500; - if (!(statusCode >= 200 && statusCode < 300)) - return reject(new Error(`Server answered with HTTP ${statusCode}`)); + const request = https.get( + url, + {...options, agent: proxyAgent}, + response => { + const statusCode = response.statusCode ?? 500; + if (!(statusCode >= 200 && statusCode < 300)) + return reject(new Error(`Server answered with HTTP ${statusCode}`)); - return resolve(response); - }); + return resolve(response); + }, + ); request.on(`error`, err => { - reject(new Error(`Error when performing the request; this may be due to a DNS error, try \`curl ${url}\` to help diagnose the issue`)); + reject( + new Error( + `Error when performing the request to ${url}; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting`, + ), + ); }); }); } @@ -54,9 +68,7 @@ export async function fetchAsJson(url: string, options?: RequestOptions) { try { return JSON.parse(asText); } catch (error) { - const truncated = asText.length > 30 - ? `${asText.slice(0, 30)}...` - : asText; + const truncated = asText.length > 30 ? `${asText.slice(0, 30)}...` : asText; throw new Error(`Couldn't parse JSON data: ${JSON.stringify(truncated)}`); } From 56aad7e45d99f72b6bc2140049a82dd8e419edd4 Mon Sep 17 00:00:00 2001 From: Spencer Kaiser Date: Thu, 27 Apr 2023 13:08:57 -0500 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Antoine du Hamel --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e589082b9..39d55d2c4 100644 --- a/README.md +++ b/README.md @@ -223,9 +223,9 @@ network interaction. There are a wide variety of networking issues that can occur while running `corepack` commands. Things to check: -- Make sure your network connection is active if you are trying to retrieve remote content (such as a version of `yarn` that is not installed locally) -- Make sure the host for your request can be resolved by your DNS; try using `curl [URL]` from your shell -- Check your proxy settings (see [Environment Variables](#environment-variables)) +- Make sure your network connection is active. +- Make sure the host for your request can be resolved by your DNS; try using `curl [URL]` from your shell. +- Check your proxy settings (see [Environment Variables](#environment-variables)). ## Contributing From 97e4cf9753c46bc5b64b52e64bfc4d5f57dc3624 Mon Sep 17 00:00:00 2001 From: Spencer Kaiser Date: Thu, 27 Apr 2023 14:34:07 -0500 Subject: [PATCH 4/5] Reverting lint issues --- sources/httpUtils.ts | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/sources/httpUtils.ts b/sources/httpUtils.ts index 18fe01921..e5929ccde 100644 --- a/sources/httpUtils.ts +++ b/sources/httpUtils.ts @@ -1,41 +1,40 @@ -import {UsageError} from 'clipanion'; -import {RequestOptions} from 'https'; -import {IncomingMessage} from 'http'; +import { UsageError } from "clipanion"; +import { RequestOptions } from "https"; +import { IncomingMessage } from "http"; export async function fetchUrlStream( url: string, - options: RequestOptions = {}, + options: RequestOptions = {} ) { - if (process.env.COREPACK_ENABLE_NETWORK === `0`) { + if (process.env.COREPACK_ENABLE_NETWORK === `0`) throw new UsageError( - `Network access disabled by the environment; can't reach ${url}`, + `Network access disabled by the environment; can't reach ${url}` ); - } - const {default: https} = await import(`https`); + const { default: https } = await import(`https`); - const {default: ProxyAgent} = await import(`proxy-agent`); + const { default: ProxyAgent } = await import(`proxy-agent`); const proxyAgent = new ProxyAgent(); return new Promise((resolve, reject) => { const request = https.get( url, - {...options, agent: proxyAgent}, - response => { + { ...options, agent: proxyAgent }, + (response) => { const statusCode = response.statusCode ?? 500; if (!(statusCode >= 200 && statusCode < 300)) return reject(new Error(`Server answered with HTTP ${statusCode}`)); return resolve(response); - }, + } ); - request.on(`error`, err => { + request.on(`error`, (err) => { reject( new Error( - `Error when performing the request to ${url}; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting`, - ), + `Error when performing the request to ${url}; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting` + ) ); }); }); @@ -47,11 +46,11 @@ export async function fetchAsBuffer(url: string, options?: RequestOptions) { return new Promise((resolve, reject) => { const chunks: Array = []; - response.on(`data`, chunk => { + response.on(`data`, (chunk) => { chunks.push(chunk); }); - response.on(`error`, error => { + response.on(`error`, (error) => { reject(error); }); From a52078ee8357c9db87e812099e9906ee159838af Mon Sep 17 00:00:00 2001 From: Spencer Kaiser Date: Thu, 27 Apr 2023 14:35:28 -0500 Subject: [PATCH 5/5] Actually reverting formatting --- sources/httpUtils.ts | 51 +++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/sources/httpUtils.ts b/sources/httpUtils.ts index e5929ccde..97fec0d83 100644 --- a/sources/httpUtils.ts +++ b/sources/httpUtils.ts @@ -1,41 +1,28 @@ -import { UsageError } from "clipanion"; -import { RequestOptions } from "https"; -import { IncomingMessage } from "http"; +import {UsageError} from 'clipanion'; +import {RequestOptions} from 'https'; +import {IncomingMessage} from 'http'; -export async function fetchUrlStream( - url: string, - options: RequestOptions = {} -) { +export async function fetchUrlStream(url: string, options: RequestOptions = {}) { if (process.env.COREPACK_ENABLE_NETWORK === `0`) - throw new UsageError( - `Network access disabled by the environment; can't reach ${url}` - ); + throw new UsageError(`Network access disabled by the environment; can't reach ${url}`); - const { default: https } = await import(`https`); + const {default: https} = await import(`https`); - const { default: ProxyAgent } = await import(`proxy-agent`); + const {default: ProxyAgent} = await import(`proxy-agent`); const proxyAgent = new ProxyAgent(); return new Promise((resolve, reject) => { - const request = https.get( - url, - { ...options, agent: proxyAgent }, - (response) => { - const statusCode = response.statusCode ?? 500; - if (!(statusCode >= 200 && statusCode < 300)) - return reject(new Error(`Server answered with HTTP ${statusCode}`)); + const request = https.get(url, {...options, agent: proxyAgent}, response => { + const statusCode = response.statusCode ?? 500; + if (!(statusCode >= 200 && statusCode < 300)) + return reject(new Error(`Server answered with HTTP ${statusCode}`)); - return resolve(response); - } - ); + return resolve(response); + }); - request.on(`error`, (err) => { - reject( - new Error( - `Error when performing the request to ${url}; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting` - ) - ); + request.on(`error`, err => { + reject(new Error(`Error when performing the request to ${url}; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting`)); }); }); } @@ -46,11 +33,11 @@ export async function fetchAsBuffer(url: string, options?: RequestOptions) { return new Promise((resolve, reject) => { const chunks: Array = []; - response.on(`data`, (chunk) => { + response.on(`data`, chunk => { chunks.push(chunk); }); - response.on(`error`, (error) => { + response.on(`error`, error => { reject(error); }); @@ -67,7 +54,9 @@ export async function fetchAsJson(url: string, options?: RequestOptions) { try { return JSON.parse(asText); } catch (error) { - const truncated = asText.length > 30 ? `${asText.slice(0, 30)}...` : asText; + const truncated = asText.length > 30 + ? `${asText.slice(0, 30)}...` + : asText; throw new Error(`Couldn't parse JSON data: ${JSON.stringify(truncated)}`); }