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

Revert "repl: always check for NODE_REPL_MODE environment variable" #34058

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 0 additions & 4 deletions doc/api/repl.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,6 @@ A list of the names of all Node.js modules, e.g., `'http'`.
<!-- YAML
added: v0.1.91
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/33461
description: The `NODE_REPL_MODE` environment variable now accounts for
all repl instances.
- version:
- v13.4.0
- v12.17.0
Expand Down
16 changes: 15 additions & 1 deletion lib/internal/main/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,21 @@ if (process.env.NODE_REPL_EXTERNAL_MODULE) {
console.log(`Welcome to Node.js ${process.version}.\n` +
'Type ".help" for more information.');

require('internal/repl').createInternalRepl();
const cliRepl = require('internal/repl');
cliRepl.createInternalRepl(process.env, (err, repl) => {
if (err) {
throw err;
}
repl.on('exit', () => {
if (repl._flushing) {
repl.pause();
return repl.once('flushHistory', () => {
process.exit();
});
}
process.exit();
});
});

// If user passed '-e' or '--eval' along with `-i` or `--interactive`,
// evaluate the code in the current context.
Expand Down
42 changes: 29 additions & 13 deletions lib/internal/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,51 @@
const {
Number,
NumberIsNaN,
ObjectCreate,
} = primordials;

const REPL = require('repl');
const { kStandaloneREPL } = require('internal/repl/utils');

function createInternalRepl(opts = {}, callback = () => {}) {
module.exports = ObjectCreate(REPL);
module.exports.createInternalRepl = createRepl;

function createRepl(env, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = null;
}
opts = {
[kStandaloneREPL]: true,
ignoreUndefined: false,
useGlobal: true,
breakEvalOnSigint: true,
...opts,
terminal: parseInt(process.env.NODE_NO_READLINE) ? false : opts.terminal,
...opts
};

const historySize = Number(process.env.NODE_REPL_HISTORY_SIZE);
if (parseInt(env.NODE_NO_READLINE)) {
opts.terminal = false;
}

if (env.NODE_REPL_MODE) {
opts.replMode = {
'strict': REPL.REPL_MODE_STRICT,
'sloppy': REPL.REPL_MODE_SLOPPY
}[env.NODE_REPL_MODE.toLowerCase().trim()];
}

if (opts.replMode === undefined) {
opts.replMode = REPL.REPL_MODE_SLOPPY;
}

const historySize = Number(env.NODE_REPL_HISTORY_SIZE);
if (!NumberIsNaN(historySize) && historySize > 0) {
opts.historySize = historySize;
} else {
opts.historySize = 1000;
}

const repl = REPL.start(opts);
const historyPath = repl.terminal ? process.env.NODE_REPL_HISTORY : '';
repl.setupHistory(historyPath, (err, repl) => {
if (err) {
throw err;
}
callback(repl);
});
const term = 'terminal' in opts ? opts.terminal : process.stdout.isTTY;
repl.setupHistory(term ? env.NODE_REPL_HISTORY : '', cb);
}

module.exports = { createInternalRepl };
11 changes: 3 additions & 8 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ const {
setupPreview,
setupReverseSearch,
} = require('internal/repl/utils');
const { hasColors } = require('internal/tty');
const {
getOwnNonIndexProperties,
propertyFilter: {
Expand Down Expand Up @@ -214,8 +213,8 @@ function REPLServer(prompt,
// If possible, check if stdout supports colors or not.
if (options.output.hasColors) {
options.useColors = options.output.hasColors();
} else {
options.useColors = hasColors();
} else if (process.env.NODE_DISABLE_COLORS === undefined) {
options.useColors = true;
}
}

Expand Down Expand Up @@ -260,6 +259,7 @@ function REPLServer(prompt,
this._domain = options.domain || domain.create();
this.useGlobal = !!useGlobal;
this.ignoreUndefined = !!ignoreUndefined;
this.replMode = replMode || module.exports.REPL_MODE_SLOPPY;
this.underscoreAssigned = false;
this.last = undefined;
this.underscoreErrAssigned = false;
Expand All @@ -269,11 +269,6 @@ function REPLServer(prompt,
// Context id for use with the inspector protocol.
this[kContextId] = undefined;

this.replMode = replMode ||
(/^\s*strict\s*$/i.test(process.env.NODE_REPL_MODE) ?
module.exports.REPL_MODE_STRICT :
module.exports.REPL_MODE_SLOPPY);

if (this.breakEvalOnSigint && eval_) {
// Allowing this would not reflect user expectations.
// breakEvalOnSigint affects only the behavior of the default eval().
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-repl-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ inout._write = function(s, _, cb) {

const repl = new REPLServer({ input: inout, output: inout, useColors: true });
inout.isTTY = true;
inout.hasColors = () => true;
const repl2 = new REPLServer({ input: inout, output: inout });

process.on('exit', function() {
Expand Down
30 changes: 16 additions & 14 deletions test/parallel/test-repl-envvars.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ require('../common');
const stream = require('stream');
const REPL = require('internal/repl');
const assert = require('assert');
const debug = require('util').debuglog('test');
const inspect = require('util').inspect;

const tests = [
{
env: { TERM: 'xterm-256' },
env: {},
expected: { terminal: true, useColors: true }
},
{
Expand All @@ -30,33 +30,35 @@ const tests = [
expected: { terminal: false, useColors: false }
},
{
env: { NODE_NO_READLINE: '0', TERM: 'xterm-256' },
env: { NODE_NO_READLINE: '0' },
expected: { terminal: true, useColors: true }
}
];

const originalEnv = process.env;

function run(test) {
const env = test.env;
const expected = test.expected;

process.env = { ...originalEnv, ...env };

const opts = {
terminal: true,
input: new stream.Readable({ read() {} }),
output: new stream.Writable({ write() {} })
};

REPL.createInternalRepl(opts, (repl) => {
debug(env);
const { terminal, useColors } = repl;
assert.deepStrictEqual({ terminal, useColors }, expected);
Object.assign(process.env, env);

REPL.createInternalRepl(process.env, opts, function(err, repl) {
assert.ifError(err);

assert.strictEqual(repl.terminal, expected.terminal,
`Expected ${inspect(expected)} with ${inspect(env)}`);
assert.strictEqual(repl.useColors, expected.useColors,
`Expected ${inspect(expected)} with ${inspect(env)}`);
for (const key of Object.keys(env)) {
delete process.env[key];
}
repl.close();
if (tests.length)
run(tests.shift());
});
}

run(tests.shift());
tests.forEach(run);
13 changes: 7 additions & 6 deletions test/parallel/test-repl-history-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,6 @@ function cleanupTmpFile() {
return true;
}

const originalEnv = process.env;

function runTest() {
const opts = tests.shift();
if (!opts) return; // All done
Expand All @@ -537,9 +535,7 @@ function runTest() {
const lastChunks = [];
let i = 0;

process.env = { ...originalEnv, ...opts.env };

REPL.createInternalRepl({
REPL.createInternalRepl(opts.env, {
input: new ActionStream(),
output: new stream.Writable({
write(chunk, _, next) {
Expand Down Expand Up @@ -574,7 +570,12 @@ function runTest() {
useColors: false,
preview: opts.preview,
terminal: true
}, function(repl) {
}, function(err, repl) {
if (err) {
console.error(`Failed test # ${numtests - tests.length}`);
throw err;
}

repl.once('close', () => {
if (opts.clean)
cleanupTmpFile();
Expand Down
11 changes: 6 additions & 5 deletions test/parallel/test-repl-history-perm.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,22 @@ const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const replHistoryPath = path.join(tmpdir.path, '.node_repl_history');

const checkResults = common.mustCall((repl) => {
const checkResults = common.mustCall(function(err, r) {
assert.ifError(err);

const stat = fs.statSync(replHistoryPath);
const fileMode = stat.mode & 0o777;
assert.strictEqual(
fileMode, 0o600,
`REPL history file should be mode 0600 but was 0${fileMode.toString(8)}`);

// Close the REPL
repl.input.emit('keypress', '', { ctrl: true, name: 'd' });
repl.input.end();
r.input.emit('keypress', '', { ctrl: true, name: 'd' });
r.input.end();
});

process.env.NODE_REPL_HISTORY = replHistoryPath;

repl.createInternalRepl(
{ NODE_REPL_HISTORY: replHistoryPath },
{
terminal: true,
input: stream,
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-repl-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ common.expectWarning({

// Create a dummy stream that does nothing
const stream = new ArrayStream();
stream.hasColors = () => true;

// 1, mostly defaults
const r1 = repl.start({
Expand Down
14 changes: 8 additions & 6 deletions test/parallel/test-repl-persistent-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ fs.createReadStream(historyFixturePath)

const runTestWrap = common.mustCall(runTest, numtests);

const originalEnv = process.env;

function runTest(assertCleaned) {
const opts = tests.shift();
if (!opts) return; // All done
Expand All @@ -210,16 +208,15 @@ function runTest(assertCleaned) {
}
}

const env = opts.env;
const test = opts.test;
const expected = opts.expected;
const clean = opts.clean;
const before = opts.before;

if (before) before();

process.env = { ...originalEnv, ...opts.env };

REPL.createInternalRepl({
REPL.createInternalRepl(env, {
input: new ActionStream(),
output: new stream.Writable({
write(chunk, _, next) {
Expand All @@ -242,7 +239,12 @@ function runTest(assertCleaned) {
prompt,
useColors: false,
terminal: true
}, function(repl) {
}, function(err, repl) {
if (err) {
console.error(`Failed test # ${numtests - tests.length}`);
throw err;
}

repl.once('close', () => {
if (repl._flushing) {
repl.once('flushHistory', onClose);
Expand Down
14 changes: 7 additions & 7 deletions test/parallel/test-repl-reverse-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,6 @@ function cleanupTmpFile() {
return true;
}


const originalEnv = { ...process.env };

function runTest() {
const opts = tests.shift();
if (!opts) return; // All done
Expand All @@ -300,9 +297,7 @@ function runTest() {
const lastChunks = [];
let i = 0;

process.env = { ...originalEnv, ...opts.env };

REPL.createInternalRepl({
REPL.createInternalRepl(opts.env, {
input: new ActionStream(),
output: new stream.Writable({
write(chunk, _, next) {
Expand Down Expand Up @@ -335,7 +330,12 @@ function runTest() {
prompt,
useColors: opts.useColors || false,
terminal: true
}, function(repl) {
}, function(err, repl) {
if (err) {
console.error(`Failed test # ${numtests - tests.length}`);
throw err;
}

repl.once('close', () => {
if (opts.clean)
cleanupTmpFile();
Expand Down
Loading