diff options
-rw-r--r-- | src/bun.js/webcore/response.zig | 31 | ||||
-rw-r--r-- | test/bun.js/fetch-gzip.test.ts | 95 | ||||
-rw-r--r-- | test/bun.js/fetch.test.js | 13 |
3 files changed, 34 insertions, 105 deletions
diff --git a/src/bun.js/webcore/response.zig b/src/bun.js/webcore/response.zig index bfe8aa557..badcd2c23 100644 --- a/src/bun.js/webcore/response.zig +++ b/src/bun.js/webcore/response.zig @@ -2879,7 +2879,7 @@ pub const Blob = struct { is_atty: ?bool = null, mode: JSC.Node.Mode = 0, seekable: ?bool = null, - max_size: SizeType = 0, + max_size: SizeType = Blob.max_size, pub fn isSeekable(this: *const FileStore) ?bool { if (this.seekable) |seekable| { @@ -3194,15 +3194,18 @@ pub const Blob = struct { if (this.size == Blob.max_size) { this.resolveSize(); if (this.size == Blob.max_size and this.store != null) { - return JSValue.jsNumberFromChar(0); + return JSC.jsNumber(std.math.inf(f64)); + } else if (this.size == 0 and this.store != null) { + if (this.store.?.data == .file and + (this.store.?.data.file.seekable orelse true) == false and + this.store.?.data.file.max_size == Blob.max_size) + { + return JSC.jsNumber(std.math.inf(f64)); + } } } - if (this.size < std.math.maxInt(i32)) { - return JSValue.jsNumber(this.size); - } - - return JSC.JSValue.jsNumberFromUint64(this.size); + return JSValue.jsNumber(this.size); } pub fn resolveSize(this: *Blob) void { @@ -3222,7 +3225,10 @@ pub const Blob = struct { var buffer: [bun.MAX_PATH_BYTES]u8 = undefined; switch (JSC.Node.Syscall.stat(store.data.file.pathlike.path.sliceZ(&buffer))) { .result => |stat| { - store.data.file.max_size = @truncate(SizeType, @intCast(u64, @maximum(stat.size, 0))); + store.data.file.max_size = if (std.os.S.ISREG(stat.mode) or stat.size > 0) + @truncate(SizeType, @intCast(u64, @maximum(stat.size, 0))) + else + Blob.max_size; store.data.file.mode = stat.mode; store.data.file.seekable = std.os.S.ISREG(stat.mode); }, @@ -3232,7 +3238,10 @@ pub const Blob = struct { } else if (store.data.file.pathlike == .fd) { switch (JSC.Node.Syscall.fstat(store.data.file.pathlike.fd)) { .result => |stat| { - store.data.file.max_size = @truncate(SizeType, @intCast(u64, @maximum(stat.size, 0))); + store.data.file.max_size = if (std.os.S.ISREG(stat.mode) or stat.size > 0) + @truncate(SizeType, @intCast(u64, @maximum(stat.size, 0))) + else + Blob.max_size; store.data.file.mode = stat.mode; store.data.file.seekable = std.os.S.ISREG(stat.mode); }, @@ -3242,12 +3251,12 @@ pub const Blob = struct { } } - if (store.data.file.seekable != null) { + if (store.data.file.seekable != null and store.data.file.max_size != Blob.max_size) { const store_size = store.data.file.max_size; const offset = this.offset; this.offset = @minimum(store_size, offset); - this.size = store_size - offset; + this.size = store_size -| offset; return; } } diff --git a/test/bun.js/fetch-gzip.test.ts b/test/bun.js/fetch-gzip.test.ts index fdde76ae8..6dc8d2a7f 100644 --- a/test/bun.js/fetch-gzip.test.ts +++ b/test/bun.js/fetch-gzip.test.ts @@ -118,38 +118,7 @@ it("fetch() with a protocol-relative redirect that returns a buffered gzip respo server.stop(); }); -it("fetch() with a gzip response works (one chunk)", async () => { - var server = Bun.serve({ - port: 6023, - -// fetch(req) { -// return new Response(Bun.file(import.meta.dir + "/fixture.html.gz"), { -// headers: { -// "Content-Encoding": "gzip", -// "Content-Type": "text/html; charset=utf-8", -// }, -// }); -// }, -// }); -// gcTick(); -// const res = await fetch(`http://${server.hostname}:${server.port}`); -// const arrayBuffer = await res.arrayBuffer(); -// expect( -// new Buffer(arrayBuffer).equals( -// new Buffer( -// await Bun.file(import.meta.dir + "/fixture.html").arrayBuffer(), -// ), -// ), -// ).toBe(true); -// gcTick(); -// server.stop(); -// }); - -// it("fetch() with a gzip response works (multiple chunks)", async () => { -// var server = Bun.serve({ -// port: 6024, - -it("fetch() with a gzip response works (one chunk, streamed, with a delay)", async () => { +it("fetch() with a gzip response works (one chunk, streamed, with a delay", async () => { var server = Bun.serve({ port: 6081, @@ -190,67 +159,7 @@ it("fetch() with a gzip response works (one chunk, streamed, with a delay)", asy server.stop(); }); -it("fetch() with a gzip response works (multiple chunks)", async () => { - var server = Bun.serve({ - port: 6024, - -// await controller.flush(); -// // sanity check -// expect( -// new Buffer(concatArrayBuffers(chunks)).equals(new Buffer(buffer)), -// ).toBe(true); -// gcTick(); -// controller.end(); -// }, -// }), -// { -// headers: { -// "Content-Encoding": "gzip", -// "Content-Type": "text/html; charset=utf-8", -// "Content-Length": "1", -// }, -// }, -// ); -// }, -// }); - -// const res = await fetch(`http://${server.hostname}:${server.port}`, {}); -// const arrayBuffer = await res.arrayBuffer(); -// expect( -// new Buffer(arrayBuffer).equals( -// new Buffer( -// await Bun.file(import.meta.dir + "/fixture.html").arrayBuffer(), -// ), -// ), -// ).toBe(true); -// gcTick(); -// server.stop(); -// }); - -// it("fetch() with a gzip response works (multiple chunks, TCP server)", async (done) => { -// const compressed = await Bun.file( -// import.meta.dir + "/fixture.html.gz", -// ).arrayBuffer(); -// const server = Bun.listen({ -// port: 4024, -// hostname: "0.0.0.0", -// socket: { -// async open(socket) { -// var corked: any[] = []; -// var cork = true; -// gcTick(); -// async function write(chunk) { -// await new Promise<void>((resolve, reject) => { -// if (cork) { -// corked.push(chunk); -// } - -// if (!cork && corked.length) { -// socket.write(corked.join("")); -// corked.length = 0; -// } - -it("fetch() with a gzip response works (multiple chunks, TCP server)", async (done) => { +it("fetch() with a gzip response works (multiple chunks, TCP server", async (done) => { const compressed = await Bun.file( import.meta.dir + "/fixture.html.gz", ).arrayBuffer(); diff --git a/test/bun.js/fetch.test.js b/test/bun.js/fetch.test.js index 9fd3b8c24..a703c955a 100644 --- a/test/bun.js/fetch.test.js +++ b/test/bun.js/fetch.test.js @@ -1,5 +1,6 @@ import { it, describe, expect } from "bun:test"; -import fs from "fs"; +import fs, { unlinkSync } from "fs"; +import { mkfifo } from "mkfifo"; import { gc } from "./gc"; const exampleFixture = fs.readFileSync( @@ -240,6 +241,16 @@ describe("Bun.file", () => { expect(blob.size).toBe(file.size); return file; }); + + it("size is Infinity on a fifo", () => { + try { + unlinkSync("/tmp/test-fifo"); + } catch (e) {} + mkfifo("/tmp/test-fifo"); + + const { size } = Bun.file("/tmp/test-fifo"); + expect(size).toBe(Infinity); + }); }); describe("Blob", () => { |