Skip to content

Commit

Permalink
feat(core,node,deno): added a check on the connect function that th…
Browse files Browse the repository at this point in the history
…e client wasn't given a `ws/s://` url as these are difficult issues to diagnose.

Signed-off-by: Alberto Ricart <[email protected]>
  • Loading branch information
aricart committed Nov 9, 2024
1 parent 68f2ea5 commit ca64bfd
Show file tree
Hide file tree
Showing 27 changed files with 151 additions and 29 deletions.
2 changes: 1 addition & 1 deletion core/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/nats-core",
"version": "3.0.0-36",
"version": "3.0.0-38",
"exports": {
".": "./src/mod.ts",
"./internal": "./src/internal_mod.ts"
Expand Down
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/nats-core",
"version": "3.0.0-36",
"version": "3.0.0-38",
"files": [
"lib/",
"LICENSE",
Expand Down
1 change: 1 addition & 0 deletions core/src/internal_mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export {
checkUnsupportedOption,
DEFAULT_MAX_RECONNECT_ATTEMPTS,
defaultOptions,
hasWsProtocol,
parseOptions,
} from "./options.ts";
export { RequestOne } from "./request.ts";
Expand Down
1 change: 1 addition & 0 deletions core/src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export {
Empty,
errors,
Events,
hasWsProtocol,
headers,
InvalidArgumentError,
InvalidOperationError,
Expand Down
18 changes: 18 additions & 0 deletions core/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ export function defaultOptions(): ConnectionOptions {
} as ConnectionOptions;
}

export function hasWsProtocol(opts?: ConnectionOptions): boolean {
if (opts) {
let { servers } = opts;
if (typeof servers === "string") {
servers = [servers];
}
if (servers) {
for (let i = 0; i < servers.length; i++) {
const s = servers[i].toLowerCase();
if (s.startsWith("ws://") || s.startsWith("wss://")) {
return true;
}
}
}
}
return false;
}

export function buildAuthenticator(
opts: ConnectionOptions,
): Authenticator {
Expand Down
2 changes: 1 addition & 1 deletion core/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This file is generated - do not edit
export const version = "3.0.0-36";
export const version = "3.0.0-38";
15 changes: 15 additions & 0 deletions core/tests/properties_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
assert,
assertEquals,
assertExists,
assertFalse,
assertMatch,
} from "jsr:@std/assert";

Expand All @@ -35,6 +36,7 @@ import {
} from "../src/internal_mod.ts";

import { NatsServer } from "../../test_helpers/launcher.ts";
import { hasWsProtocol } from "../src/options.ts";

Deno.test("properties - VERSION is semver", async () => {
const ns = await NatsServer.start();
Expand Down Expand Up @@ -192,3 +194,16 @@ Deno.test("properties - multi", () => {
assert(cc.sig.length > 0);
assertExists(cc.nkey);
});

Deno.test("properties - hasWsProtocol", () => {
assertFalse(hasWsProtocol({ servers: "127.0.0.1:4222" }));
assert(hasWsProtocol({ servers: "WS://127.0.0.1:4222" }));
assert(hasWsProtocol({ servers: "ws://127.0.0.1:4222" }));
assert(hasWsProtocol({ servers: "WSS://127.0.0.1:4222" }));
assert(hasWsProtocol({ servers: "ws://127.0.0.1:4222" }));
assert(
hasWsProtocol({
servers: ["nats://127.0.0.1:4222", "ws://127.0.0.1:4222"],
}),
);
});
2 changes: 1 addition & 1 deletion jetstream/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
"test": "deno test -A --parallel --reload --trace-leaks --quiet tests/ --import-map=import_map.json"
},
"imports": {
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-36"
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-38"
}
}
4 changes: 2 additions & 2 deletions jetstream/import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"imports": {
"@nats-io/nkeys": "jsr:@nats-io/[email protected]",
"@nats-io/nuid": "jsr:@nats-io/[email protected]",
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-36",
"@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-36/internal",
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-38",
"@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-38/internal",
"test_helpers": "../test_helpers/mod.ts",
"@std/io": "jsr:@std/[email protected]"
}
Expand Down
2 changes: 1 addition & 1 deletion jetstream/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"description": "jetstream library - this library implements all the base functionality for NATS JetStream for javascript clients",
"dependencies": {
"@nats-io/nats-core": "3.0.0-36"
"@nats-io/nats-core": "3.0.0-38"
},
"devDependencies": {
"@types/node": "^22.7.6",
Expand Down
2 changes: 1 addition & 1 deletion kv/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"test": "deno test -A --parallel --reload --quiet tests/ --import-map=import_map.json"
},
"imports": {
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-36",
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-38",
"@nats-io/jetstream": "jsr:@nats-io/jetstream@~3.0.0-25"
}
}
4 changes: 2 additions & 2 deletions kv/import_map.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"imports": {
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-36",
"@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-36/internal",
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-38",
"@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-38/internal",
"@nats-io/jetstream": "jsr:@nats-io/jetstream@~3.0.0-25",
"@nats-io/jetstream/internal": "jsr:@nats-io/jetstream@~3.0.0-25/internal",
"test_helpers": "../test_helpers/mod.ts",
Expand Down
2 changes: 1 addition & 1 deletion kv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"description": "kv library - this library implements all the base functionality for NATS KV javascript clients",
"dependencies": {
"@nats-io/jetstream": "3.0.0-25",
"@nats-io/nats-core": "3.0.0-36"
"@nats-io/nats-core": "3.0.0-38"
},
"devDependencies": {
"@types/node": "^22.7.6",
Expand Down
2 changes: 1 addition & 1 deletion obj/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"test": "deno test -A --parallel --reload --quiet tests/ --import-map=import_map.json"
},
"imports": {
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-36",
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-38",
"@nats-io/jetstream": "jsr:@nats-io/jetstream@~3.0.0-25"
}
}
4 changes: 2 additions & 2 deletions obj/import_map.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"imports": {
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-36",
"@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-36/internal",
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-38",
"@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-38/internal",
"@nats-io/jetstream": "jsr:@nats-io/jetstream@~3.0.0-25",
"@nats-io/jetstream/internal": "jsr:@nats-io/jetstream@~3.0.0-25/internal",
"test_helpers": "../test_helpers/mod.ts",
Expand Down
2 changes: 1 addition & 1 deletion obj/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"description": "obj library - this library implements all the base functionality for NATS objectstore for javascript clients",
"dependencies": {
"@nats-io/jetstream": "3.0.0-25",
"@nats-io/nats-core": "3.0.0-36"
"@nats-io/nats-core": "3.0.0-38"
},
"devDependencies": {
"@types/node": "^22.7.6",
Expand Down
2 changes: 1 addition & 1 deletion services/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
"test": "deno test -A --parallel --reload --quiet tests/ --import-map=import_map.json"
},
"imports": {
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-36"
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-38"
}
}
4 changes: 2 additions & 2 deletions services/import_map.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"imports": {
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-36",
"@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-36/internal",
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-38",
"@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-38/internal",
"test_helpers": "../test_helpers/mod.ts",
"@nats-io/nkeys": "jsr:@nats-io/[email protected]",
"@nats-io/nuid": "jsr:@nats-io/[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"description": "services library - this library implements all the base functionality for NATS services for javascript clients",
"dependencies": {
"@nats-io/nats-core": "3.0.0-36"
"@nats-io/nats-core": "3.0.0-38"
},
"devDependencies": {
"@types/node": "^22.7.6",
Expand Down
4 changes: 2 additions & 2 deletions transport-deno/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/transport-deno",
"version": "3.0.0-11",
"version": "3.0.0-13",
"exports": {
".": "./src/mod.ts"
},
Expand All @@ -20,7 +20,7 @@
},
"imports": {
"@std/io": "jsr:@std/[email protected]",
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-36",
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-38",
"@nats-io/nkeys": "jsr:@nats-io/[email protected]",
"@nats-io/nuid": "jsr:@nats-io/[email protected]"
}
Expand Down
10 changes: 10 additions & 0 deletions transport-deno/src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@ import type {
} from "@nats-io/nats-core/internal";

import {
errors,
hasWsProtocol,
NatsConnectionImpl,
setTransportFactory,
} from "@nats-io/nats-core/internal";

export function connect(opts: ConnectionOptions = {}): Promise<NatsConnection> {
if (hasWsProtocol(opts)) {
return Promise.reject(
errors.InvalidArgumentError.format(
`servers`,
`deno client doesn't support websockets, use the 'wsconnect' function instead`,
),
);
}
setTransportFactory({
factory: (): Transport => {
return new DenoTransport();
Expand Down
2 changes: 1 addition & 1 deletion transport-deno/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This file is generated - do not edit
export const version = "3.0.0-11";
export const version = "3.0.0-13";
36 changes: 35 additions & 1 deletion transport-deno/tests/basics_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
import { connect, createInbox, wsconnect } from "../src/mod.ts";
import { assertEquals } from "jsr:@std/assert";
import { assertEquals, assertRejects } from "jsr:@std/assert";

Deno.test("basics", () => {
assertEquals(typeof connect, "function");
assertEquals(typeof wsconnect, "function");
assertEquals(typeof createInbox, "function");
});

Deno.test("basics - ws urls fail", async () => {
await assertRejects(
() => {
return connect({ servers: ["ws://localhost:4222"] });
},
Error,
"'servers' deno client doesn't support websockets, use the 'wsconnect' function instead",
);

await assertRejects(
() => {
return connect({ servers: "ws://localhost:4222" });
},
Error,
"'servers' deno client doesn't support websockets, use the 'wsconnect' function instead",
);

await assertRejects(
() => {
return connect({ servers: ["wss://localhost:4222"] });
},
Error,
"'servers' deno client doesn't support websockets, use the 'wsconnect' function instead",
);

await assertRejects(
() => {
return connect({ servers: "wss://localhost:4222" });
},
Error,
"'servers' deno client doesn't support websockets, use the 'wsconnect' function instead",
);
});
10 changes: 5 additions & 5 deletions transport-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/transport-node",
"version": "3.0.0-22",
"version": "3.0.0-24",
"description": "Node.js client for NATS, a lightweight, high-performance cloud native messaging system",
"keywords": [
"nats",
Expand Down Expand Up @@ -54,7 +54,7 @@
"node": ">= 18.0.0"
},
"dependencies": {
"@nats-io/nats-core": "3.0.0-36",
"@nats-io/nats-core": "3.0.0-38",
"@nats-io/nkeys": "1.2.0-7",
"@nats-io/nuid": "2.0.1-2"
},
Expand All @@ -64,8 +64,8 @@
"nats-jwt": "^0.0.9",
"shx": "^0.3.3",
"typescript": "5.6.3",
"@nats-io/jetstream": "3.0.0-24",
"@nats-io/kv": "3.0.0-18",
"@nats-io/obj": "3.0.0-19"
"@nats-io/jetstream": "3.0.0-25",
"@nats-io/kv": "3.0.0-19",
"@nats-io/obj": "3.0.0-20"
}
}
10 changes: 10 additions & 0 deletions transport-node/src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ import {
TransportFactory,
} from "./nats-base-client";

import { errors, hasWsProtocol } from "./nats-base-client";

export function connect(opts: ConnectionOptions = {}): Promise<NatsConnection> {
if (hasWsProtocol(opts)) {
return Promise.reject(
errors.InvalidArgumentError.format(
`servers`,
`node client doesn't support websockets, use the 'wsconnect' function instead`,
),
);
}
setTransportFactory({
factory: (): Transport => {
return new NodeTransport();
Expand Down
2 changes: 1 addition & 1 deletion transport-node/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This file is generated - do not edit
export const version = "3.0.0-22";
export const version = "3.0.0-24";
33 changes: 33 additions & 0 deletions transport-node/tests/basics_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,37 @@ describe(
await ns.stop();
});
},
it("basics - ws urls fail", async () => {
await assert.rejects(
() => {
return connect({ servers: ["ws://localhost:4222"] });
},
Error,
"'servers' deno client doesn't support websockets, use the 'wsconnect' function instead",
);

await assert.rejects(
() => {
return connect({ servers: "ws://localhost:4222" });
},
Error,
"'servers' deno client doesn't support websockets, use the 'wsconnect' function instead",
);

await assert.rejects(
() => {
return connect({ servers: ["wss://localhost:4222"] });
},
Error,
"'servers' deno client doesn't support websockets, use the 'wsconnect' function instead",
);

await assert.rejects(
() => {
return connect({ servers: "wss://localhost:4222" });
},
Error,
"'servers' deno client doesn't support websockets, use the 'wsconnect' function instead",
);
}),
);

0 comments on commit ca64bfd

Please sign in to comment.