Skip to content

Commit

Permalink
move to another parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Oct 17, 2024
1 parent 73fb1c4 commit e02379e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 38 deletions.
54 changes: 29 additions & 25 deletions src/bun.js/api/bun/h2_frame_parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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)) {
Expand Down
30 changes: 17 additions & 13 deletions src/js/node/http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit e02379e

Please sign in to comment.