diff --git a/docs/runtime/nodejs-apis.md b/docs/runtime/nodejs-apis.md index 6008cb24ba2826..b48db8b88b9dfa 100644 --- a/docs/runtime/nodejs-apis.md +++ b/docs/runtime/nodejs-apis.md @@ -80,7 +80,7 @@ Some methods are not optimized yet. ### [`node:net`](https://nodejs.org/api/net.html) -🟡 Missing `BlockList` `SocketAddress` `Stream` `getDefaultAutoSelectFamily` `getDefaultAutoSelectFamilyAttemptTimeout` `setDefaultAutoSelectFamily` `setDefaultAutoSelectFamilyAttemptTimeout` +🟡 Missing `BlockList` `SocketAddress` `Stream` ### [`node:os`](https://nodejs.org/api/os.html) diff --git a/src/bun.js/node/node_net_binding.zig b/src/bun.js/node/node_net_binding.zig new file mode 100644 index 00000000000000..ce38d71c447959 --- /dev/null +++ b/src/bun.js/node/node_net_binding.zig @@ -0,0 +1,77 @@ +const std = @import("std"); +const bun = @import("root").bun; +const Environment = bun.Environment; +const JSC = bun.JSC; +const string = bun.string; +const Output = bun.Output; +const ZigString = JSC.ZigString; + +// +// + +pub var autoSelectFamilyDefault: bool = true; + +pub fn getDefaultAutoSelectFamily(global: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { + return JSC.JSFunction.create(global, "getDefaultAutoSelectFamily", (struct { + fn getter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + _ = globalThis; + _ = callframe; + return JSC.jsBoolean(autoSelectFamilyDefault); + } + }).getter, 0, .{}); +} + +pub fn setDefaultAutoSelectFamily(global: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { + return JSC.JSFunction.create(global, "setDefaultAutoSelectFamily", (struct { + fn setter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + const arguments = callframe.arguments(1); + if (arguments.len < 1) { + globalThis.throw("missing argument", .{}); + return .undefined; + } + const arg = arguments.slice()[0]; + if (!arg.isBoolean()) { + globalThis.throwInvalidArguments("autoSelectFamilyDefault", .{}); + return .undefined; + } + const value = arg.toBoolean(); + autoSelectFamilyDefault = value; + return JSC.jsBoolean(value); + } + }).setter, 1, .{}); +} + +// +// + +pub var autoSelectFamilyAttemptTimeoutDefault: u32 = 250; + +pub fn getDefaultAutoSelectFamilyAttemptTimeout(global: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { + return JSC.JSFunction.create(global, "getDefaultAutoSelectFamilyAttemptTimeout", (struct { + fn getter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + _ = globalThis; + _ = callframe; + return JSC.jsNumber(autoSelectFamilyAttemptTimeoutDefault); + } + }).getter, 0, .{}); +} + +pub fn setDefaultAutoSelectFamilyAttemptTimeout(global: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { + return JSC.JSFunction.create(global, "setDefaultAutoSelectFamilyAttemptTimeout", (struct { + fn setter(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + const arguments = callframe.arguments(1); + if (arguments.len < 1) { + globalThis.throw("missing argument", .{}); + return .undefined; + } + const arg = arguments.slice()[0]; + if (!arg.isInt32AsAnyInt()) { + globalThis.throwInvalidArguments("autoSelectFamilyAttemptTimeoutDefault", .{}); + return .undefined; + } + const value: u32 = @max(10, arg.coerceToInt32(globalThis)); + autoSelectFamilyAttemptTimeoutDefault = value; + return JSC.jsNumber(value); + } + }).setter, 1, .{}); +} diff --git a/src/js/node/net.ts b/src/js/node/net.ts index 589dce3b1c5da8..4406231304815d 100644 --- a/src/js/node/net.ts +++ b/src/js/node/net.ts @@ -990,4 +990,9 @@ export default { isIPv6, Socket, [Symbol.for("::bunternal::")]: SocketClass, + + getDefaultAutoSelectFamily: $zig("node_net_binding.zig", "getDefaultAutoSelectFamily"), + setDefaultAutoSelectFamily: $zig("node_net_binding.zig", "setDefaultAutoSelectFamily"), + getDefaultAutoSelectFamilyAttemptTimeout: $zig("node_net_binding.zig", "getDefaultAutoSelectFamilyAttemptTimeout"), + setDefaultAutoSelectFamilyAttemptTimeout: $zig("node_net_binding.zig", "setDefaultAutoSelectFamilyAttemptTimeout"), };