I've been using nexline
for a while now, and it's so convenient! I wish standard Node.js had a n async readline
implementation.
Unfortunately, I've run into a pretty deal-breaking bug. All of a sudden nexline
has started silently exiting the entire Node.js process when it reaches the end of the stream instead of returning null. Consider the following test file:
....and the following Node.js script:
import nexline from 'nexline';
(async () => {
"use strict";
let reader = nexline({
input: process.stdin
});
let items = [];
while(true) {
console.log("Getting next line");
const line = await reader.next();
console.log(`Got next line: ${line}`);
if(line === null) { console.log("was null, breaking"); break; }
items.push(line);
console.log(`Read ${items.length} objects so far`);
}
console.log(`Done, read ${items.length} in total`);
})();
Watch what happens when I execute it:
$ cat /tmp/test.txt
apple
orange
banana
pear
sbrl@box:~/path/to/PhD$ cat /tmp/test.txt | node ./test.mjs
(node:10920) ExperimentalWarning: The ESM module loader is experimental.
Getting next line
Got next line: apple
Read 1 objects so far
Getting next line
Got next line: orange
Read 2 objects so far
Getting next line
Got next line: banana
Read 3 objects so far
Getting next line
Got next line: pear
Read 4 objects so far
Getting next line
sbrl@box:~/path/to/PhD$ echo $?
0
It correctly reads the 4 lines of input, but when it tried to read the 5th it dies instead of returning null
. It doesn't even throw an error that can be caught - or indeed return a non-zero exit code.
If I force the Node.js process to stick around a bit longer with a sneaky setTimeout
at the top of the file (below the import) like this:
setTimeout(() => console.log("the future is now"), 2000);
...it has no effect other than delaying Node.js' exit.
System details:
sbrl@box:~$ node --version
v13.8.0
sbrl@box:~$ npm --version
6.13.7
sbrl@box:~$ uname -a
Linux box 5.3.0-29-generic #31-Ubuntu SMP Fri Jan 17 17:27:26 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
sbrl@box:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 19.10
Release: 19.10
Codename: eoan
Can anyone help me please? This is pretty annoying.
Edit: I've also tried Node v12.16.0, but it has no effect either and does not change anything.