-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
repl: add repl.setupHistory for programmatic repl #25895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
'use strict'; | ||
|
||
const { Interface } = require('readline'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const os = require('os'); | ||
const util = require('util'); | ||
const debug = util.debuglog('repl'); | ||
|
||
// 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 = historyPath.trim(); | ||
|
||
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); | ||
} | ||
} | ||
|
||
var timer = null; | ||
var writing = false; | ||
var 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 = data.split(/[\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); | ||
|
||
// 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 = repl.history.join(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 _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(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.