| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent cbd698a commit 1faae90
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,8 +47,6 @@ const { | |||
| 47 | 47 | kClearScreenDown | |
| 48 | 48 | } = CSI; | |
| 49 | 49 | ||
| 50 | - const { now } = process.binding('timer_wrap').Timer; | ||
| 51 | - | ||
| 52 | 50 | const kHistorySize = 30; | |
| 53 | 51 | const kMincrlfDelay = 100; | |
| 54 | 52 | // \r\n, \n, or \r followed by something other than \n | |
@@ -400,7 +398,7 @@ Interface.prototype._normalWrite = function(b) { | |||
| 400 | 398 | } | |
| 401 | 399 | var string = this._decoder.write(b); | |
| 402 | 400 | if (this._sawReturnAt && | |
| 403 | - now() - this._sawReturnAt <= this.crlfDelay) { | ||
| 401 | + Date.now() - this._sawReturnAt <= this.crlfDelay) { | ||
| 404 | 402 | string = string.replace(/^\n/, ''); | |
| 405 | 403 | this._sawReturnAt = 0; | |
| 406 | 404 | } | |
@@ -413,7 +411,7 @@ Interface.prototype._normalWrite = function(b) { | |||
| 413 | 411 | this._line_buffer = null; | |
| 414 | 412 | } | |
| 415 | 413 | if (newPartContainsEnding) { | |
| 416 | - this._sawReturnAt = string.endsWith('\r') ? now() : 0; | ||
| 414 | + this._sawReturnAt = string.endsWith('\r') ? Date.now() : 0; | ||
| 417 | 415 | ||
| 418 | 416 | // got one or more newlines; process into "line" events | |
| 419 | 417 | var lines = string.split(lineEnding); | |
@@ -908,14 +906,14 @@ Interface.prototype._ttyWrite = function(s, key) { | |||
| 908 | 906 | ||
| 909 | 907 | switch (key.name) { | |
| 910 | 908 | case 'return': // carriage return, i.e. \r | |
| 911 | - this._sawReturnAt = now(); | ||
| 909 | + this._sawReturnAt = Date.now(); | ||
| 912 | 910 | this._line(); | |
| 913 | 911 | break; | |
| 914 | 912 | ||
| 915 | 913 | case 'enter': | |
| 916 | 914 | // When key interval > crlfDelay | |
| 917 | 915 | if (this._sawReturnAt === 0 || | |
| 918 | - now() - this._sawReturnAt > this.crlfDelay) { | ||
| 916 | + Date.now() - this._sawReturnAt > this.crlfDelay) { | ||
| 919 | 917 | this._line(); | |
| 920 | 918 | } | |
| 921 | 919 | this._sawReturnAt = 0; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -873,3 +873,80 @@ function isWarned(emitter) { | |||
| 873 | 873 | assert.strictEqual(rl._prompt, '$ '); | |
| 874 | 874 | } | |
| 875 | 875 | }); | |
| 876 | + | ||
| 877 | + // For the purposes of the following tests, we do not care about the exact | ||
| 878 | + // value of crlfDelay, only that the behaviour conforms to what's expected. | ||
| 879 | + // Setting it to Infinity allows the test to succeed even under extreme | ||
| 880 | + // CPU stress. | ||
| 881 | + const crlfDelay = Infinity; | ||
| 882 | + | ||
| 883 | + [ true, false ].forEach(function(terminal) { | ||
| 884 | + // sending multiple newlines at once that does not end with a new line | ||
| 885 | + // and a `end` event(last line is) | ||
| 886 | + | ||
| 887 | + // \r\n should emit one line event, not two | ||
| 888 | + { | ||
| 889 | + const fi = new FakeInput(); | ||
| 890 | + const rli = new readline.Interface( | ||
| 891 | + { | ||
| 892 | + input: fi, | ||
| 893 | + output: fi, | ||
| 894 | + terminal: terminal, | ||
| 895 | + crlfDelay | ||
| 896 | + } | ||
| 897 | + ); | ||
| 898 | + const expectedLines = ['foo', 'bar', 'baz', 'bat']; | ||
| 899 | + let callCount = 0; | ||
| 900 | + rli.on('line', function(line) { | ||
| 901 | + assert.strictEqual(line, expectedLines[callCount]); | ||
| 902 | + callCount++; | ||
| 903 | + }); | ||
| 904 | + fi.emit('data', expectedLines.join('\r\n')); | ||
| 905 | + assert.strictEqual(callCount, expectedLines.length - 1); | ||
| 906 | + rli.close(); | ||
| 907 | + } | ||
| 908 | + | ||
| 909 | + // \r\n should emit one line event when split across multiple writes. | ||
| 910 | + { | ||
| 911 | + const fi = new FakeInput(); | ||
| 912 | + const rli = new readline.Interface({ | ||
| 913 | + input: fi, | ||
| 914 | + output: fi, | ||
| 915 | + terminal: terminal, | ||
| 916 | + crlfDelay | ||
| 917 | + }); | ||
| 918 | + const expectedLines = ['foo', 'bar', 'baz', 'bat']; | ||
| 919 | + let callCount = 0; | ||
| 920 | + rli.on('line', function(line) { | ||
| 921 | + assert.strictEqual(line, expectedLines[callCount]); | ||
| 922 | + callCount++; | ||
| 923 | + }); | ||
| 924 | + expectedLines.forEach(function(line) { | ||
| 925 | + fi.emit('data', `${line}\r`); | ||
| 926 | + fi.emit('data', '\n'); | ||
| 927 | + }); | ||
| 928 | + assert.strictEqual(callCount, expectedLines.length); | ||
| 929 | + rli.close(); | ||
| 930 | + } | ||
| 931 | + | ||
| 932 | + // Emit one line event when the delay between \r and \n is | ||
| 933 | + // over the default crlfDelay but within the setting value. | ||
| 934 | + { | ||
| 935 | + const fi = new FakeInput(); | ||
| 936 | + const delay = 125; | ||
| 937 | + const rli = new readline.Interface({ | ||
| 938 | + input: fi, | ||
| 939 | + output: fi, | ||
| 940 | + terminal: terminal, | ||
| 941 | + crlfDelay | ||
| 942 | + }); | ||
| 943 | + let callCount = 0; | ||
| 944 | + rli.on('line', () => callCount++); | ||
| 945 | + fi.emit('data', '\r'); | ||
| 946 | + setTimeout(common.mustCall(() => { | ||
| 947 | + fi.emit('data', '\n'); | ||
| 948 | + assert.strictEqual(callCount, 1); | ||
| 949 | + rli.close(); | ||
| 950 | + }), delay); | ||
| 951 | + } | ||
| 952 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments