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

repl: check for NODE_REPL_EXTERNAL_MODULE #29778

Merged
merged 1 commit into from
Oct 11, 2019
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
8 changes: 8 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,14 @@ Path to the file used to store the persistent REPL history. The default path is
`~/.node_repl_history`, which is overridden by this variable. Setting the value
to an empty string (`''` or `' '`) disables persistent REPL history.

### `NODE_REPL_EXTERNAL_MODULE=file`
devsnek marked this conversation as resolved.
Show resolved Hide resolved
<!-- YAML
added: REPLACEME
-->

Path to a Node.js module which will be loaded in place of the built-in REPL.
Overriding this value to an empty string (`''`) will use the built-in REPL.
devsnek marked this conversation as resolved.
Show resolved Hide resolved

### `NODE_TLS_REJECT_UNAUTHORIZED=value`

If `value` equals `'0'`, certificate validation is disabled for TLS connections.
Expand Down
66 changes: 36 additions & 30 deletions lib/internal/main/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,44 @@ prepareMainThreadExecution();

markBootstrapComplete();

// --input-type flag not supported in REPL
if (getOptionValue('--input-type')) {
// If we can't write to stderr, we'd like to make this a noop,
// so use console.error.
console.error('Cannot specify --input-type for REPL');
process.exit(1);
}
if (process.env.NODE_REPL_EXTERNAL_MODULE) {
require('internal/modules/cjs/loader')
.Module
._load(process.env.NODE_REPL_EXTERNAL_MODULE, undefined, true);
devsnek marked this conversation as resolved.
Show resolved Hide resolved
} else {
// --input-type flag not supported in REPL
if (getOptionValue('--input-type')) {
// If we can't write to stderr, we'd like to make this a noop,
// so use console.error.
console.error('Cannot specify --input-type for REPL');
process.exit(1);
}

console.log(`Welcome to Node.js ${process.version}.\n` +
'Type ".help" for more information.');
console.log(`Welcome to Node.js ${process.version}.\n` +
'Type ".help" for more information.');

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();
});
const cliRepl = require('internal/repl');
cliRepl.createInternalRepl(process.env, (err, repl) => {
if (err) {
throw err;
}
process.exit();
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.
if (getOptionValue('[has_eval_string]')) {
evalScript('[eval]',
getOptionValue('--eval'),
getOptionValue('--inspect-brk'),
getOptionValue('--print'));

// If user passed '-e' or '--eval' along with `-i` or `--interactive`,
// evaluate the code in the current context.
if (getOptionValue('[has_eval_string]')) {
evalScript('[eval]',
getOptionValue('--eval'),
getOptionValue('--inspect-brk'),
getOptionValue('--print'));
}
}
3 changes: 3 additions & 0 deletions test/fixtures/external-repl-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

console.log('42');
14 changes: 14 additions & 0 deletions test/pseudo-tty/test-repl-external-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

require('../common');
const fixtures = require('../common/fixtures');
const { execSync } = require('child_process');

execSync(process.execPath, {
encoding: 'utf8',
stdio: 'inherit',
env: {
...process.env,
NODE_REPL_EXTERNAL_MODULE: fixtures.path('external-repl-module.js'),
},
});
1 change: 1 addition & 0 deletions test/pseudo-tty/test-repl-external-module.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42