Skip to content

Commit

Permalink
Merge branch 'main' into websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Jan 9, 2025
2 parents 839c533 + 649ac4a commit e87b552
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 39 deletions.
7 changes: 0 additions & 7 deletions packages/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,3 @@ PLAIN should only be used over secure WebSocket (`wss://)`, direct TLS (`xmpps:`
- ☐ : Optional
- ✗ : Unavailable
- ✔ : Included

## Common issues

<details>
<summary><strong>Unable to resolve module</strong></summary>
<p>If you are using an older React Native version, please require/import <code>@xmpp/client/reat-native</code> instead of <code>@xmpp/client</code>.</p>
</details>
10 changes: 5 additions & 5 deletions packages/websocket/lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ const NS_FRAMING = "urn:ietf:params:xml:ns:xmpp-framing";

class ConnectionWebSocket extends Connection {
send(element, ...args) {
if (!element.attrs.xmlns && super.isStanza(element)) {
element.attrs.xmlns = "jabber:client";
}

element.attrs.xmlns ??= this.NS;
return super.send(element, ...args);
}

async sendMany(elements) {
for (const element of elements) {
await this.send(element);
element.attrs.xmlns ??= this.NS;
element.parent = this.root;
this.socket.write(element.toString());
this.emit("send", element);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/websocket/lib/Socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export default class Socket extends EventEmitter {
try {
this.socket.send(data);
} catch (err) {
fn(err);
fn?.(err);
return;
}
fn();
fn?.();
}
}
49 changes: 24 additions & 25 deletions packages/websocket/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import Socket from "../lib/Socket.js";
import { EventEmitter } from "@xmpp/events";
import xml from "@xmpp/xml";

test("send() adds jabber:client xmlns", () => {
test("send()", () => {
const connection = new ConnectionWebSocket();
connection.write = () => {};
connection.root = xml("root");

const element = xml("presence");

expect(element.attrs.xmlns).toBe(undefined);
expect(element.parent).toBe(null);
connection.send(element);
expect(element.attrs.xmlns).toBe("jabber:client");
expect(element.parent).toBe(connection.root);
});

test("socketParameters()", () => {
Expand All @@ -27,7 +30,7 @@ test("socketParameters()", () => {
expect(params).toBe(undefined);
});

test("DOM WebSocket error", () => {
test("WebSocket error", () => {
const socket = new Socket();
const sock = new EventEmitter();
sock.addEventListener = sock.addListener;
Expand All @@ -44,22 +47,6 @@ test("DOM WebSocket error", () => {
socket.socket.emit("error", evt);
});

test("WS WebSocket error", () => {
const socket = new Socket();
const sock = new EventEmitter();
sock.addEventListener = sock.addListener;
socket._attachSocket(sock);
socket.url = "ws://foobar";
const error = {};
const evt = { error };
socket.on("error", (err) => {
expect(err).toBe(error);
expect(err.event).toBe(evt);
expect(err.url).toBe("ws://foobar");
});
socket.socket.emit("error", evt);
});

test("socket close", () => {
expect.assertions(3);
const socket = new Socket();
Expand All @@ -81,15 +68,27 @@ test("socket close", () => {

test("sendMany", async () => {
const conn = new ConnectionWebSocket();
conn.socket = new Socket();
const spy_write = jest.spyOn(conn.socket, "write");
conn.root = xml("root");

const foo = xml("presence");
const bar = xml("presence");
const elements = [foo, bar];

const foo = xml("foo");
const bar = xml("bar");
for (const element of elements) {
expect(element.attrs.xmlns).toBe(undefined);
expect(element.parent).toBe(null);
}

const spy_send = (conn.send = jest.fn());
conn.sendMany(elements);

await conn.sendMany([foo, bar]);
for (const element of elements) {
expect(element.attrs.xmlns).toBe("jabber:client");
expect(element.parent).toBe(conn.root);
}

expect(spy_send).toHaveBeenCalledWith(foo);
expect(spy_send).toHaveBeenCalledWith(bar);
expect(spy_send).toHaveBeenCalledTimes(2);
expect(spy_write).toHaveBeenCalledWith(foo.toString());
expect(spy_write).toHaveBeenCalledWith(bar.toString());
expect(spy_write).toHaveBeenCalledTimes(2);
});

0 comments on commit e87b552

Please sign in to comment.