aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-22 00:24:34 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-22 00:24:34 -0700
commitb1389f1b021dda7f63912369e990c3a854c1cc02 (patch)
tree8bbef7342847a2823ec655112a8c2ba3dbbcace3
parentb3bd413c3baff757d41fefded36f8f18c90a52d3 (diff)
downloadbun-b1389f1b021dda7f63912369e990c3a854c1cc02.tar.gz
bun-b1389f1b021dda7f63912369e990c3a854c1cc02.tar.zst
bun-b1389f1b021dda7f63912369e990c3a854c1cc02.zip
`Response.file` -> `Bun.file`
Diffstat (limited to '')
-rw-r--r--integration/bunjs-only-snippets/response.file.test.js33
-rw-r--r--src/javascript/jsc/api/bun.zig4
-rw-r--r--src/javascript/jsc/webcore/response.zig100
3 files changed, 65 insertions, 72 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);
+});
diff --git a/src/javascript/jsc/api/bun.zig b/src/javascript/jsc/api/bun.zig
index d3b230243..9974bb108 100644
--- a/src/javascript/jsc/api/bun.zig
+++ b/src/javascript/jsc/api/bun.zig
@@ -958,6 +958,10 @@ pub const Class = NewClass(
.rfn = Bun.startServer,
.ts = d.ts{},
},
+ .file = .{
+ .rfn = JSC.WebCore.Blob.constructFile,
+ .ts = d.ts{},
+ },
},
.{
.main = .{
diff --git a/src/javascript/jsc/webcore/response.zig b/src/javascript/jsc/webcore/response.zig
index dcd8a06d6..3ff67f79d 100644
--- a/src/javascript/jsc/webcore/response.zig
+++ b/src/javascript/jsc/webcore/response.zig
@@ -86,7 +86,6 @@ pub const Response = struct {
.@"json" = .{ .rfn = constructJSON },
.@"redirect" = .{ .rfn = constructRedirect },
.@"error" = .{ .rfn = constructError },
- .@"file" = .{ .rfn = constructFile },
},
.{},
);
@@ -394,63 +393,6 @@ pub const Response = struct {
}
}
- pub fn constructFile(
- _: void,
- ctx: js.JSContextRef,
- _: js.JSObjectRef,
- _: js.JSObjectRef,
- arguments: []const js.JSValueRef,
- exception: js.ExceptionRef,
- ) js.JSObjectRef {
- var args = JSC.Node.ArgumentsSlice.from(arguments);
-
- var response = Response{
- .body = Body{
- .init = Body.Init{
- .headers = null,
- .status_code = 200,
- },
- .value = Body.Value.empty,
- },
- .allocator = getAllocator(ctx),
- .url = "",
- };
-
- var path = JSC.Node.PathOrFileDescriptor.fromJS(ctx, &args, exception) orelse {
- exception.* = JSC.toInvalidArguments("Expected file path string or file descriptor", .{}, ctx).asObjectRef();
- return js.JSValueMakeUndefined(ctx);
- };
-
- if (path == .path) {
- path.path = .{ .string = bun.PathString.init(bun.default_allocator.dupe(u8, path.path.slice()) catch unreachable) };
- }
-
- if (args.nextEat()) |init| {
- if (init.isUndefinedOrNull()) {} else if (init.isNumber()) {
- response.body.init.status_code = @intCast(u16, @minimum(@maximum(0, init.toInt32()), std.math.maxInt(u16)));
- } else {
- if (Body.Init.init(getAllocator(ctx), ctx, init.asObjectRef()) catch null) |_init| {
- response.body.init = _init;
- }
- }
- }
-
- response.body.value = .{
- .Blob = brk: {
- if (VirtualMachine.vm.getFileBlob(path)) |blob| {
- blob.ref();
- break :brk Blob.initWithStore(blob, ctx.ptr());
- }
-
- break :brk Blob.initWithStore(Blob.Store.initFile(path, null, bun.default_allocator) catch unreachable, ctx.ptr());
- },
- };
-
- var ptr = response.allocator.create(Response) catch unreachable;
- ptr.* = response;
- return Response.makeMaybePooled(ctx, ptr);
- }
-
pub fn constructJSON(
_: void,
ctx: js.JSContextRef,
@@ -1723,6 +1665,42 @@ pub const Blob = struct {
globalThis: *JSGlobalObject = undefined,
+ pub fn constructFile(
+ _: void,
+ ctx: js.JSContextRef,
+ _: js.JSObjectRef,
+ _: js.JSObjectRef,
+ arguments: []const js.JSValueRef,
+ exception: js.ExceptionRef,
+ ) js.JSObjectRef {
+ var args = JSC.Node.ArgumentsSlice.from(arguments);
+
+ var path = JSC.Node.PathOrFileDescriptor.fromJS(ctx, &args, exception) orelse {
+ exception.* = JSC.toInvalidArguments("Expected file path string or file descriptor", .{}, ctx).asObjectRef();
+ return js.JSValueMakeUndefined(ctx);
+ };
+
+ const blob = brk: {
+ if (VirtualMachine.vm.getFileBlob(path)) |blob| {
+ blob.ref();
+ break :brk Blob.initWithStore(blob, ctx.ptr());
+ }
+
+ if (path == .path) {
+ path.path = .{ .string = bun.PathString.init(
+ (bun.default_allocator.dupeZ(u8, path.path.slice()) catch unreachable)[0..path.path.slice().len],
+ ) };
+ }
+
+ break :brk Blob.initWithStore(Blob.Store.initFile(path, null, bun.default_allocator) catch unreachable, ctx.ptr());
+ };
+
+ var ptr = bun.default_allocator.create(Blob) catch unreachable;
+ ptr.* = blob;
+ ptr.allocator = bun.default_allocator;
+ return Blob.Class.make(ctx, ptr);
+ }
+
pub const Store = struct {
data: Data,
@@ -1920,7 +1898,7 @@ pub const Blob = struct {
open_frame: OpenFrameType = undefined,
errno: ?anyerror = null,
open_completion: HTTPClient.NetworkThread.Completion = undefined,
- opened_fd: JSC.Node.FileDescriptor = undefined,
+ opened_fd: JSC.Node.FileDescriptor = 0,
size: u32 = 0,
store: *Store = undefined,
@@ -2026,7 +2004,7 @@ pub const Blob = struct {
close_frame: @Frame(ReadFile.doClose) = undefined,
errno: ?anyerror = null,
open_completion: HTTPClient.NetworkThread.Completion = undefined,
- opened_fd: JSC.Node.FileDescriptor = undefined,
+ opened_fd: JSC.Node.FileDescriptor = 0,
read_completion: HTTPClient.NetworkThread.Completion = undefined,
read_len: u32 = 0,
read_off: u32 = 0,
@@ -2692,7 +2670,7 @@ pub const Blob = struct {
bun.default_allocator.destroy(handler);
var bytes = bytes_ catch |err| {
var error_string = ZigString.init(
- std.fmt.allocPrint(bun.default_allocator, "Failed to read file: {s}", .{std.mem.span(@errorName(err))}) catch unreachable,
+ std.fmt.allocPrint(bun.default_allocator, "Failed to read file \"{s}\"", .{std.mem.span(@errorName(err))}) catch unreachable,
);
error_string.mark();
blob.detach();