Skip to content
Open
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
344 changes: 193 additions & 151 deletions lib/repl.js

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions test/common/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

const ArrayStream = require('../common/arraystream');
const repl = require('node:repl');
const assert = require('node:assert');

function startNewREPLServer(replOpts = {}, testingOpts = {}) {
function startNewREPLServer(replOpts = {}) {
const input = new ArrayStream();
const output = new ArrayStream();

Expand All @@ -20,11 +19,6 @@ function startNewREPLServer(replOpts = {}, testingOpts = {}) {
...replOpts,
});

if (!testingOpts.disableDomainErrorAssert) {
// Some errors are passed to the domain, but do not callback
replServer._domain.on('error', assert.ifError);
}

return { replServer, input, output };
}

Expand Down
8 changes: 1 addition & 7 deletions test/fixtures/repl-tab-completion-nested-repls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Tab completion sometimes uses a separate REPL instance under the hood.
// That REPL instance has its own domain. Make sure domain errors trickle back
// up to the main REPL.
// Make sure errors in completion callbacks are properly thrown.
//
// Ref: https://github.com/nodejs/node/issues/21586

Expand Down Expand Up @@ -31,11 +30,6 @@ const repl = require('repl');
const putIn = new ArrayStream();
const testMe = repl.start('', putIn);

// Some errors are passed to the domain, but do not callback.
testMe._domain.on('error', function(err) {
throw err;
});

// Nesting of structures causes REPL to use a nested REPL for completion.
putIn.run([
'var top = function() {',
Expand Down
49 changes: 0 additions & 49 deletions test/parallel/test-repl-domain.js

This file was deleted.

2 changes: 0 additions & 2 deletions test/parallel/test-repl-eval-error-after-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const assert = require('node:assert');
eval$.resolve();
});
},
}, {
disableDomainErrorAssert: true,
});

replServer.write('\n');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-let-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ require('../common');
const { startNewREPLServer } = require('../common/repl');

// Regression test for https://github.com/nodejs/node/issues/6802
const { input } = startNewREPLServer({ useGlobal: true }, { disableDomainErrorAssert: true });
const { input } = startNewREPLServer({ useGlobal: true });
input.run(['let process']);
4 changes: 2 additions & 2 deletions test/parallel/test-repl-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ function testSloppyMode() {
}

