Skip to content

Commit 6d63b5c

Browse files
authored
Merge pull request #704 from nats-io/easier-resolve
[FIX] simpler way to noResolve
2 parents d657906 + 3f08c9f commit 6d63b5c

File tree

6 files changed

+42
-11
lines changed

6 files changed

+42
-11
lines changed

nats-base-client/core.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,14 @@ export interface Server {
340340
tlsName: string;
341341

342342
resolve(
343-
opts: Partial<{ fn: DnsResolveFn; randomize: boolean; debug?: boolean }>,
343+
opts: Partial<
344+
{
345+
fn: DnsResolveFn;
346+
randomize: boolean;
347+
debug?: boolean;
348+
resolve?: boolean;
349+
}
350+
>,
344351
): Promise<Server[]>;
345352
}
346353

@@ -1404,10 +1411,12 @@ export interface ConnectionOptions {
14041411
noAsyncTraces?: boolean;
14051412

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

14131422
/**

nats-base-client/options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ export function parseOptions(opts?: ConnectionOptions): ConnectionOptions {
135135
}
136136
}
137137

138+
// if not set - we set it
139+
if (options.resolve === undefined) {
140+
// set a default based on whether the client can resolve or not
141+
options.resolve = typeof getResolveFn() === "function";
142+
}
143+
138144
if (options.resolve) {
139145
if (typeof getResolveFn() !== "function") {
140146
throw new NatsError(

nats-base-client/protocol.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,12 @@ export class ProtocolHandler implements Dispatcher<ParserEvent> {
594594
}
595595

596596
async _doDial(srv: Server): Promise<void> {
597+
const { resolve } = this.options;
597598
const alts = await srv.resolve({
598599
fn: getResolveFn(),
599600
debug: this.options.debug,
600601
randomize: !this.options.noRandomize,
602+
resolve,
601603
});
602604

603605
let lastErr: Error | null = null;

nats-base-client/servers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,13 @@ export class ServerImpl implements Server {
138138
fn: DnsResolveFn;
139139
randomize: boolean;
140140
resolve: boolean;
141-
debug?: boolean;
141+
debug: boolean;
142142
}
143143
>,
144144
): Promise<Server[]> {
145-
if (!opts.fn) {
145+
if (!opts.fn || opts.resolve === false) {
146146
// we cannot resolve - transport doesn't support it
147+
// or user opted out
147148
// don't add - to resolves or we get a circ reference
148149
return [this];
149150
}

src/connect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function connect(opts: ConnectionOptions = {}): Promise<NatsConnection> {
2727
factory: (): Transport => {
2828
return new DenoTransport();
2929
},
30-
dnsResolveFn: opts.noResolve === true ? undefined : denoResolveHost,
30+
dnsResolveFn: denoResolveHost,
3131
} as TransportFactory);
3232

3333
return NatsConnectionImpl.connect(opts);

tests/basics_test.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import {
4848
collect,
4949
deferred,
5050
delay,
51-
getResolveFn,
5251
headers,
5352
isIP,
5453
NatsConnectionImpl,
@@ -1427,9 +1426,23 @@ Deno.test("basics - respond message", async () => {
14271426
});
14281427

14291428
Deno.test("basics - noResolve", async () => {
1430-
const { ns, nc } = await setup({}, { noResolve: true });
1431-
assertEquals(getResolveFn(), undefined);
1432-
await cleanup(ns, nc);
1429+
const token = Deno.env.get("NGS_CI_USER");
1430+
if (token === undefined) {
1431+
disabled(
1432+
`skipping: NGS_CI_USER is not available in the environment`,
1433+
);
1434+
return;
1435+
}
1436+
1437+
const nci = await connect({
1438+
servers: "connect.ngs.global",
1439+
authenticator: jwtAuthenticator(token),
1440+
resolve: false,
1441+
}) as NatsConnectionImpl;
1442+
1443+
const srv = nci.protocol.servers.getCurrentServer();
1444+
assertEquals(srv.resolves, undefined);
1445+
await nci.close();
14331446
});
14341447

14351448
class MM implements Msg {

0 commit comments

Comments
 (0)