diff --git a/src/bun.js/api/bun/h2_frame_parser.zig b/src/bun.js/api/bun/h2_frame_parser.zig index 7ef2c740f54c1..1600c06758800 100644 --- a/src/bun.js/api/bun/h2_frame_parser.zig +++ b/src/bun.js/api/bun/h2_frame_parser.zig @@ -3746,9 +3746,9 @@ pub const H2FrameParser = struct { } pub fn constructor(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) ?*H2FrameParser { - const args_list = callframe.arguments(1); - if (args_list.len < 1) { - globalObject.throw("Expected 1 argument", .{}); + const args_list = callframe.arguments(2); + if (args_list.len < 2) { + globalObject.throw("Expected 2 argument", .{}); return null; } @@ -3757,6 +3757,31 @@ pub const H2FrameParser = struct { globalObject.throwInvalidArguments("expected options as argument", .{}); return null; } + const socket_js = args_list.ptr[0]; + + if (!socket_js.isEmptyOrUndefinedOrNull()) { + // check if socket is provided, and if it is a valid native socket + if (JSTLSSocket.fromJS(socket_js)) |socket| { + log("TLSSocket attached", .{}); + if (socket.attachNativeCallback(.{ .h2 = this })) { + this.native_socket = .{ .tls = socket }; + } else { + socket.ref(); + + this.native_socket = .{ .tls_writeonly = socket }; + } + } else if (JSTCPSocket.fromJS(socket_js)) |socket| { + log("TCPSocket attached", .{}); + if (socket.attachNativeCallback(.{ .h2 = this })) { + this.native_socket = .{ .tcp = socket }; + } else { + socket.ref(); + + this.native_socket = .{ .tcp_writeonly = socket }; + } + } + } + var exception: JSC.C.JSValueRef = null; const context_obj = options.get(globalObject, "context") orelse { @@ -3810,28 +3835,7 @@ pub const H2FrameParser = struct { }); } }; - // check if socket is provided, and if it is a valid native socket - if (options.get(globalObject, "native")) |socket_js| { - if (JSTLSSocket.fromJS(socket_js)) |socket| { - log("TLSSocket attached", .{}); - if (socket.attachNativeCallback(.{ .h2 = this })) { - this.native_socket = .{ .tls = socket }; - } else { - socket.ref(); - - this.native_socket = .{ .tls_writeonly = socket }; - } - } else if (JSTCPSocket.fromJS(socket_js)) |socket| { - log("TCPSocket attached", .{}); - if (socket.attachNativeCallback(.{ .h2 = this })) { - this.native_socket = .{ .tcp = socket }; - } else { - socket.ref(); - - this.native_socket = .{ .tcp_writeonly = socket }; - } - } - } + if (options.get(globalObject, "settings")) |settings_js| { if (!settings_js.isEmptyOrUndefinedOrNull()) { if (!this.loadSettingsFromJSValue(globalObject, settings_js)) { diff --git a/src/js/node/http2.ts b/src/js/node/http2.ts index 72936d97851a1..ecebaf5ae6c86 100644 --- a/src/js/node/http2.ts +++ b/src/js/node/http2.ts @@ -2439,13 +2439,15 @@ class ServerHttp2Session extends Http2Session { const nativeSocket = socket[bunSocketInternal]; this.#encrypted = socket instanceof TLSSocket; - this.#parser = new H2FrameParser({ - native: nativeSocket, - context: this, - settings: options || {}, - type: 0, // server type - handlers: ServerHttp2Session.#Handlers, - }); + this.#parser = new H2FrameParser( + { + context: this, + settings: options || {}, + type: 0, // server type + handlers: ServerHttp2Session.#Handlers, + }, + nativeSocket, + ); socket.on("close", this.#onClose.bind(this)); socket.on("error", this.#onError.bind(this)); socket.on("timeout", this.#onTimeout.bind(this)); @@ -3022,12 +3024,14 @@ class ClientHttp2Session extends Http2Session { } this.#encrypted = socket instanceof TLSSocket; const nativeSocket = socket[bunSocketInternal]; - this.#parser = new H2FrameParser({ - native: nativeSocket, - context: this, - settings: options, - handlers: ClientHttp2Session.#Handlers, - }); + this.#parser = new H2FrameParser( + { + context: this, + settings: options, + handlers: ClientHttp2Session.#Handlers, + }, + nativeSocket, + ); socket.on("data", this.#onRead.bind(this)); socket.on("drain", this.#onDrain.bind(this)); socket.on("close", this.#onClose.bind(this));