Skip to content

Commit

Permalink
Merge pull request #704 from nats-io/easier-resolve
Browse files Browse the repository at this point in the history
[FIX] simpler way to noResolve
  • Loading branch information
aricart committed Jun 13, 2024
2 parents d657906 + 3f08c9f commit 6d63b5c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
17 changes: 13 additions & 4 deletions nats-base-client/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,14 @@ 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 @@ -1404,10 +1411,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.
* 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.
*/
noResolve?: boolean;
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 @@ -135,6 +135,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 @@ -594,10 +594,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 @@ -138,12 +138,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
2 changes: 1 addition & 1 deletion src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function connect(opts: ConnectionOptions = {}): Promise<NatsConnection> {
factory: (): Transport => {
return new DenoTransport();
},
dnsResolveFn: opts.noResolve === true ? undefined : denoResolveHost,
dnsResolveFn: denoResolveHost,
} as TransportFactory);

return NatsConnectionImpl.connect(opts);
Expand Down
21 changes: 17 additions & 4 deletions tests/basics_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import {
collect,
deferred,
delay,
getResolveFn,
headers,
isIP,
NatsConnectionImpl,
Expand Down Expand Up @@ -1427,9 +1426,23 @@ Deno.test("basics - respond message", async () => {
});

Deno.test("basics - noResolve", async () => {
const { ns, nc } = await setup({}, { noResolve: true });
assertEquals(getResolveFn(), undefined);
await cleanup(ns, nc);
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 6d63b5c

Please sign in to comment.