Compare commits

...

5 Commits

Author SHA1 Message Date
Alistair Smith
aeadc0204d Merge branch 'main' into a/bun-14228-nodejs-test-test-tls-lookupjs 2025-05-21 17:46:34 -07:00
Alistair Smith
1f9069f1f2 more coverage for happy path of tls.connect with .lookup fn 2025-05-21 12:57:46 -07:00
Alistair Smith
5672ef1f02 support tls/net .lookup option in connect 2025-05-21 12:14:24 -07:00
Alistair Smith
6cb2b59862 check 2025-05-21 12:11:23 -07:00
Alistair Smith
93d982c797 add test file 2025-05-21 12:03:50 -07:00
4 changed files with 106 additions and 0 deletions

View File

@@ -881,6 +881,29 @@ Socket.prototype.connect = function connect(...args) {
});
} else {
// default start
if (typeof options.lookup === "function" && host) {
options.lookup(host, { all: false, family: options.family || 0 }, (err, address) => {
if (err) {
this.emit("error", err);
this.emit("close");
return;
}
bunConnect({
data: this,
hostname: address,
port: port,
socket: this[khandlers],
tls,
allowHalfOpen: this.allowHalfOpen,
}).catch(error => {
if (!this.destroyed) {
this.emit("error", error);
this.emit("close");
}
});
});
return this;
}
bunConnect({
data: this,
hostname: host || "localhost",

View File

@@ -309,6 +309,10 @@ function TLSSocket(socket?, options?) {
options = isNetSocketOrDuplex ? { ...options, allowHalfOpen: false } : options || socket || {};
if (options.lookup !== undefined && typeof options.lookup !== "function") {
throw $ERR_INVALID_ARG_TYPE("options.lookup", "function", options.lookup);
}
NetSocket.$call(this, options);
if (typeof options === "object") {

View File

@@ -0,0 +1,33 @@
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const tls = require('tls');
['foobar', 1, {}, []].forEach(function connectThrows(input) {
const opts = {
host: 'localhost',
port: common.PORT,
lookup: input
};
assert.throws(() => {
tls.connect(opts);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
});
});
connectDoesNotThrow(common.mustCall());
function connectDoesNotThrow(input) {
const opts = {
host: 'localhost',
port: common.PORT,
lookup: input
};
tls.connect(opts);
}

View File

@@ -0,0 +1,46 @@
import { expect, it } from "bun:test";
import { once } from "events";
import { tls as COMMON_CERT } from "harness";
import { AddressInfo } from "net";
import tls from "tls";
it("tls.connect should call custom lookup and connect successfully", async () => {
let lookupCalled = false;
const server = tls.createServer({
cert: COMMON_CERT.cert,
key: COMMON_CERT.key,
});
await once(server.listen(0, "127.0.0.1"), "listening");
const { port } = server.address() as AddressInfo;
server.on("secureConnection", socket => {
socket.end("ok");
});
function customLookup(host, opts, cb) {
lookupCalled = true;
cb(null, "127.0.0.1", 4);
}
await new Promise((resolve, reject) => {
const socket = tls.connect(
{
port,
host: "localhost",
rejectUnauthorized: false,
lookup: customLookup,
},
() => {
expect(lookupCalled).toBe(true);
socket.end();
server.close();
resolve(undefined);
},
);
socket.on("error", err => {
server.close();
reject(err);
});
});
});