mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
Clean up some code
This commit is contained in:
@@ -3300,15 +3300,16 @@ pub fn NewServer(comptime ssl_enabled_: bool, comptime debug_mode_: bool) type {
|
||||
}
|
||||
|
||||
var data_value = JSC.JSValue.zero;
|
||||
|
||||
// if we converted a HeadersInit to a Headers object, we need to free it
|
||||
var fetch_headers_to_deref: ?*JSC.FetchHeaders = null;
|
||||
|
||||
defer {
|
||||
if (fetch_headers_to_deref) |fh| {
|
||||
fh.deref();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (optional) |opts| {
|
||||
getter: {
|
||||
if (opts.isEmptyOrUndefinedOrNull()) {
|
||||
@@ -3320,47 +3321,40 @@ pub fn NewServer(comptime ssl_enabled_: bool, comptime debug_mode_: bool) type {
|
||||
return JSValue.jsUndefined();
|
||||
}
|
||||
|
||||
if (opts.fastGet(globalThis, .data)) |headers_value| {
|
||||
data_value = headers_value;
|
||||
}
|
||||
|
||||
if (opts.fastGet(globalThis, .headers)) |headers_value| {
|
||||
if (headers_value.as(JSC.FetchHeaders)) |fetch_headers| {
|
||||
if (fetch_headers.fastGet(.SecWebSocketProtocol)) |protocol| {
|
||||
sec_websocket_protocol = protocol;
|
||||
}
|
||||
|
||||
if (fetch_headers.fastGet(.SecWebSocketExtensions)) |protocol| {
|
||||
sec_websocket_extensions = protocol;
|
||||
}
|
||||
|
||||
// we must write the status first so that 200 OK isn't written
|
||||
upgrader.resp.writeStatus("101 Switching Protocols");
|
||||
|
||||
fetch_headers.toUWSResponse(comptime ssl_enabled, upgrader.resp);
|
||||
break :getter;
|
||||
} else if (headers_value.isObject()) {
|
||||
if (JSC.FetchHeaders.createFromJS(globalThis, headers_value)) |fetch_headers| {
|
||||
if (fetch_headers.fastGet(.SecWebSocketProtocol)) |protocol| {
|
||||
sec_websocket_protocol = protocol;
|
||||
}
|
||||
|
||||
if (fetch_headers.fastGet(.SecWebSocketExtensions)) |protocol| {
|
||||
sec_websocket_extensions = protocol;
|
||||
}
|
||||
|
||||
// we must write the status first so that 200 OK isn't written
|
||||
upgrader.resp.writeStatus("101 Switching Protocols");
|
||||
|
||||
fetch_headers.toUWSResponse(comptime ssl_enabled, upgrader.resp);
|
||||
fetch_headers_to_deref = fetch_headers;
|
||||
}
|
||||
|
||||
if (headers_value.isEmptyOrUndefinedOrNull()) {
|
||||
break :getter;
|
||||
}
|
||||
|
||||
JSC.throwInvalidArguments("upgrade options.headers must be a Headers or an object", .{}, globalThis, exception);
|
||||
return JSValue.jsUndefined();
|
||||
}
|
||||
var fetch_headers_to_use: *JSC.FetchHeaders = headers_value.as(JSC.FetchHeaders) orelse brk: {
|
||||
if (headers_value.isObject()) {
|
||||
if (JSC.FetchHeaders.createFromJS(globalThis, headers_value)) |fetch_headers| {
|
||||
fetch_headers_to_deref = fetch_headers;
|
||||
break :brk fetch_headers;
|
||||
}
|
||||
}
|
||||
break :brk null;
|
||||
} orelse {
|
||||
JSC.throwInvalidArguments("upgrade options.headers must be a Headers or an object", .{}, globalThis, exception);
|
||||
return JSValue.jsUndefined();
|
||||
};
|
||||
|
||||
if (opts.fastGet(globalThis, .data)) |headers_value| {
|
||||
data_value = headers_value;
|
||||
if (fetch_headers_to_use.fastGet(.SecWebSocketProtocol)) |protocol| {
|
||||
sec_websocket_protocol = protocol;
|
||||
}
|
||||
|
||||
if (fetch_headers_to_use.fastGet(.SecWebSocketExtensions)) |protocol| {
|
||||
sec_websocket_extensions = protocol;
|
||||
}
|
||||
|
||||
// TODO: should we cork?
|
||||
// we must write the status first so that 200 OK isn't written
|
||||
upgrader.resp.writeStatus("101 Switching Protocols");
|
||||
fetch_headers_to_use.toUWSResponse(comptime ssl_enabled, upgrader.resp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -820,6 +820,11 @@ pub const JSServerWebSocket = struct {
|
||||
@compileLog("ServerWebSocket.finalize is not a finalizer");
|
||||
}
|
||||
|
||||
if (@TypeOf(ServerWebSocket.getBinaryType) != GetterType)
|
||||
@compileLog("Expected ServerWebSocket.getBinaryType to be a getter");
|
||||
|
||||
if (@TypeOf(ServerWebSocket.setBinaryType) != SetterType)
|
||||
@compileLog("Expected ServerWebSocket.setBinaryType to be a setter");
|
||||
if (@TypeOf(ServerWebSocket.close) != CallbackType)
|
||||
@compileLog("Expected ServerWebSocket.close to be a callback");
|
||||
if (@TypeOf(ServerWebSocket.cork) != CallbackType)
|
||||
@@ -852,6 +857,7 @@ pub const JSServerWebSocket = struct {
|
||||
@export(ServerWebSocket.constructor, .{ .name = "ServerWebSocketClass__construct" });
|
||||
@export(ServerWebSocket.cork, .{ .name = "ServerWebSocketPrototype__cork" });
|
||||
@export(ServerWebSocket.finalize, .{ .name = "ServerWebSocketClass__finalize" });
|
||||
@export(ServerWebSocket.getBinaryType, .{ .name = "ServerWebSocketPrototype__getBinaryType" });
|
||||
@export(ServerWebSocket.getBufferedAmount, .{ .name = "ServerWebSocketPrototype__getBufferedAmount" });
|
||||
@export(ServerWebSocket.getData, .{ .name = "ServerWebSocketPrototype__getData" });
|
||||
@export(ServerWebSocket.getReadyState, .{ .name = "ServerWebSocketPrototype__getReadyState" });
|
||||
@@ -859,6 +865,7 @@ pub const JSServerWebSocket = struct {
|
||||
@export(ServerWebSocket.isSubscribed, .{ .name = "ServerWebSocketPrototype__isSubscribed" });
|
||||
@export(ServerWebSocket.publish, .{ .name = "ServerWebSocketPrototype__publish" });
|
||||
@export(ServerWebSocket.send, .{ .name = "ServerWebSocketPrototype__send" });
|
||||
@export(ServerWebSocket.setBinaryType, .{ .name = "ServerWebSocketPrototype__setBinaryType" });
|
||||
@export(ServerWebSocket.setData, .{ .name = "ServerWebSocketPrototype__setData" });
|
||||
@export(ServerWebSocket.subscribe, .{ .name = "ServerWebSocketPrototype__subscribe" });
|
||||
@export(ServerWebSocket.unsubscribe, .{ .name = "ServerWebSocketPrototype__unsubscribe" });
|
||||
|
||||
Reference in New Issue
Block a user