Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node, uws): automatically detect binary message type #53

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"listhen": "^1.7.2",
"prettier": "^3.3.3",
"typescript": "^5.5.4",
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.33.0",
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.44.0",
"unbuild": "^2.0.0",
"vitest": "^2.0.0",
"wrangler": "^3.67.1",
Expand Down
9 changes: 5 additions & 4 deletions playground/_index.html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ export default /* html */ `
log("ws", "Connecting to", url, "...");
ws = new WebSocket(url);

ws.addEventListener("message", (event) => {
const { user = "system", message = "" } = event.data.startsWith("{")
? JSON.parse(event.data)
: { message: event.data };
ws.addEventListener("message", async (event) => {
const data = typeof event.data === "string" ? event.data : await event.data.text();
const { user = "system", message = "" } = data.startsWith("{")
? JSON.parse(data)
: { message: data };
log(
user,
typeof message === "string" ? message : JSON.stringify(message),
Expand Down
1 change: 1 addition & 0 deletions playground/_shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function createDemo<T extends Adapter<any, any>>(
open(peer) {
console.log(`[ws] open ${peer}`);
peer.send({ user: "server", message: `Welcome to the server ${peer}!` });
peer.send(new TextEncoder().encode("(binary message works!)"));
peer.subscribe("chat");
peer.publish("chat", { user: "server", message: `${peer} joined!` });
},
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

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

12 changes: 11 additions & 1 deletion src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@ export function toBufferLike(val: any): BufferLike {
return "";
}

if (typeof val === "string") {
const type = typeof val;

if (type === "string") {
return val;
}

if (type === "number" || type === "boolean" || type === "bigint") {
return val.toString();
}

if (type === "function" || type === "symbol") {
return "{}";
}

if (val instanceof Uint8Array || val instanceof ArrayBuffer) {
return val;
}
Expand Down
20 changes: 14 additions & 6 deletions src/adapters/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,29 @@ class NodePeer extends Peer<{
return this.ctx.node.ws.readyState;
}

send(message: any, options?: { compress?: boolean; binary?: boolean }) {
this.ctx.node.ws.send(toBufferLike(message), {
send(message: any, options?: { compress?: boolean }) {
const data = toBufferLike(message);
const isBinary = typeof data !== "string";
this.ctx.node.ws.send(data, {
compress: options?.compress,
binary: options?.binary,
binary: isBinary,
...options,
});
return 0;
}

publish(topic: string, message: any): void {
message = toBufferLike(message);
publish(topic: string, message: any, options?: { compress?: boolean }): void {
const data = toBufferLike(message);
const isBinary = typeof data !== "string";
const sendOptions = {
compress: options?.compress,
binary: isBinary,
...options,
};
for (const client of this.ctx.node.server.clients) {
const peer = (client as WebSocketT & { _peer?: NodePeer })._peer;
if (peer && peer !== this && peer._subscriptions.has(topic)) {
peer.send(message);
peer.ctx.node.ws.send(data, sendOptions);
}
}
}
Expand Down
20 changes: 8 additions & 12 deletions src/adapters/uws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,24 +174,20 @@ class UWSPeer extends Peer<{
return this._headers;
}

send(message: any, options?: { compress?: boolean; binary?: boolean }) {
return this.ctx.uws.ws.send(
toBufferLike(message),
options?.binary,
options?.compress,
);
send(message: any, options?: { compress?: boolean }) {
const data = toBufferLike(message);
const isBinary = typeof data !== "string";
return this.ctx.uws.ws.send(data, isBinary, options?.compress);
}

subscribe(topic: string): void {
this.ctx.uws.ws.subscribe(topic);
}

publish(
topic: string,
message: string,
options?: { compress?: boolean; binary?: boolean },
) {
this.ctx.uws.ws.publish(topic, message, options?.binary, options?.compress);
publish(topic: string, message: string, options?: { compress?: boolean }) {
const data = toBufferLike(message);
const isBinary = typeof data !== "string";
this.ctx.uws.ws.publish(topic, data, isBinary, options?.compress);
return 0;
}
}
Expand Down
Loading