|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +module.exports = {createRepl: createRepl}; |
| 4 | + |
| 5 | +const Interface = require('readline').Interface; |
| 6 | +const REPL = require('repl'); |
| 7 | +const path = require('path'); |
| 8 | + |
| 9 | +// XXX(chrisdickinson): The 15ms debounce value is somewhat arbitrary. |
| 10 | +// The debounce is to guard against code pasted into the REPL. |
| 11 | +const kDebounceHistoryMS = 15; |
| 12 | + |
| 13 | +try { |
| 14 | + // hack for require.resolve("./relative") to work properly. |
| 15 | + module.filename = path.resolve('repl'); |
| 16 | +} catch (e) { |
| 17 | + // path.resolve('repl') fails when the current working directory has been |
| 18 | + // deleted. Fall back to the directory name of the (absolute) executable |
| 19 | + // path. It's not really correct but what are the alternatives? |
| 20 | + const dirname = path.dirname(process.execPath); |
| 21 | + module.filename = path.resolve(dirname, 'repl'); |
| 22 | +} |
| 23 | + |
| 24 | +// hack for repl require to work properly with node_modules folders |
| 25 | +module.paths = require('module')._nodeModulePaths(module.filename); |
| 26 | + |
| 27 | +function createRepl(env, cb) { |
| 28 | + const opts = { |
| 29 | + useGlobal: true, |
| 30 | + ignoreUndefined: false |
| 31 | + }; |
| 32 | + |
| 33 | + if (parseInt(env.NODE_NO_READLINE)) { |
| 34 | + opts.terminal = false; |
| 35 | + } |
| 36 | + if (parseInt(env.NODE_DISABLE_COLORS)) { |
| 37 | + opts.useColors = false; |
| 38 | + } |
| 39 | + |
| 40 | + opts.replMode = { |
| 41 | + 'strict': REPL.REPL_MODE_STRICT, |
| 42 | + 'sloppy': REPL.REPL_MODE_SLOPPY, |
| 43 | + 'magic': REPL.REPL_MODE_MAGIC |
| 44 | + }[String(env.NODE_REPL_MODE).toLowerCase().trim()]; |
| 45 | + |
| 46 | + if (opts.replMode === undefined) { |
| 47 | + opts.replMode = REPL.REPL_MODE_MAGIC; |
| 48 | + } |
| 49 | + |
| 50 | + const historySize = Number(env.NODE_REPL_HISTORY_SIZE); |
| 51 | + if (!isNaN(historySize) && historySize > 0) { |
| 52 | + opts.historySize = historySize; |
| 53 | + } else { |
| 54 | + // XXX(chrisdickinson): set here to avoid affecting existing applications |
| 55 | + // using repl instances. |
| 56 | + opts.historySize = 1000; |
| 57 | + } |
| 58 | + |
| 59 | + const repl = REPL.start(opts); |
| 60 | + if (env.NODE_REPL_HISTORY_PATH) { |
| 61 | + return setupHistory(repl, env.NODE_REPL_HISTORY_PATH, cb); |
| 62 | + } |
| 63 | + repl._historyPrev = _replHistoryMessage; |
| 64 | + cb(null, repl); |
| 65 | +} |
| 66 | + |
| 67 | +function setupHistory(repl, historyPath, ready) { |
| 68 | + const fs = require('fs'); |
| 69 | + var timer = null; |
| 70 | + var writing = false; |
| 71 | + var pending = false; |
| 72 | + repl.pause(); |
| 73 | + fs.open(historyPath, 'a+', oninit); |
| 74 | + |
| 75 | + function oninit(err, hnd) { |
| 76 | + if (err) { |
| 77 | + return ready(err); |
| 78 | + } |
| 79 | + fs.close(hnd, onclose); |
| 80 | + } |
| 81 | + |
| 82 | + function onclose(err) { |
| 83 | + if (err) { |
| 84 | + return ready(err); |
| 85 | + } |
| 86 | + fs.readFile(historyPath, 'utf8', onread); |
| 87 | + } |
| 88 | + |
| 89 | + function onread(err, data) { |
| 90 | + if (err) { |
| 91 | + return ready(err); |
| 92 | + } |
| 93 | + |
| 94 | + if (data) { |
| 95 | + try { |
| 96 | + repl.history = JSON.parse(data); |
| 97 | + if (!Array.isArray(repl.history)) { |
| 98 | + throw new Error('Expected array, got ' + typeof repl.history); |
| 99 | + } |
| 100 | + repl.history.slice(-repl.historySize); |
| 101 | + } catch (err) { |
| 102 | + return ready( |
| 103 | + new Error(`Could not parse history data in ${historyPath}.`)); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + fs.open(historyPath, 'w', onhandle); |
| 108 | + } |
| 109 | + |
| 110 | + function onhandle(err, hnd) { |
| 111 | + if (err) { |
| 112 | + return ready(err); |
| 113 | + } |
| 114 | + repl._historyHandle = hnd; |
| 115 | + repl.on('line', online); |
| 116 | + repl.resume(); |
| 117 | + return ready(null, repl); |
| 118 | + } |
| 119 | + |
| 120 | + // ------ history listeners ------ |
| 121 | + function online() { |
| 122 | + repl._flushing = true; |
| 123 | + |
| 124 | + if (timer) { |
| 125 | + clearTimeout(timer); |
| 126 | + } |
| 127 | + |
| 128 | + timer = setTimeout(flushHistory, kDebounceHistoryMS); |
| 129 | + } |
| 130 | + |
| 131 | + function flushHistory() { |
| 132 | + timer = null; |
| 133 | + if (writing) { |
| 134 | + pending = true; |
| 135 | + return; |
| 136 | + } |
| 137 | + writing = true; |
| 138 | + const historyData = JSON.stringify(repl.history, null, 2); |
| 139 | + fs.write(repl._historyHandle, historyData, 0, 'utf8', onwritten); |
| 140 | + } |
| 141 | + |
| 142 | + function onwritten(err, data) { |
| 143 | + writing = false; |
| 144 | + if (pending) { |
| 145 | + pending = false; |
| 146 | + online(); |
| 147 | + } else { |
| 148 | + repl._flushing = Boolean(timer); |
| 149 | + if (!repl._flushing) { |
| 150 | + repl.emit('flushHistory'); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | + |
| 157 | +function _replHistoryMessage() { |
| 158 | + if (this.history.length === 0) { |
| 159 | + this._writeToOutput( |
| 160 | + '\nPersistent history support disabled. ' + |
| 161 | + 'Set the NODE_REPL_HISTORY_PATH environment variable to ' + |
| 162 | + 'a valid, user-writable path to enable.\n' |
| 163 | + ); |
| 164 | + this._refreshLine(); |
| 165 | + } |
| 166 | + this._historyPrev = Interface.prototype._historyPrev; |
| 167 | + return this._historyPrev(); |
| 168 | +} |
0 commit comments