Skip to content

Commit 76e22f7

Browse files
committed
Replace socket.isSecure with socket.secure
This helps in case the socket is proxied (eg WebWorker)
1 parent f9eba3d commit 76e22f7

File tree

9 files changed

+48
-38
lines changed

9 files changed

+48
-38
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
It aims to run everywhere JavaScript runs and make use of the best network transport available.
2323

24-
xmpp.js is known to be used with Node.js, browsers, React Native, GJS and Duktape.
24+
xmpp.js is known to be used with Node.js, browsers, WebWorker, React Native, Bun, GJS and Duktape.
2525

2626
### reliable
2727

packages/connection-tcp/Socket.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Socket as TCPSocket } from "net";
22

33
export default class Socket extends TCPSocket {
4-
isSecure() {
5-
return false;
6-
}
4+
secure = false;
75
}

packages/connection-tcp/test/Socket.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import net from "node:net";
22
import Socket from "../Socket.js";
33

4-
test("isSecure()", () => {
4+
test("secure", () => {
55
const socket = new Socket();
6-
expect(socket.isSecure()).toBe(false);
6+
expect(socket.secure).toBe(false);
77
});
88

99
test("instance of net.Socket", () => {

packages/connection/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Connection extends EventEmitter {
2323
}
2424

2525
isSecure() {
26-
return !!this.socket?.isSecure();
26+
return this.socket?.secure === true;
2727
}
2828

2929
async _streamError(condition, children) {

packages/connection/test/isSecure.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ test("isSecure()", () => {
66
conn.socket = null;
77
expect(conn.isSecure()).toBe(false);
88

9-
conn.socket = { isSecure: () => false };
9+
conn.socket = { secure: false };
1010
expect(conn.isSecure()).toBe(false);
1111

12-
conn.socket = { isSecure: () => true };
12+
conn.socket = { secure: true };
1313
expect(conn.isSecure()).toBe(true);
1414
});

packages/test/mockSocket.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import net from "node:net";
22

33
class MockSocket extends net.Socket {
4+
secure = true;
5+
46
write(data, cb) {
57
process.nextTick(() => {
68
cb?.();
79
});
810
}
9-
isSecure() {
10-
return true;
11-
}
1211
}
1312

1413
export default function mockSocket() {

packages/tls/lib/Socket.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ class Socket extends EventEmitter {
55
timeout = null;
66
#listeners = null;
77
socket = null;
8-
9-
isSecure() {
10-
return true;
11-
}
8+
secure = true;
129

1310
connect(...args) {
1411
this._attachSocket(tls.connect(...args));

packages/websocket/lib/Socket.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ import { parseURI } from "@xmpp/connection/lib/util.js";
33

44
const CODE = "ECONNERROR";
55

6+
export function isSecure(url) {
7+
const uri = parseURI(url);
8+
if (uri.protocol === "wss:") return true;
9+
if (["localhost", "127.0.0.1", "::1"].includes(uri.hostname)) return true;
10+
return false;
11+
}
12+
613
export default class Socket extends EventEmitter {
714
#listeners = null;
815
socket = null;
916
url = null;
10-
11-
isSecure() {
12-
if (!this.url) return false;
13-
const uri = parseURI(this.url);
14-
if (uri.protocol === "wss:") return true;
15-
if (["localhost", "127.0.0.1", "::1"].includes(uri.hostname)) return true;
16-
return false;
17-
}
17+
secure = false;
1818

1919
connect(url) {
2020
this.url = url;
21+
this.secure = isSecure(url);
2122
// eslint-disable-next-line n/no-unsupported-features/node-builtins
2223
this._attachSocket(new WebSocket(url, ["xmpp"]));
2324
}
@@ -52,6 +53,7 @@ export default class Socket extends EventEmitter {
5253

5354
_detachSocket() {
5455
this.url = null;
56+
this.secure = false;
5557
this.socket && this.#listeners?.unsubscribe(this.socket);
5658
this.socket = null;
5759
}
@@ -61,12 +63,18 @@ export default class Socket extends EventEmitter {
6163
}
6264

6365
write(data, fn) {
66+
function done(err) {
67+
if (!fn) return;
68+
// eslint-disable-next-line promise/catch-or-return, promise/no-promise-in-callback
69+
Promise.resolve().then(() => fn(err));
70+
}
71+
6472
try {
6573
this.socket.send(data);
6674
} catch (err) {
67-
fn?.(err);
75+
done(err);
6876
return;
6977
}
70-
fn?.();
78+
done();
7179
}
7280
}

packages/websocket/test/Socket.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1+
import { EventEmitter } from "@xmpp/events";
12
import Socket from "../lib/Socket.js";
23

3-
test("isSecure", () => {
4+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
5+
globalThis.WebSocket = EventEmitter;
6+
7+
test("secure", () => {
48
const socket = new Socket();
5-
expect(socket.isSecure()).toBe(false);
69

7-
socket.url = "ws://example.com/foo";
8-
expect(socket.isSecure()).toBe(false);
10+
expect(socket.secure).toBe(false);
11+
12+
socket.connect("ws://example.com/foo");
13+
expect(socket.secure).toBe(false);
14+
15+
socket.connect("ws://localhost/foo");
16+
expect(socket.secure).toBe(true);
917

10-
socket.url = "ws://localhost/foo";
11-
expect(socket.isSecure()).toBe(true);
18+
socket.connect("ws://127.0.0.1/foo");
19+
expect(socket.secure).toBe(true);
1220

13-
socket.url = "ws://127.0.0.1/foo";
14-
expect(socket.isSecure()).toBe(true);
21+
socket.connect("ws://[::1]/foo");
22+
expect(socket.secure).toBe(true);
1523

16-
socket.url = "ws://[::1]/foo";
17-
expect(socket.isSecure()).toBe(true);
24+
socket.connect("wss://example.com/foo");
25+
expect(socket.secure).toBe(true);
1826

19-
socket.url = "wss://example.com/foo";
20-
expect(socket.isSecure()).toBe(true);
27+
socket.socket.emit("close", { wasClean: Math.random > 0.5 });
28+
expect(socket.secure).toBe(false);
2129
});

0 commit comments

Comments
 (0)