You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Readline: emit data events along with current line events #23032
It'd be great if readline could emit 'data' events for each line, so that for await could be used:
constreadline=require('readline');conststream=require('stream');constinput=newstream.Readable();input.push(`{"some": "json","another":"json"}\n`);input.push(`{"some": "json2","another":"json2"}\n`);input.push(null);// What I wish I would do:(async()=>{constrl=readline.createInterface({input});constrows=[];forawait(constrowofrl)rows.push(row);console.log(rows)})();// workaround:constbetterReadLine=({input})=>{constoutput=newstream.PassThrough({objectMode: true});constrl=readline.createInterface({input});rl.on('line',line=>{output.write(JSON.parse(line));});rl.on('close',()=>{output.push(null);});returnoutput;};(async()=>{constrl=betterReadLine({input});constrows=[];forawait(constrowofrl)rows.push(row);console.log(rows)})();
@caub I was trying to make some workaround with an intermediate async generator that splits chunks and yields a Promise for each line, but such implementation was very slow, maybe due to many Promises involved. So I've made a shift with an intermediate async generator that splits chunks and yields a Promise for an Array with lines from the chunk, for now.
This implementation preserves line ending characters. If I need any granular line processing or transformation, I can use any Array functions (filter, map etc) on the consuming end.
Activity
devsnek commentedon Sep 22, 2018
related: #18904
devsnek commentedon Sep 22, 2018
Duplicate of #18603
vsemozhetbyt commentedon Sep 22, 2018
@caub I was trying to make some workaround with an intermediate async generator that splits chunks and yields a Promise for each line, but such implementation was very slow, maybe due to many Promises involved. So I've made a shift with an intermediate async generator that splits chunks and yields a Promise for an Array with lines from the chunk, for now.
This implementation preserves line ending characters. If I need any granular line processing or transformation, I can use any Array functions (filter, map etc) on the consuming end.
read-lines-module.js:test.js:caub commentedon Sep 22, 2018
interesting, it makes sense so (keeping the line returns also, since 'data' event should just chunk the content)
edit: I thought your snippet was what nodejs core would adopt, sry got confused, and hoping for https://github.com/nodejs/node/pull/18904/files to get merged soon
vsemozhetbyt commentedon Oct 27, 2018
@caub A new attempt to implement: #23916