| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9e9e1b4 commit 32ba8ae
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,11 @@ module.exports.createInternalRepl = createRepl; | |||
| 15 | 15 | // The debounce is to guard against code pasted into the REPL. | |
| 16 | 16 | const kDebounceHistoryMS = 15; | |
| 17 | 17 | ||
| 18 | + function _writeToOutput(repl, message) { | ||
| 19 | + repl._writeToOutput(message); | ||
| 20 | + repl._refreshLine(); | ||
| 21 | + } | ||
| 22 | + | ||
| 18 | 23 | function createRepl(env, opts, cb) { | |
| 19 | 24 | if (typeof opts === 'function') { | |
| 20 | 25 | cb = opts; | |
@@ -80,9 +85,8 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) { | |||
| 80 | 85 | try { | |
| 81 | 86 | historyPath = path.join(os.homedir(), '.node_repl_history'); | |
| 82 | 87 | } catch (err) { | |
| 83 | - repl._writeToOutput('\nError: Could not get the home directory.\n' + | ||
| 84 | - 'REPL session history will not be persisted.\n'); | ||
| 85 | - repl._refreshLine(); | ||
| 88 | + _writeToOutput(repl, '\nError: Could not get the home directory.\n' + | ||
| 89 | + 'REPL session history will not be persisted.\n'); | ||
| 86 | 90 | ||
| 87 | 91 | debug(err.stack); | |
| 88 | 92 | repl._historyPrev = _replHistoryMessage; | |
@@ -103,9 +107,8 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) { | |||
| 103 | 107 | if (err) { | |
| 104 | 108 | // Cannot open history file. | |
| 105 | 109 | // Don't crash, just don't persist history. | |
| 106 | - repl._writeToOutput('\nError: Could not open history file.\n' + | ||
| 107 | - 'REPL session history will not be persisted.\n'); | ||
| 108 | - repl._refreshLine(); | ||
| 110 | + _writeToOutput(repl, '\nError: Could not open history file.\n' + | ||
| 111 | + 'REPL session history will not be persisted.\n'); | ||
| 109 | 112 | debug(err.stack); | |
| 110 | 113 | ||
| 111 | 114 | repl._historyPrev = _replHistoryMessage; | |
@@ -132,18 +135,13 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) { | |||
| 132 | 135 | } else if (oldHistoryPath === historyPath) { | |
| 133 | 136 | // If pre-v3.0, the user had set NODE_REPL_HISTORY_FILE to | |
| 134 | 137 | // ~/.node_repl_history, warn the user about it and proceed. | |
| 135 | - repl._writeToOutput( | ||
| 136 | - '\nThe old repl history file has the same name and location as ' + | ||
| 138 | + _writeToOutput( | ||
| 139 | + repl, | ||
| 140 | + '\nThe old repl history file has the same name and location as ' + | ||
| 137 | 141 | `the new one i.e., ${historyPath} and is empty.\nUsing it as is.\n`); | |
| 138 | - repl._refreshLine(); | ||
| 139 | 142 | ||
| 140 | 143 | } else if (oldHistoryPath) { | |
| 141 | - // Grab data from the older pre-v3.0 JSON NODE_REPL_HISTORY_FILE format. | ||
| 142 | - repl._writeToOutput( | ||
| 143 | - '\nConverting old JSON repl history to line-separated history.\n' + | ||
| 144 | - `The new repl history file can be found at ${historyPath}.\n`); | ||
| 145 | - repl._refreshLine(); | ||
| 146 | - | ||
| 144 | + let threw = false; | ||
| 147 | 145 | try { | |
| 148 | 146 | // Pre-v3.0, repl history was stored as JSON. | |
| 149 | 147 | // Try and convert it to line separated history. | |
@@ -152,15 +150,31 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) { | |||
| 152 | 150 | // Only attempt to use the history if there was any. | |
| 153 | 151 | if (oldReplJSONHistory) repl.history = JSON.parse(oldReplJSONHistory); | |
| 154 | 152 | ||
| 155 | - if (!Array.isArray(repl.history)) { | ||
| 156 | - throw new Error('Expected array, got ' + typeof repl.history); | ||
| 153 | + if (Array.isArray(repl.history)) { | ||
| 154 | + repl.history = repl.history.slice(0, repl.historySize); | ||
| 155 | + } else { | ||
| 156 | + threw = true; | ||
| 157 | + _writeToOutput( | ||
| 158 | + repl, | ||
| 159 | + '\nError: The old history file data has to be an Array.\n' + | ||
| 160 | + 'REPL session history will not be persisted.\n'); | ||
| 157 | 161 | } | |
| 158 | - repl.history = repl.history.slice(0, repl.historySize); | ||
| 159 | 162 | } catch (err) { | |
| 160 | - if (err.code !== 'ENOENT') { | ||
| 161 | - return ready( | ||
| 162 | - new Error(`Could not parse history data in ${oldHistoryPath}.`)); | ||
| 163 | - } | ||
| 163 | + // Cannot open or parse history file. | ||
| 164 | + // Don't crash, just don't persist history. | ||
| 165 | + threw = true; | ||
| 166 | + const type = err instanceof SyntaxError ? 'parse' : 'open'; | ||
| 167 | + _writeToOutput(repl, `\nError: Could not ${type} old history file.\n` + | ||
| 168 | + 'REPL session history will not be persisted.\n'); | ||
| 169 | + } | ||
| 170 | + if (!threw) { | ||
| 171 | + // Grab data from the older pre-v3.0 JSON NODE_REPL_HISTORY_FILE format. | ||
| 172 | + _writeToOutput( | ||
| 173 | + repl, | ||
| 174 | + '\nConverted old JSON repl history to line-separated history.\n' + | ||
| 175 | + `The new repl history file can be found at ${historyPath}.\n`); | ||
| 176 | + } else { | ||
| 177 | + repl.history = []; | ||
| 164 | 178 | } | |
| 165 | 179 | } | |
| 166 | 180 | ||
@@ -223,12 +237,12 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) { | |||
| 223 | 237 | ||
| 224 | 238 | function _replHistoryMessage() { | |
| 225 | 239 | if (this.history.length === 0) { | |
| 226 | - this._writeToOutput( | ||
| 227 | - '\nPersistent history support disabled. ' + | ||
| 240 | + _writeToOutput( | ||
| 241 | + this, | ||
| 242 | + '\nPersistent history support disabled. ' + | ||
| 228 | 243 | 'Set the NODE_REPL_HISTORY environment\nvariable to ' + | |
| 229 | 244 | 'a valid, user-writable path to enable.\n' | |
| 230 | 245 | ); | |
| 231 | - this._refreshLine(); | ||
| 232 | 246 | } | |
| 233 | 247 | this._historyPrev = Interface.prototype._historyPrev; | |
| 234 | 248 | return this._historyPrev(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + undefined | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,4 @@ | |||
| 1 | + { | ||
| 2 | + "a": "'=^.^='", | ||
| 3 | + "b": "'hello world'" | ||
| 4 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,13 +56,19 @@ const prompt = '> '; | |||
| 56 | 56 | const replDisabled = '\nPersistent history support disabled. Set the ' + | |
| 57 | 57 | 'NODE_REPL_HISTORY environment\nvariable to a valid, ' + | |
| 58 | 58 | 'user-writable path to enable.\n'; | |
| 59 | - const convertMsg = '\nConverting old JSON repl history to line-separated ' + | ||
| 59 | + const convertMsg = '\nConverted old JSON repl history to line-separated ' + | ||
| 60 | 60 | 'history.\nThe new repl history file can be found at ' + | |
| 61 | 61 | `${path.join(common.tmpDir, '.node_repl_history')}.\n`; | |
| 62 | 62 | const homedirErr = '\nError: Could not get the home directory.\n' + | |
| 63 | 63 | 'REPL session history will not be persisted.\n'; | |
| 64 | 64 | const replFailedRead = '\nError: Could not open history file.\n' + | |
| 65 | 65 | 'REPL session history will not be persisted.\n'; | |
| 66 | + const oldHistoryFailedOpen = '\nError: Could not open old history file.\n' + | ||
| 67 | + 'REPL session history will not be persisted.\n'; | ||
| 68 | + const oldHistoryFailedParse = '\nError: Could not parse old history file.\n' + | ||
| 69 | + 'REPL session history will not be persisted.\n'; | ||
| 70 | + const oldHistoryObj = '\nError: The old history file data has to be an Array' + | ||
| 71 | + '.\nREPL session history will not be persisted.\n'; | ||
| 66 | 72 | const sameHistoryFilePaths = '\nThe old repl history file has the same name ' + | |
| 67 | 73 | 'and location as the new one i.e., ' + | |
| 68 | 74 | path.join(common.tmpDir, '.node_repl_history') + | |
@@ -72,6 +78,10 @@ const fixtures = common.fixturesDir; | |||
| 72 | 78 | const historyFixturePath = path.join(fixtures, '.node_repl_history'); | |
| 73 | 79 | const historyPath = path.join(common.tmpDir, '.fixture_copy_repl_history'); | |
| 74 | 80 | const historyPathFail = path.join(common.tmpDir, '.node_repl\u0000_history'); | |
| 81 | + const oldHistoryPathObj = path.join(fixtures, | ||
| 82 | + 'old-repl-history-file-obj.json'); | ||
| 83 | + const oldHistoryPathFaulty = path.join(fixtures, | ||
| 84 | + 'old-repl-history-file-faulty.json'); | ||
| 75 | 85 | const oldHistoryPath = path.join(fixtures, 'old-repl-history-file.json'); | |
| 76 | 86 | const enoentHistoryPath = path.join(fixtures, 'enoent-repl-history-file.json'); | |
| 77 | 87 | const emptyHistoryPath = path.join(fixtures, '.empty-repl-history-file'); | |
@@ -93,10 +103,19 @@ const tests = [ | |||
| 93 | 103 | expected: [prompt, replDisabled, prompt] | |
| 94 | 104 | }, | |
| 95 | 105 | { | |
| 96 | - env: { NODE_REPL_HISTORY: '', | ||
| 97 | - NODE_REPL_HISTORY_FILE: enoentHistoryPath }, | ||
| 106 | + env: { NODE_REPL_HISTORY_FILE: enoentHistoryPath }, | ||
| 98 | 107 | test: [UP], | |
| 99 | - expected: [prompt, replDisabled, prompt] | ||
| 108 | + expected: [prompt, oldHistoryFailedOpen, prompt] | ||
| 109 | + }, | ||
| 110 | + { | ||
| 111 | + env: { NODE_REPL_HISTORY_FILE: oldHistoryPathObj }, | ||
| 112 | + test: [UP], | ||
| 113 | + expected: [prompt, oldHistoryObj, prompt] | ||
| 114 | + }, | ||
| 115 | + { | ||
| 116 | + env: { NODE_REPL_HISTORY_FILE: oldHistoryPathFaulty }, | ||
| 117 | + test: [UP], | ||
| 118 | + expected: [prompt, oldHistoryFailedParse, prompt] | ||
| 100 | 119 | }, | |
| 101 | 120 | { | |
| 102 | 121 | env: { NODE_REPL_HISTORY: '', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments