Skip to content

Commit

Permalink
- [BREAKING] removed url connection option. Instead provide `server…
Browse files Browse the repository at this point in the history
…s` - this option can be a string or an array of strings. (#21)
  • Loading branch information
aricart authored Aug 6, 2020
1 parent 7b74c22 commit c9b1426
Show file tree
Hide file tree
Showing 35 changed files with 137 additions and 137 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ import { connect, NatsConnection } from "src/mod.ts";
// common options that you could pass look like:
const localhostAtStandardPort = {};
const localPort = { port: 4222 };
const hostAtStdPort = { url: "demo.nats.io" };
const hostPort = { url: "demo.nats.io:4222" };
const hostAtStdPort = { servers: "demo.nats.io" };
const hostPort = { servers: "demo.nats.io:4222" };

// let's try to connect to all the above, some may fail
const dials: Promise<NatsConnection>[] = [];
Expand Down Expand Up @@ -115,7 +115,7 @@ of your data as necessary.
import { connect, StringCodec } from "../../src/mod.ts";

// to create a connection to a nats-server:
const nc = await connect({ url: "demo.nats.io:4222" });
const nc = await connect({ servers: "demo.nats.io:4222" });

// create a codec
const sc = StringCodec();
Expand Down Expand Up @@ -150,7 +150,7 @@ getting the messages you unsubscribe.
import { connect, JSONCodec } from "../../src/mod.ts";

// to create a connection to a nats-server:
const nc = await connect({ url: "demo.nats.io" });
const nc = await connect({ servers: "demo.nats.io" });

// create a codec
const jc = JSONCodec();
Expand Down Expand Up @@ -183,7 +183,7 @@ will get a chance at processing the message.

```javascript
import { connect, StringCodec, Subscription } from "../../src/mod.ts";
const nc = await connect({ url: "demo.nats.io:4222" });
const nc = await connect({ servers: "demo.nats.io:4222" });
const sc = StringCodec();

// subscriptions can have wildcard subjects
Expand Down Expand Up @@ -230,7 +230,7 @@ not only to provide a service, but also to control the service.
import { connect, StringCodec, Subscription } from "../../src/mod.ts";

// create a connection
const nc = await connect({ url: "demo.nats.io" });
const nc = await connect({ servers: "demo.nats.io" });

// create a codec
const sc = StringCodec();
Expand Down Expand Up @@ -305,7 +305,7 @@ async function adminHandler(sub: Subscription) {
import { connect, StringCodec } from "../../src/mod.ts";

// create a connection
const nc = await connect({ url: "demo.nats.io:4222" });
const nc = await connect({ servers: "demo.nats.io:4222" });

// create an encoder
const sc = StringCodec();
Expand Down Expand Up @@ -348,7 +348,7 @@ async function createService(
for (let i = 1; i <= count; i++) {
const n = queue ? `${name}-${i}` : name;
const nc = await connect(
{ url: "demo.nats.io:4222", name: `${n}` },
{ servers: "demo.nats.io:4222", name: `${n}` },
);
nc.closed()
.then((err) => {
Expand Down Expand Up @@ -409,15 +409,15 @@ await Promise.all(a);
// `token` options in the NatsConnectionOptions
import { connect } from "src/mod.ts";

const nc1 = await connect({url: "nats://127.0.0.1:4222", user: "jenny", pass: "867-5309"});
const nc1 = await connect({servers: "127.0.0.1:4222", user: "jenny", pass: "867-5309"});
const nc2 = await connect({port: 4222, token: "t0pS3cret!"});
```

### Flush
```javascript
// flush sends a PING request to the server and returns a promise
// when the server responds with a PONG. The flush guarantees that
// things you published have been delivered to the server. Typically
// flush sends a PING request to the servers and returns a promise
// when the servers responds with a PONG. The flush guarantees that
// things you published have been delivered to the servers. Typically
// it is not necessary to use flush, but on tests it can be invaluable.
nc.publish('foo');
nc.publish('bar');
Expand Down
2 changes: 1 addition & 1 deletion doc/snippets/autounsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { connect, StringCodec, Subscription } from "../../src/mod.ts";

// create a connection
const nc = await connect({ url: "demo.nats.io:4222" });
const nc = await connect({ servers: "demo.nats.io:4222" });

// create a simple subscriber that listens for only one message
// and then auto unsubscribes, ending the async iterator
Expand Down
2 changes: 1 addition & 1 deletion doc/snippets/basics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { connect, StringCodec } from "../../src/mod.ts";

// to create a connection to a nats-server:
const nc = await connect({ url: "demo.nats.io:4222" });
const nc = await connect({ servers: "demo.nats.io:4222" });

// create a codec
const sc = StringCodec();
Expand Down
2 changes: 1 addition & 1 deletion doc/snippets/queuegroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function createService(
for (let i = 1; i <= count; i++) {
const n = queue ? `${name}-${i}` : name;
const nc = await connect(
{ url: "demo.nats.io:4222", name: `${n}` },
{ servers: "demo.nats.io:4222", name: `${n}` },
);
nc.closed()
.then((err) => {
Expand Down
2 changes: 1 addition & 1 deletion doc/snippets/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { connect, StringCodec, Subscription } from "../../src/mod.ts";

// create a connection
const nc = await connect({ url: "demo.nats.io" });
const nc = await connect({ servers: "demo.nats.io" });

// create a codec
const sc = StringCodec();
Expand Down
2 changes: 1 addition & 1 deletion doc/snippets/service_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { connect, StringCodec } from "../../src/mod.ts";

// create a connection
const nc = await connect({ url: "demo.nats.io:4222" });
const nc = await connect({ servers: "demo.nats.io:4222" });

// create an encoder
const sc = StringCodec();
Expand Down
2 changes: 1 addition & 1 deletion doc/snippets/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { connect, JSONCodec } from "../../src/mod.ts";

// to create a connection to a nats-server:
const nc = await connect({ url: "demo.nats.io" });
const nc = await connect({ servers: "demo.nats.io" });

// create a codec
const jc = JSONCodec();
Expand Down
2 changes: 1 addition & 1 deletion doc/snippets/sub_timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { connect, ErrorCode } from "../../src/mod.ts";

// to create a connection to a nats-server:
const nc = await connect({ url: "demo.nats.io:4222" });
const nc = await connect({ servers: "demo.nats.io:4222" });

// create subscription with a timeout, if no message arrives
// within the timeout, the subscription throws a timeout error
Expand Down
2 changes: 1 addition & 1 deletion doc/snippets/wildcard_subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

import { connect, StringCodec, Subscription } from "../../src/mod.ts";
const nc = await connect({ url: "demo.nats.io:4222" });
const nc = await connect({ servers: "demo.nats.io:4222" });
const sc = StringCodec();

// subscriptions can have wildcard subjects
Expand Down
4 changes: 2 additions & 2 deletions examples/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { connect, Nuid } from "../src/mod.ts";
const defaults = {
s: "nats://127.0.0.1:4222",
s: "127.0.0.1:4222",
c: 1000000,
};

Expand All @@ -30,7 +30,7 @@ const server = String(argv.server);
const count = parseInt(String(argv.count));
const subj = String(argv.subj) || new Nuid().next();

const nc = await connect({ url: server, debug: argv.debug });
const nc = await connect({ servers: server, debug: argv.debug });
const start = Date.now();

if (argv.req) {
Expand Down
4 changes: 2 additions & 2 deletions examples/nats-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ const argv = parse(
"s": ["server"],
},
default: {
s: "nats://127.0.0.1:4222",
s: "127.0.0.1:4222",
},
},
);

const opts = { url: argv.s } as ConnectionOptions;
const opts = { servers: argv.s } as ConnectionOptions;

const nc = await connect(opts);
(async () => {
Expand Down
4 changes: 2 additions & 2 deletions examples/nats-pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const argv = parse(
"i": ["interval"],
},
default: {
s: "nats://127.0.0.1:4222",
s: "127.0.0.1:4222",
c: 1,
i: 0,
},
Expand All @@ -23,7 +23,7 @@ const argv = parse(
},
);

const copts = { url: argv.s } as ConnectionOptions;
const copts = { servers: argv.s } as ConnectionOptions;
const subject = String(argv._[0]);
const payload = argv._[1] || "";
const count = (argv.c == -1 ? Number.MAX_SAFE_INTEGER : argv.c) || 1;
Expand Down
4 changes: 2 additions & 2 deletions examples/nats-rep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ const argv = parse(
"e": ["echo"],
},
default: {
s: "nats://127.0.0.1:4222",
s: "127.0.0.1:4222",
q: "",
},
boolean: ["echo", "headers", "debug"],
string: ["server", "queue"],
},
);

const opts = { url: argv.s } as ConnectionOptions;
const opts = { servers: argv.s } as ConnectionOptions;
const subject = argv._[0] ? String(argv._[0]) : "";
const payload = argv._[1] || "";

Expand Down
4 changes: 2 additions & 2 deletions examples/nats-req.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const argv = parse(
"t": ["timeout"],
},
default: {
s: "nats://127.0.0.1:4222",
s: "127.0.0.1:4222",
c: 1,
i: 0,
t: 1000,
Expand All @@ -24,7 +24,7 @@ const argv = parse(
},
);

const opts = { url: argv.s } as ConnectionOptions;
const opts = { servers: argv.s } as ConnectionOptions;
const subject = String(argv._[0]);
const payload = argv._[1] || "";
const count = (argv.c == -1 ? Number.MAX_SAFE_INTEGER : argv.c) || 1;
Expand Down
4 changes: 2 additions & 2 deletions examples/nats-sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ const argv = parse(
"q": ["queue"],
},
default: {
s: "nats://127.0.0.1:4222",
s: "127.0.0.1:4222",
q: "",
},
boolean: ["headers", "debug"],
string: ["server", "queue"],
},
);

const opts = { url: argv.s } as ConnectionOptions;
const opts = { servers: argv.s } as ConnectionOptions;
const subject = argv._[0] ? String(argv._[0]) : ">";

if (argv.debug) {
Expand Down
14 changes: 10 additions & 4 deletions nats-base-client/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import {
DEFAULT_MAX_PING_OUT,
DEFAULT_MAX_RECONNECT_ATTEMPTS,
DEFAULT_PING_INTERVAL,
DEFAULT_PRE,
DEFAULT_HOST,
DEFAULT_RECONNECT_TIME_WAIT,
DEFAULT_URI,
DEFAULT_HOSTPORT,
} from "./types.ts";
import { buildAuthenticator } from "./authenticator.ts";

Expand All @@ -46,9 +46,15 @@ export function defaultOptions(): ConnectionOptions {
}

export function parseOptions(opts?: ConnectionOptions): ConnectionOptions {
opts = opts || { url: DEFAULT_URI };
opts = opts || { servers: [DEFAULT_HOSTPORT] };
if (opts.port) {
opts.url = DEFAULT_PRE + opts.port;
opts.servers = [`${DEFAULT_HOST}:${opts.port}`];
}
if (typeof opts.servers === "string") {
opts.servers = [opts.servers];
}
if (opts.servers && opts.servers.length === 0) {
opts.servers = [DEFAULT_HOSTPORT];
}
const options = extend(defaultOptions(), opts);

Expand Down
4 changes: 2 additions & 2 deletions nats-base-client/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ export class ProtocolHandler {
this.pongs = [];
this.servers = new Servers(
!options.noRandomize,
this.options.servers,
this.options.url,
//@ts-ignore
options.servers,
);
this.closed = deferred<Error | void>();

Expand Down
19 changes: 7 additions & 12 deletions nats-base-client/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import {
DEFAULT_PORT,
DEFAULT_URI,
DEFAULT_HOSTPORT,
ServerInfo,
ServersChanged,
} from "./types.ts";
Expand All @@ -34,19 +34,14 @@ export class Server {
gossiped: boolean;

constructor(u: string, gossiped = false) {
// remove any of the standard protocols we are used to seeing
u = u.replace("tls://", "");
u = u.replace("ws://", "");
u = u.replace("wss://", "");
u = u.replace("nats://", "");

// remove any protocol that may have been provided
if (u.match(/^(.*:\/\/)(.*)/m)) {
u = u.replace(/^(.*:\/\/)(.*)/gm, "$2");
}
// in web environments, URL may not be a living standard
// that means that protocols other than HTTP/S are not
// parsable correctly.
if (!/^.*:\/\/.*/.test(u)) {
u = `http://${u}`;
}
let url = new URL(u);
let url = new URL(`http://${u}`);
if (!url.port) {
url.port = `${DEFAULT_PORT}`;
}
Expand Down Expand Up @@ -103,7 +98,7 @@ export class Servers {
}
} else {
if (this.servers.length === 0) {
this.addServer(DEFAULT_URI, false);
this.addServer(DEFAULT_HOSTPORT, false);
}
}
this.currentServer = this.servers[0];
Expand Down
7 changes: 3 additions & 4 deletions nats-base-client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export const DebugEvents = Object.freeze({
});

