feat(peer): use secure lazy random uuid (#64)

This commit is contained in:
Pooya Parsa
2024-08-07 12:32:00 +02:00
committed by GitHub
parent 8b9863f1ee
commit b665048d0f
6 changed files with 24 additions and 10 deletions

View File

@@ -68,7 +68,7 @@
"play:bun": "bun test/fixture/bun.ts", "play:bun": "bun test/fixture/bun.ts",
"play:cf": "wrangler dev --port 3001 -c test/fixture/wrangler.toml", "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: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:node": "jiti test/fixture/node.ts",
"play:sse": "bun test/fixture/sse.ts", "play:sse": "bun test/fixture/sse.ts",
"play:uws": "jiti test/fixture/uws.ts", "play:uws": "jiti test/fixture/uws.ts",
@@ -79,6 +79,9 @@
"resolutions": { "resolutions": {
"crossws": "workspace:*" "crossws": "workspace:*"
}, },
"dependencies": {
"uncrypto": "^0.1.3"
},
"devDependencies": { "devDependencies": {
"@cloudflare/workers-types": "^4.20240729.0", "@cloudflare/workers-types": "^4.20240729.0",
"@deno/types": "^0.0.1", "@deno/types": "^0.0.1",

4
pnpm-lock.yaml generated
View File

@@ -10,6 +10,10 @@ overrides:
importers: importers:
.: .:
dependencies:
uncrypto:
specifier: ^0.1.3
version: 0.1.3
devDependencies: devDependencies:
'@cloudflare/workers-types': '@cloudflare/workers-types':
specifier: ^4.20240729.0 specifier: ^4.20240729.0

View File

@@ -1,3 +1,5 @@
import { randomUUID } from "uncrypto";
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState // https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState
type ReadyState = 0 | 1 | 2 | 3; type ReadyState = 0 | 1 | 2 | 3;
const ReadyStateMap = { const ReadyStateMap = {
@@ -16,17 +18,18 @@ export abstract class Peer<Internal extends AdapterInternal = AdapterInternal> {
protected _internal: Internal; protected _internal: Internal;
protected _topics: Set<string>; protected _topics: Set<string>;
private static _idCounter = 0; private _id?: string;
private _id: string;
constructor(internal: Internal) { constructor(internal: Internal) {
this._id = ++Peer._idCounter + "";
this._topics = new Set(); this._topics = new Set();
this._internal = internal; this._internal = internal;
} }
get id(): string { get id(): string {
return this._id.toString(); if (!this._id) {
this._id = randomUUID();
}
return this._id;
} }
get addr(): string | undefined { get addr(): string | undefined {
@@ -66,7 +69,7 @@ export abstract class Peer<Internal extends AdapterInternal = AdapterInternal> {
} }
toString() { toString() {
return `#${this.id}`; return this.id;
} }
[Symbol.for("nodejs.util.inspect.custom")]() { [Symbol.for("nodejs.util.inspect.custom")]() {

View File

@@ -2,5 +2,8 @@ import { describe } from "vitest";
import { wsTestsExec } from "../_utils"; import { wsTestsExec } from "../_utils";
describe("deno", () => { describe("deno", () => {
wsTestsExec("deno run -A ./deno.ts", { resHeaders: false, adapter: "deno" }); wsTestsExec("deno run --unstable-byonm -A ./deno.ts", {
resHeaders: false,
adapter: "deno",
});
}); });

View File

@@ -13,7 +13,8 @@ describe("sse", () => {
}); });
await new Promise((resolve) => ev.addEventListener("open", resolve)); await new Promise((resolve) => ev.addEventListener("open", resolve));
ev.close(); ev.close();
expect(messages).toMatchObject(["Welcome to the server #1!"]); expect(messages[0]).toMatch(/Welcome to the server \w+/);
expect(messages.length).toBe(1);
}); });
}); });
}); });

View File

@@ -12,7 +12,7 @@ export function wsTests(
test("connect to websocket", async () => { test("connect to websocket", async () => {
const ws = await wsConnect(getURL()); 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 () => { test("send ping", async () => {
@@ -25,7 +25,7 @@ export function wsTests(
const ws1 = await wsConnect(getURL(), { skip: 1 }); const ws1 = await wsConnect(getURL(), { skip: 1 });
const ws2 = await wsConnect(getURL(), { skip: 1 }); const ws2 = await wsConnect(getURL(), { skip: 1 });
if (opts.pubsub !== false) { if (opts.pubsub !== false) {
expect(await ws1.next()).toBe("#4 joined!"); expect(await ws1.next()).toMatch(/\w+ joined!/);
} }
await ws1.send("hello from 1"); await ws1.send("hello from 1");
expect(await ws1.next()).toBe("hello from 1"); expect(await ws1.next()).toBe("hello from 1");