repl: make sure historyPath is trimmed

If one were to set NODE_REPL_HISTORY to a string that contains only a
space (" "), then the history file would be created with that name
which can cause problems are certain systems.

PR-URL: https://github.com/nodejs/node/pull/4539
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
This commit is contained in:
Evan Lucas 2016-01-05 06:28:27 -06:00
parent 0ec093cd41
commit da550aa063
3 changed files with 18 additions and 2 deletions

View File

@ -37,7 +37,7 @@ via the following environment variables:
- `NODE_REPL_HISTORY` - When a valid path is given, persistent REPL history - `NODE_REPL_HISTORY` - When a valid path is given, persistent REPL history
will be saved to the specified file rather than `.node_repl_history` in the will be saved to the specified file rather than `.node_repl_history` in the
user's home directory. Setting this value to `""` will disable persistent user's home directory. Setting this value to `""` will disable persistent
REPL history. REPL history. Whitespace will be trimmed from the value.
- `NODE_REPL_HISTORY_SIZE` - defaults to `1000`. Controls how many lines of - `NODE_REPL_HISTORY_SIZE` - defaults to `1000`. Controls how many lines of
history will be persisted if history is available. Must be a positive number. history will be persisted if history is available. Must be a positive number.
- `NODE_REPL_MODE` - may be any of `sloppy`, `strict`, or `magic`. Defaults - `NODE_REPL_MODE` - may be any of `sloppy`, `strict`, or `magic`. Defaults

View File

@ -55,15 +55,26 @@ function createRepl(env, opts, cb) {
} }
const repl = REPL.start(opts); const repl = REPL.start(opts);
if (opts.terminal && env.NODE_REPL_HISTORY !== '') { if (opts.terminal) {
return setupHistory(repl, env.NODE_REPL_HISTORY, return setupHistory(repl, env.NODE_REPL_HISTORY,
env.NODE_REPL_HISTORY_FILE, cb); env.NODE_REPL_HISTORY_FILE, cb);
} }
repl._historyPrev = _replHistoryMessage; repl._historyPrev = _replHistoryMessage;
cb(null, repl); cb(null, repl);
} }
function setupHistory(repl, historyPath, oldHistoryPath, ready) { function setupHistory(repl, historyPath, oldHistoryPath, ready) {
// Empty string disables persistent history.
if (typeof historyPath === 'string')
historyPath = historyPath.trim();
if (historyPath === '') {
repl._historyPrev = _replHistoryMessage;
return ready(null, repl);
}
if (!historyPath) { if (!historyPath) {
try { try {
historyPath = path.join(os.homedir(), '.node_repl_history'); historyPath = path.join(os.homedir(), '.node_repl_history');

View File

@ -85,6 +85,11 @@ const tests = [
test: [UP], test: [UP],
expected: [prompt, replDisabled, prompt] expected: [prompt, replDisabled, prompt]
}, },
{
env: { NODE_REPL_HISTORY: ' ' },
test: [UP],
expected: [prompt, replDisabled, prompt]
},
{ {
env: { NODE_REPL_HISTORY: '', env: { NODE_REPL_HISTORY: '',
NODE_REPL_HISTORY_FILE: enoentHistoryPath }, NODE_REPL_HISTORY_FILE: enoentHistoryPath },