import { describe, expect, it, test } from "bun:test"; import fs, { mkdirSync } from "fs"; import { bunEnv, bunExe, exampleHtml, exampleSite, gcTick, isWindows, tempDir, withoutAggressiveGC } from "harness"; import path, { join } from "path"; let i = 0; const IS_UV_FS_COPYFILE_DISABLED = process.platform === "win32" && process.env.BUN_FEATURE_FLAG_DISABLE_UV_FS_COPYFILE === "1"; (isWindows ? describe : describe.concurrent)("Bun.write", () => { process.platform === "win32" && process.env.BUN_FEATURE_FLAG_DISABLE_UV_FS_COPYFILE === "1"; it("Bun.write blob", async () => { using tmpbase = tempDir("bun-write-blob", {}); await Bun.write( Bun.file(join(tmpbase, "response-file.test.txt")), Bun.file(path.resolve(import.meta.dir, "fetch.js.txt")), ); await gcTick(); await Bun.write(Bun.file(join(tmpbase, "response-file.test.txt")), "blah blah blha"); await gcTick(); await Bun.write(Bun.file(join(tmpbase, "response-file.test.txt")), new Uint32Array(1024)); await gcTick(); await Bun.write(join(tmpbase, "response-file.test.txt"), new Uint32Array(1024)); await gcTick(); expect(await Bun.write(new TextEncoder().encode(tmpbase + "response-file.test.txt"), new Uint32Array(1024))).toBe( new Uint32Array(1024).byteLength, ); await gcTick(); }); describe("large file", () => { it("write large file (text)", async () => { using tmpbase = tempDir("large-file-text", {}); const filename = tmpbase + `bun-test-large-file-${Date.now()}.txt`; const content = "https://www.iana.org/assignments/media-types/media-types.xhtml,".repeat(10000); try { unlinkSync(filename); } catch (e) {} await Bun.write(filename, content); expect(await Bun.file(filename).text()).toBe(content); try { unlinkSync(filename); } catch (e) {} }); it("write large file (bytes)", async () => { using tmpbase = tempDir("large-file-bytes", {}); const filename = tmpbase + `bun-test-large-file-${Date.now()}.txt`; const content = "https://www.iana.org/assignments/media-types/media-types.xhtml,".repeat(10000); try { unlinkSync(filename + ".bytes"); } catch (e) {} var bytes = new TextEncoder().encode(content); const written = await Bun.write(filename + ".bytes", bytes); expect(written).toBe(bytes.byteLength); expect(new Buffer(await Bun.file(filename + ".bytes").arrayBuffer()).equals(bytes)).toBe(true); try { unlinkSync(filename + ".bytes"); } catch (e) {} }); it("write large file (Blob)", async () => { using tmpbase = tempDir("large-file-blob", {}); const filename = tmpbase + `bun-test-large-file-${Date.now()}.txt`; const content = "https://www.iana.org/assignments/media-types/media-types.xhtml,".repeat(10000); try { unlinkSync(filename + ".blob"); } catch (e) {} var bytes = new Blob([content]); await Bun.write(filename + ".blob", bytes); expect(await Bun.file(filename + ".blob").text()).toBe(content); try { unlinkSync(filename + ".blob"); } catch (e) {} }); }); it("Bun.file not found returns ENOENT", async () => { try { await gcTick(); await Bun.file(join("does", "not", "exist.txt")).text(); await gcTick(); } catch (exception) { expect(exception.code).toBe("ENOENT"); } await gcTick(); }); it("Bun.write file not found returns ENOENT, issue#6336", async () => { using tmpbase = tempDir("bun-write-enoent", {}); const dst = Bun.file(path.join(tmpbase, join("does", "not", "exist.txt"))); fs.rmSync(join(tmpbase, "does"), { force: true, recursive: true }); try { await gcTick(); await Bun.write(dst, "", { createPath: false }); await gcTick(); expect.unreachable(); } catch (exception) { expect(exception.code).toBe("ENOENT"); if (!IS_UV_FS_COPYFILE_DISABLED) { expect(exception.path).toBe(dst.name); } } const src = Bun.file(path.join(tmpbase, `test-bun-write-${Date.now()}.txt`)); await Bun.write(src, ""); try { await gcTick(); await Bun.write(dst, src, { createPath: false }); await gcTick(); } catch (exception) { expect(exception.code).toBe("ENOENT"); if (!IS_UV_FS_COPYFILE_DISABLED) { expect(exception.path).toBe(dst.name); } } finally { fs.unlinkSync(src.name); } }); it("Bun.write('out.txt', 'string')", async () => { using tmpbase = tempDir("bun-write-string", {}); const outpath = path.join(tmpbase, "out." + ((Math.random() * 102400) | 0).toString(32) + "txt"); for (let erase of [true, false]) { if (erase) { try { fs.unlinkSync(outpath); } catch (e) {} } await gcTick(); expect(await Bun.write(outpath, "string")).toBe("string".length); await gcTick(); const out = Bun.file(outpath); await gcTick(); expect(await out.text()).toBe("string"); await gcTick(); expect(await out.text()).toBe(fs.readFileSync(outpath, "utf8")); await gcTick(); } }); it("Bun.file -> Bun.file", async () => { using tmpbase = tempDir("bun-file-to-file", {}); try { fs.unlinkSync(path.join(tmpbase, "fetch.js.in")); } catch (e) {} await gcTick(); try { fs.unlinkSync(path.join(tmpbase, "fetch.js.out")); } catch (e) {} await gcTick(); fs.writeFileSync(tmpbase + "fetch.js.in", exampleHtml); await gcTick(); { const result = await Bun.write(Bun.file(tmpbase + "fetch.js.out"), Bun.file(tmpbase + "fetch.js.in")); await gcTick(); expect(await Bun.file(tmpbase + "fetch.js.out").text()).toBe(exampleHtml); await gcTick(); } { await Bun.write( Bun.file(tmpbase + "fetch.js.in").slice(0, (exampleHtml.length / 2) | 0), Bun.file(tmpbase + "fetch.js.out"), ); expect(await Bun.file(tmpbase + "fetch.js.in").text()).toBe( exampleHtml.substring(0, (exampleHtml.length / 2) | 0), ); } { await gcTick(); await Bun.write(tmpbase + "fetch.js.in", Bun.file(tmpbase + "fetch.js.out")); await gcTick(); expect(await Bun.file(tmpbase + "fetch.js.in").text()).toBe(exampleHtml); } }); it("Bun.file", async () => { const file = path.join(import.meta.dir, "fetch.js.txt"); await gcTick(); expect(await Bun.file(file).text()).toBe(fs.readFileSync(file, "utf8")); await gcTick(); }); it("Bun.file empty file", async () => { const file = path.join(import.meta.dir, "emptyFile"); await gcTick(); const buffer = await Bun.file(file).arrayBuffer(); expect(buffer.byteLength).toBe(0); await gcTick(); }); it("Bun.file lastModified update", async () => { using tmpbase = tempDir("bun-file-lastmodified", {}); const file = Bun.file(tmpbase + "/bun.test.lastModified.txt"); await gcTick(); // setup await Bun.write(file, "test text."); const lastModified0 = file.lastModified; // sleep some time and write the file again. await Bun.sleep(isWindows ? 1000 : 100); await Bun.write(file, "test text2."); const lastModified1 = file.lastModified; // ensure the last modified timestamp is updated. expect(lastModified1).toBeGreaterThan(lastModified0); await gcTick(); }); it("Bun.file as a Blob", async () => { const filePath = path.join(import.meta.path, "../fetch.js.txt"); const fixture = fs.readFileSync(filePath, "utf8"); // this is a Blob object with the same interface as the one returned by fetch // internally, instead of a byte array, it stores the file path! // this enables several performance optimizations var blob = Bun.file(filePath); await gcTick(); // now it reads "./fetch.js.txt" from the filesystem // it's lazy, only loads once we ask for it // if it fails, the promise will reject at this point expect(await blob.text()).toBe(fixture); await gcTick(); // BEHAVIOR CHANGE IN BUN V0.3.0 - size is never set // now that it's loaded, the size updates // expect(blob.size).toBe(fixture.length); // await gcTick(); // and it only loads once for _all_ blobs pointing to that file path // until all references are released expect((await blob.arrayBuffer()).byteLength).toBe(fixture.length); await gcTick(); const array = new Uint8Array(await blob.arrayBuffer()); await gcTick(); const text = fixture; withoutAggressiveGC(() => { for (let i = 0; i < text.length; i++) { expect(array[i]).toBe(text.charCodeAt(i)); } }); await gcTick(); expect(blob.size).toBe(fixture.length); blob = null; await gcTick(); await new Promise(resolve => setTimeout(resolve, 1)); var blob = Bun.file(filePath); expect(blob.size).toBe(fixture.length); }); it("Response -> Bun.file", async () => { const file = path.join(import.meta.dir, "fetch.js.txt"); await gcTick(); const text = fs.readFileSync(file, "utf8"); await gcTick(); const response = new Response(Bun.file(file)); await gcTick(); expect(await response.text()).toBe(text); await gcTick(); }); it("Bun.file -> Response", async () => { using tmpbase = tempDir("bun-file-to-response", {}); await using server = exampleSite("https"); // ensure the file doesn't already exist try { fs.unlinkSync(tmpbase + "fetch.js.out"); } catch {} await gcTick(); await gcTick(); const resp = await fetch(server.url, { tls: { ca: server.ca } }); await gcTick(); await gcTick(); expect(await Bun.write(tmpbase + "fetch.js.out", resp)).toBe(exampleHtml.length); expect(await Bun.file(tmpbase + "fetch.js.out").text()).toBe(exampleHtml); await gcTick(); }); it("Response -> Bun.file -> Response -> text", async () => { await gcTick(); const file = path.join(import.meta.dir, "fetch.js.txt"); await gcTick(); const text = fs.readFileSync(file, "utf8"); await gcTick(); const response = new Response(Bun.file(file)); await gcTick(); const response2 = response.clone(); await gcTick(); expect(await response2.text()).toBe(text); await gcTick(); }); it("Bun.write('output.html', '')", async () => { using tmpbase = tempDir("bun-write-output-html", {}); await Bun.write(tmpbase + "output.html", "lalalala"); expect(await Bun.write(tmpbase + "output.html", "")).toBe(0); await Bun.write(tmpbase + "output.html", "lalalala"); expect(await Bun.file(tmpbase + "output.html").text()).toBe("lalalala"); }); it("Bun.write(Bun.stdout, 'Bun.write STDOUT TEST')", async () => { expect(await Bun.write(Bun.stdout, "\nBun.write STDOUT TEST\n\n")).toBe(24); }); it("Bun.write(Bun.stderr, 'Bun.write STDERR TEST')", async () => { expect(await Bun.write(Bun.stderr, "\nBun.write STDERR TEST\n\n")).toBe(24); }); it("Bun.write(Bun.stdout, new TextEncoder().encode('Bun.write STDOUT TEST'))", async () => { expect(await Bun.write(Bun.stdout, new TextEncoder().encode("\nBun.write STDOUT TEST\n\n"))).toBe(24); }); it("Bun.write(Bun.stderr, 'new TextEncoder().encode(Bun.write STDERR TEST'))", async () => { expect(await Bun.write(Bun.stderr, new TextEncoder().encode("\nBun.write STDERR TEST\n\n"))).toBe(24); }); // These tests pass by not throwing: it("Bun.write(Bun.stdout, Bun.file(path))", async () => { await Bun.write(Bun.stdout, Bun.file(path.join(import.meta.dir, "hello-world.txt"))); }); it("Bun.write(Bun.stderr, Bun.file(path))", async () => { await Bun.write(Bun.stderr, Bun.file(path.join(import.meta.dir, "hello-world.txt"))); }); it("Bun.file(0) survives GC", async () => { for (let i = 0; i < 10; i++) { let f = Bun.file(0); await gcTick(); expect(Bun.inspect(f)).toContain("FileRef (fd: 0)"); } }); // FLAKY TEST // Since Bun.file is resolved lazily, this needs to specifically be checked it("Bun.write('output.html', HTMLRewriter.transform(Bun.file)))", async done => { using tmpbase = tempDir("html-rewriter", {}); var rewriter = new HTMLRewriter(); rewriter.on("div", { element(element) { element.setInnerContent("", { html: true }); }, }); await Bun.write(tmpbase + "html-rewriter.txt.js", "