Skip to content

Commit

Permalink
[FIX] fixed an issue affecting cluster updates when using ws:// proto…
Browse files Browse the repository at this point in the history
…col that updated gossiped servers as wss:// (#227)

[BUMP] version, nbc to 1.19.0, and ci dependencies
  • Loading branch information
aricart committed Dec 5, 2023
1 parent 4b84dc3 commit 4e5d759
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/natsws.yml
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
node-version: [21.x]
deno-version: [1.36.4]
deno-version: [1.38.3]
environment: CI
runs-on: ubuntu-latest
steps:
Expand All @@ -31,7 +31,7 @@ jobs:
with:
deno-version: ${{ matrix.deno-version }}
- name: Set NATS Server Version
run: echo "NATS_VERSION=v2.10.4" >> $GITHUB_ENV
run: echo "NATS_VERSION=v2.10.6" >> $GITHUB_ENV
- name: Set CA
run: echo "NODE_EXTRA_CA_CERTS=/home/runner/work/nats.ws/nats.ws/test/certs/ca.crt" >> $GITHUB_ENV
- name: Get nats-server
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/npm.yml
Expand Up @@ -8,8 +8,8 @@ jobs:
test:
strategy:
matrix:
node-version: [20.x]
deno-version: [1.36.4]
node-version: [21.x]
deno-version: [1.38.3]

runs-on: ubuntu-latest
permissions:
Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "nats.ws",
"version": "1.19.1",
"version": "1.20.0",
"description": "WebSocket NATS client",
"main": "./cjs/nats.js",
"module": "./esm/nats.js",
Expand Down Expand Up @@ -52,13 +52,13 @@
"nkeys.js": "1.0.5"
},
"devDependencies": {
"@types/node": "^20.6.x",
"@types/node": "^20.10.x",
"ava": "^5.3.x",
"minimist": "^1.2.8",
"nats-jwt": "^0.0.5",
"nats-jwt": "^0.0.7",
"shx": "^0.3.3",
"tslint": "^6.1.3",
"typescript": "^5.2.x",
"typescript": "^5.3.x",
"web-streams-polyfill": "^3.2.1",
"websocket": "^1.0.34",
"nyc": "^15.1.0"
Expand Down
26 changes: 22 additions & 4 deletions src/connect.ts
Expand Up @@ -19,17 +19,29 @@ import {
setTransportFactory,
Transport,
TransportFactory,
} from "https://raw.githubusercontent.com/nats-io/nats.deno/v1.18.0/nats-base-client/internal_mod.ts";
} from "https://raw.githubusercontent.com/nats-io/nats.deno/v1.19.0/nats-base-client/internal_mod.ts";

import { WsTransport } from "./ws_transport.ts";

