Skip to content

Commit

Permalink
[FEAT] added connect option noResolve which disables name resolutio…
Browse files Browse the repository at this point in the history
…n on the client

FIX #625
  • Loading branch information
aricart committed Jun 13, 2024
1 parent 600bce0 commit 71e7f61
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ The following is the list of connection options and default values.
| `noAsyncTraces` | `false` | When `true` the client will not add additional context to errors associated with request operations. Setting this option to `true` will greatly improve performance of request/reply and JetStream publishers. |
| `noEcho` | `false` | Subscriptions receive messages published by the client. Requires server support (1.2.0). If set to true, and the server does not support the feature, an error with code `NO_ECHO_NOT_SUPPORTED` is emitted, and the connection is aborted. Note that it is possible for this error to be emitted on reconnect when the server reconnects to a server that does not support the feature. |
| `noRandomize` | `false` | If set, the order of user-specified servers is randomized. |
| `noResolve` | none | If true, client will not resolve host names. |
| `pass` | | Sets the password for a connection. |
| `pedantic` | `false` | Turns on strict subject format checks. |
| `pingInterval` | `120000` | Number of milliseconds between client-sent pings. |
Expand Down
6 changes: 6 additions & 0 deletions nats-base-client/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,12 @@ export interface ConnectionOptions {
* additional context as to where the operation was started.
*/
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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion nats-base-client/internal_mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export { Nuid, nuid } from "./nuid.ts";
export type { ServiceClient, TypedSubscriptionOptions } from "./types.ts";

export { MsgImpl } from "./msg.ts";
export { setTransportFactory } from "./transport.ts";
export { getResolveFn, setTransportFactory } from "./transport.ts";
export type { Transport, TransportFactory } from "./transport.ts";
export { Connect, INFO, ProtocolHandler } from "./protocol.ts";
export type { Backoff, Deferred, Delay, Perf, Timeout } from "./util.ts";
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: denoResolveHost,
dnsResolveFn: opts.noResolve === true ? undefined : denoResolveHost,
} as TransportFactory);

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

Deno.test("basics - noResolve", async () => {
const { ns, nc } = await setup({}, { noResolve: true });
assertEquals(getResolveFn(), undefined);
await cleanup(ns, nc);
});

class MM implements Msg {
data!: Uint8Array;
sid: number;
Expand Down

0 comments on commit 71e7f61

Please sign in to comment.