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

node:child_process: support defining extra pipes #7958

Merged
merged 21 commits into from
Jan 5, 2024
Merged
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
4 changes: 0 additions & 4 deletions packages/bun-usockets/src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,6 @@ struct us_socket_t *us_socket_from_fd(struct us_socket_context_t *ctx, int socke

us_internal_socket_context_link_socket(ctx, s);

if (ctx->on_open) {
ctx->on_open(s, 0, 0, 0);
}

return s;
#endif
}
Expand Down
4 changes: 3 additions & 1 deletion src/bun.js/api/bun.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ export default [
signalCode: {
getter: "getSignalCode",
},

exited: {
getter: "getExited",
},
stdio: {
getter: "getStdio",
},
},
}),
];
58 changes: 41 additions & 17 deletions src/bun.js/api/bun/socket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ pub const SocketConfig = struct {
}

hostname_or_unix: {
if (opts.getTruthy(globalObject, "fd")) |fd_| {
if (fd_.isNumber()) {
break :hostname_or_unix;
}
}

if (opts.getTruthy(globalObject, "unix")) |unix_socket| {
if (!unix_socket.isString()) {
exception.* = JSC.toInvalidArguments("Expected \"unix\" to be a string", .{}, globalObject).asObjectRef();
Expand Down Expand Up @@ -497,6 +503,7 @@ pub const Listener = struct {
host: []const u8,
port: u16,
},
fd: bun.FileDescriptor,

pub fn clone(this: UnixOrHost) UnixOrHost {
switch (this) {
Expand All @@ -506,8 +513,14 @@ pub const Listener = struct {
};
},
.host => |h| {
return .{ .host = .{ .host = (bun.default_allocator.dupe(u8, h.host) catch unreachable), .port = this.host.port } };
return .{
.host = .{
.host = (bun.default_allocator.dupe(u8, h.host) catch unreachable),
.port = this.host.port,
},
};
},
.fd => |f| return .{ .fd = f },
}
}

Expand All @@ -519,6 +532,7 @@ pub const Listener = struct {
.host => |h| {
bun.default_allocator.free(h.host);
},
.fd => {}, // this is an integer
}
}
};
Expand Down Expand Up @@ -557,10 +571,7 @@ pub const Listener = struct {
return JSValue.jsUndefined();
}

pub fn listen(
globalObject: *JSC.JSGlobalObject,
opts: JSValue,
) JSValue {
pub fn listen(globalObject: *JSC.JSGlobalObject, opts: JSValue) JSValue {
log("listen", .{});
var exception_ = [1]JSC.JSValueRef{null};
const exception: JSC.C.ExceptionRef = &exception_;
Expand All @@ -581,7 +592,7 @@ pub const Listener = struct {
var hostname_or_unix = socket_config.hostname_or_unix;
const port = socket_config.port;
var ssl = socket_config.ssl;
var handlers = &socket_config.handlers;
var handlers = socket_config.handlers;
var protos: ?[]const u8 = null;
const exclusive = socket_config.exclusive;
handlers.is_server = true;
Expand Down Expand Up @@ -690,6 +701,10 @@ pub const Listener = struct {
defer bun.default_allocator.free(host);
break :brk uws.us_socket_context_listen_unix(@intFromBool(ssl_enabled), socket_context, host, socket_flags, 8);
},
.fd => {
// don't call listen() on an fd
return .zero;
},
}
} orelse {
defer {
Expand All @@ -716,7 +731,7 @@ pub const Listener = struct {
};

var socket = Listener{
.handlers = handlers.*,
.handlers = handlers,
.connection = connection,
.ssl = ssl_enabled,
.socket_context = socket_context,
Expand Down Expand Up @@ -869,15 +884,13 @@ pub const Listener = struct {
if (this.connection != .host) {
return JSValue.jsUndefined();
}

return ZigString.init(this.connection.host.host).withEncoding().toValueGC(globalObject);
}

pub fn getPort(this: *Listener, _: *JSC.JSGlobalObject) callconv(.C) JSValue {
if (this.connection != .host) {
return JSValue.jsUndefined();
}

return JSValue.jsNumber(this.connection.host.port);
}

Expand Down Expand Up @@ -943,10 +956,17 @@ pub const Listener = struct {
return .zero;
};

const connection: Listener.UnixOrHost = if (port) |port_| .{
.host = .{ .host = (hostname_or_unix.cloneIfNeeded(bun.default_allocator) catch unreachable).slice(), .port = port_ },
} else .{
.unix = (hostname_or_unix.cloneIfNeeded(bun.default_allocator) catch unreachable).slice(),
const connection: Listener.UnixOrHost = blk: {
if (opts.getTruthy(globalObject, "fd")) |fd_| {
if (fd_.isNumber()) {
const fd: bun.FileDescriptor = fd_.asInt32();
break :blk .{ .fd = fd };
}
}
if (port) |_| {
break :blk .{ .host = .{ .host = (hostname_or_unix.cloneIfNeeded(bun.default_allocator) catch unreachable).slice(), .port = port.? } };
}
break :blk .{ .unix = (hostname_or_unix.cloneIfNeeded(bun.default_allocator) catch unreachable).slice() };
};

if (ssl_enabled) {
Expand Down Expand Up @@ -1147,24 +1167,28 @@ fn NewSocket(comptime ssl: bool) type {
pub fn doConnect(this: *This, connection: Listener.UnixOrHost, socket_ctx: *uws.SocketContext) !void {
switch (connection) {
.host => |c| {
_ = @This().Socket.connectPtr(
_ = This.Socket.connectPtr(
normalizeHost(c.host),
c.port,
socket_ctx,
@This(),
This,
this,
"socket",
) orelse return error.ConnectionFailed;
},
.unix => |u| {
_ = @This().Socket.connectUnixPtr(
_ = This.Socket.connectUnixPtr(
u,
socket_ctx,
@This(),
This,
this,
"socket",
) orelse return error.ConnectionFailed;
},
.fd => |f| {
const socket = This.Socket.fromFd(socket_ctx, f, This, this, "socket") orelse return error.ConnectionFailed;
this.onOpen(socket);
},
}
}

Expand Down
Loading
Loading