Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js: de-class-ify node:net.Socket #17997

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions docs/api/tcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Bun.listen({
socket: {
data(socket, data) {}, // message received from client
open(socket) {}, // socket opened
close(socket) {}, // socket closed
close(socket, error) {}, // socket closed
drain(socket) {}, // socket ready for more data
error(socket, error) {}, // error handler
},
Expand All @@ -30,7 +30,7 @@ Bun.listen({
open(socket) {},
data(socket, data) {},
drain(socket) {},
close(socket) {},
close(socket, error) {},
error(socket, error) {},
},
});
Expand Down Expand Up @@ -122,7 +122,7 @@ const socket = await Bun.connect({
socket: {
data(socket, data) {},
open(socket) {},
close(socket) {},
close(socket, error) {},
drain(socket) {},
error(socket, error) {},

Expand Down
2 changes: 1 addition & 1 deletion packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6239,7 +6239,7 @@ declare module "bun" {
* @param socket
*/
open?(socket: Socket<Data>): void | Promise<void>;
close?(socket: Socket<Data>): void | Promise<void>;
close?(socket: Socket<Data>, error?: Error): void | Promise<void>;
error?(socket: Socket<Data>, error: Error): void | Promise<void>;
data?(socket: Socket<Data>, data: BinaryTypeList[DataBinaryType]): void | Promise<void>;
drain?(socket: Socket<Data>): void | Promise<void>;
Expand Down
32 changes: 2 additions & 30 deletions src/bun.js/api/bun/socket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1220,37 +1220,9 @@ pub const Listener = struct {
if (ssl.?.server_name) |s| {
server_name = bun.default_allocator.dupe(u8, s[0..bun.len(s)]) catch bun.outOfMemory();
}
uws.NewSocketHandler(true).configure(
socket_context,
true,
*TLSSocket,
struct {
pub const onOpen = NewSocket(true).onOpen;
pub const onClose = NewSocket(true).onClose;
pub const onData = NewSocket(true).onData;
pub const onWritable = NewSocket(true).onWritable;
pub const onTimeout = NewSocket(true).onTimeout;
pub const onConnectError = NewSocket(true).onConnectError;
pub const onEnd = NewSocket(true).onEnd;
pub const onHandshake = NewSocket(true).onHandshake;
},
);
uws.NewSocketHandler(true).configure(socket_context, true, *TLSSocket, NewSocket(true));
} else {
uws.NewSocketHandler(false).configure(
socket_context,
true,
*TCPSocket,
struct {
pub const onOpen = NewSocket(false).onOpen;
pub const onClose = NewSocket(false).onClose;
pub const onData = NewSocket(false).onData;
pub const onWritable = NewSocket(false).onWritable;
pub const onTimeout = NewSocket(false).onTimeout;
pub const onConnectError = NewSocket(false).onConnectError;
pub const onEnd = NewSocket(false).onEnd;
pub const onHandshake = NewSocket(false).onHandshake;
},
);
uws.NewSocketHandler(false).configure(socket_context, true, *TCPSocket, NewSocket(false));
}

default_data.ensureStillAlive();
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/bindings/BunProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1958,7 +1958,7 @@ static JSValue constructReportObjectComplete(VM& vm, Zig::GlobalObject* globalOb
stackArray->push(globalObject, JSC::jsString(vm, line.toString().trim(isASCIIWhitespace)));
});

javascriptStack->putDirect(vm, JSC::Identifier::fromString(vm, "stack"_s), stackArray, 0);
javascriptStack->putDirect(vm, vm.propertyNames->stack, stackArray, 0);
}

JSC::JSObject* errorProperties = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), 1);
Expand Down
12 changes: 10 additions & 2 deletions src/deps/uws.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1627,12 +1627,14 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type {
// debug("us_socket_shutdown({d})", .{@intFromPtr(this.socket)});
switch (this.socket) {
.connected => |socket| {
debug("us_socket_shutdown({d})", .{@intFromPtr(socket)});
return us_socket_shutdown(
comptime ssl_int,
socket,
);
},
.connecting => |socket| {
debug("us_connecting_socket_shutdown({d})", .{@intFromPtr(socket)});
return us_connecting_socket_shutdown(
comptime ssl_int,
socket,
Expand All @@ -1651,14 +1653,14 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type {
pub fn shutdownRead(this: ThisSocket) void {
switch (this.socket) {
.connected => |socket| {
// debug("us_socket_shutdown_read({d})", .{@intFromPtr(socket)});
debug("us_socket_shutdown_read({d})", .{@intFromPtr(socket)});
return us_socket_shutdown_read(
comptime ssl_int,
socket,
);
},
.connecting => |socket| {
// debug("us_connecting_socket_shutdown_read({d})", .{@intFromPtr(socket)});
debug("us_connecting_socket_shutdown_read({d})", .{@intFromPtr(socket)});
return us_connecting_socket_shutdown_read(
comptime ssl_int,
socket,
Expand All @@ -1677,12 +1679,14 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type {
pub fn isShutdown(this: ThisSocket) bool {
switch (this.socket) {
.connected => |socket| {
debug("us_socket_is_shut_down({d})", .{@intFromPtr(socket)});
return us_socket_is_shut_down(
comptime ssl_int,
socket,
) > 0;
},
.connecting => |socket| {
debug("us_connecting_socket_is_shut_down({d})", .{@intFromPtr(socket)});
return us_connecting_socket_is_shut_down(
comptime ssl_int,
socket,
Expand All @@ -1709,12 +1713,14 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type {
pub fn getError(this: ThisSocket) i32 {
switch (this.socket) {
.connected => |socket| {
debug("us_socket_get_error({d})", .{@intFromPtr(socket)});
return us_socket_get_error(
comptime ssl_int,
socket,
);
},
.connecting => |socket| {
debug("us_connecting_socket_get_error({d})", .{@intFromPtr(socket)});
return us_connecting_socket_get_error(
comptime ssl_int,
socket,
Expand All @@ -1740,6 +1746,7 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type {
pub fn localPort(this: ThisSocket) i32 {
switch (this.socket) {
.connected => |socket| {
debug("us_socket_local_port({d})", .{@intFromPtr(socket)});
return us_socket_local_port(
comptime ssl_int,
socket,
Expand All @@ -1751,6 +1758,7 @@ pub fn NewSocketHandler(comptime is_ssl: bool) type {
pub fn remoteAddress(this: ThisSocket, buf: [*]u8, length: *i32) void {
switch (this.socket) {
.connected => |socket| {
debug("us_socket_remote_address({d})", .{@intFromPtr(socket)});
return us_socket_remote_address(
comptime ssl_int,
socket,
Expand Down
Loading