Skip to content

Commit

Permalink
Replace socket.isSecure with socket.secure
Browse files Browse the repository at this point in the history
This helps in case the socket is proxied (eg WebWorker)
  • Loading branch information
sonnyp committed Jan 10, 2025
1 parent f9eba3d commit 76e22f7
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions packages/connection-tcp/Socket.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Socket as TCPSocket } from "net";

export default class Socket extends TCPSocket {
isSecure() {
return false;
}
secure = false;
}
4 changes: 2 additions & 2 deletions packages/connection-tcp/test/Socket.js
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Connection extends EventEmitter {
}

isSecure() {
return !!this.socket?.isSecure();
return this.socket?.secure === true;
}

async _streamError(condition, children) {
Expand Down
4 changes: 2 additions & 2 deletions packages/connection/test/isSecure.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
5 changes: 2 additions & 3 deletions packages/test/mockSocket.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
5 changes: 1 addition & 4 deletions packages/tls/lib/Socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
28 changes: 18 additions & 10 deletions packages/websocket/lib/Socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]));
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
}
}
32 changes: 20 additions & 12 deletions packages/websocket/test/Socket.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 76e22f7

Please sign in to comment.