Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/js/node/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/js/node/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ function TLSSocket(socket?, options?) {

options = isNetSocketOrDuplex ? { ...options, allowHalfOpen: false } : options || socket || {};

if (options.lookup !== undefined && typeof options.lookup !== "function") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be validateFunction?

throw $ERR_INVALID_ARG_TYPE("options.lookup", "function", options.lookup);
}

NetSocket.$call(this, options);

if (typeof options === "object") {
Expand Down
33 changes: 33 additions & 0 deletions test/js/node/test/parallel/test-tls-lookup.js
Original file line number Diff line number Diff line change
@@ -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);
}
46 changes: 46 additions & 0 deletions test/js/node/tls/node-tls-custom-lookup.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});