Skip to content
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
42323f8
Port `SocketConfig` to bindings generator
taylordotfish Oct 16, 2025
2436617
[autofix.ci] apply automated fixes
autofix-ci[bot] Oct 16, 2025
74a159c
Fix Windows
taylordotfish Oct 16, 2025
95fe4ec
bindgenv2: add `T.nullable.loose`
taylordotfish Oct 16, 2025
439aaac
bindgenv2: add `.loose` for primitives
taylordotfish Oct 17, 2025
84ef46c
Fix tests
taylordotfish Oct 17, 2025
0fa9dbe
Fix `server_name`
taylordotfish Oct 17, 2025
96ec56f
Reorder initializers
taylordotfish Oct 17, 2025
d6fad20
Detect duplicate enum values
taylordotfish Oct 17, 2025
aaf1160
Fix return type
taylordotfish Oct 17, 2025
4bc6f44
Remove duplicate uniqueness check
taylordotfish Oct 17, 2025
ef13d5d
Attempt to fix memory leak
taylordotfish Oct 17, 2025
b5b016a
Use `isArray`
taylordotfish Oct 17, 2025
d7b5a6c
Fix `quotedValues`
taylordotfish Oct 17, 2025
b8ea07a
Remove useless check
taylordotfish Oct 17, 2025
312a35d
Use uniform allocator for Handlers
taylordotfish Oct 17, 2025
41e7a24
Fix Listener allocator
taylordotfish Oct 17, 2025
b77e5f6
Merge remote-tracking branch 'origin/main' into taylor.fish/socket-co…
taylordotfish Oct 17, 2025
b4ef231
`handlers.vm` → `vm`
taylordotfish Oct 17, 2025
fccfb90
Fix deinit
taylordotfish Oct 17, 2025
909bb68
Remove outdated comment
taylordotfish Oct 17, 2025
f1db3bb
Fix leak when `Handlers.fromGenerated` fails
taylordotfish Oct 17, 2025
b044163
Improve sorting
taylordotfish Oct 17, 2025
ee5fd2d
[autofix.ci] apply automated fixes
autofix-ci[bot] Oct 17, 2025
bcbf1df
Use `readonly`
taylordotfish Oct 17, 2025
13cc00b
Better error message
taylordotfish Oct 17, 2025
1921806
Simplify destructuring
taylordotfish Oct 17, 2025
718161a
Fix error message
taylordotfish Oct 18, 2025
89b551e
Add explanatory comment
Jarred-Sumner Oct 18, 2025
2a8ad7a
Convert hostname to string
taylordotfish Oct 18, 2025
7b30f0e
Merge branch 'main' into taylor.fish/socket-config
Jarred-Sumner Oct 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 16 additions & 45 deletions src/bun.js/api/bun/socket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,8 @@ pub fn NewSocket(comptime ssl: bool) type {
// clean onOpen callback so only called in the first handshake and not in every renegotiation
// on servers this would require a different approach but it's not needed because our servers will not call handshake multiple times
// servers don't support renegotiation
this.handlers.?.onOpen.unprotect();
this.handlers.?.onOpen = .zero;
handlers.onOpen.unprotect();
handlers.onOpen = .zero;
}
} else {
// call handhsake callback with authorized and authorization error if has one
Expand Down Expand Up @@ -1349,14 +1349,10 @@ pub fn NewSocket(comptime ssl: bool) type {
return globalObject.throw("Expected \"socket\" option", .{});
};

var prev_handlers = this.getHandlers();

const handlers = try Handlers.fromJS(globalObject, socket_obj, prev_handlers.is_server);

prev_handlers.unprotect();
this.handlers.?.* = handlers; // TODO: this is a memory leak
this.handlers.?.withAsyncContextIfNeeded(globalObject);
this.handlers.?.protect();
const this_handlers = this.getHandlers();
const handlers = try Handlers.fromJS(globalObject, socket_obj, this_handlers.is_server);
this_handlers.deinit();
this_handlers.* = handlers;

return .js_undefined;
}
Expand Down Expand Up @@ -1398,7 +1394,7 @@ pub fn NewSocket(comptime ssl: bool) type {
return .zero;
}

