Skip to content

Commit 952e731

Browse files
committed
Add support for Node-specific TLS options
Introduced `NodeTlsOptions` and `NodeConnectionOptions` types to handle Node.js-specific TLS configurations, like `rejectUnauthorized`. Updated `connect` function and related logic to support these new options, and added a relevant test case for TLS behavior. Signed-off-by: Alberto Ricart <[email protected]>
1 parent 9b63bc1 commit 952e731

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

Diff for: transport-node/src/connect.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,24 @@ import {
1818
NatsConnection,
1919
NatsConnectionImpl,
2020
setTransportFactory,
21+
type TlsOptions,
2122
Transport,
2223
TransportFactory,
2324
} from "./nats-base-client";
2425

2526
import { errors, hasWsProtocol } from "./nats-base-client";
2627

27-
export function connect(opts: ConnectionOptions = {}): Promise<NatsConnection> {
28+
export type NodeTlsOptions = {
29+
rejectUnauthorized?: boolean;
30+
} & TlsOptions;
31+
32+
export type NodeConnectionOptions = Omit<ConnectionOptions, "tls"> & {
33+
tls?: NodeTlsOptions | null;
34+
};
35+
36+
export function connect(
37+
opts: NodeConnectionOptions = {},
38+
): Promise<NatsConnection> {
2839
if (hasWsProtocol(opts)) {
2940
return Promise.reject(
3041
errors.InvalidArgumentError.format(

Diff for: transport-node/src/mod.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
* See the License for the specific language governing permissions and
1313
* limitations under the License.
1414
*/
15-
export { connect } from "./connect";
15+
export { connect, NodeConnectionOptions } from "./connect";
1616
export * from "./nats-base-client";

Diff for: transport-node/src/node_transport.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { existsSync, readFile } from "node:fs";
3333
import dns from "node:dns";
3434
import { Buffer } from "node:buffer";
3535
import { version } from "./version";
36+
import type { NodeConnectionOptions } from "./connect";
3637

3738
export const VERSION = version;
3839
const LANG = "nats.js";
@@ -56,7 +57,7 @@ export class NodeTransport implements Transport {
5657
}
5758
async connect(
5859
hp: { hostname: string; port: number; tlsName: string },
59-
options: ConnectionOptions,
60+
options: NodeConnectionOptions,
6061
): Promise<void> {
6162
this.tlsName = hp.tlsName;
6263
this.options = options;

Diff for: transport-node/tests/tls_test.js

+23
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,29 @@ describe("tls", { timeout: 20_000, concurrency: true, forceExit: true }, () => {
312312
},
313313
});
314314

315+
await nc.flush();
316+
await nc.close();
317+
await ns.stop();
318+
});
319+
it("tls first reject unauthorized", async () => {
320+
const ns = await NatsServer.start({
321+
host: "0.0.0.0",
322+
tls: {
323+
handshake_first: true,
324+
cert_file: resolve(join(dir, "./tests/certs/server.pem")),
325+
key_file: resolve(join(dir, "./tests/certs/key.pem")),
326+
ca_file: resolve(join(dir, "./tests/certs/ca.pem")),
327+
},
328+
});
329+
330+
const nc = await connect({
331+
port: ns.port,
332+
tls: {
333+
handshakeFirst: true,
334+
rejectUnauthorized: false,
335+
},
336+
});
337+
315338
await nc.flush();
316339
await nc.close();
317340
await ns.stop();

0 commit comments

Comments
 (0)