aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/api/bun/socket.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/bun.js/api/bun/socket.zig')
-rw-r--r--src/bun.js/api/bun/socket.zig71
1 files changed, 49 insertions, 22 deletions
diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig
index f4dcfb01c..911dc9e89 100644
--- a/src/bun.js/api/bun/socket.zig
+++ b/src/bun.js/api/bun/socket.zig
@@ -232,38 +232,65 @@ pub const SocketConfig = struct {
}
}
- if (opts.getTruthy(globalObject, "hostname")) |hostname| {
- if (hostname.isEmptyOrUndefinedOrNull() or !hostname.isString()) {
- exception.* = JSC.toInvalidArguments("Expected \"hostname\" to be a string", .{}, globalObject).asObjectRef();
- return null;
- }
+ 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();
+ return null;
+ }
- const port_value = opts.get(globalObject, "port") orelse JSValue.zero;
- if (port_value.isEmptyOrUndefinedOrNull() or !port_value.isNumber() or port_value.toInt64() > std.math.maxInt(u16) or port_value.toInt64() < 0) {
- exception.* = JSC.toInvalidArguments("Expected \"port\" to be a number between 0 and 65535", .{}, globalObject).asObjectRef();
- return null;
- }
+ hostname_or_unix = unix_socket.getZigString(globalObject).toSlice(bun.default_allocator);
- hostname_or_unix = hostname.getZigString(globalObject).toSlice(bun.default_allocator);
- port = port_value.toU16();
+ if (strings.hasPrefixComptime(hostname_or_unix.slice(), "file://") or strings.hasPrefixComptime(hostname_or_unix.slice(), "unix://") or strings.hasPrefixComptime(hostname_or_unix.slice(), "sock://")) {
+ hostname_or_unix.ptr += 7;
+ hostname_or_unix.len -|= 7;
+ }
- if (hostname_or_unix.len == 0) {
- exception.* = JSC.toInvalidArguments("Expected \"hostname\" to be a non-empty string", .{}, globalObject).asObjectRef();
- return null;
- }
- } else if (opts.getTruthy(globalObject, "unix")) |unix_socket| {
- if (unix_socket.isEmptyOrUndefinedOrNull() or !unix_socket.isString()) {
- exception.* = JSC.toInvalidArguments("Expected \"unix\" to be a string", .{}, globalObject).asObjectRef();
- return null;
+ if (hostname_or_unix.len > 0) {
+ break :hostname_or_unix;
+ }
}
- hostname_or_unix = unix_socket.getZigString(globalObject).toSlice(bun.default_allocator);
+ if (opts.getTruthy(globalObject, "hostname")) |hostname| {
+ if (!hostname.isString()) {
+ exception.* = JSC.toInvalidArguments("Expected \"hostname\" to be a string", .{}, globalObject).asObjectRef();
+ return null;
+ }
+
+ var port_value = opts.get(globalObject, "port") orelse JSValue.zero;
+ hostname_or_unix = hostname.getZigString(globalObject).toSlice(bun.default_allocator);
+
+ if (port_value.isEmptyOrUndefinedOrNull() and hostname_or_unix.len > 0) {
+ const parsed_url = bun.URL.parse(hostname_or_unix.slice());
+ if (parsed_url.getPort()) |port_num| {
+ port_value = JSValue.jsNumber(port_num);
+ hostname_or_unix.ptr = parsed_url.hostname.ptr;
+ hostname_or_unix.len = @truncate(u32, parsed_url.hostname.len);
+ }
+ }
+
+ if (port_value.isEmptyOrUndefinedOrNull() or !port_value.isNumber() or port_value.toInt64() > std.math.maxInt(u16) or port_value.toInt64() < 0) {
+ exception.* = JSC.toInvalidArguments("Expected \"port\" to be a number between 0 and 65535", .{}, globalObject).asObjectRef();
+ return null;
+ }
+
+ port = port_value.toU16();
+
+ if (hostname_or_unix.len == 0) {
+ exception.* = JSC.toInvalidArguments("Expected \"hostname\" to be a non-empty string", .{}, globalObject).asObjectRef();
+ return null;
+ }
+
+ if (hostname_or_unix.len > 0) {
+ break :hostname_or_unix;
+ }
+ }
if (hostname_or_unix.len == 0) {
exception.* = JSC.toInvalidArguments("Expected \"unix\" to be a non-empty string", .{}, globalObject).asObjectRef();
return null;
}
- } else {
+
exception.* = JSC.toInvalidArguments("Expected either \"hostname\" or \"unix\"", .{}, globalObject).asObjectRef();
return null;
}