All files / lib/internal/repl history.js

92% Statements 161/175
83.33% Branches 30/36
100% Functions 11/11
92% Lines 161/175

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 1762x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 13x 13x 13x 13x 2x 75x 75x 75x 75x 75x 75x 34x 34x 34x 41x 45x 12x 12x 12x 2x 2x 2x 2x 2x 2x 2x 12x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 36x 39x 39x 39x 36x     36x 36x 39x 39x 36x     36x 36x 15x 30x 21x 21x 36x 36x 36x 39x 39x 36x     36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 39x 39x 39x 85x 85x 85x 57x 57x 85x 85x 85x 39x 39x 64x 64x       64x 64x 64x 64x 39x 39x 64x 64x     64x 64x 64x 59x 59x 64x 64x 39x 39x 36x       36x 36x 36x 75x 2x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x  
'use strict';
 
const {
  ArrayPrototypeJoin,
  Boolean,
  FunctionPrototype,
  StringPrototypeSplit,
  StringPrototypeTrim,
} = primordials;
 
const { Interface } = require('readline');
const path = require('path');
const fs = require('fs');
const os = require('os');
let debug = require('internal/util/debuglog').debuglog('repl', (fn) => {
  debug = fn;
});
const { clearTimeout, setTimeout } = require('timers');
 
const noop = FunctionPrototype;
 
// XXX(chrisdickinson): The 15ms debounce value is somewhat arbitrary.
// The debounce is to guard against code pasted into the REPL.
const kDebounceHistoryMS = 15;
 
module.exports = setupHistory;
 
function _writeToOutput(repl, message) {
  repl._writeToOutput(message);
  repl._refreshLine();
}
 
function setupHistory(repl, historyPath, ready) {
  // Empty string disables persistent history
  if (typeof historyPath === 'string')
    historyPath = StringPrototypeTrim(historyPath);
 
  if (historyPath === '') {
    repl._historyPrev = _replHistoryMessage;
    return ready(null, repl);
  }
 
  if (!historyPath) {
    try {
      historyPath = path.join(os.homedir(), '.node_repl_history');
    } catch (err) {
      _writeToOutput(repl, '\nError: Could not get the home directory.\n' +
        'REPL session history will not be persisted.\n');
 
      debug(err.stack);
      repl._historyPrev = _replHistoryMessage;
      return ready(null, repl);
    }
  }
 
  let timer = null;
  let writing = false;
  let pending = false;
  repl.pause();
  // History files are conventionally not readable by others:
  // https://github.com/nodejs/node/issues/3392
  // https://github.com/nodejs/node/pull/3394
  fs.open(historyPath, 'a+', 0o0600, oninit);
 
  function oninit(err, hnd) {
    if (err) {
      // Cannot open history file.
      // Don't crash, just don't persist history.
      _writeToOutput(repl, '\nError: Could not open history file.\n' +
        'REPL session history will not be persisted.\n');
      debug(err.stack);
 
      repl._historyPrev = _replHistoryMessage;
      repl.resume();
      return ready(null, repl);
    }
    fs.close(hnd, onclose);
  }
 
  function onclose(err) {
    if (err) {
      return ready(err);
    }
    fs.readFile(historyPath, 'utf8', onread);
  }
 
  function onread(err, data) {
    if (err) {
      return ready(err);
    }
 
    if (data) {
      repl.history = StringPrototypeSplit(data, /[\n\r]+/, repl.historySize);
    } else {
      repl.history = [];
    }
 
    fs.open(historyPath, 'r+', onhandle);
  }
 
  function onhandle(err, hnd) {
    if (err) {
      return ready(err);
    }
    fs.ftruncate(hnd, 0, (err) => {
      repl._historyHandle = hnd;
      repl.on('line', online);
      repl.once('exit', onexit);
 
      // Reading the file data out erases it
      repl.once('flushHistory', function() {
        repl.resume();
        ready(null, repl);
      });
      flushHistory();
    });
  }
 
  // ------ history listeners ------
  function online(line) {
    repl._flushing = true;
 
    if (timer) {
      clearTimeout(timer);
    }
 
    timer = setTimeout(flushHistory, kDebounceHistoryMS);
  }
 
  function flushHistory() {
    timer = null;
    if (writing) {
      pending = true;
      return;
    }
    writing = true;
    const historyData = ArrayPrototypeJoin(repl.history, os.EOL);
    fs.write(repl._historyHandle, historyData, 0, 'utf8', onwritten);
  }
 
  function onwritten(err, data) {
    writing = false;
    if (pending) {
      pending = false;
      online();
    } else {
      repl._flushing = Boolean(timer);
      if (!repl._flushing) {
        repl.emit('flushHistory');
      }
    }
  }
 
  function onexit() {
    if (repl._flushing) {
      repl.once('flushHistory', onexit);
      return;
    }
    repl.off('line', online);
    fs.close(repl._historyHandle, noop);
  }
}
 
function _replHistoryMessage() {
  if (this.history.length === 0) {
    _writeToOutput(
      this,
      '\nPersistent history support disabled. ' +
      'Set the NODE_REPL_HISTORY environment\nvariable to ' +
      'a valid, user-writable path to enable.\n'
    );
  }
  this._historyPrev = Interface.prototype._historyPrev;
  return this._historyPrev();
}