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(peer): use secure lazy random uuid #64

Merged
merged 1 commit into from
Aug 7, 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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"play:bun": "bun test/fixture/bun.ts",
"play:cf": "wrangler dev --port 3001 -c test/fixture/wrangler.toml",
"play:cf-durable": "wrangler dev --port 3001 -c test/fixture/wrangler-durable.toml",
"play:deno": "deno run -A test/fixture/deno.ts",
"play:deno": "deno run --unstable-byonm -A test/fixture/deno.ts",
"play:node": "jiti test/fixture/node.ts",
"play:sse": "bun test/fixture/sse.ts",
"play:uws": "jiti test/fixture/uws.ts",
Expand All @@ -79,6 +79,9 @@
"resolutions": {
"crossws": "workspace:*"
},
"dependencies": {
"uncrypto": "^0.1.3"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240729.0",
"@deno/types": "^0.0.1",
Expand Down
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

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

13 changes: 8 additions & 5 deletions src/peer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { randomUUID } from "uncrypto";

// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState
type ReadyState = 0 | 1 | 2 | 3;
const ReadyStateMap = {
Expand All @@ -16,17 +18,18 @@ export abstract class Peer<Internal extends AdapterInternal = AdapterInternal> {
protected _internal: Internal;
protected _topics: Set<string>;

private static _idCounter = 0;
private _id: string;
private _id?: string;

constructor(internal: Internal) {
this._id = ++Peer._idCounter + "";
this._topics = new Set();
this._internal = internal;
}

get id(): string {
return this._id.toString();
if (!this._id) {
this._id = randomUUID();
}
return this._id;
}

get addr(): string | undefined {
Expand Down Expand Up @@ -66,7 +69,7 @@ export abstract class Peer<Internal extends AdapterInternal = AdapterInternal> {
}

toString() {
return `#${this.id}`;
return this.id;
}

[Symbol.for("nodejs.util.inspect.custom")]() {
Expand Down
5 changes: 4 additions & 1 deletion test/adapters/deno.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ import { describe } from "vitest";
import { wsTestsExec } from "../_utils";

describe("deno", () => {
wsTestsExec("deno run -A ./deno.ts", { resHeaders: false, adapter: "deno" });
wsTestsExec("deno run --unstable-byonm -A ./deno.ts", {
resHeaders: false,
adapter: "deno",
});
});
3 changes: 2 additions & 1 deletion test/adapters/sse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe("sse", () => {
});
await new Promise((resolve) => ev.addEventListener("open", resolve));
ev.close();
expect(messages).toMatchObject(["Welcome to the server #1!"]);
expect(messages[0]).toMatch(/Welcome to the server \w+/);
expect(messages.length).toBe(1);
});
});
});
4 changes: 2 additions & 2 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function wsTests(

test("connect to websocket", async () => {
const ws = await wsConnect(getURL());
expect(await ws.next()).toBe("Welcome to the server #1!");
expect(await ws.next()).toMatch(/Welcome to the server \w+/);
});

test("send ping", async () => {
Expand All @@ -25,7 +25,7 @@ export function wsTests(
const ws1 = await wsConnect(getURL(), { skip: 1 });
const ws2 = await wsConnect(getURL(), { skip: 1 });
if (opts.pubsub !== false) {
expect(await ws1.next()).toBe("#4 joined!");
expect(await ws1.next()).toMatch(/\w+ joined!/);
}
await ws1.send("hello from 1");
expect(await ws1.next()).toBe("hello from 1");
Expand Down
Loading