Skip to content

Commit

Permalink
[FEAT] resolve connection option
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed Jun 13, 2024
1 parent 50d342c commit 207ae85
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
12 changes: 7 additions & 5 deletions nats-base-client/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ export interface Server {
tlsName: string;

resolve(
opts: Partial<{ fn: DnsResolveFn; randomize: boolean; debug?: boolean }>,
opts: Partial<{ fn: DnsResolveFn; randomize: boolean; debug?: boolean, resolve?: boolean }>,
): Promise<Server[]>;
}

Expand Down Expand Up @@ -1050,10 +1050,12 @@ export interface ConnectionOptions {
noAsyncTraces?: boolean;

/**
* When true, the connect function will remove any name resolution provided by
* the transport. In some environments (browsers) this is a no-op option.
*/
noResolve?: boolean;
* When false, the connect function will not perform any hostname resolution. Note that
* by default this option will be true if the client supports hostname resolution.
* Note that on clients that don't supported (mainly the websocket client, setting this
* option to true, will throw an exception as this option is not available.
*/
resolve?: boolean;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions nats-base-client/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ export function parseOptions(opts?: ConnectionOptions): ConnectionOptions {
}
}

// if not set - we set it
if (options.resolve === undefined) {
// set a default based on whether the client can resolve or not
options.resolve = typeof getResolveFn() === "function";
}

if (options.resolve) {
if (typeof getResolveFn() !== "function") {
throw new NatsError(
Expand Down
2 changes: 2 additions & 0 deletions nats-base-client/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,10 +589,12 @@ export class ProtocolHandler implements Dispatcher<ParserEvent> {
}

async _doDial(srv: Server): Promise<void> {
const { resolve } = this.options;
const alts = await srv.resolve({
fn: getResolveFn(),
debug: this.options.debug,
randomize: !this.options.noRandomize,
resolve
});

let lastErr: Error | null = null;
Expand Down
5 changes: 3 additions & 2 deletions nats-base-client/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,13 @@ export class ServerImpl implements Server {
fn: DnsResolveFn;
randomize: boolean;
resolve: boolean;
debug?: boolean;
debug: boolean;
}
>,
): Promise<Server[]> {
if (!opts.fn) {
if (!opts.fn || opts.resolve === false) {
// we cannot resolve - transport doesn't support it
// or user opted out
// don't add - to resolves or we get a circ reference
return [this];
}
Expand Down
23 changes: 18 additions & 5 deletions nats-base-client/tests/basics_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
Empty,
ErrorCode,
Feature,
getResolveFn,
headers,
isIP,
JSONCodec,
Expand Down Expand Up @@ -1425,10 +1424,24 @@ Deno.test("basics - respond message", async () => {
});


Deno.test("basics - noResolve", async () => {
const { ns, nc } = await _setup(connect,{}, { noResolve: true });
assertEquals(getResolveFn(), undefined);
await cleanup(ns, nc);
Deno.test("basics - resolve", async () => {
const token = Deno.env.get("NGS_CI_USER");
if (token === undefined) {
disabled(
`skipping: NGS_CI_USER is not available in the environment`,
);
return;
}

const nci = await connect({
servers: "connect.ngs.global",
authenticator: jwtAuthenticator(token),
resolve: false,
}) as NatsConnectionImpl;

const srv = nci.protocol.servers.getCurrentServer();
assertEquals(srv.resolves, undefined);
await nci.close();
});

class MM implements Msg {
Expand Down

0 comments on commit 207ae85

Please sign in to comment.