Compare commits

..

2 Commits

Author SHA1 Message Date
pfgithub
663e009651 Sync Node.js tests with upstream 2025-05-30 01:00:53 +00:00
pfg
2f8b8f82d0 fix tls generic stream test 2025-05-29 17:59:42 -07:00
4 changed files with 137 additions and 47 deletions

View File

@@ -88,6 +88,7 @@ function isIP(s): 0 | 4 | 6 {
}
const bunTlsSymbol = Symbol.for("::buntls::");
const bunSocketServerConnections = Symbol.for("::bunnetserverconnections::");
const bunSocketServerOptions = Symbol.for("::bunnetserveroptions::");
const owner_symbol = Symbol("owner_symbol");
@@ -228,6 +229,7 @@ const SocketHandlers: SocketHandler = {
self[kwriteCallback] = null;
callback(error);
}
self.emit("error", error);
},
open(socket) {
@@ -337,7 +339,7 @@ const ServerHandlers: SocketHandler = {
const data = this.data;
if (!data) return;
data.server._connections--;
data.server[bunSocketServerConnections]--;
{
if (!data[kclosed]) {
data[kclosed] = true;
@@ -383,7 +385,7 @@ const ServerHandlers: SocketHandler = {
return;
}
}
if (self.maxConnections != null && self._connections >= self.maxConnections) {
if (self.maxConnections && self[bunSocketServerConnections] >= self.maxConnections) {
const data = {
localAddress: _socket.localAddress,
localPort: _socket.localPort || this.localPort,
@@ -402,7 +404,7 @@ const ServerHandlers: SocketHandler = {
const bunTLS = _socket[bunTlsSymbol];
const isTLS = typeof bunTLS === "function";
self._connections++;
self[bunSocketServerConnections]++;
if (pauseOnConnect) {
_socket.pause();
@@ -728,6 +730,8 @@ function Socket(options?) {
this[kclosed] = false;
this[kended] = false;
this.connecting = false;
this._host = undefined;
this._port = undefined;
this[bunTLSConnectOptions] = null;
this.timeout = 0;
this[kwriteCallback] = undefined;
@@ -916,7 +920,9 @@ Socket.prototype.connect = function connect(...args) {
process.nextTick(() => {
this.resume();
});
this.connecting = true;
if (!connection && !fd) {
this.connecting = true;
}
}
if (fd) {
return this;
@@ -1554,6 +1560,7 @@ function lookupAndConnect(self, options) {
$debug("connect: find host", host, addressType);
$debug("connect: dns options", dnsopts);
self._host = host;
self._port = port;
const lookup = options.lookup || dns.lookup;
if (dnsopts.family !== 4 && dnsopts.family !== 6 && !localAddress && autoSelectFamily) {
@@ -2070,6 +2077,7 @@ function Server(options?, connectionListener?) {
// https://nodejs.org/api/net.html#netcreateserveroptions-connectionlistener
const {
maxConnections, //
allowHalfOpen = false,
keepAlive = false,
keepAliveInitialDelay = 0,
@@ -2086,6 +2094,7 @@ function Server(options?, connectionListener?) {
this._unref = false;
this.listeningId = 1;
this[bunSocketServerConnections] = 0;
this[bunSocketServerOptions] = undefined;
this.allowHalfOpen = allowHalfOpen;
this.keepAlive = keepAlive;
@@ -2093,6 +2102,7 @@ function Server(options?, connectionListener?) {
this.highWaterMark = highWaterMark;
this.pauseOnConnect = Boolean(pauseOnConnect);
this.noDelay = noDelay;
this.maxConnections = Number.isSafeInteger(maxConnections) && maxConnections > 0 ? maxConnections : 0;
options.connectionListener = connectionListener;
this[bunSocketServerOptions] = options;
@@ -2155,7 +2165,7 @@ Server.prototype[Symbol.asyncDispose] = function () {
};
Server.prototype._emitCloseIfDrained = function _emitCloseIfDrained() {
if (this._handle || this._connections > 0) {
if (this._handle || this[bunSocketServerConnections] > 0) {
return;
}
process.nextTick(() => {
@@ -2184,7 +2194,7 @@ Server.prototype.getConnections = function getConnections(callback) {
//in Bun case we will never error on getConnections
//node only errors if in the middle of the couting the server got disconnected, what never happens in Bun
//if disconnected will only pass null as well and 0 connected
callback(null, this._handle ? this._connections : 0);
callback(null, this._handle ? this[bunSocketServerConnections] : 0);
}
return this;
};

View File

@@ -0,0 +1,83 @@
'use strict';
const common = require('../common');
const { createMockedLookup } = require('../common/dns');
const assert = require('assert');
const { createConnection, createServer, setDefaultAutoSelectFamily } = require('net');
const autoSelectFamilyAttemptTimeout = common.defaultAutoSelectFamilyAttemptTimeout;
// Test that IPV4 is reached by default if IPV6 is not reachable and the default is enabled
{
const ipv4Server = createServer((socket) => {
socket.on('data', common.mustCall(() => {
socket.write('response-ipv4');
socket.end();
}));
});
ipv4Server.listen(0, '127.0.0.1', common.mustCall(() => {
setDefaultAutoSelectFamily(true);
const connection = createConnection({
host: 'example.org',
port: ipv4Server.address().port,
lookup: createMockedLookup('::1', '127.0.0.1'),
autoSelectFamilyAttemptTimeout,
});
let response = '';
connection.setEncoding('utf-8');
connection.on('data', (chunk) => {
response += chunk;
});
connection.on('end', common.mustCall(() => {
assert.strictEqual(response, 'response-ipv4');
ipv4Server.close();
}));
connection.write('request');
}));
}
// Test that IPV4 is not reached by default if IPV6 is not reachable and the default is disabled
{
const ipv4Server = createServer((socket) => {
socket.on('data', common.mustCall(() => {
socket.write('response-ipv4');
socket.end();
}));
});
ipv4Server.listen(0, '127.0.0.1', common.mustCall(() => {
setDefaultAutoSelectFamily(false);
const port = ipv4Server.address().port;
const connection = createConnection({
host: 'example.org',
port,
lookup: createMockedLookup('::1', '127.0.0.1'),
});
connection.on('ready', common.mustNotCall());
connection.on('error', common.mustCall((error) => {
if (common.hasIPv6) {
assert.strictEqual(error.code, 'ECONNREFUSED');
assert.strictEqual(error.message, `connect ECONNREFUSED ::1:${port}`);
} else if (error.code === 'EAFNOSUPPORT') {
assert.strictEqual(error.message, `connect EAFNOSUPPORT ::1:${port} - Local (undefined:undefined)`);
} else if (error.code === 'EUNATCH') {
assert.strictEqual(error.message, `connect EUNATCH ::1:${port} - Local (:::0)`);
} else {
assert.strictEqual(error.code, 'EADDRNOTAVAIL');
assert.strictEqual(error.message, `connect EADDRNOTAVAIL ::1:${port} - Local (:::0)`);
}
ipv4Server.close();
}));
}));
}

View File

@@ -1,41 +0,0 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
let firstSocket;
const dormantServer = net.createServer(common.mustNotCall());
const server = net.createServer(common.mustCall((socket) => {
firstSocket = socket;
}));
dormantServer.maxConnections = 0;
server.maxConnections = 1;
dormantServer.on('drop', common.mustCall((data) => {
assert.strictEqual(!!data.localAddress, true);
assert.strictEqual(!!data.localPort, true);
assert.strictEqual(!!data.remoteAddress, true);
assert.strictEqual(!!data.remotePort, true);
assert.strictEqual(!!data.remoteFamily, true);
dormantServer.close();
}));
server.on('drop', common.mustCall((data) => {
assert.strictEqual(!!data.localAddress, true);
assert.strictEqual(!!data.localPort, true);
assert.strictEqual(!!data.remoteAddress, true);
assert.strictEqual(!!data.remotePort, true);
assert.strictEqual(!!data.remoteFamily, true);
firstSocket.destroy();
server.close();
}));
dormantServer.listen(0, () => {
net.createConnection(dormantServer.address().port);
});
server.listen(0, () => {
net.createConnection(server.address().port);
net.createConnection(server.address().port);
});

View File

@@ -0,0 +1,38 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const fixtures = require('../common/fixtures');
const { duplexPair } = require('stream');
const assert = require('assert');
const { TLSSocket, connect } = require('tls');
const key = fixtures.readKey('agent1-key.pem');
const cert = fixtures.readKey('agent1-cert.pem');
const ca = fixtures.readKey('ca1-cert.pem');
const [ clientSide, serverSide ] = duplexPair();
const clientTLS = connect({
socket: clientSide,
ca,
host: 'agent1' // Hostname from certificate
});
const serverTLS = new TLSSocket(serverSide, {
isServer: true,
key,
cert,
ca
});
assert.strictEqual(clientTLS.connecting, false);
assert.strictEqual(serverTLS.connecting, false);
clientTLS.on('secureConnect', common.mustCall(() => {
clientTLS.write('foobar', common.mustCall(() => {
assert.strictEqual(serverTLS.read().toString(), 'foobar');
assert.strictEqual(clientTLS._handle.writeQueueSize, 0);
}));
assert.ok(clientTLS._handle.writeQueueSize > 0);
}));