export function wsUrlParseFn(u: string): string {
export function wsUrlParseFn(u: string, encrypted?: boolean): string {
const ut = /^(.*:\/\/)(.*)/;
if (!ut.test(u)) {
u = `https://${u}`;
// if we have no hint to encrypted and no protocol, assume encrypted
// else we fix the url from the update to match
if (typeof encrypted === "boolean") {
u = `${encrypted === true ? "https" : "http"}://${u}`;
} else {
u = `https://${u}`;
}
}
let url = new URL(u);
const srcProto = url.protocol.toLowerCase();
if (srcProto === "ws:") {
encrypted = false;
}
if (srcProto === "wss:") {
encrypted = true;
}
if (srcProto !== "https:" && srcProto !== "http") {
u = u.replace(/^(.*:\/\/)(.*)/gm, "$2");
url = new URL(`http://${u}`);
Expand All @@ -48,10 +60,16 @@ export function wsUrlParseFn(u: string): string {
port = url.port || "80";
protocol = "ws:";
break;
default:
case "https:":
case "wss:":
case "tls:":
port = url.port || "443";
protocol = "wss:";
break;
default:
port = url.port || encrypted === true ? "443" : "80";
protocol = encrypted === true ? "wss:" : "ws:";
break;
}
return `${protocol}//${host}:${port}${path}${search}`;
}
Expand Down
4 changes: 2 additions & 2 deletions src/mod.ts
Expand Up @@ -12,6 +12,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from "https://raw.githubusercontent.com/nats-io/nats.deno/v1.18.0/nats-base-client/mod.ts";
export * from "https://raw.githubusercontent.com/nats-io/nats.deno/v1.18.0/jetstream/mod.ts";
export * from "https://raw.githubusercontent.com/nats-io/nats.deno/v1.19.0/nats-base-client/mod.ts";
export * from "https://raw.githubusercontent.com/nats-io/nats.deno/v1.19.0/jetstream/mod.ts";
export { connect } from "./connect.ts";
2 changes: 1 addition & 1 deletion src/nats-base-client.ts
Expand Up @@ -13,4 +13,4 @@
* limitations under the License.
*/
// this import here to drive the build system
export * from "https://raw.githubusercontent.com/nats-io/nats.deno/v1.18.0/nats-base-client/internal_mod.ts";
export * from "https://raw.githubusercontent.com/nats-io/nats.deno/v1.19.0/nats-base-client/internal_mod.ts";
32 changes: 16 additions & 16 deletions src/ws_transport.ts
Expand Up @@ -19,7 +19,7 @@ import type {
Server,
ServerInfo,
Transport,
} from "https://raw.githubusercontent.com/nats-io/nats.deno/v1.18.0/nats-base-client/internal_mod.ts";
} from "https://raw.githubusercontent.com/nats-io/nats.deno/v1.19.0/nats-base-client/internal_mod.ts";
import {
checkOptions,
DataBuffer,
Expand All @@ -30,9 +30,9 @@ import {
INFO,
NatsError,
render,
} from "https://raw.githubusercontent.com/nats-io/nats.deno/v1.18.0/nats-base-client/internal_mod.ts";
} from "https://raw.githubusercontent.com/nats-io/nats.deno/v1.19.0/nats-base-client/internal_mod.ts";

const VERSION = "1.19.1";
const VERSION = "1.20.0";
const LANG = "nats.ws";

export type WsSocketFactory = (u: string, opts: ConnectionOptions) => Promise<{
Expand Down Expand Up @@ -102,14 +102,14 @@ export class WsTransport implements Transport {
this.socket.binaryType = "arraybuffer";

this.socket.onopen = () => {
if(this.isDiscarded()) {
if (this.isDiscarded()) {
return;
}
// we don't do anything here...
};

this.socket.onmessage = (me: MessageEvent) => {
if(this.isDiscarded()) {
if (this.isDiscarded()) {
return;
}
this.yields.push(new Uint8Array(me.data));
Expand Down Expand Up @@ -144,7 +144,7 @@ export class WsTransport implements Transport {

// @ts-ignore: CloseEvent is provided in browsers
this.socket.onclose = (evt: CloseEvent) => {
if(this.isDiscarded()) {
if (this.isDiscarded()) {
return;
}
this.socketClosed = true;
Expand All @@ -158,7 +158,7 @@ export class WsTransport implements Transport {

// @ts-ignore: signature can be any
this.socket.onerror = (e: ErrorEvent | Event): void => {
if(this.isDiscarded()) {
if (this.isDiscarded()) {
return;
}
const evt = e as ErrorEvent;
Expand All @@ -181,8 +181,8 @@ export class WsTransport implements Transport {
}

private async _closed(err?: Error, internal = true): Promise<void> {
if(this.isDiscarded()) {
return
if (this.isDiscarded()) {
return;
}
if (!this.connected) return;
if (this.done) return;
Expand Down Expand Up @@ -215,7 +215,7 @@ export class WsTransport implements Transport {
async *iterate(): AsyncIterableIterator<Uint8Array> {
while (true) {
if (this.isDiscarded()) {
return
return;
}
if (this.yields.length === 0) {
await this.signal;
Expand Down Expand Up @@ -275,10 +275,10 @@ export class WsTransport implements Transport {

// check to see if we are discarded, as the connection
// may not have been closed, we attempt it here as well.
isDiscarded():boolean {
if(this.done) {
isDiscarded(): boolean {
if (this.done) {
this.discard();
return true
return true;
}
return false;
}
Expand All @@ -288,11 +288,11 @@ export class WsTransport implements Transport {
// Firefox for example, will keep connections going,
// so eventually if it succeeds, the client will have
// an additional transport running. With this
discard () {
discard() {
this.done = true;
try {
this.socket?.close()
} catch(_err) {
this.socket?.close();
} catch (_err) {
// ignored
}
}
Expand Down

0 comments on commit 4e5d759

Please sign in to comment.