@@ -7,15 +7,18 @@ const fs = require('fs');
7
7
const os = require ( 'os' ) ;
8
8
const util = require ( 'util' ) ;
9
9
const debug = util . debuglog ( 'repl' ) ;
10
- const errors = require ( 'internal/errors' ) ;
11
-
12
10
module . exports = Object . create ( REPL ) ;
13
11
module . exports . createInternalRepl = createRepl ;
14
12
15
13
// XXX(chrisdickinson): The 15ms debounce value is somewhat arbitrary.
16
14
// The debounce is to guard against code pasted into the REPL.
17
15
const kDebounceHistoryMS = 15 ;
18
16
17
+ function _writeToOutput ( repl , message ) {
18
+ repl . _writeToOutput ( message ) ;
19
+ repl . _refreshLine ( ) ;
20
+ }
21
+
19
22
function createRepl ( env , opts , cb ) {
20
23
if ( typeof opts === 'function' ) {
21
24
cb = opts ;
@@ -81,9 +84,8 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
81
84
try {
82
85
historyPath = path . join ( os . homedir ( ) , '.node_repl_history' ) ;
83
86
} catch ( err ) {
84
- repl . _writeToOutput ( '\nError: Could not get the home directory.\n' +
85
- 'REPL session history will not be persisted.\n' ) ;
86
- repl . _refreshLine ( ) ;
87
+ _writeToOutput ( repl , '\nError: Could not get the home directory.\n' +
88
+ 'REPL session history will not be persisted.\n' ) ;
87
89
88
90
debug ( err . stack ) ;
89
91
repl . _historyPrev = _replHistoryMessage ;
@@ -104,9 +106,8 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
104
106
if ( err ) {
105
107
// Cannot open history file.
106
108
// Don't crash, just don't persist history.
107
- repl . _writeToOutput ( '\nError: Could not open history file.\n' +
108
- 'REPL session history will not be persisted.\n' ) ;
109
- repl . _refreshLine ( ) ;
109
+ _writeToOutput ( repl , '\nError: Could not open history file.\n' +
110
+ 'REPL session history will not be persisted.\n' ) ;
110
111
debug ( err . stack ) ;
111
112
112
113
repl . _historyPrev = _replHistoryMessage ;
@@ -133,18 +134,13 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
133
134
} else if ( oldHistoryPath === historyPath ) {
134
135
// If pre-v3.0, the user had set NODE_REPL_HISTORY_FILE to
135
136
// ~/.node_repl_history, warn the user about it and proceed.
136
- repl . _writeToOutput (
137
- '\nThe old repl history file has the same name and location as ' +
137
+ _writeToOutput (
138
+ repl ,
139
+ '\nThe old repl history file has the same name and location as ' +
138
140
`the new one i.e., ${ historyPath } and is empty.\nUsing it as is.\n` ) ;
139
- repl . _refreshLine ( ) ;
140
141
141
142
} else if ( oldHistoryPath ) {
142
- // Grab data from the older pre-v3.0 JSON NODE_REPL_HISTORY_FILE format.
143
- repl . _writeToOutput (
144
- '\nConverting old JSON repl history to line-separated history.\n' +
145
- `The new repl history file can be found at ${ historyPath } .\n` ) ;
146
- repl . _refreshLine ( ) ;
147
-
143
+ let threw = false ;
148
144
try {
149
145
// Pre-v3.0, repl history was stored as JSON.
150
146
// Try and convert it to line separated history.
@@ -153,16 +149,31 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
153
149
// Only attempt to use the history if there was any.
154
150
if ( oldReplJSONHistory ) repl . history = JSON . parse ( oldReplJSONHistory ) ;
155
151
156
- if ( ! Array . isArray ( repl . history ) ) {
157
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
158
- typeof repl . history , 'Array' ) ;
152
+ if ( Array . isArray ( repl . history ) ) {
153
+ repl . history = repl . history . slice ( 0 , repl . historySize ) ;
154
+ } else {
155
+ threw = true ;
156
+ _writeToOutput (
157
+ repl ,
158
+ '\nError: The old history file data has to be an Array.\n' +
159
+ 'REPL session history will not be persisted.\n' ) ;
159
160
}
160
- repl . history = repl . history . slice ( 0 , repl . historySize ) ;
161
161
} catch ( err ) {
162
- if ( err . code !== 'ENOENT' ) {
163
- return ready (
164
- new errors . Error ( 'ERR_PARSE_HISTORY_DATA' , oldHistoryPath ) ) ;
165
- }
162
+ // Cannot open or parse history file.
163
+ // Don't crash, just don't persist history.
164
+ threw = true ;
165
+ const type = err instanceof SyntaxError ? 'parse' : 'open' ;
166
+ _writeToOutput ( repl , `\nError: Could not ${ type } old history file.\n` +
167
+ 'REPL session history will not be persisted.\n' ) ;
168
+ }
169
+ if ( ! threw ) {
170
+ // Grab data from the older pre-v3.0 JSON NODE_REPL_HISTORY_FILE format.
171
+ _writeToOutput (
172
+ repl ,
173
+ '\nConverted old JSON repl history to line-separated history.\n' +
174
+ `The new repl history file can be found at ${ historyPath } .\n` ) ;
175
+ } else {
176
+ repl . history = [ ] ;
166
177
}
167
178
}
168
179
@@ -225,12 +236,12 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
225
236
226
237
function _replHistoryMessage ( ) {
227
238
if ( this . history . length === 0 ) {
228
- this . _writeToOutput (
229
- '\nPersistent history support disabled. ' +
239
+ _writeToOutput (
240
+ this ,
241
+ '\nPersistent history support disabled. ' +
230
242
'Set the NODE_REPL_HISTORY environment\nvariable to ' +
231
243
'a valid, user-writable path to enable.\n'
232
244
) ;
233
- this . _refreshLine ( ) ;
234
245
}
235
246
this . _historyPrev = Interface . prototype . _historyPrev ;
236
247
return this . _historyPrev ( ) ;
0 commit comments