From 76e22f73a22321a1a412ef836cff65eb92ae35f1 Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Fri, 10 Jan 2025 16:18:15 +0100 Subject: [PATCH] Replace socket.isSecure with socket.secure This helps in case the socket is proxied (eg WebWorker) --- README.md | 2 +- packages/connection-tcp/Socket.js | 4 +--- packages/connection-tcp/test/Socket.js | 4 ++-- packages/connection/index.js | 2 +- packages/connection/test/isSecure.js | 4 ++-- packages/test/mockSocket.js | 5 ++-- packages/tls/lib/Socket.js | 5 +--- packages/websocket/lib/Socket.js | 28 ++++++++++++++-------- packages/websocket/test/Socket.js | 32 ++++++++++++++++---------- 9 files changed, 48 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 4bd5dc95..c5d2e95d 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ It aims to run everywhere JavaScript runs and make use of the best network transport available. -xmpp.js is known to be used with Node.js, browsers, React Native, GJS and Duktape. +xmpp.js is known to be used with Node.js, browsers, WebWorker, React Native, Bun, GJS and Duktape. ### reliable diff --git a/packages/connection-tcp/Socket.js b/packages/connection-tcp/Socket.js index 7fd8d457..31171370 100644 --- a/packages/connection-tcp/Socket.js +++ b/packages/connection-tcp/Socket.js @@ -1,7 +1,5 @@ import { Socket as TCPSocket } from "net"; export default class Socket extends TCPSocket { - isSecure() { - return false; - } + secure = false; } diff --git a/packages/connection-tcp/test/Socket.js b/packages/connection-tcp/test/Socket.js index 18177858..f90b8fe1 100644 --- a/packages/connection-tcp/test/Socket.js +++ b/packages/connection-tcp/test/Socket.js @@ -1,9 +1,9 @@ import net from "node:net"; import Socket from "../Socket.js"; -test("isSecure()", () => { +test("secure", () => { const socket = new Socket(); - expect(socket.isSecure()).toBe(false); + expect(socket.secure).toBe(false); }); test("instance of net.Socket", () => { diff --git a/packages/connection/index.js b/packages/connection/index.js index e641b133..5c1cfcbb 100644 --- a/packages/connection/index.js +++ b/packages/connection/index.js @@ -23,7 +23,7 @@ class Connection extends EventEmitter { } isSecure() { - return !!this.socket?.isSecure(); + return this.socket?.secure === true; } async _streamError(condition, children) { diff --git a/packages/connection/test/isSecure.js b/packages/connection/test/isSecure.js index e76571d5..242a58ba 100644 --- a/packages/connection/test/isSecure.js +++ b/packages/connection/test/isSecure.js @@ -6,9 +6,9 @@ test("isSecure()", () => { conn.socket = null; expect(conn.isSecure()).toBe(false); - conn.socket = { isSecure: () => false }; + conn.socket = { secure: false }; expect(conn.isSecure()).toBe(false); - conn.socket = { isSecure: () => true }; + conn.socket = { secure: true }; expect(conn.isSecure()).toBe(true); }); diff --git a/packages/test/mockSocket.js b/packages/test/mockSocket.js index 5bb0c455..8d93a088 100644 --- a/packages/test/mockSocket.js +++ b/packages/test/mockSocket.js @@ -1,14 +1,13 @@ import net from "node:net"; class MockSocket extends net.Socket { + secure = true; + write(data, cb) { process.nextTick(() => { cb?.(); }); } - isSecure() { - return true; - } } export default function mockSocket() { diff --git a/packages/tls/lib/Socket.js b/packages/tls/lib/Socket.js index 84e28ade..f96ac449 100644 --- a/packages/tls/lib/Socket.js +++ b/packages/tls/lib/Socket.js @@ -5,10 +5,7 @@ class Socket extends EventEmitter { timeout = null; #listeners = null; socket = null; - - isSecure() { - return true; - } + secure = true; connect(...args) { this._attachSocket(tls.connect(...args)); diff --git a/packages/websocket/lib/Socket.js b/packages/websocket/lib/Socket.js index 802f196b..84a8ba04 100644 --- a/packages/websocket/lib/Socket.js +++ b/packages/websocket/lib/Socket.js @@ -3,21 +3,22 @@ import { parseURI } from "@xmpp/connection/lib/util.js"; const CODE = "ECONNERROR"; +export function isSecure(url) { + const uri = parseURI(url); + if (uri.protocol === "wss:") return true; + if (["localhost", "127.0.0.1", "::1"].includes(uri.hostname)) return true; + return false; +} + export default class Socket extends EventEmitter { #listeners = null; socket = null; url = null; - - isSecure() { - if (!this.url) return false; - const uri = parseURI(this.url); - if (uri.protocol === "wss:") return true; - if (["localhost", "127.0.0.1", "::1"].includes(uri.hostname)) return true; - return false; - } + secure = false; connect(url) { this.url = url; + this.secure = isSecure(url); // eslint-disable-next-line n/no-unsupported-features/node-builtins this._attachSocket(new WebSocket(url, ["xmpp"])); } @@ -52,6 +53,7 @@ export default class Socket extends EventEmitter { _detachSocket() { this.url = null; + this.secure = false; this.socket && this.#listeners?.unsubscribe(this.socket); this.socket = null; } @@ -61,12 +63,18 @@ export default class Socket extends EventEmitter { } write(data, fn) { + function done(err) { + if (!fn) return; + // eslint-disable-next-line promise/catch-or-return, promise/no-promise-in-callback + Promise.resolve().then(() => fn(err)); + } + try { this.socket.send(data); } catch (err) { - fn?.(err); + done(err); return; } - fn?.(); + done(); } } diff --git a/packages/websocket/test/Socket.js b/packages/websocket/test/Socket.js index 9f8d4c5d..ea8ad7cc 100644 --- a/packages/websocket/test/Socket.js +++ b/packages/websocket/test/Socket.js @@ -1,21 +1,29 @@ +import { EventEmitter } from "@xmpp/events"; import Socket from "../lib/Socket.js"; -test("isSecure", () => { +// eslint-disable-next-line n/no-unsupported-features/node-builtins +globalThis.WebSocket = EventEmitter; + +test("secure", () => { const socket = new Socket(); - expect(socket.isSecure()).toBe(false); - socket.url = "ws://example.com/foo"; - expect(socket.isSecure()).toBe(false); + expect(socket.secure).toBe(false); + + socket.connect("ws://example.com/foo"); + expect(socket.secure).toBe(false); + + socket.connect("ws://localhost/foo"); + expect(socket.secure).toBe(true); - socket.url = "ws://localhost/foo"; - expect(socket.isSecure()).toBe(true); + socket.connect("ws://127.0.0.1/foo"); + expect(socket.secure).toBe(true); - socket.url = "ws://127.0.0.1/foo"; - expect(socket.isSecure()).toBe(true); + socket.connect("ws://[::1]/foo"); + expect(socket.secure).toBe(true); - socket.url = "ws://[::1]/foo"; - expect(socket.isSecure()).toBe(true); + socket.connect("wss://example.com/foo"); + expect(socket.secure).toBe(true); - socket.url = "wss://example.com/foo"; - expect(socket.isSecure()).toBe(true); + socket.socket.emit("close", { wasClean: Math.random > 0.5 }); + expect(socket.secure).toBe(false); });