export const DEFAULT_PORT = 4222;
export const DEFAULT_PRE = "nats://127.0.0.1:";
export const DEFAULT_URI = DEFAULT_PRE + DEFAULT_PORT;
export const DEFAULT_HOST = "127.0.0.1";
export const DEFAULT_HOSTPORT = `${DEFAULT_HOST}:${DEFAULT_PORT}`;

// DISCONNECT Parameters, 2 sec wait, 10 tries
export const DEFAULT_RECONNECT_TIME_WAIT = 2 * 1000;
Expand Down Expand Up @@ -93,11 +93,10 @@ export interface ConnectionOptions {
reconnectJitter?: number;
reconnectJitterTLS?: number;
reconnectTimeWait?: number;
servers?: Array<string>;
servers?: Array<string> | string;
timeout?: number;
tls?: boolean | TlsOptions;
token?: string;
url?: string;
user?: string;
verbose?: boolean;
waitOnFirstConnect?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion tests/auth_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Deno.test("auth - pub perm", async () => {
});

Deno.test("auth - user and token is rejected", async () => {
connect({ url: "nats://127.0.0.1:4222", user: "derek", token: "foobar" })
connect({ servers: "127.0.0.1:4222", user: "derek", token: "foobar" })
.then(async (nc) => {
await nc.close();
fail("should not have connected");
Expand Down
Loading

0 comments on commit c9b1426

Please sign in to comment.