Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/csv-stringify/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,17 @@ const stringify = function () {
callback(err);
});
stringifier.on("end", function () {
let data;
try {
callback(undefined, chunks.join(""));
data = chunks.join("");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data = chunks.join(""); will never throw an error, try/catch is not used

} catch (err) {
// This can happen if the `chunks` is extremely long; it may throw
// "Cannot create a string longer than 0x1fffffe8 characters"
// See [#386](https://github.com/adaltas/node-csv/pull/386)
callback(err);
return;
}
callback(undefined, data);
});
}
if (data !== undefined) {
Expand Down
32 changes: 32 additions & 0 deletions packages/csv-stringify/test/api.callback.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import "should";
import assert from "node:assert/strict";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not using assert, only should.js

import { spawnSync } from "node:child_process";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that spaning a new process is necessary to illustrate this issue.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that spaning a new process is necessary to illustrate this issue.

Also, using a sync api (spawnSync here) is not appropriate.

import { stringify } from "../lib/index.js";

describe("api.callback", function () {
Expand Down Expand Up @@ -39,6 +41,36 @@ describe("api.callback", function () {
);
});

it("does not invoke a throwing callback twice", function () {
const result = spawnSync(
process.execPath,
[
"--input-type=module",
"--eval",
`
import assert from "node:assert/strict";
import { stringify } from ${JSON.stringify(new URL("../lib/index.js", import.meta.url).href)};

const error = new Error("Callback failed");
let calls = 0;
let caught = false;
process.once("uncaughtException", (err) => {
assert.equal(err, error);
assert.equal(calls, 1);
caught = true;
});
process.on("exit", () => assert.equal(caught, true));
stringify([["value"]], () => {
calls++;
throw error;
});
`,
],
{ encoding: "utf8" },
);
assert.equal(result.status, 0, result.stderr);
});

it("catch error in end handler, see #386", function (next) {
const input = Array.from({ length: 200000 }).map(() =>
Array.from({ length: 100 }).map(
Expand Down