Skip to content

Commit

Permalink
fix: do not show download prompt when downloading JSON (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Feb 21, 2024
1 parent 6aab5fc commit bc137a0
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions sources/httpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,12 @@ import {once} from 'events';
import {stderr, stdin} from 'process';
import {Readable} from 'stream';

export async function fetch(input: string | URL, init?: RequestInit) {
async function fetch(input: string | URL, init?: RequestInit) {
if (process.env.COREPACK_ENABLE_NETWORK === `0`)
throw new UsageError(`Network access disabled by the environment; can't reach ${input}`);

const agent = await getProxyAgent(input);

if (process.env.COREPACK_ENABLE_DOWNLOAD_PROMPT === `1`) {
console.error(`Corepack is about to download ${input}.`);
if (stdin.isTTY && !process.env.CI) {
stderr.write(`\nDo you want to continue? [Y/n] `);
stdin.resume();
const chars = await once(stdin, `data`);
stdin.pause();
if (
chars[0][0] === 0x6e || // n
chars[0][0] === 0x4e // N
) {
throw new UsageError(`Aborted by the user`);
}
}
}

let response;
try {
response = await globalThis.fetch(input, {
Expand Down Expand Up @@ -55,6 +39,22 @@ export async function fetchAsJson(input: string | URL, init?: RequestInit) {
}

export async function fetchUrlStream(input: string | URL, init?: RequestInit) {
if (process.env.COREPACK_ENABLE_DOWNLOAD_PROMPT === `1`) {
console.error(`Corepack is about to download ${input}.`);
if (stdin.isTTY && !process.env.CI) {
stderr.write(`\nDo you want to continue? [Y/n] `);
stdin.resume();
const chars = await once(stdin, `data`);
stdin.pause();
if (
chars[0][0] === 0x6e || // n
chars[0][0] === 0x4e // N
) {
throw new UsageError(`Aborted by the user`);
}
}
}

const response = await fetch(input, init);
const webStream = response.body;
assert(webStream, `Expected stream to be set`);
Expand Down

0 comments on commit bc137a0

Please sign in to comment.