var handlers = try Handlers.fromJS(globalObject, socket_obj, this.isServer());
const handlers = try Handlers.fromJS(globalObject, socket_obj, this.isServer());

if (globalObject.hasException()) {
return .zero;
Expand Down Expand Up @@ -1435,10 +1431,8 @@ pub fn NewSocket(comptime ssl: bool) type {
const options = socket_config.asUSockets();
const ext_size = @sizeOf(WrappedSocket);

var handlers_ptr = bun.handleOom(bun.default_allocator.create(Handlers));
handlers.withAsyncContextIfNeeded(globalObject);
const handlers_ptr = bun.handleOom(handlers.vm.allocator.create(Handlers));
handlers_ptr.* = handlers;
handlers_ptr.protect();
var tls = bun.new(TLSSocket, .{
.ref_count = .init(),
.handlers = handlers_ptr,
Expand Down Expand Up @@ -1489,7 +1483,7 @@ pub fn NewSocket(comptime ssl: bool) type {

tls.deref();

handlers_ptr.unprotect();
handlers_ptr.deinit();
bun.default_allocator.destroy(handlers_ptr);

// If BoringSSL gave us an error code, let's use it.
Expand All @@ -1514,29 +1508,10 @@ pub fn NewSocket(comptime ssl: bool) type {
const new_context = new_socket.context().?;
tls.socket_context = new_context; // owns the new tls context that have a ref from the old one
tls.ref();
const vm = handlers.vm;

var raw_handlers_ptr = bun.handleOom(bun.default_allocator.create(Handlers));
raw_handlers_ptr.* = blk: {
const this_handlers = this.getHandlers();
break :blk .{
.vm = vm,
.globalObject = globalObject,
.onOpen = this_handlers.onOpen,
.onClose = this_handlers.onClose,
.onData = this_handlers.onData,
.onWritable = this_handlers.onWritable,
.onTimeout = this_handlers.onTimeout,
.onConnectError = this_handlers.onConnectError,
.onEnd = this_handlers.onEnd,
.onError = this_handlers.onError,
.onHandshake = this_handlers.onHandshake,
.binary_type = this_handlers.binary_type,
.is_server = this_handlers.is_server,
};
};

raw_handlers_ptr.protect();
const this_handlers = this.getHandlers();
const raw_handlers_ptr = bun.handleOom(this_handlers.vm.allocator.create(Handlers));
raw_handlers_ptr.* = this_handlers.clone();

const raw = bun.new(TLSSocket, .{
.ref_count = .init(),
Expand All @@ -1563,7 +1538,7 @@ pub fn NewSocket(comptime ssl: bool) type {
tls.markActive();

// we're unrefing the original instance and refing the TLS instance
tls.poll_ref.ref(this.getHandlers().vm);
tls.poll_ref.ref(this_handlers.vm);

// mark both instances on socket data
if (new_socket.ext(WrappedSocket)) |ctx| {
Expand Down Expand Up @@ -1933,7 +1908,8 @@ pub fn jsUpgradeDuplexToTLS(globalObject: *jsc.JSGlobalObject, callframe: *jsc.C
return globalObject.throw("Expected \"socket\" option", .{});
};

var handlers = try Handlers.fromJS(globalObject, socket_obj, false);
const is_server = false; // A duplex socket is always handled as a client
const handlers = try Handlers.fromJS(globalObject, socket_obj, is_server);

var ssl_opts: ?jsc.API.ServerConfig.SSLConfig = null;
if (try opts.getTruthy(globalObject, "tls")) |tls| {
Expand All @@ -1953,13 +1929,8 @@ pub fn jsUpgradeDuplexToTLS(globalObject: *jsc.JSGlobalObject, callframe: *jsc.C
default_data.ensureStillAlive();
}

const is_server = false; // A duplex socket is always handled as a client

var handlers_ptr = bun.handleOom(handlers.vm.allocator.create(Handlers));
const handlers_ptr = bun.handleOom(handlers.vm.allocator.create(Handlers));
handlers_ptr.* = handlers;
handlers_ptr.is_server = is_server;
handlers_ptr.withAsyncContextIfNeeded(globalObject);
handlers_ptr.protect();
var tls = bun.new(TLSSocket, .{
.ref_count = .init(),
.handlers = handlers_ptr,
Expand Down
Loading