aboutsummaryrefslogtreecommitdiff
path: root/integration/bunjs-only-snippets/response.file.test.js
diff options
context:
space:
mode:
Diffstat (limited to 'integration/bunjs-only-snippets/response.file.test.js')
-rw-r--r--integration/bunjs-only-snippets/response.file.test.js65
1 files changed, 60 insertions, 5 deletions
diff --git a/integration/bunjs-only-snippets/response.file.test.js b/integration/bunjs-only-snippets/response.file.test.js
index d094c0a62..6a879c758 100644
--- a/integration/bunjs-only-snippets/response.file.test.js
+++ b/integration/bunjs-only-snippets/response.file.test.js
@@ -2,12 +2,28 @@ import fs from "fs";
import { it, expect } from "bun:test";
import path from "path";
+function gc() {
+ Bun.gc(true);
+}
+
+// we must ensure that finalizers are run
+// so that the reference-counting logic is exercised
+function gcTick() {
+ gc();
+ return new Promise((resolve) => {
+ setTimeout(resolve, 0);
+ });
+}
+
it("Bun.file not found returns ENOENT", async () => {
try {
+ await gcTick();
await Bun.file("/does/not/exist.txt").text();
+ await gcTick();
} catch (exception) {
expect(exception.code).toBe("ENOENT");
}
+ await gcTick();
});
it("Bun.write('out.txt', 'string')", async () => {
@@ -17,11 +33,15 @@ it("Bun.write('out.txt', 'string')", async () => {
fs.unlinkSync(path.join("/tmp", "out.txt"));
} catch (e) {}
}
-
+ await gcTick();
await Bun.write("/tmp/out.txt", "string");
+ await gcTick();
const out = Bun.file("/tmp/out.txt");
+ await gcTick();
expect(await out.text()).toBe("string");
+ await gcTick();
expect(await out.text()).toBe(fs.readFileSync("/tmp/out.txt", "utf8"));
+ await gcTick();
}
});
@@ -30,38 +50,47 @@ it("Bun.write blob", async () => {
Bun.file("/tmp/response-file.test.txt"),
Bun.file(path.join(import.meta.dir, "fetch.js.txt"))
);
+ await gcTick();
await Bun.write(Bun.file("/tmp/response-file.test.txt"), "blah blah blha");
+ await gcTick();
await Bun.write(
Bun.file("/tmp/response-file.test.txt"),
new Uint32Array(1024)
);
+ await gcTick();
await Bun.write("/tmp/response-file.test.txt", new Uint32Array(1024));
+ await gcTick();
expect(
await Bun.write(
new TextEncoder().encode("/tmp/response-file.test.txt"),
new Uint32Array(1024)
)
).toBe(new Uint32Array(1024).byteLength);
+ await gcTick();
});
it("Bun.file -> Bun.file", async () => {
try {
fs.unlinkSync(path.join("/tmp", "fetch.js.in"));
} catch (e) {}
-
+ await gcTick();
try {
fs.unlinkSync(path.join("/tmp", "fetch.js.out"));
} catch (e) {}
-
+ await gcTick();
const file = path.join(import.meta.dir, "fetch.js.txt");
+ await gcTick();
const text = fs.readFileSync(file, "utf8");
fs.writeFileSync("/tmp/fetch.js.in", text, { mode: 0644 });
+ await gcTick();
{
const result = await Bun.write(
Bun.file("/tmp/fetch.js.out"),
Bun.file("/tmp/fetch.js.in")
);
+ await gcTick();
expect(await Bun.file("/tmp/fetch.js.out").text()).toBe(text);
+ await gcTick();
}
{
@@ -75,14 +104,18 @@ it("Bun.file -> Bun.file", async () => {
}
{
+ await gcTick();
await Bun.write("/tmp/fetch.js.in", Bun.file("/tmp/fetch.js.out"));
+ await gcTick();
expect(await Bun.file("/tmp/fetch.js.in").text()).toBe(text);
}
});
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 as a Blob", async () => {
@@ -92,27 +125,34 @@ it("Bun.file as a Blob", async () => {
// internally, instead of a byte array, it stores the file path!
// this enables several performance optimizations
var blob = Bun.file(filePath);
+ await gcTick();
// no size because we haven't read it from disk yet
expect(blob.size).toBe(0);
+ 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();
// 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;
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;
- Bun.gc(true);
+ await gcTick();
await new Promise((resolve) => setTimeout(resolve, 1));
// now we're back
var blob = Bun.file(filePath);
@@ -121,9 +161,13 @@ it("Bun.file as a Blob", async () => {
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 () => {
@@ -131,18 +175,29 @@ it("Bun.file -> Response", async () => {
try {
fs.unlinkSync("/tmp/fetch.js.out");
} catch {}
-
+ await gcTick();
const file = path.join(import.meta.dir, "fetch.js.txt");
+ await gcTick();
const text = fs.readFileSync(file, "utf8");
+ await gcTick();
const resp = await fetch("https://example.com");
+ await gcTick();
expect(await Bun.write("/tmp/fetch.js.out", resp)).toBe(text.length);
+ await gcTick();
expect(await Bun.file("/tmp/fetch.js.out").text()).toBe(text);
+ 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();
});
r' /> Tony Sullivan 5-11/+20 2022-09-28Update README.md (#4898)Gravatar stijlmassi 1-2/+3 2022-09-28Fix test (#4904)Gravatar Bjorn Lu 2-1/+7 2022-09-28[ci] formatGravatar FredKSchott 2-4/+4 2022-09-28redesign basics template (#4879)Gravatar Fred K. Schott 3-88/+34 2022-09-28[ci] formatGravatar bluwy 1-2/+2 2022-09-28Remove shamefully-hoist (#4842)Gravatar Bjorn Lu 104-527/+768 2022-09-28[ci] formatGravatar matthewp 4-14/+16 2022-09-28Hoist hydration script out of slot templates (#4891)Gravatar Matthew Phillips 13-43/+165 2022-09-28Ensure head content rendered once with lazy layouts (#4892)Gravatar Matthew Phillips 9-3/+59 2022-09-27fixed typing (#4893)Gravatar tweenietomatoes 1-1/+1 2022-09-27[ci] release (#4846)create-astro@1.1.0astro@1.3.1@astrojs/webapi@1.1.0@astrojs/vercel@2.0.1@astrojs/mdx@0.11.2@astrojs/image@0.8.0Gravatar Fred K. Bot 60-185/+169 2022-09-27fix: post API routes in SSG should warn or error during dev mode (#4878)Gravatar Rishi Raj Jain 3-2/+17 2022-09-27docs: Fix links to Tailwind examples (#4883)Gravatar Deanmv 1-1/+1 2022-09-27Set SSR target webworker for Vercel edge (#4884)Gravatar Bjorn Lu 2-0/+6 2022-09-27[ci] update lockfile (#4885)Gravatar Fred K. Bot 1-86/+79 2022-09-26[ci] formatGravatar bholmesdev 3-23/+19 2022-09-26Fix: correctly transform `import.meta.env.*` in MDX (#4858)Gravatar Ben Holmes 12-233/+454 2022-09-26Change negative lookbehind to lookahead (#4866)Gravatar Rishi Raj Jain 1-1/+1 2022-09-26add double check on astro file return type to display more human readable err...Gravatar Steven Yung 6-2/+61 2022-09-26[ci] update lockfile (#4862)Gravatar Fred K. Bot 1-81/+81 2022-09-26fix: Script with innerHTML not working on Safari (#4861)Gravatar Rishi Raj Jain 3-3/+10 2022-09-26Prevent /undefined catch-all routes in dev (#4873)Gravatar Bjorn Lu 6-9/+66 2022-09-26fix: 🐛 BUG: class:list directive adding class attribute when undefined (#4...Gravatar Rishi Raj Jain 2-2/+9 2022-09-26docs: Standardize common integration READMEs (#4874)Gravatar Jake Strawn 7-6/+66 2022-09-26docs: Update references to support channel in Discord. (#4872)Gravatar Jake Strawn 12-12/+12 2022-09-26[ci] formatGravatar bluwy 1-1/+1 2022-09-26fix: "chunks" directory appears in build output, if custom modules are import...Gravatar Rishi Raj Jain 2-6/+34 2022-09-23[ci] formatGravatar matthewp 1-1/+1 2022-09-23Define toStringTag another way (#4855)Gravatar Matthew Phillips 2-4/+12 2022-09-23update SSR example to match recent change on Astro API Context (#4854)Gravatar Steven Yung 2-4/+6 2022-09-23[ci] update lockfile (#4852)Gravatar Fred K. Bot 1-373/+402