@narumitw/pi-chat 0.1.6 → 0.1.7

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.
Files changed (49) hide show
  1. package/README.md +17 -34
  2. package/dist/chunks/{chat-view-EZ24L3LM.js → chat-view-7OBZGYZM.ts} +2 -2
  3. package/dist/chunks/chat-view-7OBZGYZM.ts.map +7 -0
  4. package/dist/chunks/{chunk-ZR2JESR5.js → chunk-7FD7DDSU.ts} +6 -36
  5. package/dist/chunks/chunk-7FD7DDSU.ts.map +7 -0
  6. package/dist/chunks/{chunk-XYZZR6DW.js → chunk-EOZIFFAD.ts} +6 -23
  7. package/dist/chunks/chunk-EOZIFFAD.ts.map +7 -0
  8. package/dist/chunks/{chunk-UE3UAOJX.js → chunk-LN3NAZRC.ts} +1 -1
  9. package/dist/chunks/{chunk-UE3UAOJX.js.map → chunk-LN3NAZRC.ts.map} +1 -1
  10. package/dist/chunks/{chunk-OQLAKS7O.js → chunk-MHSYNYS3.ts} +4 -7
  11. package/dist/chunks/chunk-MHSYNYS3.ts.map +7 -0
  12. package/dist/chunks/{chunk-UQPD3L6X.js → chunk-WUCSLYAK.ts} +1 -1
  13. package/dist/chunks/chunk-WUCSLYAK.ts.map +7 -0
  14. package/dist/chunks/{directory-network-EWFZFLGK.js → directory-network-6OPGA4EH.ts} +7 -19
  15. package/dist/chunks/directory-network-6OPGA4EH.ts.map +7 -0
  16. package/dist/chunks/{menu-2KEDM6NJ.js → menu-S7MZCVYM.ts} +5 -5
  17. package/dist/chunks/menu-S7MZCVYM.ts.map +7 -0
  18. package/dist/chunks/{network-MXCIGGQ2.js → network-Y4Z4JJ5J.ts} +3 -3
  19. package/dist/chunks/network-Y4Z4JJ5J.ts.map +7 -0
  20. package/dist/chunks/{widget-4OBABJDY.js → widget-QT53EBN6.ts} +4 -10
  21. package/dist/chunks/widget-QT53EBN6.ts.map +7 -0
  22. package/dist/index.ts +20 -66
  23. package/dist/index.ts.map +2 -2
  24. package/package.json +54 -55
  25. package/src/chat-session.ts +571 -594
  26. package/src/chat-view.ts +221 -226
  27. package/src/directory-network.ts +347 -365
  28. package/src/external.d.ts +56 -66
  29. package/src/identity.ts +74 -88
  30. package/src/menu.ts +535 -541
  31. package/src/network-contract.ts +5 -5
  32. package/src/network.ts +154 -157
  33. package/src/nickname.ts +6 -6
  34. package/src/pi-chat.ts +700 -774
  35. package/src/protocol.ts +296 -335
  36. package/src/public-room-directory.ts +276 -310
  37. package/src/room.ts +54 -58
  38. package/src/settings.ts +234 -259
  39. package/src/text.ts +57 -57
  40. package/src/widget.ts +56 -72
  41. package/dist/chunks/chat-view-EZ24L3LM.js.map +0 -7
  42. package/dist/chunks/chunk-OQLAKS7O.js.map +0 -7
  43. package/dist/chunks/chunk-UQPD3L6X.js.map +0 -7
  44. package/dist/chunks/chunk-XYZZR6DW.js.map +0 -7
  45. package/dist/chunks/chunk-ZR2JESR5.js.map +0 -7
  46. package/dist/chunks/directory-network-EWFZFLGK.js.map +0 -7
  47. package/dist/chunks/menu-2KEDM6NJ.js.map +0 -7
  48. package/dist/chunks/network-MXCIGGQ2.js.map +0 -7
  49. 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
- kind: "private" | "public";
7
- label: string;
8
- id: string;
9
- topic: Buffer;
10
- key: Buffer;
11
- invite?: string;
12
- slug?: string;
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
- 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
- };
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
- 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);
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
- 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
- };
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
- 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
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
- return room.id === id || legacyRoomId(room) === id;
67
+ return room.id === id || legacyRoomId(room) === id;
70
68
  }
71
69
 
72
70
  function deriveRoomKey(material: Uint8Array): Buffer {
73
- return Buffer.from(
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
- return createHash("sha256")
80
- .update(domain)
81
- .update(Buffer.from([0]))
82
- .update(material)
83
- .digest();
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
- return Buffer.from(topic).subarray(0, 5).toString("base64url");
83
+ return Buffer.from(topic).subarray(0, 5).toString("base64url");
88
84
  }