diff options
author | 2022-03-22 00:24:34 -0700 | |
---|---|---|
committer | 2022-03-22 00:24:34 -0700 | |
commit | b1389f1b021dda7f63912369e990c3a854c1cc02 (patch) | |
tree | 8bbef7342847a2823ec655112a8c2ba3dbbcace3 /integration/bunjs-only-snippets/response.file.test.js | |
parent | b3bd413c3baff757d41fefded36f8f18c90a52d3 (diff) | |
download | bun-b1389f1b021dda7f63912369e990c3a854c1cc02.tar.gz bun-b1389f1b021dda7f63912369e990c3a854c1cc02.tar.zst bun-b1389f1b021dda7f63912369e990c3a854c1cc02.zip |
`Response.file` -> `Bun.file`
Diffstat (limited to 'integration/bunjs-only-snippets/response.file.test.js')
-rw-r--r-- | integration/bunjs-only-snippets/response.file.test.js | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/integration/bunjs-only-snippets/response.file.test.js b/integration/bunjs-only-snippets/response.file.test.js index ed4844876..ab7c51b5f 100644 --- a/integration/bunjs-only-snippets/response.file.test.js +++ b/integration/bunjs-only-snippets/response.file.test.js @@ -1,21 +1,19 @@ import fs from "fs"; import { it, expect } from "bun:test"; import path from "path"; -it("Response.file", async () => { +it("Bun.file", async () => { const file = path.join(import.meta.dir, "fetch.js.txt"); - expect(await Response.file(file).text()).toBe(fs.readFileSync(file, "utf8")); + expect(await Bun.file(file).text()).toBe(fs.readFileSync(file, "utf8")); }); -it("Response.file as a blob", async () => { +it("Bun.file as a Blob", async () => { const filePath = path.join(import.meta.url, "../fetch.js.txt"); const fixture = fs.readFileSync(filePath, "utf8"); - // this is a Response object with the same interface as the one returned by fetch + // 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 response = Response.file(filePath); + var blob = Bun.file(filePath); - // at this point, it's still just a file path - var blob = await response.blob(); // no size because we haven't read it from disk yet expect(blob.size).toBe(0); // now it reads "./fetch.js.txt" from the filesystem @@ -33,13 +31,26 @@ it("Response.file as a blob", async () => { for (let i = 0; i < text.length; i++) { expect(array[i]).toBe(text.charCodeAt(i)); } - expect(blob.size).toBe(fixture.size); + expect(blob.size).toBe(fixture.length); blob = null; - response = null; Bun.gc(true); await new Promise((resolve) => setTimeout(resolve, 1)); // now we're back - var response = Response.file(file); - var blob = await response.blob(); + var blob = Bun.file(filePath); expect(blob.size).toBe(0); }); + +it("Response -> Bun.file", async () => { + const file = path.join(import.meta.dir, "fetch.js.txt"); + const text = fs.readFileSync(file, "utf8"); + const response = new Response(Bun.file(file)); + expect(await response.text()).toBe(text); +}); + +it("Response -> Bun.file -> Response -> text", async () => { + const file = path.join(import.meta.dir, "fetch.js.txt"); + const text = fs.readFileSync(file, "utf8"); + const response = new Response(Bun.file(file)); + const response2 = response.clone(); + expect(await response2.text()).toBe(text); +}); |