Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: prompt user before downloading software #360

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ same major line. Should you need to upgrade to a new major, use an explicit
not to lookup on the remote registry for the latest version of the selected
package manager.

- `COREPACK_ENABLE_EXPLICIT_VALIDATION_BEFORE_DOWNLOAD` can be set to `0` to
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
prevent Corepack showing the URL when it needs to download software, or can be
set to `1` to have the URL shown. By default, when Corepack is called
explicitly (e.g. `corepack pnpm …`), it is set to `0`; when Corepack is called
implicitely (e.g. `pnpm …`), it is set to `1`.
When standard input is a TTY and no CI environment is detected, Corepack will
ask for user input before starting the download.

- `COREPACK_ENABLE_NETWORK` can be set to `0` to prevent Corepack from accessing
the network (in which case you'll be responsible for hydrating the package
manager versions that will be required for the projects you'll run, using
Expand Down
2 changes: 2 additions & 0 deletions mkshims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ async function main() {
const corepackPath = path.join(distDir, `corepack.js`);
fs.writeFileSync(corepackPath, [
`#!/usr/bin/env node`,
`process.env.COREPACK_ENABLE_EXPLICIT_VALIDATION_BEFORE_DOWNLOAD??='0';`,
`require('./lib/corepack.cjs').runMain(process.argv.slice(2));`,
].join(`\n`));
fs.chmodSync(corepackPath, 0o755);
Expand All @@ -32,6 +33,7 @@ async function main() {
const entryPath = path.join(distDir, `${binaryName}.js`);
const entryScript = [
`#!/usr/bin/env node`,
`process.env.COREPACK_ENABLE_EXPLICIT_VALIDATION_BEFORE_DOWNLOAD??='1'`,
`require('./lib/corepack.cjs').runMain(['${binaryName}', ...process.argv.slice(2)]);`,
].join(`\n`);

Expand Down
24 changes: 21 additions & 3 deletions sources/httpUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {UsageError} from 'clipanion';
import {RequestOptions} from 'https';
import {IncomingMessage, ClientRequest} from 'http';
import {UsageError} from 'clipanion';
import {once} from 'events';
import type {RequestOptions} from 'https';
import type {IncomingMessage, ClientRequest} from 'http';
import {stderr, stdin} from 'process';

export async function fetchUrlStream(url: string, options: RequestOptions = {}) {
if (process.env.COREPACK_ENABLE_NETWORK === `0`)
Expand All @@ -12,6 +14,22 @@ export async function fetchUrlStream(url: string, options: RequestOptions = {})

const proxyAgent = new ProxyAgent();

if (process.env.COREPACK_ENABLE_EXPLICIT_VALIDATION_BEFORE_DOWNLOAD === `1`) {
console.error(`Corepack is about to download ${url}.`);
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`);
}
}
}

return new Promise<IncomingMessage>((resolve, reject) => {
const createRequest = (url: string) => {
const request: ClientRequest = https.get(url, {...options, agent: proxyAgent}, response => {
Expand Down
Loading