-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
option.to
breaks the stream and exits the program
#333
Comments
I'm having the same issue, here's my slightly shorter repro: import * as fs from "node:fs";
import * as stream from "node:stream/promises";
import { parse } from "csv-parse";
const parser = fs.createReadStream("data.csv").pipe(
parse({
// to_line: 3
})
);
parser.on("readable", () => {
let record;
while ((record = parser.read()) !== null) {
console.log(record);
}
});
console.info("waiting for stream to finish...");
await stream.finished(parser);
console.info("this won't get logged if you uncomment the to_line option."); |
FWIW, |
Follow-up from my previous comment, one way around it is adding: parser.on('end', parser.destroy); This will correctly resolve the promise and the code after However, now you might get an error due to the stream being closed prematurely. I fixed it with: await stream.finished(parser).catch((e) => {
if (e && e.code === "ERR_STREAM_PREMATURE_CLOSE") {
// 🔥 this is fine 🔥
} else {
console.error("Parser error", e);
}
}); EDIT: See discussion below. Using |
I am looking at it as well, trying to read the official doc and extract some sense. Using the more robust strategy of try-and-fail, I get to the same conclusion, |
Instead of
Try
This seems to prevent the |
It is also associated with the |
Calling |
OK, |
Thanks for the fix! Just for posterity of before a new release is made, I realized that using const parser = fs.createReadStream("data.csv").pipe(
parse({
to_line: 2,
})
);
for await (const record of parser) {
console.log(record);
}
console.info("done."); Which seems to work correctly without any errors/exceptions. |
The all stream API is hard to grasp. I am not even sure whether the Anyway, new versions are published:
|
I am updating my npm dependencies, and this change in ca3f55b is breaking my CSV parsing done client-side in the browser. It seems that Here is a code sandbox with the error: https://codesandbox.io/s/jolly-matan-5y0er4?file=/src/index.js My actual usage differs from this simple example. I am reading the first line of a file selected by the user to be uploaded, and I parse the CSV headers to ensure each are valid, something like this: const csvParser = parse({ delimiter: ",", fromLine: 1, toLine: 1 }).on("data", onParseData).on("error", onParseOrReadError);
// csvParser.destroy = () => csvParser; // <=== adding this solves the TypeError
const fileSlice = file.slice(0, 1024 * 4); // reading the first 4kb just in case it contains many long headers
fileSlice.text().then((v) => {
csvParser.write(v);
csvParser.end();
}).catch(onParseOrReadError); |
Can you go inside the source code of the parser and add a condition, for example if (this.destroy !== undefined) {
this.on('end', this.destroy);
} I would also need sth in ./demo to replicate the issue. |
I am re-opening this issue. I did some testing after issue #410. Issue #333 is fixed with
Here is the note I left inside the source code. I have the feeling that this could come from Node.js and be out of grasp. We welcome anyone who would stop by with the expertise to help us. this.end()
// Fix #333 and break #410
// ko: api.stream.iterator.coffee
// ko with v21.4.0, ok with node v20.5.1: api.stream.finished # aborted (with generate())
// ko: api.stream.finished # aborted (with Readable)
// this.destroy()
// Fix #410 and partially break #333
// ok: api.stream.iterator.coffee
// ok: api.stream.finished # aborted (with generate())
// broken: api.stream.finished # aborted (with Readable)
this.on('end', this.destroy); I took the decision to fix issue #410, re-open this issue and release the patch version 5.5.3. Motivation for the patch SemVer is that this issue has not been fixed properly and was broken with the upcoming Node.js versions. I acknowledge this is a very controversial decision in term of SemVer. |
@wdavidw I faced this too. Maybe enabling |
I would rather update the documentation. Writing content to stdout or stderr would potentially break usage when people are expected to pipe the result from this library. |
Describe the bug
Using
csv-parse
package to parse a readable stream. Basically this example from your documentation. Introducing theto
orto_line
option seems to exit the stream without executing anything afterawait finished(parser);
in line 27 below:To Reproduce
The example is provided in the description above.
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: