From edf2f11b459da06f5aa47a46a7e16fd2f6950603 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 7 Aug 2024 12:29:25 +0200 Subject: [PATCH] feat(peer): use secure lazy random uuid --- package.json | 5 ++++- pnpm-lock.yaml | 4 ++++ src/peer.ts | 13 ++++++++----- test/adapters/deno.test.ts | 5 ++++- test/adapters/sse.test.ts | 3 ++- test/tests.ts | 4 ++-- 6 files changed, 24 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 406182d..a7c32a4 100644 --- a/package.json +++ b/package.json @@ -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", @@ -79,6 +79,9 @@ "resolutions": { "crossws": "workspace:*" }, + "dependencies": { + "uncrypto": "^0.1.3" + }, "devDependencies": { "@cloudflare/workers-types": "^4.20240729.0", "@deno/types": "^0.0.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 50e16a8..345105d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -10,6 +10,10 @@ overrides: importers: .: + dependencies: + uncrypto: + specifier: ^0.1.3 + version: 0.1.3 devDependencies: '@cloudflare/workers-types': specifier: ^4.20240729.0 diff --git a/src/peer.ts b/src/peer.ts index db73442..f554d41 100644 --- a/src/peer.ts +++ b/src/peer.ts @@ -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 = { @@ -16,17 +18,18 @@ export abstract class Peer { protected _internal: Internal; protected _topics: Set; - 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 { @@ -66,7 +69,7 @@ export abstract class Peer { } toString() { - return `#${this.id}`; + return this.id; } [Symbol.for("nodejs.util.inspect.custom")]() { diff --git a/test/adapters/deno.test.ts b/test/adapters/deno.test.ts index 9c95b5f..bc108b1 100644 --- a/test/adapters/deno.test.ts +++ b/test/adapters/deno.test.ts @@ -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", + }); }); diff --git a/test/adapters/sse.test.ts b/test/adapters/sse.test.ts index 9e95b55..3b5fabf 100644 --- a/test/adapters/sse.test.ts +++ b/test/adapters/sse.test.ts @@ -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); }); }); }); diff --git a/test/tests.ts b/test/tests.ts index 0914199..6a03abb 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -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 () => { @@ -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");