chirli 0.0.0-stage → 0.2.0
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/ARCHITECTURE.md +103 -0
- package/CONTRIBUTING.md +56 -0
- package/LICENSE +22 -0
- package/README.md +281 -2
- package/SECURITY.md +55 -0
- package/THIRD_PARTY_NOTICES.md +51 -0
- package/dist/allowlist.d.ts +3 -0
- package/dist/allowlist.js +9 -0
- package/dist/announcements.d.ts +25 -0
- package/dist/announcements.js +142 -0
- package/dist/attachments.d.ts +43 -0
- package/dist/attachments.js +340 -0
- package/dist/blossom.d.ts +30 -0
- package/dist/blossom.js +233 -0
- package/dist/bot-handlers.d.ts +70 -0
- package/dist/bot-handlers.js +317 -0
- package/dist/bot.d.ts +63 -0
- package/dist/bot.js +244 -0
- package/dist/client.d.ts +221 -0
- package/dist/client.js +1197 -0
- package/dist/command-discovery.d.ts +4 -0
- package/dist/command-discovery.js +33 -0
- package/dist/command-schema.d.ts +4 -0
- package/dist/command-schema.js +101 -0
- package/dist/commands.d.ts +32 -0
- package/dist/commands.js +253 -0
- package/dist/defaults.d.ts +9 -0
- package/dist/defaults.js +22 -0
- package/dist/errors.d.ts +55 -0
- package/dist/errors.js +126 -0
- package/dist/inbox.d.ts +55 -0
- package/dist/inbox.js +376 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +11 -0
- package/dist/key-sync.d.ts +33 -0
- package/dist/key-sync.js +293 -0
- package/dist/keys.d.ts +66 -0
- package/dist/keys.js +418 -0
- package/dist/message-storage-sqlite.d.ts +6 -0
- package/dist/message-storage-sqlite.js +90 -0
- package/dist/message-storage.d.ts +32 -0
- package/dist/message-storage.js +1 -0
- package/dist/messaging-metadata.d.ts +48 -0
- package/dist/messaging-metadata.js +280 -0
- package/dist/metadata.d.ts +7 -0
- package/dist/metadata.js +63 -0
- package/dist/modes.d.ts +26 -0
- package/dist/modes.js +108 -0
- package/dist/nip4e-compat.d.ts +4 -0
- package/dist/nip4e-compat.js +14 -0
- package/dist/nipcd.d.ts +109 -0
- package/dist/nipcd.js +555 -0
- package/dist/protocol.d.ts +68 -0
- package/dist/protocol.js +302 -0
- package/dist/public-inbox.d.ts +66 -0
- package/dist/public-inbox.js +295 -0
- package/dist/public-thread.d.ts +62 -0
- package/dist/public-thread.js +436 -0
- package/dist/relay-pool.d.ts +43 -0
- package/dist/relay-pool.js +429 -0
- package/dist/relay-roles.d.ts +18 -0
- package/dist/relay-roles.js +55 -0
- package/dist/relay.d.ts +52 -0
- package/dist/relay.js +493 -0
- package/dist/request-timing.d.ts +12 -0
- package/dist/request-timing.js +37 -0
- package/dist/session.d.ts +16 -0
- package/dist/session.js +51 -0
- package/dist/signer.d.ts +13 -0
- package/dist/signer.js +1 -0
- package/dist/signers/bunker-relays.d.ts +15 -0
- package/dist/signers/bunker-relays.js +87 -0
- package/dist/signers/bunker.d.ts +7 -0
- package/dist/signers/bunker.js +194 -0
- package/dist/signers/nsec.d.ts +3 -0
- package/dist/signers/nsec.js +62 -0
- package/dist/signers.d.ts +18 -0
- package/dist/signers.js +59 -0
- package/dist/storage-sqlite.d.ts +29 -0
- package/dist/storage-sqlite.js +221 -0
- package/dist/storage.d.ts +85 -0
- package/dist/storage.js +1 -0
- package/dist/store.d.ts +96 -0
- package/dist/store.js +404 -0
- package/docs/ACCESS.md +28 -0
- package/docs/API.md +382 -0
- package/docs/INTEROPERABILITY.md +32 -0
- package/docs/KEYS.md +75 -0
- package/docs/NIP-CD.md +112 -0
- package/docs/PUBLIC.md +88 -0
- package/docs/RECOVERY.md +59 -0
- package/docs/RELAYS.md +145 -0
- package/docs/SESSIONS.md +108 -0
- package/docs/SIGNERS.md +59 -0
- package/docs/STORAGE.md +71 -0
- package/docs/TUTORIAL.md +178 -0
- package/package.json +47 -4
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type Event, type EventTemplate } from "nostr-tools";
|
|
2
|
+
import { type Rumor } from "./protocol.js";
|
|
3
|
+
import type { FileAttachment } from "./attachments.js";
|
|
4
|
+
import type { Transport } from "./relay.js";
|
|
5
|
+
export interface Input {
|
|
6
|
+
readonly id: string;
|
|
7
|
+
readonly sender: string;
|
|
8
|
+
readonly createdAt: number;
|
|
9
|
+
readonly kind: number;
|
|
10
|
+
readonly text: string;
|
|
11
|
+
readonly file?: FileAttachment;
|
|
12
|
+
readonly event: Event | Rumor;
|
|
13
|
+
}
|
|
14
|
+
export interface ThreadResult {
|
|
15
|
+
entries: Input[];
|
|
16
|
+
complete: boolean;
|
|
17
|
+
coverage: "local-conversation" | "public-ancestors";
|
|
18
|
+
reason?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface ThreadOptions {
|
|
21
|
+
limit?: number;
|
|
22
|
+
}
|
|
23
|
+
export interface PublicThreadOptions {
|
|
24
|
+
timeoutMs?: number;
|
|
25
|
+
maxEvents?: number;
|
|
26
|
+
maxLookups?: number;
|
|
27
|
+
maxDepth?: number;
|
|
28
|
+
}
|
|
29
|
+
export interface RootReference {
|
|
30
|
+
id?: string;
|
|
31
|
+
address?: string;
|
|
32
|
+
external?: string;
|
|
33
|
+
kind?: string;
|
|
34
|
+
author?: string;
|
|
35
|
+
hint?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface ThreadStructure {
|
|
38
|
+
root?: RootReference;
|
|
39
|
+
parent?: RootReference;
|
|
40
|
+
explicitRoot: boolean;
|
|
41
|
+
}
|
|
42
|
+
export declare const inputOf: (event: Event | Rumor) => Input;
|
|
43
|
+
export declare function publicEvent(input: Event): Event | undefined;
|
|
44
|
+
export declare function eventCoordinate(event: Event): string | undefined;
|
|
45
|
+
export declare const referenceKey: (ref: RootReference) => string;
|
|
46
|
+
export declare function threadStructure(event: Event): ThreadStructure;
|
|
47
|
+
export declare function threadLimit(limit?: number): number;
|
|
48
|
+
/** Bounded root-batched graph, followed only along the trigger's ancestor path. */
|
|
49
|
+
export declare class PublicThreads {
|
|
50
|
+
private transport;
|
|
51
|
+
private relays;
|
|
52
|
+
readonly timeout: number;
|
|
53
|
+
readonly maxEvents: number;
|
|
54
|
+
readonly maxLookups: number;
|
|
55
|
+
readonly maxDepth: number;
|
|
56
|
+
constructor(transport: Transport, relays: string[], options?: PublicThreadOptions);
|
|
57
|
+
private budget;
|
|
58
|
+
private filter;
|
|
59
|
+
rootForReply(trigger: Event): Promise<RootReference>;
|
|
60
|
+
resolve(trigger: Event, options?: ThreadOptions): Promise<ThreadResult>;
|
|
61
|
+
}
|
|
62
|
+
export declare function commentReply(trigger: Event, root: RootReference, text: string, hint?: string): EventTemplate;
|
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import { matchFilter, verifyEvent, } from "nostr-tools";
|
|
2
|
+
import { decode } from "nostr-tools/nip19";
|
|
3
|
+
import { hexKey, validTimestamp } from "./protocol.js";
|
|
4
|
+
export const inputOf = (event) => ({
|
|
5
|
+
id: event.id,
|
|
6
|
+
sender: event.pubkey,
|
|
7
|
+
createdAt: event.created_at,
|
|
8
|
+
kind: event.kind,
|
|
9
|
+
text: event.kind === 15 ? "" : event.content,
|
|
10
|
+
event: structuredClone(event),
|
|
11
|
+
});
|
|
12
|
+
export function publicEvent(input) {
|
|
13
|
+
try {
|
|
14
|
+
const event = structuredClone(input);
|
|
15
|
+
return ![13, 14, 15, 1059].includes(event.kind) &&
|
|
16
|
+
validTimestamp(event.created_at) &&
|
|
17
|
+
verifyEvent(event)
|
|
18
|
+
? event
|
|
19
|
+
: undefined;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const coordinate = (value) => {
|
|
26
|
+
const match = /^(\d+):([0-9a-f]{64}):([\s\S]*)$/.exec(value);
|
|
27
|
+
if (!match || Number(match[1]) > 65535 || value.length > 4096)
|
|
28
|
+
throw new Error("Malformed thread address");
|
|
29
|
+
const kind = Number(match[1]);
|
|
30
|
+
if (!(kind === 0 ||
|
|
31
|
+
kind === 3 ||
|
|
32
|
+
(kind >= 10000 && kind < 20000) ||
|
|
33
|
+
(kind >= 30000 && kind < 40000)))
|
|
34
|
+
throw new Error("Thread address is not replaceable");
|
|
35
|
+
return { kind, pubkey: match[2], identifier: match[3] };
|
|
36
|
+
};
|
|
37
|
+
export function eventCoordinate(event) {
|
|
38
|
+
if (event.kind >= 30000 && event.kind < 40000)
|
|
39
|
+
return `${event.kind}:${event.pubkey}:${event.tags.find((t) => t[0] === "d")?.[1] ?? ""}`;
|
|
40
|
+
if (event.kind === 0 ||
|
|
41
|
+
event.kind === 3 ||
|
|
42
|
+
(event.kind >= 10000 && event.kind < 20000))
|
|
43
|
+
return `${event.kind}:${event.pubkey}:`;
|
|
44
|
+
}
|
|
45
|
+
export const referenceKey = (ref) => ref.address
|
|
46
|
+
? `a:${ref.address}`
|
|
47
|
+
: ref.id
|
|
48
|
+
? `e:${ref.id}`
|
|
49
|
+
: `i:${ref.external}`;
|
|
50
|
+
const selfReference = (event) => ({
|
|
51
|
+
id: event.id,
|
|
52
|
+
...(eventCoordinate(event) ? { address: eventCoordinate(event) } : {}),
|
|
53
|
+
kind: String(event.kind),
|
|
54
|
+
author: event.pubkey,
|
|
55
|
+
});
|
|
56
|
+
function singleton(event, name) {
|
|
57
|
+
const tags = event.tags.filter((t) => t[0] === name);
|
|
58
|
+
if (tags.length > 1)
|
|
59
|
+
throw new Error(`Ambiguous thread ${name}`);
|
|
60
|
+
return tags[0];
|
|
61
|
+
}
|
|
62
|
+
function modernReference(event, upper) {
|
|
63
|
+
const id = singleton(event, upper ? "E" : "e"), address = singleton(event, upper ? "A" : "a"), external = singleton(event, upper ? "I" : "i"), kind = singleton(event, upper ? "K" : "k")?.[1];
|
|
64
|
+
if (!kind || (!id && !address && !external) || (external && (id || address)))
|
|
65
|
+
throw new Error("Missing or conflicting thread reference");
|
|
66
|
+
if (id && !hexKey(id[1]))
|
|
67
|
+
throw new Error("Malformed thread event ID");
|
|
68
|
+
const point = address ? coordinate(address[1]) : undefined;
|
|
69
|
+
if (point && String(point.kind) !== kind)
|
|
70
|
+
throw new Error("Thread address kind mismatch");
|
|
71
|
+
if (!external && !/^\d+$/.test(kind))
|
|
72
|
+
throw new Error("Invalid thread kind");
|
|
73
|
+
if (!external &&
|
|
74
|
+
(!Number.isSafeInteger(Number(kind)) || Number(kind) > 65535))
|
|
75
|
+
throw new Error("Invalid thread kind");
|
|
76
|
+
const authors = event.tags
|
|
77
|
+
.filter((t) => t[0] === (upper ? "P" : "p"))
|
|
78
|
+
.map((t) => t[1])
|
|
79
|
+
.filter(hexKey);
|
|
80
|
+
if (upper && authors.length !== 1 && !external)
|
|
81
|
+
throw new Error("Missing root author");
|
|
82
|
+
const hintAuthor = id?.[3];
|
|
83
|
+
if (hintAuthor && !hexKey(hintAuthor))
|
|
84
|
+
throw new Error("Invalid thread author");
|
|
85
|
+
const author = point?.pubkey ??
|
|
86
|
+
hintAuthor ??
|
|
87
|
+
(authors.length === 1 ? authors[0] : undefined);
|
|
88
|
+
if (upper && !external && (!author || authors[0] !== author))
|
|
89
|
+
throw new Error("Conflicting root author");
|
|
90
|
+
if (point && hintAuthor && point.pubkey !== hintAuthor)
|
|
91
|
+
throw new Error("Conflicting address author");
|
|
92
|
+
if (external && (!external[1] || external[1].length > 4096))
|
|
93
|
+
throw new Error("Invalid external scope");
|
|
94
|
+
return {
|
|
95
|
+
...(id ? { id: id[1] } : {}),
|
|
96
|
+
...(address ? { address: address[1] } : {}),
|
|
97
|
+
...(external ? { external: external[1] } : {}),
|
|
98
|
+
kind,
|
|
99
|
+
author,
|
|
100
|
+
hint: (address ?? id ?? external)?.[2],
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
export function threadStructure(event) {
|
|
104
|
+
if (event.kind === 1111)
|
|
105
|
+
return {
|
|
106
|
+
root: modernReference(event, true),
|
|
107
|
+
parent: modernReference(event, false),
|
|
108
|
+
explicitRoot: true,
|
|
109
|
+
};
|
|
110
|
+
if (event.kind !== 1)
|
|
111
|
+
return { root: selfReference(event), explicitRoot: true };
|
|
112
|
+
const quotes = new Set();
|
|
113
|
+
for (const match of event.content.matchAll(/(?:nostr:)?((?:note1|nevent1|naddr1)[0-9a-z]+)/g))
|
|
114
|
+
try {
|
|
115
|
+
const value = decode(match[1]);
|
|
116
|
+
if (value.type === "note")
|
|
117
|
+
quotes.add(value.data);
|
|
118
|
+
if (value.type === "nevent")
|
|
119
|
+
quotes.add(value.data.id);
|
|
120
|
+
if (value.type === "naddr")
|
|
121
|
+
quotes.add(`${value.data.kind}:${value.data.pubkey}:${value.data.identifier}`);
|
|
122
|
+
}
|
|
123
|
+
catch { }
|
|
124
|
+
const tags = event.tags.filter((t) => (t[0] === "e" || t[0] === "a") &&
|
|
125
|
+
t[3] !== "mention" &&
|
|
126
|
+
(t[3] === "root" ||
|
|
127
|
+
t[3] === "reply" ||
|
|
128
|
+
(!t[3] && t[0] === "e" && !quotes.has(t[1]))));
|
|
129
|
+
const roots = tags.filter((t) => t[3] === "root"), parents = tags.filter((t) => t[3] === "reply"), positional = tags.filter((t) => !t[3]);
|
|
130
|
+
if (roots.length > 1 || parents.length > 1)
|
|
131
|
+
throw new Error("Ambiguous legacy thread markers");
|
|
132
|
+
const root = roots[0] ?? positional[0], parent = parents[0] ?? positional.at(-1) ?? root;
|
|
133
|
+
const ref = (tag) => {
|
|
134
|
+
if (tag[0] === "a") {
|
|
135
|
+
const value = coordinate(tag[1]);
|
|
136
|
+
return {
|
|
137
|
+
address: tag[1],
|
|
138
|
+
kind: String(value.kind),
|
|
139
|
+
author: value.pubkey,
|
|
140
|
+
hint: tag[2],
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
if (!hexKey(tag[1]) || (tag[4] && !hexKey(tag[4])))
|
|
144
|
+
throw new Error("Malformed legacy thread reference");
|
|
145
|
+
return { id: tag[1], author: tag[4] || undefined, hint: tag[2] };
|
|
146
|
+
};
|
|
147
|
+
return {
|
|
148
|
+
root: root ? ref(root) : undefined,
|
|
149
|
+
parent: parent ? ref(parent) : undefined,
|
|
150
|
+
explicitRoot: roots.length > 0,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function matches(ref, event) {
|
|
154
|
+
return ((!ref.id || ref.id === event.id) &&
|
|
155
|
+
(!ref.address || ref.address === eventCoordinate(event)) &&
|
|
156
|
+
(!ref.kind || ref.kind === String(event.kind)) &&
|
|
157
|
+
(!ref.author || ref.author === event.pubkey));
|
|
158
|
+
}
|
|
159
|
+
export function threadLimit(limit = 30) {
|
|
160
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 100)
|
|
161
|
+
throw new TypeError("thread limit must be in 1..100");
|
|
162
|
+
return limit;
|
|
163
|
+
}
|
|
164
|
+
/** Bounded root-batched graph, followed only along the trigger's ancestor path. */
|
|
165
|
+
export class PublicThreads {
|
|
166
|
+
transport;
|
|
167
|
+
relays;
|
|
168
|
+
timeout;
|
|
169
|
+
maxEvents;
|
|
170
|
+
maxLookups;
|
|
171
|
+
maxDepth;
|
|
172
|
+
constructor(transport, relays, options = {}) {
|
|
173
|
+
this.transport = transport;
|
|
174
|
+
this.relays = relays;
|
|
175
|
+
this.timeout = options.timeoutMs ?? 3000;
|
|
176
|
+
this.maxEvents = options.maxEvents ?? 500;
|
|
177
|
+
this.maxLookups = options.maxLookups ?? 16;
|
|
178
|
+
this.maxDepth = options.maxDepth ?? 64;
|
|
179
|
+
for (const [name, value, min, max] of [
|
|
180
|
+
["timeoutMs", this.timeout, 1, 30000],
|
|
181
|
+
["maxEvents", this.maxEvents, 1, 2000],
|
|
182
|
+
["maxLookups", this.maxLookups, 1, 64],
|
|
183
|
+
["maxDepth", this.maxDepth, 1, 256],
|
|
184
|
+
])
|
|
185
|
+
if (!Number.isSafeInteger(value) || value < min || value > max)
|
|
186
|
+
throw new TypeError(`Invalid public thread ${name}`);
|
|
187
|
+
}
|
|
188
|
+
budget() {
|
|
189
|
+
const deadline = performance.now() + this.timeout;
|
|
190
|
+
let lookups = 0;
|
|
191
|
+
let bounded = false;
|
|
192
|
+
return {
|
|
193
|
+
get bounded() {
|
|
194
|
+
return bounded;
|
|
195
|
+
},
|
|
196
|
+
query: async (filter) => {
|
|
197
|
+
if (++lookups > this.maxLookups)
|
|
198
|
+
throw new Error("lookup-limit");
|
|
199
|
+
const left = deadline - performance.now();
|
|
200
|
+
if (left <= 0)
|
|
201
|
+
throw new Error("timeout");
|
|
202
|
+
let timer;
|
|
203
|
+
try {
|
|
204
|
+
const events = await Promise.race([
|
|
205
|
+
this.transport.query(this.relays, {
|
|
206
|
+
...filter,
|
|
207
|
+
limit: this.maxEvents,
|
|
208
|
+
}),
|
|
209
|
+
new Promise((_, reject) => {
|
|
210
|
+
timer = setTimeout(() => reject(new Error("timeout")), left);
|
|
211
|
+
}),
|
|
212
|
+
]);
|
|
213
|
+
if (events.length >= this.maxEvents)
|
|
214
|
+
bounded = true;
|
|
215
|
+
return events.flatMap((frame) => {
|
|
216
|
+
const event = publicEvent(frame);
|
|
217
|
+
return event && matchFilter(filter, event) ? [event] : [];
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
finally {
|
|
221
|
+
clearTimeout(timer);
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
filter(ref) {
|
|
227
|
+
if (ref.id)
|
|
228
|
+
return { ids: [ref.id] };
|
|
229
|
+
if (ref.address) {
|
|
230
|
+
const c = coordinate(ref.address);
|
|
231
|
+
return {
|
|
232
|
+
authors: [c.pubkey],
|
|
233
|
+
kinds: [c.kind],
|
|
234
|
+
...(c.kind >= 30000 ? { "#d": [c.identifier] } : {}),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
if (ref.id)
|
|
238
|
+
return { ids: [ref.id] };
|
|
239
|
+
}
|
|
240
|
+
async rootForReply(trigger) {
|
|
241
|
+
const budget = this.budget(), visited = new Set();
|
|
242
|
+
let current = trigger;
|
|
243
|
+
for (let depth = 0; depth < this.maxDepth; depth++) {
|
|
244
|
+
if (visited.has(current.id))
|
|
245
|
+
throw new Error("Cyclic public thread");
|
|
246
|
+
visited.add(current.id);
|
|
247
|
+
const structure = threadStructure(current);
|
|
248
|
+
if (current.kind === 1111) {
|
|
249
|
+
if (!structure.root)
|
|
250
|
+
throw new Error("Missing public thread root");
|
|
251
|
+
return structure.root;
|
|
252
|
+
}
|
|
253
|
+
if (!structure.parent)
|
|
254
|
+
return selfReference(current);
|
|
255
|
+
const ref = structure.explicitRoot ? structure.root : structure.parent;
|
|
256
|
+
if (!ref)
|
|
257
|
+
throw new Error("Missing public thread root");
|
|
258
|
+
const filter = this.filter(ref);
|
|
259
|
+
if (!filter)
|
|
260
|
+
throw new Error("Unresolved public root");
|
|
261
|
+
const events = (await budget.query(filter))
|
|
262
|
+
.filter((event) => matches(ref, event))
|
|
263
|
+
.sort((a, b) => b.created_at - a.created_at || a.id.localeCompare(b.id));
|
|
264
|
+
if (!events[0])
|
|
265
|
+
throw new Error("Missing public thread root");
|
|
266
|
+
current = events[0];
|
|
267
|
+
if (structure.explicitRoot) {
|
|
268
|
+
const parsed = threadStructure(current);
|
|
269
|
+
if (parsed.parent && current.kind === 1)
|
|
270
|
+
throw new Error("Conflicting legacy root");
|
|
271
|
+
if (current.kind === 1111) {
|
|
272
|
+
if (!parsed.root)
|
|
273
|
+
throw new Error("Missing public thread root");
|
|
274
|
+
return parsed.root;
|
|
275
|
+
}
|
|
276
|
+
return selfReference(current);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
throw new Error("Public thread depth limit");
|
|
280
|
+
}
|
|
281
|
+
async resolve(trigger, options = {}) {
|
|
282
|
+
const limit = threadLimit(options.limit), budget = this.budget(), events = new Map([[trigger.id, trigger]]), batched = new Set(), seen = new Set();
|
|
283
|
+
const ancestors = [];
|
|
284
|
+
let current = trigger, complete = true, reason;
|
|
285
|
+
const declared = trigger.kind === 1111 ? threadStructure(trigger).root : undefined;
|
|
286
|
+
const add = (items) => {
|
|
287
|
+
for (const event of items) {
|
|
288
|
+
if (events.size >= this.maxEvents && !events.has(event.id)) {
|
|
289
|
+
complete = false;
|
|
290
|
+
reason = "size-limit";
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
events.set(event.id, event);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
const find = (ref) => [...events.values()]
|
|
297
|
+
.filter((event) => matches(ref, event))
|
|
298
|
+
.sort((a, b) => b.created_at - a.created_at || a.id.localeCompare(b.id))[0];
|
|
299
|
+
const batch = async (ref) => {
|
|
300
|
+
const key = referenceKey(ref);
|
|
301
|
+
if (batched.has(key))
|
|
302
|
+
return;
|
|
303
|
+
batched.add(key);
|
|
304
|
+
const filters = [];
|
|
305
|
+
const target = this.filter(ref);
|
|
306
|
+
if (target)
|
|
307
|
+
filters.push(target);
|
|
308
|
+
if (ref.id)
|
|
309
|
+
filters.push({ kinds: [1], "#e": [ref.id] }, { kinds: [1111], "#E": [ref.id] });
|
|
310
|
+
if (ref.address)
|
|
311
|
+
filters.push({ kinds: [1], "#a": [ref.address] }, { kinds: [1111], "#A": [ref.address] });
|
|
312
|
+
if (ref.external)
|
|
313
|
+
filters.push({ kinds: [1111], "#I": [ref.external] });
|
|
314
|
+
add((await Promise.all(filters.map((filter) => budget.query(filter)))).flat());
|
|
315
|
+
const root = find(ref), address = root && eventCoordinate(root);
|
|
316
|
+
if (address && !ref.address)
|
|
317
|
+
await batch({ ...ref, address });
|
|
318
|
+
};
|
|
319
|
+
try {
|
|
320
|
+
const structure = threadStructure(trigger);
|
|
321
|
+
if (structure.root)
|
|
322
|
+
await batch(structure.root);
|
|
323
|
+
for (let depth = 0; depth < this.maxDepth; depth++) {
|
|
324
|
+
if (seen.has(current.id)) {
|
|
325
|
+
complete = false;
|
|
326
|
+
reason = "cycle";
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
seen.add(current.id);
|
|
330
|
+
const parts = threadStructure(current);
|
|
331
|
+
if (current.kind === 1111 && parts.root)
|
|
332
|
+
await batch(parts.root);
|
|
333
|
+
const parent = parts.parent;
|
|
334
|
+
if (!parent)
|
|
335
|
+
break;
|
|
336
|
+
if (parent.external) {
|
|
337
|
+
if (!parts.root ||
|
|
338
|
+
referenceKey(parent) !== referenceKey(parts.root)) {
|
|
339
|
+
complete = false;
|
|
340
|
+
reason = "external-parent";
|
|
341
|
+
}
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
let next = find(parent);
|
|
345
|
+
if (!next) {
|
|
346
|
+
const filter = this.filter(parent);
|
|
347
|
+
if (filter)
|
|
348
|
+
add(await budget.query(filter));
|
|
349
|
+
next = find(parent);
|
|
350
|
+
}
|
|
351
|
+
if (!next) {
|
|
352
|
+
complete = false;
|
|
353
|
+
reason = "missing-parent";
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
if (next.id === trigger.id || seen.has(next.id)) {
|
|
357
|
+
complete = false;
|
|
358
|
+
reason = "cycle";
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
if (declared) {
|
|
362
|
+
const nextParts = threadStructure(next);
|
|
363
|
+
if (next.kind === 1111 &&
|
|
364
|
+
nextParts.root &&
|
|
365
|
+
referenceKey(nextParts.root) !== referenceKey(declared)) {
|
|
366
|
+
complete = false;
|
|
367
|
+
reason = "root-mismatch";
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
if (!nextParts.parent && !matches(declared, next)) {
|
|
371
|
+
complete = false;
|
|
372
|
+
reason = "root-mismatch";
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
ancestors.push(next);
|
|
377
|
+
current = next;
|
|
378
|
+
if (depth === this.maxDepth - 1) {
|
|
379
|
+
complete = false;
|
|
380
|
+
reason = "depth-limit";
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
catch (error) {
|
|
385
|
+
complete = false;
|
|
386
|
+
reason =
|
|
387
|
+
error instanceof Error &&
|
|
388
|
+
["timeout", "lookup-limit"].includes(error.message)
|
|
389
|
+
? error.message
|
|
390
|
+
: "query-failed";
|
|
391
|
+
}
|
|
392
|
+
if (budget.bounded) {
|
|
393
|
+
complete = false;
|
|
394
|
+
reason ??= "query-window";
|
|
395
|
+
}
|
|
396
|
+
if (ancestors.length > limit) {
|
|
397
|
+
complete = false;
|
|
398
|
+
reason ??= "entry-limit";
|
|
399
|
+
}
|
|
400
|
+
// Topological oldest-to-parent order, never timestamp-sort sibling events into the context.
|
|
401
|
+
return {
|
|
402
|
+
entries: ancestors.slice(0, limit).reverse().map(inputOf),
|
|
403
|
+
complete,
|
|
404
|
+
coverage: "public-ancestors",
|
|
405
|
+
...(reason ? { reason } : {}),
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
export function commentReply(trigger, root, text, hint = "") {
|
|
410
|
+
if (!text)
|
|
411
|
+
throw new TypeError("Nonempty reply text required");
|
|
412
|
+
if (!root.kind || (!root.external && !root.author))
|
|
413
|
+
throw new Error("Incomplete public root");
|
|
414
|
+
const tags = [];
|
|
415
|
+
if (root.address)
|
|
416
|
+
tags.push(["A", root.address]);
|
|
417
|
+
else if (root.id) {
|
|
418
|
+
if (!root.author)
|
|
419
|
+
throw new Error("Incomplete public root");
|
|
420
|
+
tags.push(["E", root.id, "", root.author]);
|
|
421
|
+
}
|
|
422
|
+
else if (root.external)
|
|
423
|
+
tags.push(["I", root.external]);
|
|
424
|
+
else
|
|
425
|
+
throw new Error("Missing public root");
|
|
426
|
+
tags.push(["K", root.kind]);
|
|
427
|
+
if (root.author)
|
|
428
|
+
tags.push(["P", root.author]);
|
|
429
|
+
tags.push(["e", trigger.id, hint, trigger.pubkey], ["k", String(trigger.kind)], ["p", trigger.pubkey]);
|
|
430
|
+
return {
|
|
431
|
+
kind: 1111,
|
|
432
|
+
created_at: Math.floor(Date.now() / 1000),
|
|
433
|
+
tags,
|
|
434
|
+
content: text,
|
|
435
|
+
};
|
|
436
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import WebSocket from "ws";
|
|
2
|
+
import { AbstractRelay, type Subscription as NostrSubscription } from "nostr-tools/abstract-relay";
|
|
3
|
+
import { type Event, type EventTemplate, type Filter } from "nostr-tools";
|
|
4
|
+
import { RelayError } from "./errors.js";
|
|
5
|
+
export interface RelayTimeouts {
|
|
6
|
+
connectionTimeoutMs: number;
|
|
7
|
+
eoseTimeoutMs: number;
|
|
8
|
+
publishTimeoutMs: number;
|
|
9
|
+
authTimeoutMs: number;
|
|
10
|
+
idleTimeoutMs: number;
|
|
11
|
+
heartbeatIntervalMs: number;
|
|
12
|
+
heartbeatTimeoutMs: number;
|
|
13
|
+
}
|
|
14
|
+
export declare const DEFAULT_RELAY_TIMEOUTS: Readonly<RelayTimeouts>;
|
|
15
|
+
interface Consumer {
|
|
16
|
+
failed(error: RelayError): void;
|
|
17
|
+
authenticating(): void;
|
|
18
|
+
authenticated(): void;
|
|
19
|
+
wireEose(id: string): void;
|
|
20
|
+
rawEvent(id: string): void;
|
|
21
|
+
}
|
|
22
|
+
export interface RelayLease {
|
|
23
|
+
readonly authenticating: boolean;
|
|
24
|
+
ready: Promise<void>;
|
|
25
|
+
subscribe(filter: Filter, params: Parameters<AbstractRelay["subscribe"]>[1]): NostrSubscription;
|
|
26
|
+
publish(event: Event, afterAuth?: boolean): Promise<string>;
|
|
27
|
+
release(): void;
|
|
28
|
+
}
|
|
29
|
+
/** One nostr-tools physical relay per exact URL. Durable retries belong to InboxSubscription. */
|
|
30
|
+
export declare class RelayPool {
|
|
31
|
+
private timeouts;
|
|
32
|
+
private sign?;
|
|
33
|
+
private createSocket;
|
|
34
|
+
private connections;
|
|
35
|
+
private closed;
|
|
36
|
+
constructor(timeouts: RelayTimeouts, sign?: ((template: EventTemplate) => Promise<Event>) | undefined, createSocket?: (url: string) => WebSocket);
|
|
37
|
+
isConnected(url: string): boolean;
|
|
38
|
+
acquire(url: string, consumer: Consumer): RelayLease;
|
|
39
|
+
private error;
|
|
40
|
+
private connect;
|
|
41
|
+
close(): void;
|
|
42
|
+
}
|
|
43
|
+
export {};
|