diff options
author | 2023-06-26 07:15:56 +0800 | |
---|---|---|
committer | 2023-06-25 16:15:56 -0700 | |
commit | fcf9f0a7eeb3d462d5c6c2110ecdf5a4460c1736 (patch) | |
tree | 4f1852ecbbfd2e2638a0b4012e24c54321f6b96b | |
parent | 33903ea892db861416f1f68d4c40536540719f4e (diff) | |
download | bun-fcf9f0a7eeb3d462d5c6c2110ecdf5a4460c1736.tar.gz bun-fcf9f0a7eeb3d462d5c6c2110ecdf5a4460c1736.tar.zst bun-fcf9f0a7eeb3d462d5c6c2110ecdf5a4460c1736.zip |
Fix the parameters of WriteStream constructor. (#3402)
* Fix the parameters of the `WriteStream` constructor.
Close: https://github.com/oven-sh/bun/issues/3395
* test append mode in `createWriteStream`
* fix lint
* wait first stream finished
-rw-r--r-- | src/js/node/fs.js | 4 | ||||
-rw-r--r-- | test/js/node/fs/fs.test.ts | 38 |
2 files changed, 40 insertions, 2 deletions
diff --git a/src/js/node/fs.js b/src/js/node/fs.js index 2bd752d19..8d9f0d235 100644 --- a/src/js/node/fs.js +++ b/src/js/node/fs.js @@ -642,8 +642,8 @@ WriteStream = (function (InternalWriteStream) { }); return Object.defineProperty( - function WriteStream(options) { - return new InternalWriteStream(options); + function WriteStream(path, options) { + return new InternalWriteStream(path, options); }, Symbol.hasInstance, { diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts index 37c3253a4..0353968fe 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -1068,6 +1068,44 @@ describe("createWriteStream", () => { expect(exception.code).toBe("ERR_INVALID_ARG_TYPE"); } }); + + it("writing in append mode should not truncate the file", async () => { + const path = `${tmpdir()}/fs.test.js/${Date.now()}.createWriteStreamAppend.txt`; + const stream = createWriteStream(path, { + // @ts-ignore-next-line + flags: "a", + }); + stream.write("first line\n"); + stream.end(); + + await new Promise((resolve, reject) => { + stream.on("error", e => { + reject(e); + }); + + stream.on("finish", () => { + resolve(true); + }); + }); + + const stream2 = createWriteStream(path, { + // @ts-ignore-next-line + flags: "a", + }); + stream2.write("second line\n"); + stream2.end(); + + return await new Promise((resolve, reject) => { + stream2.on("error", e => { + reject(e); + }); + + stream2.on("finish", () => { + expect(readFileSync(path, "utf8")).toBe("first line\nsecond line\n"); + resolve(true); + }); + }); + }); }); describe("fs/promises", () => { |