From 32de361d7087f7170aa8920ef9daac210a777f10 Mon Sep 17 00:00:00 2001 From: Mattias Runge-Broberg Date: Sat, 30 May 2020 22:17:09 +0200 Subject: [PATCH] readline: add history event and option to set initial history Add a history event which is emitted when the history has been changed. This enables persisting of the history in some way but also to allows a listener to alter the history. One use-case could be to prevent passwords from ending up in the history. A constructor option is also added to allow for setting an initial history list when creating a Readline interface. PR-URL: https://github.com/nodejs/node/pull/33662 Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell --- doc/api/readline.md | 35 +++++++++- lib/readline.js | 22 ++++++- test/parallel/test-readline-interface.js | 81 ++++++++++++++++-------- 3 files changed, 106 insertions(+), 32 deletions(-) diff --git a/doc/api/readline.md b/doc/api/readline.md index ac6a8b9b14c529..d6f65b434ec8a5 100644 --- a/doc/api/readline.md +++ b/doc/api/readline.md @@ -88,6 +88,28 @@ rl.on('line', (input) => { }); ``` +### Event: `'history'` + + +The `'history'` event is emitted whenever the history array has changed. + +The listener function is called with an array containing the history array. +It will reflect all changes, added lines and removed lines due to +`historySize` and `removeHistoryDuplicates`. + +The primary purpose is to allow a listener to persist the history. +It is also possible for the listener to change the history object. This +could be useful to prevent certain lines to be added to the history, like +a password. + +```js +rl.on('history', (history) => { + console.log(`Received: ${history}`); +}); +``` + ### Event: `'pause'`