aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-10-15 22:48:27 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-10-15 22:48:27 -0700
commit9830e50a29b15a9a1871301c112c0758aaaad5ff (patch)
treefce9d6369b5d4d53ad2b0067f2dd352671f6f232 /src
parent820e6605f80f7c015a35e1988656c44e891ba6bf (diff)
downloadbun-9830e50a29b15a9a1871301c112c0758aaaad5ff.tar.gz
bun-9830e50a29b15a9a1871301c112c0758aaaad5ff.tar.zst
bun-9830e50a29b15a9a1871301c112c0758aaaad5ff.zip
Simplify code that turns a fetch() response into a JSResponse
Diffstat (limited to 'src')
-rw-r--r--src/bun.js/webcore/response.zig42
1 files changed, 24 insertions, 18 deletions
diff --git a/src/bun.js/webcore/response.zig b/src/bun.js/webcore/response.zig
index 0b6f09162..f0f118bf5 100644
--- a/src/bun.js/webcore/response.zig
+++ b/src/bun.js/webcore/response.zig
@@ -632,12 +632,8 @@ pub const Fetch = struct {
return fetch_error.toErrorInstance(this.global_this);
}
- pub fn onResolve(this: *FetchTasklet) JSValue {
- var allocator = this.global_this.bunVM().allocator;
- const http_response = this.result.response;
- var response = allocator.create(Response) catch unreachable;
- var internal_blob = this.response_buffer.list.toManaged(this.response_buffer.allocator);
-
+ fn toBodyValue(this: *FetchTasklet) Body.Value {
+ var response_buffer = this.response_buffer.list;
this.response_buffer = .{
.allocator = default_allocator,
.list = .{
@@ -646,19 +642,22 @@ pub const Fetch = struct {
},
};
- var body_value: Body.Value = undefined;
- if (internal_blob.items.len < InlineBlob.available_bytes) {
- body_value = .{ .InlineBlob = InlineBlob.init(internal_blob.items) };
- internal_blob.deinit();
- } else {
- body_value = .{
- .InternalBlob = .{
- .bytes = internal_blob,
- },
- };
+ if (response_buffer.items.len < InlineBlob.available_bytes) {
+ const inline_blob = InlineBlob.init(response_buffer.items);
+ defer response_buffer.deinit(bun.default_allocator);
+ return .{ .InlineBlob = inline_blob };
}
- response.* = Response{
+ return .{
+ .InternalBlob = .{
+ .bytes = response_buffer.toManaged(bun.default_allocator),
+ },
+ };
+ }
+
+ fn toResponse(this: *FetchTasklet, allocator: std.mem.Allocator) Response {
+ const http_response = this.result.response;
+ return Response{
.allocator = allocator,
.url = allocator.dupe(u8, this.result.href) catch unreachable,
.status_text = allocator.dupe(u8, http_response.status) catch unreachable,
@@ -668,9 +667,15 @@ pub const Fetch = struct {
.headers = FetchHeaders.createFromPicoHeaders(http_response.headers),
.status_code = @truncate(u16, http_response.status_code),
},
- .value = body_value,
+ .value = this.toBodyValue(),
},
};
+ }
+
+ pub fn onResolve(this: *FetchTasklet) JSValue {
+ var allocator = this.global_this.bunVM().allocator;
+ var response = allocator.create(Response) catch unreachable;
+ response.* = this.toResponse(allocator);
return Response.makeMaybePooled(@ptrCast(js.JSContextRef, this.global_this), response);
}
@@ -5522,6 +5527,7 @@ fn BodyMixin(comptime Type: type) type {
globalThis: *JSC.JSGlobalObject,
) callconv(.C) JSValue {
var body: *Body.Value = this.getBodyValue();
+
if (body.* == .Used) {
// TODO: make this closed
return JSC.WebCore.ReadableStream.empty(globalThis);