Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 25 additions & 6 deletions packages/cli-repl/src/async-repl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable chai-friendly/no-unused-expressions */
import type { REPLServer, ReplOptions } from 'repl';
import type { ReadLineOptions } from 'readline';
import type { EventEmitter } from 'events';
import { Recoverable, start as originalStart } from 'repl';
import isRecoverableError from 'is-recoverable-error';
import { promisify } from 'util';
Expand Down Expand Up @@ -33,6 +35,20 @@ export type EvalFinishEvent = EvalStartEvent & ({
export const evalStart = Symbol('async-repl:evalStart');
export const evalFinish = Symbol('async-repl:evalFinish');

// Helper for temporarily disabling an event on an EventEmitter.
type RestoreEvents = { restore: () => void };
function disableEvent(emitter: EventEmitter, event: string): RestoreEvents {
const rawListeners = emitter.rawListeners(event);
emitter.removeAllListeners(event);
return {
restore() {
for (const listener of rawListeners) {
emitter.on(event, listener as any);
}
}
};
}

// Start a REPLSever that supports asynchronous evaluation, rather than just
// synchronous, and integrates nicely with Ctrl+C handling in that respect.
export function start(opts: AsyncREPLOptions): REPLServer {
Expand All @@ -54,7 +70,8 @@ export function start(opts: AsyncREPLOptions): REPLServer {
let previousExitListeners: any[] = [];

let sigintListener: (() => void) | undefined = undefined;
let previousSigintListeners: any[] = [];
let replSigint: RestoreEvents | undefined = undefined;
let processSigint: RestoreEvents | undefined = undefined;

try {
result = await new Promise((resolve, reject) => {
Expand All @@ -67,9 +84,10 @@ export function start(opts: AsyncREPLOptions): REPLServer {
// itself if the `customEval` itself is interrupted.
reject(new Error('Asynchronous execution was interrupted by `SIGINT`'));
};
previousSigintListeners = repl.rawListeners('SIGINT');

repl.removeAllListeners('SIGINT');
replSigint = disableEvent(repl, 'SIGINT');
processSigint = disableEvent(process, 'SIGINT');

repl.once('SIGINT', sigintListener);
}

Expand All @@ -93,9 +111,10 @@ export function start(opts: AsyncREPLOptions): REPLServer {
repl.removeListener('SIGINT', sigintListener);
process.removeListener('SIGINT', sigintListener);
}
for (const listener of previousSigintListeners) {
repl.on('SIGINT', listener);
}
// Oh no, a TypeScript bug? 🐞 https://github.com/microsoft/TypeScript/issues/43287
// `as any` to the rescue!
(replSigint as any)?.restore?.();
(processSigint as any)?.restore?.();

repl.removeListener('exit', exitListener);
for (const listener of previousExitListeners) {
Expand Down
7 changes: 7 additions & 0 deletions packages/cli-repl/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,13 @@ describe('e2e', function() {
await result;
shell.assertContainsError('interrupted');
});
it('interrupts load()', async() => {
const filename = path.resolve(__dirname, 'fixtures', 'load', 'infinite-loop.js');
const result = shell.executeLine(`load(${JSON.stringify(filename)})`);
setTimeout(() => shell.kill('SIGINT'), 1000);
await result;
shell.assertContainsError('interrupted');
});
it('behaves normally after an exception', async() => {
await shell.executeLine('throw new Error()');
await new Promise((resolve) => setTimeout(resolve, 100));
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-repl/test/fixtures/load/infinite-loop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line no-constant-condition
while (true);