mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(ext/node): out-of-order writes of fs.createWriteStream (#23244)
This PR follows this fix (https://github.com/nodejs/node/pull/52005) in Node.js. Stream's construct callback happens one tick earlier by this change, and it prevents the reordering of the first few chunks in `node:stream.Writable` closes #20284
This commit is contained in:
parent
49f6e2e79e
commit
2670c1d580
2 changed files with 36 additions and 4 deletions
|
@ -1665,7 +1665,7 @@ var require_destroy = __commonJS({
|
||||||
} else if (err) {
|
} else if (err) {
|
||||||
errorOrDestroy(stream, err, true);
|
errorOrDestroy(stream, err, true);
|
||||||
} else {
|
} else {
|
||||||
process.nextTick(emitConstructNT, stream);
|
stream.emit(kConstruct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -1676,9 +1676,6 @@ var require_destroy = __commonJS({
|
||||||
nextTick(onConstruct, err);
|
nextTick(onConstruct, err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function emitConstructNT(stream) {
|
|
||||||
stream.emit(kConstruct);
|
|
||||||
}
|
|
||||||
function isRequest(stream) {
|
function isRequest(stream) {
|
||||||
return stream && stream.setHeader && typeof stream.abort === "function";
|
return stream && stream.setHeader && typeof stream.abort === "function";
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { join } from "node:path";
|
||||||
import { tmpdir } from "node:os";
|
import { tmpdir } from "node:os";
|
||||||
import {
|
import {
|
||||||
constants,
|
constants,
|
||||||
|
createWriteStream,
|
||||||
existsSync,
|
existsSync,
|
||||||
mkdtempSync,
|
mkdtempSync,
|
||||||
promises,
|
promises,
|
||||||
|
@ -14,6 +15,7 @@ import {
|
||||||
writeFileSync,
|
writeFileSync,
|
||||||
} from "node:fs";
|
} from "node:fs";
|
||||||
import { constants as fsPromiseConstants, cp } from "node:fs/promises";
|
import { constants as fsPromiseConstants, cp } from "node:fs/promises";
|
||||||
|
import process from "node:process";
|
||||||
import { pathToAbsoluteFileUrl } from "../unit/test_util.ts";
|
import { pathToAbsoluteFileUrl } from "../unit/test_util.ts";
|
||||||
|
|
||||||
Deno.test(
|
Deno.test(
|
||||||
|
@ -121,3 +123,36 @@ Deno.test(
|
||||||
assert(dataRead === "Hello");
|
assert(dataRead === "Hello");
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// TODO(kt3k): Delete this test case, and instead enable the compat case
|
||||||
|
// `test/parallel/test-fs-writestream-open-write.js`, when we update
|
||||||
|
// `tests/node_compat/runner/suite`.
|
||||||
|
Deno.test("[node/fs createWriteStream", async () => {
|
||||||
|
const { promise, resolve, reject } = Promise.withResolvers<void>();
|
||||||
|
const tempDir = await Deno.makeTempDir();
|
||||||
|
const file = join(tempDir, "file.txt");
|
||||||
|
try {
|
||||||
|
const w = createWriteStream(file);
|
||||||
|
|
||||||
|
w.on("open", () => {
|
||||||
|
w.write("hello, ");
|
||||||
|
|
||||||
|
process.nextTick(() => {
|
||||||
|
w.write("world");
|
||||||
|
w.end();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
w.on("close", async () => {
|
||||||
|
try {
|
||||||
|
assertEquals(await Deno.readTextFile(file), "hello, world");
|
||||||
|
resolve();
|
||||||
|
} catch (e) {
|
||||||
|
reject(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
await promise;
|
||||||
|
} finally {
|
||||||
|
await Deno.remove(tempDir, { recursive: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue