repl: make sure historyPath is trimmed
authorEvan Lucas <evanlucas@me.com>
Tue, 5 Jan 2016 12:28:27 +0000 (06:28 -0600)
committerMyles Borins <mborins@us.ibm.com>
Mon, 15 Feb 2016 19:30:23 +0000 (11:30 -0800)
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>
doc/api/repl.markdown
lib/internal/repl.js
test/parallel/test-repl-persistent-history.js

index 99e9d0e26d87208bc0dec7080e6066bb676ef846..ad429dc0f858b674ed6888992fab867f9e8bf66e 100644 (file)
@@ -37,7 +37,7 @@ via the following environment variables:
  - `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
    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
    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
index e6b41fbdd89b654c26d3c7badcadd74e1dd6cc45..371446a83bd4fd19cc76116668d2efe52293858c 100644 (file)
@@ -55,15 +55,26 @@ function createRepl(env, opts, cb) {
   }
 
   const repl = REPL.start(opts);
-  if (opts.terminal && env.NODE_REPL_HISTORY !== '') {
+  if (opts.terminal) {
     return setupHistory(repl, env.NODE_REPL_HISTORY,
                         env.NODE_REPL_HISTORY_FILE, cb);
   }
+
   repl._historyPrev = _replHistoryMessage;
   cb(null, repl);
 }
 
 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) {
     try {
       historyPath = path.join(os.homedir(), '.node_repl_history');
index 81e728974f179d4f8422325e7b97cc33ab35bb15..387cef7e97a79483715a6a1a5ea897fd9e4dc70a 100644 (file)
@@ -85,6 +85,11 @@ const tests = [
     test: [UP],
     expected: [prompt, replDisabled, prompt]
   },
+  {
+    env: { NODE_REPL_HISTORY: ' ' },
+    test: [UP],
+    expected: [prompt, replDisabled, prompt]
+  },
   {
     env: { NODE_REPL_HISTORY: '',
            NODE_REPL_HISTORY_FILE: enoentHistoryPath },