@narumitw/pi-chat 0.1.2 → 0.1.3

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.
@@ -0,0 +1,286 @@
1
+ // @generated by scripts/build-runtime.mjs; do not edit.
2
+ // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader.
3
+ import {
4
+ MAX_EVENT_AGE_MS,
5
+ MAX_FRAME_BYTES,
6
+ MAX_GOSSIP_HOPS,
7
+ createPublicRoom,
8
+ signIdentityPayload,
9
+ verifyIdentityPayload
10
+ } from "./chunk-4O25EPNQ.js";
11
+
12
+ // src/public-room-directory.ts
13
+ import { createHash } from "node:crypto";
14
+ var MAX_EVENT_FUTURE_MS = 6e4;
15
+ var DIRECTORY_RECORD_TTL_MS = 9e4;
16
+ var MAX_DIRECTORY_RECORDS = 4096;
17
+ var MAX_DIRECTORY_ROOMS = 512;
18
+ var MAX_DIRECTORY_SEEN = 16384;
19
+ var DIRECTORY_SEEN_TTL_MS = 10 * 6e4;
20
+ var PUBLIC_KEY = /^[0-9a-f]{64}$/u;
21
+ var SIGNATURE = /^[A-Za-z0-9_-]{86}$/u;
22
+ var PUBLIC_ROOM_DIRECTORY_TOPIC = createHash("sha256").update("pi-chat/public-room-directory/v2", "utf8").digest();
23
+ var DirectoryCatalog = class {
24
+ now;
25
+ maxRecords;
26
+ maxRooms;
27
+ records = /* @__PURE__ */ new Map();
28
+ seen = /* @__PURE__ */ new Map();
29
+ seenOrder = [];
30
+ truncated = false;
31
+ constructor(options = {}) {
32
+ this.now = options.now ?? Date.now;
33
+ this.maxRecords = positiveBound(options.maxRecords, MAX_DIRECTORY_RECORDS);
34
+ this.maxRooms = positiveBound(options.maxRooms, MAX_DIRECTORY_ROOMS);
35
+ }
36
+ accept(event) {
37
+ const now = this.now();
38
+ if (!verifyDirectoryPresence(event, now)) return false;
39
+ const seenKey = `${event.origin}:${event.id}`;
40
+ if (this.hasSeen(seenKey)) return false;
41
+ this.remember(seenKey);
42
+ const existing = this.records.get(event.origin);
43
+ if (existing && (existing.issuedAt > event.issuedAt || existing.issuedAt === event.issuedAt && existing.id.localeCompare(event.id) >= 0)) {
44
+ return false;
45
+ }
46
+ if (!existing && this.records.size >= this.maxRecords) {
47
+ this.truncated = true;
48
+ return false;
49
+ }
50
+ const knownSlugs = new Set(
51
+ [...this.records.values()].filter(({ status }) => status === "online").map(({ slug }) => slug)
52
+ );
53
+ if (event.status === "online" && !knownSlugs.has(event.slug) && knownSlugs.size >= this.maxRooms) {
54
+ this.truncated = true;
55
+ return false;
56
+ }
57
+ this.records.set(event.origin, event);
58
+ return true;
59
+ }
60
+ snapshot() {
61
+ this.prune();
62
+ const counts = /* @__PURE__ */ new Map();
63
+ for (const event of this.records.values()) {
64
+ if (event.status === "online") {
65
+ counts.set(event.slug, (counts.get(event.slug) ?? 0) + 1);
66
+ }
67
+ }
68
+ return {
69
+ rooms: sortDirectoryRooms(
70
+ [...counts].map(([slug, estimatedParticipants]) => ({ slug, estimatedParticipants }))
71
+ ),
72
+ partial: this.truncated
73
+ };
74
+ }
75
+ currentEvents() {
76
+ this.prune();
77
+ return [...this.records.values()].sort(
78
+ (left, right) => right.issuedAt - left.issuedAt || left.origin.localeCompare(right.origin)
79
+ );
80
+ }
81
+ markPartial() {
82
+ this.truncated = true;
83
+ }
84
+ prune() {
85
+ const now = this.now();
86
+ for (const [origin, event] of this.records) {
87
+ const ttl = event.status === "leaving" ? MAX_EVENT_AGE_MS : DIRECTORY_RECORD_TTL_MS;
88
+ if (event.issuedAt < now - ttl) this.records.delete(origin);
89
+ }
90
+ this.pruneSeen();
91
+ }
92
+ hasSeen(key) {
93
+ this.pruneSeen();
94
+ return this.seen.has(key);
95
+ }
96
+ remember(key) {
97
+ const expiresAt = this.now() + DIRECTORY_SEEN_TTL_MS;
98
+ this.seen.set(key, expiresAt);
99
+ this.seenOrder.push({ key, expiresAt });
100
+ while (this.seen.size > MAX_DIRECTORY_SEEN) {
101
+ const removed = this.seenOrder.shift();
102
+ if (removed && this.seen.get(removed.key) === removed.expiresAt) {
103
+ this.seen.delete(removed.key);
104
+ }
105
+ }
106
+ }
107
+ pruneSeen() {
108
+ const now = this.now();
109
+ while (this.seenOrder[0] && this.seenOrder[0].expiresAt <= now) {
110
+ const removed = this.seenOrder.shift();
111
+ if (removed && this.seen.get(removed.key) === removed.expiresAt) {
112
+ this.seen.delete(removed.key);
113
+ }
114
+ }
115
+ }
116
+ };
117
+ function createDirectoryPresence(identity, slugValue, status, issuedAt, id) {
118
+ const slug = validatedSlug(slugValue);
119
+ if (!slug) throw new Error("A public room slug must use lowercase letters, numbers, or hyphens.");
120
+ if (status !== "online" && status !== "leaving") {
121
+ throw new Error("Directory presence state is invalid.");
122
+ }
123
+ if (!Number.isSafeInteger(issuedAt) || issuedAt < 0) {
124
+ throw new Error("Directory presence timestamp is invalid.");
125
+ }
126
+ if (!validId(id)) throw new Error("Directory presence id is invalid.");
127
+ const unsigned = {
128
+ v: 2,
129
+ kind: "directory-presence",
130
+ slug,
131
+ origin: identity.publicKey.toString("hex"),
132
+ id,
133
+ issuedAt,
134
+ status
135
+ };
136
+ return {
137
+ ...unsigned,
138
+ signature: signIdentityPayload(identity, canonicalDirectoryPresence(unsigned))
139
+ };
140
+ }
141
+ function verifyDirectoryPresence(value, now = Date.now()) {
142
+ const event = parseDirectoryPresence(value);
143
+ if (!event) return false;
144
+ if (event.issuedAt < now - MAX_EVENT_AGE_MS || event.issuedAt > now + MAX_EVENT_FUTURE_MS) {
145
+ return false;
146
+ }
147
+ const { signature, ...unsigned } = event;
148
+ return verifyIdentityPayload(
149
+ Buffer.from(event.origin, "hex"),
150
+ canonicalDirectoryPresence(unsigned),
151
+ signature
152
+ );
153
+ }
154
+ function createDirectoryWireMessage(event, hops = MAX_GOSSIP_HOPS, partial = false) {
155
+ if (!Number.isSafeInteger(hops) || hops < 1 || hops > MAX_GOSSIP_HOPS) {
156
+ throw new Error("Directory gossip hop budget is invalid.");
157
+ }
158
+ return { v: 2, type: "directory", event, hops, ...partial ? { partial: true } : {} };
159
+ }
160
+ function parseDirectoryWireMessage(value) {
161
+ if (!isRecord(value) || value.v !== 2 || value.type !== "directory" || !Number.isSafeInteger(value.hops) || value.hops < 1 || value.hops > MAX_GOSSIP_HOPS) {
162
+ return void 0;
163
+ }
164
+ if (value.partial !== void 0 && value.partial !== true) return void 0;
165
+ const event = parseDirectoryPresence(value.event);
166
+ return event ? {
167
+ v: 2,
168
+ type: "directory",
169
+ event,
170
+ hops: value.hops,
171
+ ...value.partial === true ? { partial: true } : {}
172
+ } : void 0;
173
+ }
174
+ function encodeDirectoryFrame(message) {
175
+ const payload = Buffer.from(JSON.stringify(message), "utf8");
176
+ if (payload.length > MAX_FRAME_BYTES) throw new Error("Directory frame exceeds the size limit.");
177
+ const header = Buffer.allocUnsafe(4);
178
+ header.writeUInt32BE(payload.length);
179
+ return Buffer.concat([header, payload]);
180
+ }
181
+ var DirectoryFrameDecoder = class {
182
+ buffer = Buffer.alloc(0);
183
+ push(chunk) {
184
+ if (chunk.byteLength === 0) return [];
185
+ const incoming = Buffer.from(chunk);
186
+ const messages = [];
187
+ let offset = 0;
188
+ while (offset < incoming.length) {
189
+ if (this.buffer.length < 4) {
190
+ const headerBytes = Math.min(4 - this.buffer.length, incoming.length - offset);
191
+ this.buffer = Buffer.concat([this.buffer, incoming.subarray(offset, offset + headerBytes)]);
192
+ offset += headerBytes;
193
+ if (this.buffer.length < 4) break;
194
+ }
195
+ const length = this.buffer.readUInt32BE(0);
196
+ if (length > MAX_FRAME_BYTES) {
197
+ this.buffer = Buffer.alloc(0);
198
+ throw new Error("Directory frame exceeds the size limit.");
199
+ }
200
+ const frameBytes = length + 4;
201
+ if (this.buffer.length < frameBytes) {
202
+ const payloadBytes = Math.min(frameBytes - this.buffer.length, incoming.length - offset);
203
+ this.buffer = Buffer.concat([
204
+ this.buffer,
205
+ incoming.subarray(offset, offset + payloadBytes)
206
+ ]);
207
+ offset += payloadBytes;
208
+ if (this.buffer.length < frameBytes) break;
209
+ }
210
+ const payload = this.buffer.subarray(4, frameBytes);
211
+ this.buffer = Buffer.alloc(0);
212
+ let value;
213
+ try {
214
+ value = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(payload));
215
+ } catch {
216
+ throw new Error("Directory frame is not valid UTF-8 JSON.");
217
+ }
218
+ const parsed = parseDirectoryWireMessage(value);
219
+ if (!parsed) throw new Error("Directory frame has an invalid message shape.");
220
+ messages.push(parsed);
221
+ }
222
+ return messages;
223
+ }
224
+ };
225
+ function sortDirectoryRooms(rooms) {
226
+ return [...rooms].sort(
227
+ (left, right) => right.estimatedParticipants - left.estimatedParticipants || left.slug.localeCompare(right.slug)
228
+ );
229
+ }
230
+ function parseDirectoryPresence(value) {
231
+ if (!isRecord(value) || value.v !== 2 || value.kind !== "directory-presence" || !validatedSlug(value.slug) || !PUBLIC_KEY.test(String(value.origin)) || !validId(value.id) || !Number.isSafeInteger(value.issuedAt) || value.issuedAt < 0 || value.status !== "online" && value.status !== "leaving" || typeof value.signature !== "string" || !SIGNATURE.test(value.signature)) {
232
+ return void 0;
233
+ }
234
+ return {
235
+ v: 2,
236
+ kind: "directory-presence",
237
+ slug: value.slug,
238
+ origin: value.origin,
239
+ id: value.id,
240
+ issuedAt: value.issuedAt,
241
+ status: value.status,
242
+ signature: value.signature
243
+ };
244
+ }
245
+ function canonicalDirectoryPresence(event) {
246
+ return Buffer.from(
247
+ JSON.stringify([
248
+ 2,
249
+ "directory-presence",
250
+ event.slug,
251
+ event.origin,
252
+ event.id,
253
+ event.issuedAt,
254
+ event.status
255
+ ]),
256
+ "utf8"
257
+ );
258
+ }
259
+ function validatedSlug(value) {
260
+ if (typeof value !== "string") return void 0;
261
+ try {
262
+ return createPublicRoom(value).slug;
263
+ } catch {
264
+ return void 0;
265
+ }
266
+ }
267
+ function positiveBound(value, fallback) {
268
+ return Number.isSafeInteger(value) && (value ?? 0) > 0 ? Math.min(value ?? fallback, fallback) : fallback;
269
+ }
270
+ function validId(value) {
271
+ return typeof value === "string" && value.length > 0 && value.length <= 64 && !/\p{Cc}/u.test(value);
272
+ }
273
+ function isRecord(value) {
274
+ return typeof value === "object" && value !== null && !Array.isArray(value);
275
+ }
276
+
277
+ export {
278
+ PUBLIC_ROOM_DIRECTORY_TOPIC,
279
+ DirectoryCatalog,
280
+ createDirectoryPresence,
281
+ createDirectoryWireMessage,
282
+ encodeDirectoryFrame,
283
+ DirectoryFrameDecoder,
284
+ sortDirectoryRooms
285
+ };
286
+ //# sourceMappingURL=chunk-6YVQGIUO.ts.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/public-room-directory.ts"],
4
+ "sourcesContent": ["import { createHash } from \"node:crypto\";\nimport type { ChatIdentity } from \"./identity.js\";\nimport { signIdentityPayload, verifyIdentityPayload } from \"./identity.js\";\nimport {\n\tcreatePublicRoom,\n\tMAX_EVENT_AGE_MS,\n\tMAX_FRAME_BYTES,\n\tMAX_GOSSIP_HOPS,\n} from \"./protocol.js\";\n\nconst MAX_EVENT_FUTURE_MS = 60_000;\nconst DIRECTORY_RECORD_TTL_MS = 90_000;\nconst MAX_DIRECTORY_RECORDS = 4_096;\nconst MAX_DIRECTORY_ROOMS = 512;\nconst MAX_DIRECTORY_SEEN = 16_384;\nconst DIRECTORY_SEEN_TTL_MS = 10 * 60_000;\nconst PUBLIC_KEY = /^[0-9a-f]{64}$/u;\nconst SIGNATURE = /^[A-Za-z0-9_-]{86}$/u;\n\nexport const PUBLIC_ROOM_DIRECTORY_TOPIC = createHash(\"sha256\")\n\t.update(\"pi-chat/public-room-directory/v2\", \"utf8\")\n\t.digest();\n\nexport interface DirectoryPresence {\n\tv: 2;\n\tkind: \"directory-presence\";\n\tslug: string;\n\torigin: string;\n\tid: string;\n\tissuedAt: number;\n\tstatus: \"online\" | \"leaving\";\n\tsignature: string;\n}\n\nexport interface DirectoryWireMessage {\n\tv: 2;\n\ttype: \"directory\";\n\tevent: DirectoryPresence;\n\thops: number;\n\tpartial?: true;\n}\n\nexport interface PublicRoomListing {\n\tslug: string;\n\testimatedParticipants: number;\n}\n\nexport interface PublicRoomBrowseResult {\n\trooms: PublicRoomListing[];\n\tpartial: boolean;\n}\n\nexport interface DirectoryCatalogOptions {\n\tnow?: () => number;\n\tmaxRecords?: number;\n\tmaxRooms?: number;\n}\n\ninterface SeenRecord {\n\tkey: string;\n\texpiresAt: number;\n}\n\nexport class DirectoryCatalog {\n\tprivate readonly now: () => number;\n\tprivate readonly maxRecords: number;\n\tprivate readonly maxRooms: number;\n\tprivate readonly records = new Map<string, DirectoryPresence>();\n\tprivate readonly seen = new Map<string, number>();\n\tprivate readonly seenOrder: SeenRecord[] = [];\n\tprivate truncated = false;\n\n\tconstructor(options: DirectoryCatalogOptions = {}) {\n\t\tthis.now = options.now ?? Date.now;\n\t\tthis.maxRecords = positiveBound(options.maxRecords, MAX_DIRECTORY_RECORDS);\n\t\tthis.maxRooms = positiveBound(options.maxRooms, MAX_DIRECTORY_ROOMS);\n\t}\n\n\taccept(event: DirectoryPresence): boolean {\n\t\tconst now = this.now();\n\t\tif (!verifyDirectoryPresence(event, now)) return false;\n\t\tconst seenKey = `${event.origin}:${event.id}`;\n\t\tif (this.hasSeen(seenKey)) return false;\n\t\tthis.remember(seenKey);\n\t\tconst existing = this.records.get(event.origin);\n\t\tif (\n\t\t\texisting &&\n\t\t\t(existing.issuedAt > event.issuedAt ||\n\t\t\t\t(existing.issuedAt === event.issuedAt && existing.id.localeCompare(event.id) >= 0))\n\t\t) {\n\t\t\treturn false;\n\t\t}\n\t\tif (!existing && this.records.size >= this.maxRecords) {\n\t\t\tthis.truncated = true;\n\t\t\treturn false;\n\t\t}\n\t\tconst knownSlugs = new Set(\n\t\t\t[...this.records.values()]\n\t\t\t\t.filter(({ status }) => status === \"online\")\n\t\t\t\t.map(({ slug }) => slug),\n\t\t);\n\t\tif (\n\t\t\tevent.status === \"online\" &&\n\t\t\t!knownSlugs.has(event.slug) &&\n\t\t\tknownSlugs.size >= this.maxRooms\n\t\t) {\n\t\t\tthis.truncated = true;\n\t\t\treturn false;\n\t\t}\n\t\tthis.records.set(event.origin, event);\n\t\treturn true;\n\t}\n\n\tsnapshot(): PublicRoomBrowseResult {\n\t\tthis.prune();\n\t\tconst counts = new Map<string, number>();\n\t\tfor (const event of this.records.values()) {\n\t\t\tif (event.status === \"online\") {\n\t\t\t\tcounts.set(event.slug, (counts.get(event.slug) ?? 0) + 1);\n\t\t\t}\n\t\t}\n\t\treturn {\n\t\t\trooms: sortDirectoryRooms(\n\t\t\t\t[...counts].map(([slug, estimatedParticipants]) => ({ slug, estimatedParticipants })),\n\t\t\t),\n\t\t\tpartial: this.truncated,\n\t\t};\n\t}\n\n\tcurrentEvents(): DirectoryPresence[] {\n\t\tthis.prune();\n\t\treturn [...this.records.values()].sort(\n\t\t\t(left, right) => right.issuedAt - left.issuedAt || left.origin.localeCompare(right.origin),\n\t\t);\n\t}\n\n\tmarkPartial(): void {\n\t\tthis.truncated = true;\n\t}\n\n\tprivate prune(): void {\n\t\tconst now = this.now();\n\t\tfor (const [origin, event] of this.records) {\n\t\t\tconst ttl = event.status === \"leaving\" ? MAX_EVENT_AGE_MS : DIRECTORY_RECORD_TTL_MS;\n\t\t\tif (event.issuedAt < now - ttl) this.records.delete(origin);\n\t\t}\n\t\tthis.pruneSeen();\n\t}\n\n\tprivate hasSeen(key: string): boolean {\n\t\tthis.pruneSeen();\n\t\treturn this.seen.has(key);\n\t}\n\n\tprivate remember(key: string): void {\n\t\tconst expiresAt = this.now() + DIRECTORY_SEEN_TTL_MS;\n\t\tthis.seen.set(key, expiresAt);\n\t\tthis.seenOrder.push({ key, expiresAt });\n\t\twhile (this.seen.size > MAX_DIRECTORY_SEEN) {\n\t\t\tconst removed = this.seenOrder.shift();\n\t\t\tif (removed && this.seen.get(removed.key) === removed.expiresAt) {\n\t\t\t\tthis.seen.delete(removed.key);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate pruneSeen(): void {\n\t\tconst now = this.now();\n\t\twhile (this.seenOrder[0] && this.seenOrder[0].expiresAt <= now) {\n\t\t\tconst removed = this.seenOrder.shift();\n\t\t\tif (removed && this.seen.get(removed.key) === removed.expiresAt) {\n\t\t\t\tthis.seen.delete(removed.key);\n\t\t\t}\n\t\t}\n\t}\n}\n\nexport function createDirectoryPresence(\n\tidentity: ChatIdentity,\n\tslugValue: string,\n\tstatus: DirectoryPresence[\"status\"],\n\tissuedAt: number,\n\tid: string,\n): DirectoryPresence {\n\tconst slug = validatedSlug(slugValue);\n\tif (!slug) throw new Error(\"A public room slug must use lowercase letters, numbers, or hyphens.\");\n\tif (status !== \"online\" && status !== \"leaving\") {\n\t\tthrow new Error(\"Directory presence state is invalid.\");\n\t}\n\tif (!Number.isSafeInteger(issuedAt) || issuedAt < 0) {\n\t\tthrow new Error(\"Directory presence timestamp is invalid.\");\n\t}\n\tif (!validId(id)) throw new Error(\"Directory presence id is invalid.\");\n\tconst unsigned: Omit<DirectoryPresence, \"signature\"> = {\n\t\tv: 2,\n\t\tkind: \"directory-presence\",\n\t\tslug,\n\t\torigin: identity.publicKey.toString(\"hex\"),\n\t\tid,\n\t\tissuedAt,\n\t\tstatus,\n\t};\n\treturn {\n\t\t...unsigned,\n\t\tsignature: signIdentityPayload(identity, canonicalDirectoryPresence(unsigned)),\n\t};\n}\n\nexport function verifyDirectoryPresence(value: DirectoryPresence, now = Date.now()): boolean {\n\tconst event = parseDirectoryPresence(value);\n\tif (!event) return false;\n\tif (event.issuedAt < now - MAX_EVENT_AGE_MS || event.issuedAt > now + MAX_EVENT_FUTURE_MS) {\n\t\treturn false;\n\t}\n\tconst { signature, ...unsigned } = event;\n\treturn verifyIdentityPayload(\n\t\tBuffer.from(event.origin, \"hex\"),\n\t\tcanonicalDirectoryPresence(unsigned),\n\t\tsignature,\n\t);\n}\n\nexport function createDirectoryWireMessage(\n\tevent: DirectoryPresence,\n\thops = MAX_GOSSIP_HOPS,\n\tpartial = false,\n): DirectoryWireMessage {\n\tif (!Number.isSafeInteger(hops) || hops < 1 || hops > MAX_GOSSIP_HOPS) {\n\t\tthrow new Error(\"Directory gossip hop budget is invalid.\");\n\t}\n\treturn { v: 2, type: \"directory\", event, hops, ...(partial ? { partial: true as const } : {}) };\n}\n\nexport function parseDirectoryWireMessage(value: unknown): DirectoryWireMessage | undefined {\n\tif (\n\t\t!isRecord(value) ||\n\t\tvalue.v !== 2 ||\n\t\tvalue.type !== \"directory\" ||\n\t\t!Number.isSafeInteger(value.hops) ||\n\t\t(value.hops as number) < 1 ||\n\t\t(value.hops as number) > MAX_GOSSIP_HOPS\n\t) {\n\t\treturn undefined;\n\t}\n\tif (value.partial !== undefined && value.partial !== true) return undefined;\n\tconst event = parseDirectoryPresence(value.event);\n\treturn event\n\t\t? {\n\t\t\t\tv: 2,\n\t\t\t\ttype: \"directory\",\n\t\t\t\tevent,\n\t\t\t\thops: value.hops as number,\n\t\t\t\t...(value.partial === true ? { partial: true as const } : {}),\n\t\t\t}\n\t\t: undefined;\n}\n\nexport function encodeDirectoryFrame(message: DirectoryWireMessage): Buffer {\n\tconst payload = Buffer.from(JSON.stringify(message), \"utf8\");\n\tif (payload.length > MAX_FRAME_BYTES) throw new Error(\"Directory frame exceeds the size limit.\");\n\tconst header = Buffer.allocUnsafe(4);\n\theader.writeUInt32BE(payload.length);\n\treturn Buffer.concat([header, payload]);\n}\n\nexport class DirectoryFrameDecoder {\n\tprivate buffer = Buffer.alloc(0);\n\n\tpush(chunk: Uint8Array): DirectoryWireMessage[] {\n\t\tif (chunk.byteLength === 0) return [];\n\t\tconst incoming = Buffer.from(chunk);\n\t\tconst messages: DirectoryWireMessage[] = [];\n\t\tlet offset = 0;\n\t\twhile (offset < incoming.length) {\n\t\t\tif (this.buffer.length < 4) {\n\t\t\t\tconst headerBytes = Math.min(4 - this.buffer.length, incoming.length - offset);\n\t\t\t\tthis.buffer = Buffer.concat([this.buffer, incoming.subarray(offset, offset + headerBytes)]);\n\t\t\t\toffset += headerBytes;\n\t\t\t\tif (this.buffer.length < 4) break;\n\t\t\t}\n\t\t\tconst length = this.buffer.readUInt32BE(0);\n\t\t\tif (length > MAX_FRAME_BYTES) {\n\t\t\t\tthis.buffer = Buffer.alloc(0);\n\t\t\t\tthrow new Error(\"Directory frame exceeds the size limit.\");\n\t\t\t}\n\t\t\tconst frameBytes = length + 4;\n\t\t\tif (this.buffer.length < frameBytes) {\n\t\t\t\tconst payloadBytes = Math.min(frameBytes - this.buffer.length, incoming.length - offset);\n\t\t\t\tthis.buffer = Buffer.concat([\n\t\t\t\t\tthis.buffer,\n\t\t\t\t\tincoming.subarray(offset, offset + payloadBytes),\n\t\t\t\t]);\n\t\t\t\toffset += payloadBytes;\n\t\t\t\tif (this.buffer.length < frameBytes) break;\n\t\t\t}\n\t\t\tconst payload = this.buffer.subarray(4, frameBytes);\n\t\t\tthis.buffer = Buffer.alloc(0);\n\t\t\tlet value: unknown;\n\t\t\ttry {\n\t\t\t\tvalue = JSON.parse(new TextDecoder(\"utf-8\", { fatal: true }).decode(payload)) as unknown;\n\t\t\t} catch {\n\t\t\t\tthrow new Error(\"Directory frame is not valid UTF-8 JSON.\");\n\t\t\t}\n\t\t\tconst parsed = parseDirectoryWireMessage(value);\n\t\t\tif (!parsed) throw new Error(\"Directory frame has an invalid message shape.\");\n\t\t\tmessages.push(parsed);\n\t\t}\n\t\treturn messages;\n\t}\n}\n\nexport function sortDirectoryRooms(rooms: readonly PublicRoomListing[]): PublicRoomListing[] {\n\treturn [...rooms].sort(\n\t\t(left, right) =>\n\t\t\tright.estimatedParticipants - left.estimatedParticipants ||\n\t\t\tleft.slug.localeCompare(right.slug),\n\t);\n}\n\nfunction parseDirectoryPresence(value: unknown): DirectoryPresence | undefined {\n\tif (\n\t\t!isRecord(value) ||\n\t\tvalue.v !== 2 ||\n\t\tvalue.kind !== \"directory-presence\" ||\n\t\t!validatedSlug(value.slug) ||\n\t\t!PUBLIC_KEY.test(String(value.origin)) ||\n\t\t!validId(value.id) ||\n\t\t!Number.isSafeInteger(value.issuedAt) ||\n\t\t(value.issuedAt as number) < 0 ||\n\t\t(value.status !== \"online\" && value.status !== \"leaving\") ||\n\t\ttypeof value.signature !== \"string\" ||\n\t\t!SIGNATURE.test(value.signature)\n\t) {\n\t\treturn undefined;\n\t}\n\treturn {\n\t\tv: 2,\n\t\tkind: \"directory-presence\",\n\t\tslug: value.slug as string,\n\t\torigin: value.origin as string,\n\t\tid: value.id,\n\t\tissuedAt: value.issuedAt as number,\n\t\tstatus: value.status,\n\t\tsignature: value.signature,\n\t};\n}\n\nfunction canonicalDirectoryPresence(event: Omit<DirectoryPresence, \"signature\">): Buffer {\n\treturn Buffer.from(\n\t\tJSON.stringify([\n\t\t\t2,\n\t\t\t\"directory-presence\",\n\t\t\tevent.slug,\n\t\t\tevent.origin,\n\t\t\tevent.id,\n\t\t\tevent.issuedAt,\n\t\t\tevent.status,\n\t\t]),\n\t\t\"utf8\",\n\t);\n}\n\nfunction validatedSlug(value: unknown): string | undefined {\n\tif (typeof value !== \"string\") return undefined;\n\ttry {\n\t\treturn createPublicRoom(value).slug;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction positiveBound(value: number | undefined, fallback: number): number {\n\treturn Number.isSafeInteger(value) && (value ?? 0) > 0\n\t\t? Math.min(value ?? fallback, fallback)\n\t\t: fallback;\n}\n\nfunction validId(value: unknown): value is string {\n\treturn (\n\t\ttypeof value === \"string\" && value.length > 0 && value.length <= 64 && !/\\p{Cc}/u.test(value)\n\t);\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;AAAA,SAAS,kBAAkB;AAU3B,IAAM,sBAAsB;AAC5B,IAAM,0BAA0B;AAChC,IAAM,wBAAwB;AAC9B,IAAM,sBAAsB;AAC5B,IAAM,qBAAqB;AAC3B,IAAM,wBAAwB,KAAK;AACnC,IAAM,aAAa;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,SAAU,QAAO;AACtC,MAAI;AACH,WAAO,iBAAiB,KAAK,EAAE;AAAA,EAChC,QAAQ;AACP,WAAO;AAAA,EACR;AACD;AAEA,SAAS,cAAc,OAA2B,UAA0B;AAC3E,SAAO,OAAO,cAAc,KAAK,MAAM,SAAS,KAAK,IAClD,KAAK,IAAI,SAAS,UAAU,QAAQ,IACpC;AACJ;AAEA,SAAS,QAAQ,OAAiC;AACjD,SACC,OAAO,UAAU,YAAY,MAAM,SAAS,KAAK,MAAM,UAAU,MAAM,CAAC,UAAU,KAAK,KAAK;AAE9F;AAEA,SAAS,SAAS,OAAkD;AACnE,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;AAC3E;",
6
+ "names": []
7
+ }
@@ -0,0 +1,72 @@
1
+ // @generated by scripts/build-runtime.mjs; do not edit.
2
+ // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader.
3
+
4
+ // src/text.ts
5
+ var REPLACEMENT = "\uFFFD";
6
+ function sanitizeChatText(value) {
7
+ const normalized = value.replace(/\r\n?/gu, "\n");
8
+ let result = "";
9
+ for (let index = 0; index < normalized.length; index += 1) {
10
+ const code = normalized.charCodeAt(index);
11
+ if (code === 27) {
12
+ index = escapeSequenceEnd(normalized, index);
13
+ result += REPLACEMENT;
14
+ continue;
15
+ }
16
+ const point = normalized.codePointAt(index) ?? code;
17
+ if (isBidiControl(point) || isUnsafeControl(point)) {
18
+ result += REPLACEMENT;
19
+ continue;
20
+ }
21
+ if (code === 9) {
22
+ result += " ";
23
+ continue;
24
+ }
25
+ result += String.fromCodePoint(point);
26
+ if (point > 65535) index += 1;
27
+ }
28
+ return result;
29
+ }
30
+ function sanitizeSingleLine(value) {
31
+ return sanitizeChatText(value).replace(/\n+/gu, " ");
32
+ }
33
+ function escapeSequenceEnd(value, start) {
34
+ const introducer = value.charCodeAt(start + 1);
35
+ if (introducer === 93) {
36
+ for (let index = start + 2; index < value.length; index += 1) {
37
+ const code = value.charCodeAt(index);
38
+ if (code === 7) return index;
39
+ if (code === 27 && value.charCodeAt(index + 1) === 92) return index + 1;
40
+ }
41
+ return value.length - 1;
42
+ }
43
+ if (introducer === 80) {
44
+ for (let index = start + 2; index < value.length; index += 1) {
45
+ if (value.charCodeAt(index) === 27 && value.charCodeAt(index + 1) === 92) {
46
+ return index + 1;
47
+ }
48
+ }
49
+ return value.length - 1;
50
+ }
51
+ if (introducer === 91) {
52
+ for (let index = start + 2; index < value.length; index += 1) {
53
+ const code = value.charCodeAt(index);
54
+ if (code >= 64 && code <= 126) return index;
55
+ }
56
+ return value.length - 1;
57
+ }
58
+ return Math.min(start + 1, value.length - 1);
59
+ }
60
+ function isUnsafeControl(point) {
61
+ if (point === 10 || point === 9) return false;
62
+ return point <= 31 || point >= 127 && point <= 159;
63
+ }
64
+ function isBidiControl(point) {
65
+ return point === 1564 || point === 8206 || point === 8207 || point >= 8234 && point <= 8238 || point >= 8294 && point <= 8297;
66
+ }
67
+
68
+ export {
69
+ sanitizeChatText,
70
+ sanitizeSingleLine
71
+ };
72
+ //# sourceMappingURL=chunk-IVUX27XD.ts.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/text.ts"],
4
+ "sourcesContent": ["const REPLACEMENT = \"\uFFFD\";\n\nexport function sanitizeChatText(value: string): string {\n\tconst normalized = value.replace(/\\r\\n?/gu, \"\\n\");\n\tlet result = \"\";\n\tfor (let index = 0; index < normalized.length; index += 1) {\n\t\tconst code = normalized.charCodeAt(index);\n\t\tif (code === 0x1b) {\n\t\t\tindex = escapeSequenceEnd(normalized, index);\n\t\t\tresult += REPLACEMENT;\n\t\t\tcontinue;\n\t\t}\n\t\tconst point = normalized.codePointAt(index) ?? code;\n\t\tif (isBidiControl(point) || isUnsafeControl(point)) {\n\t\t\tresult += REPLACEMENT;\n\t\t\tcontinue;\n\t\t}\n\t\tif (code === 0x09) {\n\t\t\tresult += \" \";\n\t\t\tcontinue;\n\t\t}\n\t\tresult += String.fromCodePoint(point);\n\t\tif (point > 0xffff) index += 1;\n\t}\n\treturn result;\n}\n\nexport function sanitizeSingleLine(value: string): string {\n\treturn sanitizeChatText(value).replace(/\\n+/gu, \" \");\n}\n\nfunction escapeSequenceEnd(value: string, start: number): number {\n\tconst introducer = value.charCodeAt(start + 1);\n\tif (introducer === 0x5d) {\n\t\tfor (let index = start + 2; index < value.length; index += 1) {\n\t\t\tconst code = value.charCodeAt(index);\n\t\t\tif (code === 0x07) return index;\n\t\t\tif (code === 0x1b && value.charCodeAt(index + 1) === 0x5c) return index + 1;\n\t\t}\n\t\treturn value.length - 1;\n\t}\n\tif (introducer === 0x50) {\n\t\tfor (let index = start + 2; index < value.length; index += 1) {\n\t\t\tif (value.charCodeAt(index) === 0x1b && value.charCodeAt(index + 1) === 0x5c) {\n\t\t\t\treturn index + 1;\n\t\t\t}\n\t\t}\n\t\treturn value.length - 1;\n\t}\n\tif (introducer === 0x5b) {\n\t\tfor (let index = start + 2; index < value.length; index += 1) {\n\t\t\tconst code = value.charCodeAt(index);\n\t\t\tif (code >= 0x40 && code <= 0x7e) return index;\n\t\t}\n\t\treturn value.length - 1;\n\t}\n\treturn Math.min(start + 1, value.length - 1);\n}\n\nfunction isUnsafeControl(point: number): boolean {\n\tif (point === 0x0a || point === 0x09) return false;\n\treturn point <= 0x1f || (point >= 0x7f && point <= 0x9f);\n}\n\nfunction isBidiControl(point: number): boolean {\n\treturn (\n\t\tpoint === 0x061c ||\n\t\tpoint === 0x200e ||\n\t\tpoint === 0x200f ||\n\t\t(point >= 0x202a && point <= 0x202e) ||\n\t\t(point >= 0x2066 && point <= 0x2069)\n\t);\n}\n"],
5
+ "mappings": ";;;;AAAA,IAAM,cAAc;AAEb,SAAS,iBAAiB,OAAuB;AACvD,QAAM,aAAa,MAAM,QAAQ,WAAW,IAAI;AAChD,MAAI,SAAS;AACb,WAAS,QAAQ,GAAG,QAAQ,WAAW,QAAQ,SAAS,GAAG;AAC1D,UAAM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AM,QAAO;AAAA,IAC1C;AACA,WAAO,MAAM,SAAS;AAAA,EACvB;AACA,SAAO,KAAK,IAAI,QAAQ,GAAG,MAAM,SAAS,CAAC;AAC5C;AAEA,SAAS,gBAAgB,OAAwB;AAChD,MAAI,UAAU,MAAQ,UAAU,EAAM,QAAO;AAC7C,SAAO,SAAS,MAAS,SAAS,OAAQ,SAAS;AACpD;AAEA,SAAS,cAAc,OAAwB;AAC9C,SACC,UAAU,QACV,UAAU,QACV,UAAU,QACT,SAAS,QAAU,SAAS,QAC5B,SAAS,QAAU,SAAS;AAE/B;",
6
+ "names": []
7
+ }
@@ -0,0 +1,10 @@
1
+ // @generated by scripts/build-runtime.mjs; do not edit.
2
+ // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader.
3
+
4
+ // src/network-contract.ts
5
+ var MAX_DIRECT_NEIGHBORS = 8;
6
+
7
+ export {
8
+ MAX_DIRECT_NEIGHBORS
9
+ };
10
+ //# sourceMappingURL=chunk-RCEMXXHH.ts.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/network-contract.ts"],
4
+ "sourcesContent": ["import type { ChatIdentity } from \"./identity.js\";\nimport type { RoomDescriptor } from \"./room.js\";\n\nexport const MAX_DIRECT_NEIGHBORS = 8;\n\nexport interface HyperswarmTransportOptions {\n\troom: RoomDescriptor;\n\tidentity: ChatIdentity;\n\tmaxPeers?: number;\n\tdht?: unknown;\n\tbootstrap?: unknown[];\n}\n"],
5
+ "mappings": ";;;;AAGO,IAAM,uBAAuB;",
6
+ "names": []
7
+ }