Compare commits

...

1 Commits

Author SHA1 Message Date
Don Isaac
d637c2a33a fix(node/net): add test-net-socket-constructor 2025-03-03 13:50:44 -08:00
2 changed files with 74 additions and 1 deletions

View File

@@ -27,7 +27,7 @@ const { ExceptionWithHostPort } = require("internal/shared");
import type { SocketListener } from "bun";
import type { ServerOpts, Server as ServerType } from "node:net";
const { getTimerDuration } = require("internal/timers");
const { validateFunction, validateNumber } = require("internal/validators");
const { validateFunction, validateNumber, validateUint32 } = require("internal/validators");
// IPv4 Segment
const v4Seg = "(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])";
@@ -494,6 +494,7 @@ const Socket = (function (InternalSocket) {
_parent;
_parentWrap;
#socket;
#fd: number | undefined = undefined;
server;
pauseOnConnect = false;
#upgraded;
@@ -514,6 +515,7 @@ const Socket = (function (InternalSocket) {
noDelay = false,
keepAlive = false,
keepAliveInitialDelay = 0,
fd,
...opts
} = options || {};
@@ -533,6 +535,11 @@ const Socket = (function (InternalSocket) {
this.#pendingRead = undefined;
this.#upgraded = null;
if (fd != null) {
validateUint32(fd, "options.fd");
this.#fd = fd;
}
this[kSetNoDelay] = Boolean(noDelay);
this[kSetKeepAlive] = Boolean(keepAlive);
this[kSetKeepAliveInitialDelay] = ~~(keepAliveInitialDelay / 1000);
@@ -1015,6 +1022,7 @@ const Socket = (function (InternalSocket) {
} else {
this.destroy($ERR_SOCKET_CLOSED_BEFORE_CONNECTION("ERR_SOCKET_CLOSED_BEFORE_CONNECTION"));
}
return this;
}
setKeepAlive(enable = false, initialDelayMsecs = 0) {

View File

@@ -0,0 +1,65 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const net = require('net');
assert.throws(() => {
new net.Socket({ fd: -1 });
}, { code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => {
new net.Socket({ fd: 'foo' });
}, { code: 'ERR_INVALID_ARG_TYPE' });
function test(sock, readable, writable) {
let socket;
if (sock instanceof net.Socket) {
socket = sock;
} else {
socket = new net.Socket(sock);
socket.unref();
}
assert.strictEqual(socket.readable, readable);
assert.strictEqual(socket.writable, writable);
}
if (cluster.isPrimary) {
test(undefined, true, true);
const server = net.createServer(common.mustCall((socket) => {
socket.unref();
test(socket, true, true);
test({ handle: socket._handle }, true, true);
test({ handle: socket._handle, readable: true, writable: true },
true, true);
server.close();
}));
server.listen(common.mustCall(() => {
const { port } = server.address();
const socket = net.connect(port, common.mustCall(() => {
test(socket, true, true);
socket.end();
}));
test(socket, true, true);
}));
cluster.setupPrimary({
stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'pipe', 'pipe', 'pipe']
});
const worker = cluster.fork();
worker.on('exit', common.mustCall((code, signal) => {
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
}));
} else {
test(4, true, true);
test({ fd: 5 }, true, true);
test({ fd: 6, readable: true, writable: true }, true, true);
process.disconnect();
}