@narumitw/pi-chat 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -34
- package/dist/chunks/{chat-view-EZ24L3LM.js → chat-view-7OBZGYZM.ts} +2 -2
- package/dist/chunks/chat-view-7OBZGYZM.ts.map +7 -0
- package/dist/chunks/{chunk-ZR2JESR5.js → chunk-7FD7DDSU.ts} +6 -36
- package/dist/chunks/chunk-7FD7DDSU.ts.map +7 -0
- package/dist/chunks/{chunk-XYZZR6DW.js → chunk-EOZIFFAD.ts} +6 -23
- package/dist/chunks/chunk-EOZIFFAD.ts.map +7 -0
- package/dist/chunks/{chunk-UE3UAOJX.js → chunk-LN3NAZRC.ts} +1 -1
- package/dist/chunks/{chunk-UE3UAOJX.js.map → chunk-LN3NAZRC.ts.map} +1 -1
- package/dist/chunks/{chunk-OQLAKS7O.js → chunk-MHSYNYS3.ts} +4 -7
- package/dist/chunks/chunk-MHSYNYS3.ts.map +7 -0
- package/dist/chunks/{chunk-UQPD3L6X.js → chunk-WUCSLYAK.ts} +1 -1
- package/dist/chunks/chunk-WUCSLYAK.ts.map +7 -0
- package/dist/chunks/{directory-network-EWFZFLGK.js → directory-network-6OPGA4EH.ts} +7 -19
- package/dist/chunks/directory-network-6OPGA4EH.ts.map +7 -0
- package/dist/chunks/{menu-2KEDM6NJ.js → menu-S7MZCVYM.ts} +5 -5
- package/dist/chunks/menu-S7MZCVYM.ts.map +7 -0
- package/dist/chunks/{network-MXCIGGQ2.js → network-Y4Z4JJ5J.ts} +3 -3
- package/dist/chunks/network-Y4Z4JJ5J.ts.map +7 -0
- package/dist/chunks/{widget-4OBABJDY.js → widget-QT53EBN6.ts} +4 -10
- package/dist/chunks/widget-QT53EBN6.ts.map +7 -0
- package/dist/index.ts +20 -66
- package/dist/index.ts.map +2 -2
- package/package.json +54 -55
- package/src/chat-session.ts +571 -594
- package/src/chat-view.ts +221 -226
- package/src/directory-network.ts +347 -365
- package/src/external.d.ts +56 -66
- package/src/identity.ts +74 -88
- package/src/menu.ts +535 -541
- package/src/network-contract.ts +5 -5
- package/src/network.ts +154 -157
- package/src/nickname.ts +6 -6
- package/src/pi-chat.ts +700 -774
- package/src/protocol.ts +296 -335
- package/src/public-room-directory.ts +276 -310
- package/src/room.ts +54 -58
- package/src/settings.ts +234 -259
- package/src/text.ts +57 -57
- package/src/widget.ts +56 -72
- package/dist/chunks/chat-view-EZ24L3LM.js.map +0 -7
- package/dist/chunks/chunk-OQLAKS7O.js.map +0 -7
- package/dist/chunks/chunk-UQPD3L6X.js.map +0 -7
- package/dist/chunks/chunk-XYZZR6DW.js.map +0 -7
- package/dist/chunks/chunk-ZR2JESR5.js.map +0 -7
- package/dist/chunks/directory-network-EWFZFLGK.js.map +0 -7
- package/dist/chunks/menu-2KEDM6NJ.js.map +0 -7
- package/dist/chunks/network-MXCIGGQ2.js.map +0 -7
- package/dist/chunks/widget-4OBABJDY.js.map +0 -7
package/src/room.ts
CHANGED
|
@@ -3,86 +3,82 @@ import { createHash, hkdfSync } from "node:crypto";
|
|
|
3
3
|
const PUBLIC_SLUG = /^[a-z0-9][a-z0-9-]{0,47}$/u;
|
|
4
4
|
|
|
5
5
|
export interface RoomDescriptor {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
kind: "private" | "public";
|
|
7
|
+
label: string;
|
|
8
|
+
id: string;
|
|
9
|
+
topic: Buffer;
|
|
10
|
+
key: Buffer;
|
|
11
|
+
invite?: string;
|
|
12
|
+
slug?: string;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export function createPrivateRoom(secret: Uint8Array): RoomDescriptor {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
16
|
+
if (secret.byteLength !== 32) throw new Error("Private room secrets must be 32 bytes.");
|
|
17
|
+
const bytes = Buffer.from(secret);
|
|
18
|
+
const topic = domainHash("pi-chat/discovery/private/v2", bytes);
|
|
19
|
+
return {
|
|
20
|
+
kind: "private",
|
|
21
|
+
label: `private ${shortRoomId(topic)}`,
|
|
22
|
+
id: topic.toString("base64url"),
|
|
23
|
+
topic,
|
|
24
|
+
key: deriveRoomKey(bytes),
|
|
25
|
+
invite: `pichat:v2:${bytes.toString("base64url")}`,
|
|
26
|
+
};
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
export function parseInvite(value: string): RoomDescriptor {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
const match = /^pichat:v[12]:([A-Za-z0-9_-]{43})$/u.exec(value.trim());
|
|
31
|
+
const encodedSecret = match?.[1];
|
|
32
|
+
if (!encodedSecret) throw new Error("This is not a valid Pi Chat invite.");
|
|
33
|
+
const secret = Buffer.from(encodedSecret, "base64url");
|
|
34
|
+
if (secret.length !== 32) throw new Error("This is not a valid Pi Chat invite.");
|
|
35
|
+
return createPrivateRoom(secret);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
export function createPublicRoom(slug: string): RoomDescriptor {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
39
|
+
if (!PUBLIC_SLUG.test(slug)) {
|
|
40
|
+
throw new Error("A public room slug must use lowercase letters, numbers, or hyphens.");
|
|
41
|
+
}
|
|
42
|
+
const material = Buffer.from(`pi-chat/public/v2:${slug}`, "utf8");
|
|
43
|
+
const topic = createHash("sha256").update(material).digest();
|
|
44
|
+
return {
|
|
45
|
+
kind: "public",
|
|
46
|
+
label: `#${slug}`,
|
|
47
|
+
id: topic.toString("base64url"),
|
|
48
|
+
topic,
|
|
49
|
+
key: deriveRoomKey(material),
|
|
50
|
+
slug,
|
|
51
|
+
};
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
export function legacyRoomId(room: RoomDescriptor): string | undefined {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
? domainHash("pi-chat/discovery/private/v1", secret).toString("base64url")
|
|
65
|
-
: undefined;
|
|
55
|
+
if (room.kind === "public" && room.slug) {
|
|
56
|
+
return createHash("sha256")
|
|
57
|
+
.update(Buffer.from(`pi-chat/public/v1:${room.slug}`, "utf8"))
|
|
58
|
+
.digest("base64url");
|
|
59
|
+
}
|
|
60
|
+
const encodedSecret = /^pichat:v[12]:([A-Za-z0-9_-]{43})$/u.exec(room.invite ?? "")?.[1];
|
|
61
|
+
if (!encodedSecret) return undefined;
|
|
62
|
+
const secret = Buffer.from(encodedSecret, "base64url");
|
|
63
|
+
return secret.length === 32 ? domainHash("pi-chat/discovery/private/v1", secret).toString("base64url") : undefined;
|
|
66
64
|
}
|
|
67
65
|
|
|
68
66
|
export function isCompatibleRoomId(room: RoomDescriptor, id: string): boolean {
|
|
69
|
-
|
|
67
|
+
return room.id === id || legacyRoomId(room) === id;
|
|
70
68
|
}
|
|
71
69
|
|
|
72
70
|
function deriveRoomKey(material: Uint8Array): Buffer {
|
|
73
|
-
|
|
74
|
-
hkdfSync("sha256", material, Buffer.from("pi-chat/v2"), Buffer.from("room-handshake"), 32),
|
|
75
|
-
);
|
|
71
|
+
return Buffer.from(hkdfSync("sha256", material, Buffer.from("pi-chat/v2"), Buffer.from("room-handshake"), 32));
|
|
76
72
|
}
|
|
77
73
|
|
|
78
74
|
function domainHash(domain: string, material: Uint8Array): Buffer {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
75
|
+
return createHash("sha256")
|
|
76
|
+
.update(domain)
|
|
77
|
+
.update(Buffer.from([0]))
|
|
78
|
+
.update(material)
|
|
79
|
+
.digest();
|
|
84
80
|
}
|
|
85
81
|
|
|
86
82
|
function shortRoomId(topic: Uint8Array): string {
|
|
87
|
-
|
|
83
|
+
return Buffer.from(topic).subarray(0, 5).toString("base64url");
|
|
88
84
|
}
|