| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
There was a problem hiding this comment.
Good work, LGTM
Sorry, something went wrong.
Sorry, something went wrong.
There was a problem hiding this comment.
Shouldn't this really be writeFileSync() to avoid any potential race conditions?
Sorry, something went wrong.
There was a problem hiding this comment.
Can't this instead be moved to the common.mustCall() callback?
Sorry, something went wrong.
There was a problem hiding this comment.
It can be removed entirely. Test files do not need to clean up the temp directory. Tests that use the temp directory clean it up themselves at the start of the test with tmpdir.refresh().
Sorry, something went wrong.
Sorry, something went wrong.
There was a problem hiding this comment.
I think it would be nice to check how many iterations of the loop there were
Sorry, something went wrong.
There was a problem hiding this comment.
A nit: destruction?
Sorry, something went wrong.
There was a problem hiding this comment.
A nit: arrow function?
Sorry, something went wrong.
|
Just a tiny doubt: the readline module works with strings only and users may get used to this (line in the listener argument is always a string). Should we demand from a user to use readable.setEncoding('utf8'); here? Maybe we can do this internally? BTW, the test does not use this call. |
Sorry, something went wrong.
|
@vsemozhetbyt By demand you mean explicitly mention this in documentation? |
Sorry, something went wrong.
There was a problem hiding this comment.
nit: in a project it may not be clear what Interface is
Sorry, something went wrong.
There was a problem hiding this comment.
lgtm with nits
Sorry, something went wrong.
|
@prog1dev Yes, documented advice. |
Sorry, something went wrong.
|
@prog1dev I've compiled the branch and tested the slightly edited doc example code on Windows 7 x64.
'use strict';
const readline = require('readline');
const fs = require('fs');
async function processLineByLine(readable) {
// readable.setEncoding('utf8');
const rli = readline.createInterface({
input: readable,
crlfDelay: Infinity
});
let i = 0;
for await (const line of rli) {
console.log(`Line ${++i}: ${line}`);
}
}
processLineByLine(fs.createReadStream(__filename)).catch(console.error);(node:3148) ExperimentalWarning: Interface[Symbol.asyncIterator] is an experimental feature. This feature could change at any time
Line 1: 'use strict';
const readline = require('readline');
const fs = require('fs');
async function processLineByLine(readable) {
// readable.setEncoding('utf8');
const rli = readline.createInterface({
input: readable,
crlfDelay: Infinity
});
let i = 0;
for await (const line of rli) {
console.log(`Line ${i}: ${line}`);
}
}
processLineByLine(fs.createReadStream(__filename)).catch(console.error);Dear reviewers, please chime in. |
Sorry, something went wrong.
There was a problem hiding this comment.
As in @vsemozhetbyt’s example, the added method doesn’t work. readline emits 'line' events that needed to be listened for, not 'data' as is the case for readable streams. As such, the general stream async iterator cannot be used for readline.
The PR just gets an async iterator of the input stream. That completely skips the readline mechanism, and as such does not work.
Sorry, something went wrong.
|
@mcollina Looks like no matter how much lines file has there is only one iteration of for-await-of. Here is code example: async function print(readable) {
readable.setEncoding('utf8');
let data = '';
let iterations = 0;
for await (const k of readable) {
data += k;
iterations++;
}
console.log(data);
console.log(iterations);
}
print(fs.createReadStream('file')).catch(console.log);
Is this behaviour intentional? Its different for readable streams in object mode |
Sorry, something went wrong.
|
It seems the longing readline to be more like streams has some backing, for example #16178. TBH, when I started to learn Node.js, using readline for a file processing seemed to me a bit hacky, with all the terminal abundant API to be ignored. And it becomes more awkward with things like crlfDelay: Infinity. But this is a pretty basic tool for text file processing to be outsourced to more handy userland modules. And this async API would be a big step towards making readline more usable outside the CLI domain till we will have something more concise. |
Sorry, something went wrong.
There was a problem hiding this comment.
-1 based on @vsemozhetbyt exmaple.
Sorry, something went wrong.
I'm pretty sure this is is because of internal buffering. You should try reading a different file. This is an experimental feature, and I would be surprise if it does not have bugs. Feel free to send PRs to fix those. |
Sorry, something went wrong.
|
@BridgeAR Im working on this in my spare time |
Sorry, something went wrong.
Sorry, something went wrong.
| return null; | ||
|
|
||
| return this._buffer.shift(); | ||
| }; |
There was a problem hiding this comment.
Can we make this function private?
Sorry, something went wrong.
There was a problem hiding this comment.
You mean to rename it to _read?
Sorry, something went wrong.
|
|
||
| this._enableBuffer = true; | ||
| this.close(); | ||
| this.input.setEncoding('utf8'); |
There was a problem hiding this comment.
It's definitely not super friendly when a module like readline changes stream settings, especially when we only do it with async iteration. We should add this feature without it.
Sorry, something went wrong.
| emitExperimentalWarning('readline Interface[Symbol.asyncIterator]'); | ||
|
|
||
| this._enableBuffer = true; | ||
| this.close(); |
There was a problem hiding this comment.
We definitely should not close the readline interface when we start iterating. This will emit events like 'close' that can be very much misleading to users.
Sorry, something went wrong.
There was a problem hiding this comment.
In order for it to work stream must be in paused mode so I can get data with stream.read() calls. So instead of closing I guess I can pause it with this.input.pause()
Sorry, something went wrong.
| Interface.prototype[Symbol.asyncIterator] = function() { | ||
| emitExperimentalWarning('readline Interface[Symbol.asyncIterator]'); | ||
|
|
||
| this._enableBuffer = true; |
There was a problem hiding this comment.
I don't see this ever getting restored after the completion of iteration, and I feel like it should be. Correct me if I'm wrong :)
Sorry, something went wrong.
|
There is still a lot of logic shared between ReadableAsyncIterator and ReadlineAsyncIterator, like the entire this[kLastResolve] = null;
this[kLastReject] = null;
this[kError] = null;
this[kEnded] = false;
this[kLastPromise] = null;chunk in the constructor, and also the this[kHandlePromise] function. BaseAsyncIterator could definitely have some more common code. |
Sorry, something went wrong.
| this.isCompletionEnabled = true; | ||
| this._sawKeyPress = false; | ||
| this._previousKey = null; | ||
| this._enableBuffer = false; |
There was a problem hiding this comment.
Can you please add a comment on what this does? Also, I would prefer if this was a Symbol instead.
Sorry, something went wrong.
|
Let me toy with an idea.. how about we implement this on top of a PassThrough instead? |
Sorry, something went wrong.
|
What's the status on this? |
Sorry, something went wrong.
|
@jasnell Stalled. Maybe later this week I can check this PassThrough idea. Also at that time I could not find an easy way to solve this #18904 (comment) issue |
Sorry, something went wrong.
|
@prog1dev Would it be okay with you if I were to take over this PR? |
Sorry, something went wrong.
Co-authored-by: Ivan Filenko <ivan.filenko@protonmail.com> Fixes: nodejs#18603 Refs: nodejs#18904
Co-authored-by: Ivan Filenko <ivan.filenko@protonmail.com> Fixes: nodejs#18603 Refs: nodejs#18904 PR-URL: nodejs#23916 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
Co-authored-by: Ivan Filenko <ivan.filenko@protonmail.com> Fixes: nodejs#18603 Refs: nodejs#18904 PR-URL: nodejs#23916 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
| Back | FazBrowse Home | New Git URL |
Sync readline API with for-await-of support in readable streams
Resolves #18603
Checklist
Affected core subsystem(s)
readline, stream, doc, test
@vsemozhetbyt @mcollina