Closed regseb closed 1 month ago
The CI shows this fix only works for Nodejs v16+. You can try to add a condition to test against nodejs version.
I replaced the nextTick
only for the promise and it seems to work on all versions of Node. Maybe the callbacks are executed directly after, while the promises are executed on the next event loop.
@tschaub can you review this? I am not sure about the side effect of this change.
I tested mock-fs with the latest version 5.3.0 and the problem is still there.
index.js
import mock from "mock-fs";
import yauzl from "yauzl";
if (!process.argv.includes("without-mock")) {
mock({ "foo.zip": mock.load("foo.zip") });
}
// Test with setImmediate.
yauzl.open("foo.zip", (err, zipfile) => {
if (err) throw err;
setImmediate(() => {
zipfile.on("error", (err) => console.log("[setImmediate] error", err));
zipfile.on("entry", (entry) => console.log("[setImmediate] entry", entry.fileName));
zipfile.on("end", () => console.log("[setImmediate] end"));
});
});
// Wait one second between the two tests.
await new Promise((r) => setTimeout(r, 1000));
// Test with Promise.
const zipfile = await new Promise((resolve, reject) => {
yauzl.open("foo.zip", (err, zipfile) => {
if (err) reject(err);
resolve(zipfile);
});
});
zipfile.on("error", (err) => console.log("[Promise] error", err));
zipfile.on("entry", (entry) => console.log("[Promise] entry", entry.fileName));
zipfile.on("end", () => console.log("[Promise] end"));
package.json
{
"name": "testcase",
"version": "1.0.0",
"type": "module",
"dependencies": {
"mock-fs": "5.3.0",
"yauzl": "3.1.3"
}
}
npm install
zip foo.zip package.json
(or zip package.json
with an other tool)node index.js
Current:
[setImmediate] end [Promise] end
Expected:
[setImmediate] entry package.json [setImmediate] end [Promise] entry package.json [Promise] end
@tschaub Can you review this PR to fix the problem?
Replace
process.nextTick()
bysetImmediate()
to be closer to the reality of asynchronous methods. This pull request fixes #352 testcase with yauzl.