fix: do not show download prompt when downloading JSON (#383)

This commit is contained in:
Antoine du Hamel 2024-02-21 16:46:37 +01:00 committed by GitHub
parent 6aab5fc17f
commit bc137a0073
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 17 deletions

View File

@ -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, {
@ -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`);