aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-04-01 17:08:15 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-04-01 17:08:15 -0700
commitbed878f2905ce25f9b1d42f7ae2fe774c15f8702 (patch)
tree4bd1f8e0bff9072054e65c0ae3bae7014be08492
parent0066d162cb812d49a59a28d6f1d187f9feb8a1ca (diff)
downloadbun-jarred/fetchheaders.tar.gz
bun-jarred/fetchheaders.tar.zst
bun-jarred/fetchheaders.zip
Make `FetchHeaders` workjarred/fetchheaders
-rw-r--r--integration/bunjs-only-snippets/fetch.test.js1
-rw-r--r--src/javascript/jsc/api/server.zig2
-rw-r--r--src/javascript/jsc/bindings/bindings.cpp58
-rw-r--r--src/javascript/jsc/bindings/bindings.zig35
-rw-r--r--src/javascript/jsc/bindings/headers-cpp.h10
-rw-r--r--src/javascript/jsc/bindings/headers.h11
-rw-r--r--src/javascript/jsc/bindings/headers.zig6
-rw-r--r--src/javascript/jsc/bindings/webcore/FetchHeaders.h2
-rw-r--r--src/javascript/jsc/webcore/response.zig74
9 files changed, 95 insertions, 104 deletions
diff --git a/integration/bunjs-only-snippets/fetch.test.js b/integration/bunjs-only-snippets/fetch.test.js
index 76cd5123b..9aea4d6d0 100644
--- a/integration/bunjs-only-snippets/fetch.test.js
+++ b/integration/bunjs-only-snippets/fetch.test.js
@@ -2,7 +2,6 @@ import { it, describe, expect } from "bun:test";
import fs from "fs";
function gc() {
- // console.trace();
Bun.gc(true);
}
diff --git a/src/javascript/jsc/api/server.zig b/src/javascript/jsc/api/server.zig
index aeb13b6bc..b87022594 100644
--- a/src/javascript/jsc/api/server.zig
+++ b/src/javascript/jsc/api/server.zig
@@ -827,7 +827,7 @@ pub fn NewServer(comptime ssl_enabled: bool, comptime debug_mode: bool) type {
this.writeStatus(status);
- if (response.body.init.headers.as(JSC.FetchHeaders)) |headers_| {
+ if (response.body.init.headers) |headers_| {
this.writeHeaders(headers_);
headers_.deref();
} else if (this.blob.content_type.len > 0) {
diff --git a/src/javascript/jsc/bindings/bindings.cpp b/src/javascript/jsc/bindings/bindings.cpp
index 9bb5b33f3..c3684e2bd 100644
--- a/src/javascript/jsc/bindings/bindings.cpp
+++ b/src/javascript/jsc/bindings/bindings.cpp
@@ -110,6 +110,11 @@ void WebCore__FetchHeaders__toUWSResponse(WebCore__FetchHeaders* arg0, bool is_s
}
}
+WebCore__FetchHeaders* WebCore__FetchHeaders__createEmpty()
+{
+ RefPtr<WebCore::FetchHeaders> headers = adoptRef(*new WebCore::FetchHeaders({ WebCore::FetchHeaders::Guard::None, {} }));
+ return headers.leakRef();
+}
void WebCore__FetchHeaders__append(WebCore__FetchHeaders* headers, const ZigString* arg1, const ZigString* arg2)
{
headers->append(Zig::toString(*arg1), Zig::toString(*arg2));
@@ -128,9 +133,10 @@ WebCore__FetchHeaders* WebCore__FetchHeaders__createFromJS(JSC__JSGlobalObject*
auto throwScope = DECLARE_THROW_SCOPE(lexicalGlobalObject->vm());
auto init = argument0.value().isUndefined() ? std::optional<Converter<IDLUnion<IDLSequence<IDLSequence<IDLByteString>>, IDLRecord<IDLByteString, IDLByteString>>>::ReturnType>() : std::optional<Converter<IDLUnion<IDLSequence<IDLSequence<IDLByteString>>, IDLRecord<IDLByteString, IDLByteString>>>::ReturnType>(convert<IDLUnion<IDLSequence<IDLSequence<IDLByteString>>, IDLRecord<IDLByteString, IDLByteString>>>(*lexicalGlobalObject, argument0.value()));
RETURN_IF_EXCEPTION(throwScope, nullptr);
- return WebCoreCast<WebCore::JSFetchHeaders, WebCore__FetchHeaders>(
- JSC::JSValue::encode(WebCore::toJSNewlyCreated(lexicalGlobalObject, globalObject, WebCore::FetchHeaders::create(WTFMove(init)).releaseReturnValue())),
- &lexicalGlobalObject->vm());
+ RefPtr<WebCore::FetchHeaders> headers = adoptRef(*new WebCore::FetchHeaders({ WebCore::FetchHeaders::Guard::None, {} }));
+ if (init)
+ headers->fill(WTFMove(init.value()));
+ return headers.leakRef();
}
JSC__JSValue WebCore__FetchHeaders__toJS(WebCore__FetchHeaders* headers, JSC__JSGlobalObject* lexicalGlobalObject)
@@ -143,24 +149,19 @@ JSC__JSValue WebCore__FetchHeaders__clone(WebCore__FetchHeaders* headers, JSC__J
{
Zig::GlobalObject* globalObject = reinterpret_cast<Zig::GlobalObject*>(arg1);
auto clone = WebCore::FetchHeaders::create();
- if (headers->size() > 0) {
- auto iter = headers->createIterator();
- uint32_t i = 0;
- unsigned count = 0;
- for (auto pair = iter.next(); pair; pair = iter.next()) {
- auto name = pair->key;
- auto value = pair->value;
- clone->append(name.isolatedCopy(), value.isolatedCopy());
- }
- }
+ clone->fill(*headers);
return JSC::JSValue::encode(WebCore::toJSNewlyCreated(arg1, globalObject, WTFMove(clone)));
}
-// WebCore__FetchHeaders* WebCore__FetchHeaders__cloneThis(WebCore__FetchHeaders* headers)
-// {
+WebCore__FetchHeaders* WebCore__FetchHeaders__cloneThis(WebCore__FetchHeaders* headers)
+{
+ RefPtr<WebCore::FetchHeaders> clone = adoptRef(*new WebCore::FetchHeaders({ WebCore::FetchHeaders::Guard::None, {} }));
+ if (headers->size() > 0) {
+ clone->fill(*headers);
+ }
-// return JSC::JSValue::encode(WebCore::toJSNewlyCreated(globalObject, globalObject, WebCore::FetchHeaders::create(*headers)));
-// }
+ return clone.leakRef();
+}
void WebCore__FetchHeaders__copyTo(WebCore__FetchHeaders* headers, StringPointer* names, StringPointer* values, unsigned char* buf)
{
@@ -201,7 +202,7 @@ typedef struct PicoHTTPHeaders {
const PicoHTTPHeader* ptr;
size_t len;
} PicoHTTPHeaders;
-JSC__JSValue WebCore__FetchHeaders__createFromPicoHeaders_(JSC__JSGlobalObject* arg0, const void* arg1)
+WebCore::FetchHeaders* WebCore__FetchHeaders__createFromPicoHeaders_(JSC__JSGlobalObject* arg0, const void* arg1)
{
PicoHTTPHeaders pico_headers = *reinterpret_cast<const PicoHTTPHeaders*>(arg1);
Vector<KeyValuePair<String, String>> pairs;
@@ -212,12 +213,12 @@ JSC__JSValue WebCore__FetchHeaders__createFromPicoHeaders_(JSC__JSGlobalObject*
pairs.uncheckedAppend(KeyValuePair<String, String>(name, value));
}
- Ref<WebCore::FetchHeaders> headers = WebCore::FetchHeaders::create();
- headers->fill(WebCore::FetchHeaders::Init(pairs));
+ RefPtr<WebCore::FetchHeaders> headers = adoptRef(*new WebCore::FetchHeaders({ WebCore::FetchHeaders::Guard::None, {} }));
+ headers->fill(WebCore::FetchHeaders::Init(WTFMove(pairs)));
pairs.releaseBuffer();
- return JSC::JSValue::encode(WebCore::toJSNewlyCreated(arg0, reinterpret_cast<Zig::GlobalObject*>(arg0), WTFMove(headers)));
+ return headers.leakRef();
}
-JSC__JSValue WebCore__FetchHeaders__createFromUWS(JSC__JSGlobalObject* arg0, void* arg1)
+WebCore::FetchHeaders* WebCore__FetchHeaders__createFromUWS(JSC__JSGlobalObject* arg0, void* arg1)
{
uWS::HttpRequest req = *reinterpret_cast<uWS::HttpRequest*>(arg1);
Vector<KeyValuePair<String, String>> pairs;
@@ -228,14 +229,15 @@ JSC__JSValue WebCore__FetchHeaders__createFromUWS(JSC__JSGlobalObject* arg0, voi
pairs.uncheckedAppend(KeyValuePair<String, String>(name, value));
}
- Ref<WebCore::FetchHeaders> headers = WebCore::FetchHeaders::create();
- headers->fill(WebCore::FetchHeaders::Init(pairs));
+ RefPtr<WebCore::FetchHeaders> headers = adoptRef(*new WebCore::FetchHeaders({ WebCore::FetchHeaders::Guard::None, {} }));
+ headers->fill(WebCore::FetchHeaders::Init(WTFMove(pairs)));
pairs.releaseBuffer();
- return JSC::JSValue::encode(WebCore::toJS(arg0, reinterpret_cast<Zig::GlobalObject*>(arg0), headers));
+ return headers.leakRef();
}
void WebCore__FetchHeaders__deref(WebCore__FetchHeaders* arg0)
{
- arg0->deref();
+ RefPtr<WebCore::FetchHeaders> clone = arg0;
+ clone->deref();
}
JSC__JSValue WebCore__FetchHeaders__createValue(JSC__JSGlobalObject* arg0, StringPointer* arg1, StringPointer* arg2, const ZigString* arg3, uint32_t count)
@@ -250,9 +252,9 @@ JSC__JSValue WebCore__FetchHeaders__createValue(JSC__JSGlobalObject* arg0, Strin
}
Ref<WebCore::FetchHeaders> headers = WebCore::FetchHeaders::create();
- headers->fill(WebCore::FetchHeaders::Init(pairs));
+ headers->fill(WebCore::FetchHeaders::Init(WTFMove(pairs)));
pairs.releaseBuffer();
- return JSC::JSValue::encode(WebCore::toJS(arg0, reinterpret_cast<Zig::GlobalObject*>(arg0), headers));
+ return JSC::JSValue::encode(WebCore::toJSNewlyCreated(arg0, reinterpret_cast<Zig::GlobalObject*>(arg0), WTFMove(headers)));
}
void WebCore__FetchHeaders__get_(WebCore__FetchHeaders* headers, const ZigString* arg1, ZigString* arg2)
{
diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig
index 0f8d5de00..8d916af11 100644
--- a/src/javascript/jsc/bindings/bindings.zig
+++ b/src/javascript/jsc/bindings/bindings.zig
@@ -435,8 +435,11 @@ const Api = @import("../../../api/schema.zig").Api;
pub const FetchHeaders = opaque {
pub const shim = Shimmer("WebCore", "FetchHeaders", @This());
- const cppFn = shim.cppFn;
pub const name = "WebCore::FetchHeaders";
+ pub const include = "FetchHeaders.h";
+ pub const namespace = "WebCore";
+
+ const cppFn = shim.cppFn;
pub fn createValue(
global: *JSGlobalObject,
@@ -491,7 +494,7 @@ pub const FetchHeaders = opaque {
pub fn createFromUWS(
global: *JSGlobalObject,
uws_request: *anyopaque,
- ) JSValue {
+ ) *FetchHeaders {
return shim.cppFn("createFromUWS", .{
global,
uws_request,
@@ -515,20 +518,14 @@ pub const FetchHeaders = opaque {
len: usize,
};
- pub fn createEmpty(
- global: *JSGlobalObject,
- ) JSValue {
- const pico_ = PicoHeaders{ .ptr = undefined, .len = 0 };
- return shim.cppFn("createFromPicoHeaders_", .{
- global,
- &pico_,
- });
+ pub fn createEmpty() *FetchHeaders {
+ return shim.cppFn("createEmpty", .{});
}
pub fn createFromPicoHeaders(
global: *JSGlobalObject,
pico_headers: anytype,
- ) JSValue {
+ ) *FetchHeaders {
const out = PicoHeaders{ .ptr = pico_headers.ptr, .len = pico_headers.len };
const result = shim.cppFn("createFromPicoHeaders_", .{
global,
@@ -540,7 +537,7 @@ pub const FetchHeaders = opaque {
pub fn createFromPicoHeaders_(
global: *JSGlobalObject,
pico_headers: *const anyopaque,
- ) JSValue {
+ ) *FetchHeaders {
return shim.cppFn("createFromPicoHeaders_", .{
global,
pico_headers,
@@ -696,6 +693,7 @@ pub const FetchHeaders = opaque {
"copyTo",
"count",
"createFromJS",
+ "createEmpty",
"createFromPicoHeaders_",
"createFromUWS",
"createValue",
@@ -1990,10 +1988,6 @@ pub const String = extern struct {
};
};
-pub const BuiltinName = enum(u8) {
- headers,
-};
-
pub const JSValue = enum(u64) {
_,
@@ -2586,10 +2580,6 @@ pub const JSValue = enum(u64) {
return cppFn("getIfPropertyExistsImpl", .{ this, global, ptr, len });
}
- pub fn getHiddenIfPropertyExistsImpl(this: JSValue, global: *JSGlobalObject, ptr: [*]const u8, len: u32) JSValue {
- return cppFn("getHiddenIfPropertyExistsImpl", .{ this, global, ptr, len });
- }
-
pub fn getSymbolDescription(this: JSValue, global: *JSGlobalObject, str: *ZigString) void {
cppFn("getSymbolDescription", .{ this, global, str });
}
@@ -2655,11 +2645,6 @@ pub const JSValue = enum(u64) {
return if (@enumToInt(value) != 0) value else return null;
}
- pub fn getHidden(this: JSValue, global: *JSGlobalObject, property: []const u8) ?JSValue {
- const value = getIfPropertyExistsImpl(this, global, property.ptr, @intCast(u32, property.len));
- return if (@enumToInt(value) != 0) value else return null;
- }
-
pub fn getTruthy(this: JSValue, global: *JSGlobalObject, property: []const u8) ?JSValue {
if (get(this, global, property)) |prop| {
if (@enumToInt(prop) == 0 or prop.isUndefinedOrNull()) return null;
diff --git a/src/javascript/jsc/bindings/headers-cpp.h b/src/javascript/jsc/bindings/headers-cpp.h
index 9d0ce2113..fa70542cd 100644
--- a/src/javascript/jsc/bindings/headers-cpp.h
+++ b/src/javascript/jsc/bindings/headers-cpp.h
@@ -1,4 +1,4 @@
-//-- AUTOGENERATED FILE -- 1648790662
+//-- AUTOGENERATED FILE -- 1648857986
// clang-format off
#pragma once
@@ -16,6 +16,14 @@
extern "C" const size_t JSC__JSObject_object_size_ = sizeof(JSC::JSObject);
extern "C" const size_t JSC__JSObject_object_align_ = alignof(JSC::JSObject);
+#ifndef INCLUDED_FetchHeaders_h
+#define INCLUDED_FetchHeaders_h
+#include FetchHeaders.h
+#endif
+
+extern "C" const size_t WebCore__FetchHeaders_object_size_ = sizeof(WebCore::FetchHeaders);
+extern "C" const size_t WebCore__FetchHeaders_object_align_ = alignof(WebCore::FetchHeaders);
+
#ifndef INCLUDED_JavaScriptCore_JSCell_h
#define INCLUDED_JavaScriptCore_JSCell_h
#include JavaScriptCore/JSCell.h
diff --git a/src/javascript/jsc/bindings/headers.h b/src/javascript/jsc/bindings/headers.h
index 9175274cb..a3a12d138 100644
--- a/src/javascript/jsc/bindings/headers.h
+++ b/src/javascript/jsc/bindings/headers.h
@@ -1,5 +1,5 @@
// clang-format: off
-//-- AUTOGENERATED FILE -- 1648790662
+//-- AUTOGENERATED FILE -- 1648857986
#pragma once
#include <stddef.h>
@@ -312,20 +312,23 @@ CPP_DECL JSC__JSValue ZigString__toValueGC(const ZigString* arg0, JSC__JSGlobalO
CPP_DECL WebCore__DOMURL* WebCore__DOMURL__cast_(JSC__JSValue JSValue0, JSC__VM* arg1);
CPP_DECL void WebCore__DOMURL__href_(WebCore__DOMURL* arg0, ZigString* arg1);
CPP_DECL void WebCore__DOMURL__pathname_(WebCore__DOMURL* arg0, ZigString* arg1);
+
+#pragma mark - WebCore::FetchHeaders
+
CPP_DECL void WebCore__FetchHeaders__append(WebCore__FetchHeaders* arg0, const ZigString* arg1, const ZigString* arg2);
CPP_DECL WebCore__FetchHeaders* WebCore__FetchHeaders__cast_(JSC__JSValue JSValue0, JSC__VM* arg1);
CPP_DECL JSC__JSValue WebCore__FetchHeaders__clone(WebCore__FetchHeaders* arg0, JSC__JSGlobalObject* arg1);
CPP_DECL WebCore__FetchHeaders* WebCore__FetchHeaders__cloneThis(WebCore__FetchHeaders* arg0);
CPP_DECL void WebCore__FetchHeaders__copyTo(WebCore__FetchHeaders* arg0, StringPointer* arg1, StringPointer* arg2, unsigned char* arg3);
CPP_DECL void WebCore__FetchHeaders__count(WebCore__FetchHeaders* arg0, uint32_t* arg1, uint32_t* arg2);
+CPP_DECL WebCore__FetchHeaders* WebCore__FetchHeaders__createEmpty();
CPP_DECL WebCore__FetchHeaders* WebCore__FetchHeaders__createFromJS(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1);
-CPP_DECL JSC__JSValue WebCore__FetchHeaders__createFromPicoHeaders_(JSC__JSGlobalObject* arg0, const void* arg1);
-CPP_DECL JSC__JSValue WebCore__FetchHeaders__createFromUWS(JSC__JSGlobalObject* arg0, void* arg1);
+CPP_DECL WebCore__FetchHeaders* WebCore__FetchHeaders__createFromPicoHeaders_(JSC__JSGlobalObject* arg0, const void* arg1);
+CPP_DECL WebCore__FetchHeaders* WebCore__FetchHeaders__createFromUWS(JSC__JSGlobalObject* arg0, void* arg1);
CPP_DECL JSC__JSValue WebCore__FetchHeaders__createValue(JSC__JSGlobalObject* arg0, StringPointer* arg1, StringPointer* arg2, const ZigString* arg3, uint32_t arg4);
CPP_DECL void WebCore__FetchHeaders__deref(WebCore__FetchHeaders* arg0);
CPP_DECL void WebCore__FetchHeaders__get_(WebCore__FetchHeaders* arg0, const ZigString* arg1, ZigString* arg2);
CPP_DECL bool WebCore__FetchHeaders__has(WebCore__FetchHeaders* arg0, const ZigString* arg1);
-CPP_DECL uint32_t WebCore__FetchHeaders__keyCount(WebCore__FetchHeaders* arg0);
CPP_DECL void WebCore__FetchHeaders__put_(WebCore__FetchHeaders* arg0, const ZigString* arg1, const ZigString* arg2);
CPP_DECL void WebCore__FetchHeaders__remove(WebCore__FetchHeaders* arg0, const ZigString* arg1);
CPP_DECL JSC__JSValue WebCore__FetchHeaders__toJS(WebCore__FetchHeaders* arg0, JSC__JSGlobalObject* arg1);
diff --git a/src/javascript/jsc/bindings/headers.zig b/src/javascript/jsc/bindings/headers.zig
index 2eb6ad46f..6fd6b644b 100644
--- a/src/javascript/jsc/bindings/headers.zig
+++ b/src/javascript/jsc/bindings/headers.zig
@@ -160,14 +160,14 @@ pub extern fn WebCore__FetchHeaders__clone(arg0: ?*WebCore__FetchHeaders, arg1:
pub extern fn WebCore__FetchHeaders__cloneThis(arg0: ?*WebCore__FetchHeaders) ?*WebCore__FetchHeaders;
pub extern fn WebCore__FetchHeaders__copyTo(arg0: ?*WebCore__FetchHeaders, arg1: [*c]StringPointer, arg2: [*c]StringPointer, arg3: [*c]u8) void;
pub extern fn WebCore__FetchHeaders__count(arg0: ?*WebCore__FetchHeaders, arg1: [*c]u32, arg2: [*c]u32) void;
+pub extern fn WebCore__FetchHeaders__createEmpty(...) ?*WebCore__FetchHeaders;
pub extern fn WebCore__FetchHeaders__createFromJS(arg0: [*c]JSC__JSGlobalObject, JSValue1: JSC__JSValue) ?*WebCore__FetchHeaders;
-pub extern fn WebCore__FetchHeaders__createFromPicoHeaders_(arg0: [*c]JSC__JSGlobalObject, arg1: ?*const anyopaque) JSC__JSValue;
-pub extern fn WebCore__FetchHeaders__createFromUWS(arg0: [*c]JSC__JSGlobalObject, arg1: ?*anyopaque) JSC__JSValue;
+pub extern fn WebCore__FetchHeaders__createFromPicoHeaders_(arg0: [*c]JSC__JSGlobalObject, arg1: ?*const anyopaque) ?*WebCore__FetchHeaders;
+pub extern fn WebCore__FetchHeaders__createFromUWS(arg0: [*c]JSC__JSGlobalObject, arg1: ?*anyopaque) ?*WebCore__FetchHeaders;
pub extern fn WebCore__FetchHeaders__createValue(arg0: [*c]JSC__JSGlobalObject, arg1: [*c]StringPointer, arg2: [*c]StringPointer, arg3: [*c]const ZigString, arg4: u32) JSC__JSValue;
pub extern fn WebCore__FetchHeaders__deref(arg0: ?*WebCore__FetchHeaders) void;
pub extern fn WebCore__FetchHeaders__get_(arg0: ?*WebCore__FetchHeaders, arg1: [*c]const ZigString, arg2: [*c]ZigString) void;
pub extern fn WebCore__FetchHeaders__has(arg0: ?*WebCore__FetchHeaders, arg1: [*c]const ZigString) bool;
-pub extern fn WebCore__FetchHeaders__keyCount(arg0: ?*WebCore__FetchHeaders) u32;
pub extern fn WebCore__FetchHeaders__put_(arg0: ?*WebCore__FetchHeaders, arg1: [*c]const ZigString, arg2: [*c]const ZigString) void;
pub extern fn WebCore__FetchHeaders__remove(arg0: ?*WebCore__FetchHeaders, arg1: [*c]const ZigString) void;
pub extern fn WebCore__FetchHeaders__toJS(arg0: ?*WebCore__FetchHeaders, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue;
diff --git a/src/javascript/jsc/bindings/webcore/FetchHeaders.h b/src/javascript/jsc/bindings/webcore/FetchHeaders.h
index c6a74880f..b116cf978 100644
--- a/src/javascript/jsc/bindings/webcore/FetchHeaders.h
+++ b/src/javascript/jsc/bindings/webcore/FetchHeaders.h
@@ -89,10 +89,10 @@ public:
void setGuard(Guard);
Guard guard() const { return m_guard; }
-private:
FetchHeaders(Guard, HTTPHeaderMap&&);
explicit FetchHeaders(const FetchHeaders&);
+private:
Guard m_guard;
HTTPHeaderMap m_headers;
};
diff --git a/src/javascript/jsc/webcore/response.zig b/src/javascript/jsc/webcore/response.zig
index a23ca69ce..a4c83fc43 100644
--- a/src/javascript/jsc/webcore/response.zig
+++ b/src/javascript/jsc/webcore/response.zig
@@ -173,8 +173,7 @@ pub const Response = struct {
}
pub fn header(this: *const Response, comptime name: []const u8) ?[]const u8 {
- const headers_ = (this.body.init.headers.as(FetchHeaders) orelse return null);
- return headers_.get(name);
+ return (this.body.init.headers orelse return null).get(name);
}
pub const Props = struct {};
@@ -281,25 +280,21 @@ pub const Response = struct {
return js.JSValueMakeBoolean(ctx, this.isOK());
}
- fn getOrCreateHeaders(this: *Response, globalThis: *JSGlobalObject) *FetchHeaders {
- if (this.body.init.headers.isEmpty()) {
- this.body.init.headers = FetchHeaders.createEmpty(globalThis).as(FetchHeaders);
+ fn getOrCreateHeaders(this: *Response) *FetchHeaders {
+ if (this.body.init.headers == null) {
+ this.body.init.headers = FetchHeaders.createEmpty();
}
- return this.body.init.headers;
+ return this.body.init.headers.?;
}
pub fn getHeaders(
this: *Response,
ctx: js.JSContextRef,
- obj: js.JSValueRef,
+ _: js.JSValueRef,
_: js.JSStringRef,
_: js.ExceptionRef,
) js.JSValueRef {
- var headers_symbol = &ZigString.init("headers_");
- const thisValue = JSC.JSValue.fromRef(obj);
- const headers_symbol_ = JSC.JSValue.symbolFor(ctx.ptr(), headers_symbol);
- this.body.init.headers = JSC.JSValue.get(headers_symbol_) orelse FetchHeaders.createEmpty(ctx.ptr());
- return this.body.init.headers.asObjectRef();
+ return this.getOrCreateHeaders().toJS(ctx.ptr()).asObjectRef();
}
pub fn doClone(
@@ -312,11 +307,8 @@ pub const Response = struct {
) js.JSValueRef {
var cloned = this.clone(getAllocator(ctx), ctx.ptr());
var val = Response.makeMaybePooled(ctx, cloned);
- if (!cloned.body.init.headers.isEmpty()) {
- var headers_symbol = &ZigString.init("headers_");
- const thisValue = JSC.JSValue.fromRef(val);
- const headers_symbol_ = JSC.JSValue.symbolFor(ctx.ptr(), headers_symbol);
- cloned.body.init.headers = thisValue.get(headers_symbol_) orelse FetchHeaders.createEmpty(ctx.ptr());
+ if (this.body.init.headers) |headers| {
+ cloned.body.init.headers = headers.cloneThis();
}
return val;
@@ -472,7 +464,7 @@ pub const Response = struct {
}
}
- var headers_ref = response.getOrCreateHeaders(ctx.ptr());
+ var headers_ref = response.getOrCreateHeaders();
headers_ref.putDefault("content-type", MimeType.json.value);
var ptr = response.allocator.create(Response) catch unreachable;
ptr.* = response;
@@ -521,7 +513,7 @@ pub const Response = struct {
}
}
- response.body.init.headers = response.getOrCreateHeaders(ctx.ptr());
+ response.body.init.headers = response.getOrCreateHeaders();
response.body.init.status_code = 302;
var headers_ref = response.body.init.headers.?;
headers_ref.put("location", url_string_slice.slice());
@@ -963,7 +955,7 @@ pub const Fetch = struct {
} else if (first_arg.asCheckLoaded(Request)) |request| {
url = ZigURL.parse(request.url.dupe(getAllocator(ctx)) catch unreachable);
method = request.method;
- if (request.headers.as(FetchHeaders)) |head| {
+ if (request.headers) |head| {
headers = Headers.from(head, bun.default_allocator) catch unreachable;
}
var blob = request.body.use();
@@ -3490,18 +3482,23 @@ pub const Body = struct {
}
pub fn deinit(this: *Body, _: std.mem.Allocator) void {
+ if (this.init.headers) |headers| {
+ headers.deref();
+ this.init.headers = null;
+ }
this.value.deinit();
}
pub const Init = struct {
- headers: JSValue = JSValue.zero,
+ headers: ?*FetchHeaders = null,
status_code: u16,
method: Method = Method.GET,
- pub fn clone(this: Init, globalThis: *JSGlobalObject) Init {
+ pub fn clone(this: Init, _: *JSGlobalObject) Init {
var that = this;
- if (this.headers.as(FetchHeaders)) |head| {
- that.headers = head.clone(globalThis);
+ var headers = this.headers;
+ if (headers) |head| {
+ that.headers = head.cloneThis();
}
return that;
@@ -3523,12 +3520,9 @@ pub const Body = struct {
if (js.JSObjectGetProperty(ctx, init_ref, property_name_ref, null)) |header_prop| {
const header_val = JSValue.fromRef(header_prop);
if (header_val.as(FetchHeaders)) |orig| {
- result.headers = orig.clone(ctx.ptr());
+ result.headers = orig.cloneThis();
} else {
- result.headers = if (FetchHeaders.createFromJS(ctx.ptr(), header_val)) |headers|
- headers.toJS(ctx.ptr())
- else
- result.headers;
+ result.headers = FetchHeaders.createFromJS(ctx.ptr(), header_val);
}
}
}
@@ -3551,7 +3545,7 @@ pub const Body = struct {
}
}
- if (result.headers.isEmptyOrUndefinedOrNull() and result.status_code < 200) return null;
+ if (result.headers == null and result.status_code < 200) return null;
return result;
}
};
@@ -3776,7 +3770,7 @@ pub const Body = struct {
exception: js.ExceptionRef,
) Body {
var body = Body{
- .init = Init{ .status_code = 200 },
+ .init = Init{ .headers = null, .status_code = 200 },
};
const value = JSC.JSValue.fromRef(body_ref);
var allocator = getAllocator(ctx);
@@ -3808,7 +3802,7 @@ pub const Body = struct {
// https://developer.mozilla.org/en-US/docs/Web/API/Request
pub const Request = struct {
url: ZigString = ZigString.Empty,
- headers: JSValue = JSValue.zero,
+ headers: ?*FetchHeaders = null,
body: Body.Value = Body.Value{ .Empty = .{} },
method: Method = Method.GET,
uws_request: ?*uws.Request = null,
@@ -3825,7 +3819,7 @@ pub const Request = struct {
}
pub fn mimeType(this: *const Request) string {
- if (this.headers.as(FetchHeaders)) |headers| {
+ if (this.headers) |headers| {
// Remember, we always lowercase it
// hopefully doesn't matter here tho
if (headers.get("content-type")) |content_type| {
@@ -4021,7 +4015,7 @@ pub const Request = struct {
_: js.JSStringRef,
_: js.ExceptionRef,
) js.JSValueRef {
- if (this.headers.as(FetchHeaders)) |headers_ref| {
+ if (this.headers) |headers_ref| {
if (headers_ref.get("referrer")) |referrer| {
return ZigString.init(referrer).toValueGC(ctx.ptr()).asRef();
}
@@ -4132,15 +4126,15 @@ pub const Request = struct {
_: js.JSStringRef,
_: js.ExceptionRef,
) js.JSValueRef {
- if (this.headers.isEmptyOrUndefinedOrNull()) {
+ if (this.headers == null) {
if (this.uws_request) |req| {
this.headers = FetchHeaders.createFromUWS(ctx.ptr(), req);
} else {
- this.headers = FetchHeaders.createEmpty(ctx.ptr());
+ this.headers = FetchHeaders.createEmpty();
}
}
- return this.headers.asObjectRef();
+ return this.headers.?.toJS(ctx.ptr()).asObjectRef();
}
pub fn cloneInto(
@@ -4154,8 +4148,8 @@ pub const Request = struct {
.url = ZigString.init(allocator.dupe(u8, this.url.slice()) catch unreachable),
.method = this.method,
};
- if (this.headers.as(FetchHeaders)) |head| {
- req.headers = head.clone(globalThis);
+ if (this.headers) |head| {
+ req.headers = head.cloneThis();
} else if (this.uws_request) |uws_req| {
req.headers = FetchHeaders.createFromUWS(globalThis, uws_req);
}
@@ -4403,7 +4397,7 @@ pub const FetchEvent = struct {
var needs_mime_type = true;
var content_length: ?usize = null;
- if (response.body.init.headers.as(JSC.FetchHeaders)) |headers_ref| {
+ if (response.body.init.headers) |headers_ref| {
var headers = Headers.from(headers_ref, request_context.allocator) catch unreachable;
var i: usize = 0;