Skip to content

Commit 515627c

Browse files
committed
Coerce to Min/max range
1 parent f5cf531 commit 515627c

3 files changed

Lines changed: 30 additions & 23 deletions

File tree

doc/api/readline.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,9 @@ added: v0.1.98
359359
check, otherwise the history caching mechanism is not initialized at all.
360360
* `prompt` - the prompt string to use. Default: `'> '`
361361
* `crlfDelay` {number} If the delay between `\r` and `\n` exceeds
362-
`crlfDelay`, both `\r` and `\n` will be treated as separate end-of-line
363-
input. Default to `100` milliseconds.
362+
`crlfDelay` milliseconds, both `\r` and `\n` will be treated as separate
363+
end-of-line input. Default to `100` milliseconds.
364+
`crlfDelay` will be coerced to `[100, 2000]` range.
364365

365366
The `readline.createInterface()` method creates a new `readline.Interface`
366367
instance.

lib/readline.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
'use strict';
88

99
const kHistorySize = 30;
10-
const kcrlfDelayInMillis = 100;
10+
const kMincrlfDelay = 100;
11+
const kMaxcrlfDelay = 2000;
1112

1213
const util = require('util');
1314
const debug = util.debuglog('readline');
@@ -21,20 +22,18 @@ const isFullWidthCodePoint = internalReadline.isFullWidthCodePoint;
2122
const stripVTControlCharacters = internalReadline.stripVTControlCharacters;
2223

2324

24-
exports.createInterface = function(input, output, completer, terminal,
25-
crlfDelay = kcrlfDelayInMillis) {
25+
exports.createInterface = function(input, output, completer, terminal) {
2626
var rl;
2727
if (arguments.length === 1) {
2828
rl = new Interface(input);
2929
} else {
30-
rl = new Interface(input, output, completer, terminal, crlfDelay);
30+
rl = new Interface(input, output, completer, terminal);
3131
}
3232
return rl;
3333
};
3434

3535

36-
function Interface(input, output, completer, terminal,
37-
crlfDelay = kcrlfDelayInMillis) {
36+
function Interface(input, output, completer, terminal) {
3837
if (!(this instanceof Interface)) {
3938
// call the constructor preserving original number of arguments
4039
const self = Object.create(Interface.prototype);
@@ -48,6 +47,7 @@ function Interface(input, output, completer, terminal,
4847

4948
EventEmitter.call(this);
5049
var historySize;
50+
let crlfDelay;
5151
let prompt = '> ';
5252

5353
if (arguments.length === 1) {
@@ -59,9 +59,7 @@ function Interface(input, output, completer, terminal,
5959
if (input.prompt !== undefined) {
6060
prompt = input.prompt;
6161
}
62-
if (input.crlfDelay !== undefined) {
63-
crlfDelay = input.crlfDelay;
64-
}
62+
crlfDelay = input.crlfDelay;
6563
input = input.input;
6664
}
6765

@@ -79,12 +77,6 @@ function Interface(input, output, completer, terminal,
7977
throw new TypeError('Argument "historySize" must be a positive number');
8078
}
8179

82-
if (typeof crlfDelay !== 'number' ||
83-
isNaN(crlfDelay) ||
84-
crlfDelay <= 0) {
85-
throw new TypeError('Argument "crlfDelay" must be a positive number > 0');
86-
}
87-
8880
// backwards compat; check the isTTY prop of the output stream
8981
// when `terminal` was not specified
9082
if (terminal === undefined && !(output === null || output === undefined)) {
@@ -96,7 +88,8 @@ function Interface(input, output, completer, terminal,
9688
this.output = output;
9789
this.input = input;
9890
this.historySize = historySize;
99-
this.crlfDelay = crlfDelay;
91+
this.crlfDelay = Math.max(kMincrlfDelay,
92+
Math.min(kMaxcrlfDelay, crlfDelay >>> 0));
10093

10194
// Check arity, 2 - for async, 1 for sync
10295
if (typeof completer === 'function') {

test/parallel/test-readline-interface.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ function isWarned(emitter) {
2626
return false;
2727
}
2828

29+
{
30+
// Default crlfDelay is 100ms
31+
const fi = new FakeInput();
32+
let rli = new readline.Interface({ input: fi, output: fi });
33+
assert.strictEqual(rli.crlfDelay, 100);
34+
rli.close();
35+
36+
// Minimum crlfDelay is 100ms
37+
rli = new readline.Interface({ input: fi, output: fi, crlfDelay: 0});
38+
assert.strictEqual(rli.crlfDelay, 100);
39+
rli.close();
40+
41+
// Maximum crlfDelay is 2000ms
42+
rli = new readline.Interface({ input: fi, output: fi, crlfDelay: 1 << 30});
43+
assert.strictEqual(rli.crlfDelay, 2000);
44+
rli.close();
45+
}
46+
2947
[ true, false ].forEach(function(terminal) {
3048
var fi;
3149
var rli;
@@ -46,11 +64,6 @@ function isWarned(emitter) {
4664
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal});
4765
assert.strictEqual(rli.historySize, 30);
4866

49-
// default crlfDelay is 100ms
50-
fi = new FakeInput();
51-
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal});
52-
assert.strictEqual(rli.crlfDelay, 100);
53-
5467
fi.emit('data', 'asdf\n');
5568
assert.deepStrictEqual(rli.history, terminal ? ['asdf'] : undefined);
5669
rli.close();

0 commit comments

Comments
 (0)