function testStrictMode() {
const { input, output } = startNewREPLServer({ replMode: repl.REPL_MODE_STRICT, terminal: false, prompt: '> ' }, {
disableDomainErrorAssert: true,
const { input, output } = startNewREPLServer({
replMode: repl.REPL_MODE_STRICT, terminal: false, prompt: '> '
});

input.emit('data', 'x = 3\n');
Expand Down
69 changes: 69 additions & 0 deletions test/parallel/test-repl-multiple-instances-async-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

// This test verifies that when multiple REPL instances exist concurrently,
// async errors are correctly routed to the REPL instance that created them.

const common = require('../common');
const assert = require('assert');
const repl = require('repl');
const { Writable, PassThrough } = require('stream');

// Create two REPLs with separate inputs and outputs
let output1 = '';
let output2 = '';

const input1 = new PassThrough();
const input2 = new PassThrough();

const writable1 = new Writable({
write(chunk, encoding, callback) {
output1 += chunk.toString();
callback();
}
});

const writable2 = new Writable({
write(chunk, encoding, callback) {
output2 += chunk.toString();
callback();
}
});

const r1 = repl.start({
input: input1,
output: writable1,
terminal: false,
prompt: 'R1> ',
});

const r2 = repl.start({
input: input2,
output: writable2,
terminal: false,
prompt: 'R2> ',
});

// Create async error in REPL 1
input1.write('setTimeout(() => { throw new Error("error from repl1") }, 10)\n');

// Create async error in REPL 2
input2.write('setTimeout(() => { throw new Error("error from repl2") }, 20)\n');

setTimeout(common.mustCall(() => {
r1.close();
r2.close();

// Verify error from REPL 1 went to REPL 1's output
assert.match(output1, /error from repl1/,
'REPL 1 should have received its own async error');

// Verify error from REPL 2 went to REPL 2's output
assert.match(output2, /error from repl2/,
'REPL 2 should have received its own async error');

// Verify errors did not cross over to wrong REPL
assert.doesNotMatch(output1, /error from repl2/,
'REPL 1 should not have received REPL 2\'s error');
assert.doesNotMatch(output2, /error from repl1/,
'REPL 2 should not have received REPL 1\'s error');
}), 100);
2 changes: 0 additions & 2 deletions test/parallel/test-repl-pretty-custom-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function run({ command, expected }) {
const { replServer, output } = startNewREPLServer({
terminal: false,
useColors: false
}, {
disableDomainErrorAssert: true,
});

replServer.write(`${command}\n`);
Expand Down
5 changes: 1 addition & 4 deletions test/parallel/test-repl-pretty-stack-custom-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ const { startNewREPLServer } = require('../common/repl');

const testingReplPrompt = '_REPL_TESTING_PROMPT_>';

const { replServer, output } = startNewREPLServer(
{ prompt: testingReplPrompt },
{ disableDomainErrorAssert: true }
);
const { replServer, output } = startNewREPLServer({ prompt: testingReplPrompt });

replServer.write('throw new Error("foo[a]")\n');

Expand Down
13 changes: 5 additions & 8 deletions test/parallel/test-repl-pretty-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ const { startNewREPLServer } = require('../common/repl');
const stackRegExp = /(at .*REPL\d+:)[0-9]+:[0-9]+/g;

function run({ command, expected, ...extraREPLOptions }, i) {
const { replServer, output } = startNewREPLServer(
{
terminal: false,
useColors: false,
...extraREPLOptions
},
{ disableDomainErrorAssert: true }
);
const { replServer, output } = startNewREPLServer({
terminal: false,
useColors: false,
...extraREPLOptions
});

replServer.write(`${command}\n`);
if (typeof expected === 'string') {
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-repl-preview-newlines.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const { startNewREPLServer } = require('../common/repl');

common.skipIfInspectorDisabled();

const { input, output } = startNewREPLServer(
{ useColors: true }, { disableDomainErrorAssert: true }
);
const { input, output } = startNewREPLServer({ useColors: true });

output.accumulator = '';

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-syntax-error-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ process.on('exit', () => {
assert.strictEqual(found, true);
});

const { input, output } = startNewREPLServer({}, { disableDomainErrorAssert: true });
const { input, output } = startNewREPLServer();

output.write = (data) => {
// Matching only on a minimal piece of the stack because the string will vary
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-tab-complete-crash.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const common = require('../common');
const assert = require('assert');
const { startNewREPLServer } = require('../common/repl');

const { replServer, input } = startNewREPLServer({}, { disableDomainErrorAssert: true });
const { replServer, input } = startNewREPLServer();

// https://github.com/nodejs/node/issues/3346
// Tab-completion should be empty
Expand Down
2 changes: 0 additions & 2 deletions test/parallel/test-repl-tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ const testMe = repl.start('', putIn, function(cmd, context, filename,
callback(null, cmd);
});

testMe._domain.on('error', common.mustNotCall());

testMe.complete('', common.mustSucceed());
2 changes: 1 addition & 1 deletion test/parallel/test-repl-top-level-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ async function ordinaryTests() {
['k', '234'],
['const k = await Promise.resolve(345)', "Uncaught SyntaxError: Identifier 'k' has already been declared"],
// Regression test for https://github.com/nodejs/node/issues/43777.
['await Promise.resolve(123), Promise.resolve(456)', 'Promise {', { line: 0 }],
['await Promise.resolve(123), Promise.resolve(456)', 'Promise { 456 }'],
['await Promise.resolve(123), await Promise.resolve(456)', '456'],
['await (Promise.resolve(123), Promise.resolve(456))', '456'],
];
Expand Down
17 changes: 6 additions & 11 deletions test/parallel/test-repl-uncaught-exception-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ const common = require('../common');
const assert = require('assert');
const { startNewREPLServer } = require('../common/repl');

const { replServer, output } = startNewREPLServer(
{
prompt: '',
terminal: false,
useColors: false,
global: false,
},
{
disableDomainErrorAssert: true
},
);
const { replServer, output } = startNewREPLServer({
prompt: '',
terminal: false,
useColors: false,
global: false,
});

replServer.write(
'process.nextTick(() => {\n' +
Expand Down
25 changes: 10 additions & 15 deletions test/parallel/test-repl-uncaught-exception-evalcallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@ const common = require('../common');
const assert = require('assert');
const { startNewREPLServer } = require('../common/repl');

const { replServer, output } = startNewREPLServer(
{
prompt: '',
terminal: false,
useColors: false,
global: false,
eval: common.mustCall((code, context, filename, cb) => {
replServer.setPrompt('prompt! ');
cb(new Error('err'));
})
},
{
disableDomainErrorAssert: true
},
);
const { replServer, output } = startNewREPLServer({
prompt: '',
terminal: false,
useColors: false,
global: false,
eval: common.mustCall((code, context, filename, cb) => {
replServer.setPrompt('prompt! ');
cb(new Error('err'));
})
});

replServer.write('foo\n');

Expand Down
15 changes: 5 additions & 10 deletions test/parallel/test-repl-uncaught-exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ const { startNewREPLServer } = require('../common/repl');
let count = 0;

function run({ command, expected, useColors = false }) {
const { replServer, output } = startNewREPLServer(
{
prompt: '',
terminal: false,
useColors,
},
{
disableDomainErrorAssert: true
},
);
const { replServer, output } = startNewREPLServer({
prompt: '',
terminal: false,
useColors,
});

replServer.write(`${command}\n`);

Expand Down
2 changes: 0 additions & 2 deletions test/parallel/test-repl-underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,6 @@ function testError() {
prompt: testingReplPrompt,
replMode: repl.REPL_MODE_STRICT,
preview: false,
}, {
disableDomainErrorAssert: true
});

replServer.write(`_error; // initial value undefined
Expand Down
2 changes: 1 addition & 1 deletion test/pummel/test-repl-paste-big-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { startNewREPLServer } = require('../common/repl');

const cpuUsage = process.cpuUsage();

const { replServer } = startNewREPLServer({}, { disableDomainErrorAssert: true });
const { replServer } = startNewREPLServer();
replServer.input.emit('data', '{}');
replServer.input.emit('keypress', '', { name: 'left' });
replServer.input.emit('data', 'node');
Expand Down
Loading