@peer2peer/relay 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +19 -2
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +88 -0
- package/dist/client.d.ts +3 -0
- package/dist/client.js +23 -0
- package/dist/config.d.ts +266 -0
- package/dist/config.js +326 -0
- package/dist/crypto.d.ts +159 -0
- package/dist/crypto.js +514 -0
- package/dist/identity.d.ts +84 -0
- package/dist/identity.js +386 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +14 -0
- package/dist/links.d.ts +29 -0
- package/dist/links.js +98 -0
- package/dist/log.d.ts +19 -0
- package/dist/log.js +35 -0
- package/dist/members.d.ts +109 -0
- package/dist/members.js +369 -0
- package/dist/metrics.d.ts +13 -0
- package/dist/metrics.js +66 -0
- package/dist/net.d.ts +23 -0
- package/dist/net.js +103 -0
- package/dist/ratelimit.d.ts +16 -0
- package/dist/ratelimit.js +70 -0
- package/dist/server.d.ts +271 -0
- package/dist/server.js +1951 -0
- package/dist/sqlite-store.d.ts +184 -0
- package/dist/sqlite-store.js +1068 -0
- package/dist/store.d.ts +368 -0
- package/dist/store.js +492 -0
- package/package.json +57 -4
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
export interface StoredEnvelope {
|
|
2
|
+
/** Monotonic, never reused: arrival order within the relay. Sent on the wire only encrypted. */
|
|
3
|
+
rid: number;
|
|
4
|
+
to: string;
|
|
5
|
+
tdid: string;
|
|
6
|
+
env: string;
|
|
7
|
+
size: number;
|
|
8
|
+
receivedAt: number;
|
|
9
|
+
}
|
|
10
|
+
export interface NewEnvelope {
|
|
11
|
+
to: string;
|
|
12
|
+
tdid: string;
|
|
13
|
+
env: string;
|
|
14
|
+
size: number;
|
|
15
|
+
receivedAt: number;
|
|
16
|
+
/** Keyed hash of the sender's IP bucket. */
|
|
17
|
+
src: string;
|
|
18
|
+
}
|
|
19
|
+
export interface MailboxCaps {
|
|
20
|
+
maxEnvelopes: number;
|
|
21
|
+
maxBytes: number;
|
|
22
|
+
}
|
|
23
|
+
export interface StoredIdentityDoc {
|
|
24
|
+
hash: string;
|
|
25
|
+
iss: string;
|
|
26
|
+
ver: number;
|
|
27
|
+
prev: string | null;
|
|
28
|
+
merge: string | null;
|
|
29
|
+
jws: string;
|
|
30
|
+
devices: string[];
|
|
31
|
+
/** Seconds. */
|
|
32
|
+
iat: number;
|
|
33
|
+
exp: number;
|
|
34
|
+
receivedAt: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Derived per-userId state, rewritten on every accepted publish. A tombstone
|
|
38
|
+
* (`leftAt` set) has no documents, but keeps the last allowlist and head
|
|
39
|
+
* version, so rule 6 and the version floor survive the documents.
|
|
40
|
+
*/
|
|
41
|
+
export interface IdentityIndex {
|
|
42
|
+
iss: string;
|
|
43
|
+
/** Hash of the head (highest ver; tie: higher hash). Empty for a tombstone. */
|
|
44
|
+
head: string;
|
|
45
|
+
headVer: number;
|
|
46
|
+
/** The head's `exp`, in seconds. */
|
|
47
|
+
headExp: number;
|
|
48
|
+
/** Hashes of every branch tip, highest first. Empty for a tombstone. */
|
|
49
|
+
tips: string[];
|
|
50
|
+
/** §2.3 rule 6: devices listed in every tip, intersected with `cap` when set. */
|
|
51
|
+
allowed: string[];
|
|
52
|
+
/**
|
|
53
|
+
* Set while the held history doesn't chain to what the relay knew before
|
|
54
|
+
* (a replaced unanchored root, or a re-entry after a tombstone): the
|
|
55
|
+
* allowlist can't grow past it until a document chains onto that history.
|
|
56
|
+
*/
|
|
57
|
+
cap: string[] | null;
|
|
58
|
+
/** When the identity was tombstoned (owner left, or head long expired). */
|
|
59
|
+
leftAt: number | null;
|
|
60
|
+
}
|
|
61
|
+
/** Seen-device rows kept per userId (128 versions × 10 devices, with headroom). */
|
|
62
|
+
export declare const MAX_SEEN_PER_ISS = 2048;
|
|
63
|
+
/**
|
|
64
|
+
* A short-code invite mailbox entry (P4): an opaque sealed blob the relay
|
|
65
|
+
* can't read, keyed by `lid` (a hash of the code secret it never sees).
|
|
66
|
+
*/
|
|
67
|
+
export interface StoredInvite {
|
|
68
|
+
lid: string;
|
|
69
|
+
/** The userId that put it (per-iss caps; only it may list or delete it). */
|
|
70
|
+
iss: string;
|
|
71
|
+
blob: string;
|
|
72
|
+
size: number;
|
|
73
|
+
createdAt: number;
|
|
74
|
+
expiresAt: number;
|
|
75
|
+
/** Keyed hash of the putter's IP bucket (/48 for IPv6), under a key held only in memory, as for envelopes. */
|
|
76
|
+
src: string;
|
|
77
|
+
}
|
|
78
|
+
export interface CapState {
|
|
79
|
+
revoked: boolean;
|
|
80
|
+
uses: number;
|
|
81
|
+
}
|
|
82
|
+
export interface SweepCutoffs {
|
|
83
|
+
/** Capability state untouched since this is deleted (caps live ≤ 90 days). */
|
|
84
|
+
capsBefore: number;
|
|
85
|
+
/** Identity versions received before this are deleted unless they are a branch tip. */
|
|
86
|
+
identityVersionsBefore: number;
|
|
87
|
+
/** Seen-device rows older than this (ms) are deleted. */
|
|
88
|
+
seenBefore?: number;
|
|
89
|
+
/** Now (ms): when swept versions' dids outside the allowlist are remembered (R4-M1). */
|
|
90
|
+
nowMs?: number;
|
|
91
|
+
/** Device bindings not seen since this are deleted. */
|
|
92
|
+
devicesBefore: number;
|
|
93
|
+
}
|
|
94
|
+
export interface SweepResult {
|
|
95
|
+
caps: number;
|
|
96
|
+
identityDocs: number;
|
|
97
|
+
devices: number;
|
|
98
|
+
/** Seen-device rows deleted (past the tombstone window). */
|
|
99
|
+
seen?: number;
|
|
100
|
+
}
|
|
101
|
+
export interface StoreCounts {
|
|
102
|
+
envelopes: number;
|
|
103
|
+
envelopeBytes: number;
|
|
104
|
+
devices: number;
|
|
105
|
+
identityDocs: number;
|
|
106
|
+
}
|
|
107
|
+
export interface Store {
|
|
108
|
+
/**
|
|
109
|
+
* Prepares the store before the relay accepts sends: rebuilds the running
|
|
110
|
+
* totals (and derived device state) in chunks that yield to the event loop.
|
|
111
|
+
*/
|
|
112
|
+
init(): Promise<void>;
|
|
113
|
+
/** Runs `fn` atomically (re-entrant: nested calls join the outer transaction). */
|
|
114
|
+
transaction<T>(fn: () => T): T;
|
|
115
|
+
getMeta(key: string): string | undefined;
|
|
116
|
+
setMeta(key: string, value: string): void;
|
|
117
|
+
deviceIssuer(did: string): string | undefined;
|
|
118
|
+
/** Binds did -> iss if unbound (returns true), and refreshes last-seen when it is bound to iss. */
|
|
119
|
+
touchDevice(did: string, iss: string, now: number): boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Bindings that could be evicted, least recently seen first (ties by did):
|
|
122
|
+
* no stored envelopes and not in any held identity allowlist. Keyset paging:
|
|
123
|
+
* pass the last row of the previous page as `after`. The caller still skips
|
|
124
|
+
* live connections.
|
|
125
|
+
*/
|
|
126
|
+
evictableDevices(limit: number, after?: {
|
|
127
|
+
lastSeen: number;
|
|
128
|
+
did: string;
|
|
129
|
+
}): {
|
|
130
|
+
did: string;
|
|
131
|
+
iss: string;
|
|
132
|
+
lastSeen: number;
|
|
133
|
+
}[];
|
|
134
|
+
deleteDevice(did: string): void;
|
|
135
|
+
/** Appends an envelope, then drops the mailbox's oldest until it fits `caps`. */
|
|
136
|
+
putEnvelope(e: NewEnvelope, caps: MailboxCaps): {
|
|
137
|
+
rid: number;
|
|
138
|
+
dropped: number;
|
|
139
|
+
droppedBytes: number;
|
|
140
|
+
};
|
|
141
|
+
/** Envelopes of mailbox (to, tdid) with rid > afterRid received at or after notBefore, oldest first. */
|
|
142
|
+
envelopesAfter(to: string, tdid: string, afterRid: number, notBefore: number, limit: number): StoredEnvelope[];
|
|
143
|
+
/** Deletes the given rids, but only those in mailbox (to, tdid). */
|
|
144
|
+
ackEnvelopes(to: string, tdid: string, rids: readonly number[]): {
|
|
145
|
+
count: number;
|
|
146
|
+
bytes: number;
|
|
147
|
+
};
|
|
148
|
+
/** Stored bytes addressed to `to` (every mailbox of that userId). A counter read, never an aggregate. */
|
|
149
|
+
bytesForRecipient(to: string): number;
|
|
150
|
+
/** Stored bytes in mailbox (to, tdid). A counter read. */
|
|
151
|
+
mailboxBytes(to: string, tdid: string): number;
|
|
152
|
+
/**
|
|
153
|
+
* Frees at least `bytes` from recipient `to`: the source holding the most
|
|
154
|
+
* bytes for `to` loses its oldest envelopes to `to` first. Returns the bytes freed.
|
|
155
|
+
*/
|
|
156
|
+
evictRecipient(to: string, bytes: number, policy?: EvictPolicy): number;
|
|
157
|
+
/**
|
|
158
|
+
* Frees at least `bytes` store-wide: the source holding the most bytes loses
|
|
159
|
+
* the oldest envelopes of its largest mailbox first. Returns the bytes freed.
|
|
160
|
+
*/
|
|
161
|
+
evictGlobal(bytes: number, policy?: EvictPolicy): number;
|
|
162
|
+
/** Stored bytes tagged with source `src`. A counter read. */
|
|
163
|
+
sourceBytes(src: string): number;
|
|
164
|
+
/** Frees at least `bytes` of source `src`'s own envelopes, oldest of its largest mailbox first. */
|
|
165
|
+
evictSource(src: string, bytes: number): number;
|
|
166
|
+
/** Deletes up to `limit` envelopes received before `before`, oldest first, keeping totals exact. */
|
|
167
|
+
expireBatch(before: number, limit: number): {
|
|
168
|
+
count: number;
|
|
169
|
+
bytes: number;
|
|
170
|
+
};
|
|
171
|
+
capState(key: string): CapState;
|
|
172
|
+
revokeCap(key: string, now: number): void;
|
|
173
|
+
useCap(key: string, now: number): void;
|
|
174
|
+
identityDoc(hash: string): StoredIdentityDoc | undefined;
|
|
175
|
+
identityDocs(iss: string): StoredIdentityDoc[];
|
|
176
|
+
/** False when the hash is already stored. */
|
|
177
|
+
putIdentityDoc(d: StoredIdentityDoc): boolean;
|
|
178
|
+
deleteIdentityDocs(hashes: readonly string[]): void;
|
|
179
|
+
/** Deletes every document of `iss`, keeping its index row. */
|
|
180
|
+
deleteIdentityDocsOf(iss: string): void;
|
|
181
|
+
identityIndex(iss: string): IdentityIndex | undefined;
|
|
182
|
+
putIdentityIndex(idx: IdentityIndex): void;
|
|
183
|
+
/** Deletes every document and the index of `iss`. */
|
|
184
|
+
deleteIdentity(iss: string): void;
|
|
185
|
+
/**
|
|
186
|
+
* Device ids of history this relay discarded for `iss` (a tombstone, or a
|
|
187
|
+
* replaced unanchored root), with when (ms). Restricted auth refuses them
|
|
188
|
+
* (they were listed once and aren't now); kept for the tombstone window
|
|
189
|
+
* (91 days, longer than any certificate lives), capped per userId, and
|
|
190
|
+
* deleted with the identity. Nothing else about revocations is stored:
|
|
191
|
+
* clients enforce no-return from their own chains.
|
|
192
|
+
*/
|
|
193
|
+
seenDevices(iss: string, sinceMs: number): string[];
|
|
194
|
+
addSeen(iss: string, dids: readonly string[], at: number): void;
|
|
195
|
+
/** Live (non-tombstone) userIds whose head `exp` (seconds) is before `headExpBeforeSec`. */
|
|
196
|
+
expiredIdentities(headExpBeforeSec: number, limit: number): string[];
|
|
197
|
+
/** Tombstoned userIds whose `leftAt` is before `leftBefore`. */
|
|
198
|
+
staleTombstones(leftBefore: number, limit: number): string[];
|
|
199
|
+
/** P4 invite mailboxes. False when `lid` is already held (never overwritten). */
|
|
200
|
+
putInvite(i: StoredInvite): boolean;
|
|
201
|
+
/** Deletes and returns the live entry for `lid` (single use); expired entries are deleted and not returned. */
|
|
202
|
+
takeInvite(lid: string, now: number): StoredInvite | undefined;
|
|
203
|
+
/** Deletes `lid` only if `iss` put it; true when something was deleted. */
|
|
204
|
+
deleteInvite(lid: string, iss: string): boolean;
|
|
205
|
+
/** The live lids `iss` holds, oldest first. */
|
|
206
|
+
invitesOf(iss: string, now: number): string[];
|
|
207
|
+
/** Lids of `iss` the relay evicted (full store) that would not have expired yet. */
|
|
208
|
+
evictedInvitesOf(iss: string, now: number): string[];
|
|
209
|
+
/** Invite bytes `iss` holds. A counter read (kept per userId). */
|
|
210
|
+
inviteBytesOfIss(iss: string): number;
|
|
211
|
+
/** Bytes held across every invite (live or not yet swept). A counter read. */
|
|
212
|
+
inviteBytes(): number;
|
|
213
|
+
/** Bytes held by invites tagged with source `src`. */
|
|
214
|
+
inviteBytesOfSrc(src: string): number;
|
|
215
|
+
/**
|
|
216
|
+
* Frees at least `bytes` (P4, full store). With `now`, invites already
|
|
217
|
+
* expired at `now` go first (oldest expiry first, no tombstone), so a sweep
|
|
218
|
+
* that hasn't caught up never costs a live invite. Then the userId holding
|
|
219
|
+
* the most invite bytes (a per-userId counter, not an aggregate) loses its
|
|
220
|
+
* newest invite, repeatedly, each leaving an `evicted` tombstone until it
|
|
221
|
+
* would have expired. Returns the bytes freed.
|
|
222
|
+
*/
|
|
223
|
+
evictInvites(bytes: number, now?: number): number;
|
|
224
|
+
/**
|
|
225
|
+
* Deletes at most `limit` expired invites (oldest expiry first, keeping the
|
|
226
|
+
* per-userId totals row by row) and at most `limit` expired `evicted`
|
|
227
|
+
* tombstones. Callers loop and yield (F-4).
|
|
228
|
+
*/
|
|
229
|
+
sweepInvites(now: number, limit: number): {
|
|
230
|
+
invites: number;
|
|
231
|
+
evicted: number;
|
|
232
|
+
};
|
|
233
|
+
/** Deletes at most `limit` expired rows from each of caps, identity versions and devices (F-4: callers loop and yield). */
|
|
234
|
+
sweep(cutoffs: SweepCutoffs, limit: number): SweepResult;
|
|
235
|
+
/** Stored envelope count and bytes: a counter read. */
|
|
236
|
+
envelopeTotals(): {
|
|
237
|
+
envelopes: number;
|
|
238
|
+
bytes: number;
|
|
239
|
+
};
|
|
240
|
+
counts(): StoreCounts;
|
|
241
|
+
/** A cheap liveness probe for /healthz. */
|
|
242
|
+
ping(): boolean;
|
|
243
|
+
close(): void;
|
|
244
|
+
}
|
|
245
|
+
/** Every identity version referenced as `prev` or `merge` by another held version. */
|
|
246
|
+
export declare function referencedHashes(docs: readonly StoredIdentityDoc[]): Set<string>;
|
|
247
|
+
/**
|
|
248
|
+
* How eviction ranks source groups (F-6, revised after the pre-release review). No
|
|
249
|
+
* group is preferred for its age or the process that tagged it:
|
|
250
|
+
* 1. the sending source pays first, but only when its group is over its
|
|
251
|
+
* fair share: more than both `floor` and `fairShare` (the quota divided
|
|
252
|
+
* by the number of groups). An honest sender to a full mailbox is under
|
|
253
|
+
* its share, so it never evicts its own older mail;
|
|
254
|
+
* 2. otherwise the largest group among all groups above `floor`;
|
|
255
|
+
* 3. and only when none is above it, the largest group overall.
|
|
256
|
+
* So a flood (before or after a restart, from one source or spread across
|
|
257
|
+
* many under the floor) pays for itself before it touches anyone else's
|
|
258
|
+
* parked mail, and a restart doesn't make older groups preferred victims.
|
|
259
|
+
*/
|
|
260
|
+
export interface EvictPolicy {
|
|
261
|
+
floor?: number;
|
|
262
|
+
sender?: string;
|
|
263
|
+
/** The quota the eviction is for: the fair share is quota / number of groups. */
|
|
264
|
+
quota?: number;
|
|
265
|
+
}
|
|
266
|
+
/** Picks the group to evict from, per EvictPolicy. `groups` defaults to the candidates' count. */
|
|
267
|
+
export declare function pickSource(cands: readonly {
|
|
268
|
+
src: string;
|
|
269
|
+
bytes: number;
|
|
270
|
+
}[], policy?: EvictPolicy, groups?: number): string | undefined;
|
|
271
|
+
/** The running totals an envelope counts toward: (kind, key). */
|
|
272
|
+
export declare function totalKeys(to: string, tdid: string, src: string): [string, string][];
|
|
273
|
+
export declare class MemoryStore implements Store {
|
|
274
|
+
private readonly devices;
|
|
275
|
+
private readonly mailboxes;
|
|
276
|
+
private readonly caps;
|
|
277
|
+
private readonly docs;
|
|
278
|
+
private readonly index;
|
|
279
|
+
private readonly seen;
|
|
280
|
+
private readonly revoked;
|
|
281
|
+
private readonly meta;
|
|
282
|
+
private readonly invites;
|
|
283
|
+
/** Invite bytes per userId (eviction picks the biggest holder without a scan). */
|
|
284
|
+
private readonly inviteIss;
|
|
285
|
+
/** Evicted lids, until their invite would have expired (so the inviter can tell evicted from taken). */
|
|
286
|
+
private readonly evicted;
|
|
287
|
+
private inviteTotal;
|
|
288
|
+
private nextRid;
|
|
289
|
+
init(): Promise<void>;
|
|
290
|
+
transaction<T>(fn: () => T): T;
|
|
291
|
+
getMeta(key: string): string | undefined;
|
|
292
|
+
setMeta(key: string, value: string): void;
|
|
293
|
+
evictableDevices(limit: number, after?: {
|
|
294
|
+
lastSeen: number;
|
|
295
|
+
did: string;
|
|
296
|
+
}): {
|
|
297
|
+
did: string;
|
|
298
|
+
iss: string;
|
|
299
|
+
lastSeen: number;
|
|
300
|
+
}[];
|
|
301
|
+
deleteDevice(did: string): void;
|
|
302
|
+
deviceIssuer(did: string): string | undefined;
|
|
303
|
+
touchDevice(did: string, iss: string, now: number): boolean;
|
|
304
|
+
putEnvelope(e: NewEnvelope, caps: MailboxCaps): {
|
|
305
|
+
rid: number;
|
|
306
|
+
dropped: number;
|
|
307
|
+
droppedBytes: number;
|
|
308
|
+
};
|
|
309
|
+
envelopesAfter(to: string, tdid: string, afterRid: number, notBefore: number, limit: number): StoredEnvelope[];
|
|
310
|
+
ackEnvelopes(to: string, tdid: string, rids: readonly number[]): {
|
|
311
|
+
count: number;
|
|
312
|
+
bytes: number;
|
|
313
|
+
};
|
|
314
|
+
bytesForRecipient(to: string): number;
|
|
315
|
+
mailboxBytes(to: string, tdid: string): number;
|
|
316
|
+
private all;
|
|
317
|
+
private remove;
|
|
318
|
+
/** The key with the most bytes, grouping `items` by `keyOf`. */
|
|
319
|
+
private static largest;
|
|
320
|
+
/** Bytes per source among `items`. */
|
|
321
|
+
private static sums;
|
|
322
|
+
evictRecipient(to: string, bytes: number, policy?: EvictPolicy): number;
|
|
323
|
+
evictGlobal(bytes: number, policy?: EvictPolicy): number;
|
|
324
|
+
sourceBytes(src: string): number;
|
|
325
|
+
evictSource(src: string, bytes: number): number;
|
|
326
|
+
expireBatch(before: number, limit: number): {
|
|
327
|
+
count: number;
|
|
328
|
+
bytes: number;
|
|
329
|
+
};
|
|
330
|
+
capState(key: string): CapState;
|
|
331
|
+
revokeCap(key: string, now: number): void;
|
|
332
|
+
useCap(key: string, now: number): void;
|
|
333
|
+
identityDoc(hash: string): StoredIdentityDoc | undefined;
|
|
334
|
+
identityDocs(iss: string): StoredIdentityDoc[];
|
|
335
|
+
putIdentityDoc(d: StoredIdentityDoc): boolean;
|
|
336
|
+
deleteIdentityDocs(hashes: readonly string[]): void;
|
|
337
|
+
deleteIdentityDocsOf(iss: string): void;
|
|
338
|
+
identityIndex(iss: string): IdentityIndex | undefined;
|
|
339
|
+
putIdentityIndex(idx: IdentityIndex): void;
|
|
340
|
+
deleteIdentity(iss: string): void;
|
|
341
|
+
seenDevices(iss: string, sinceMs: number): string[];
|
|
342
|
+
addSeen(iss: string, dids: readonly string[], at: number): void;
|
|
343
|
+
expiredIdentities(headExpBeforeSec: number, limit: number): string[];
|
|
344
|
+
staleTombstones(leftBefore: number, limit: number): string[];
|
|
345
|
+
envelopeTotals(): {
|
|
346
|
+
envelopes: number;
|
|
347
|
+
bytes: number;
|
|
348
|
+
};
|
|
349
|
+
sweep(c: SweepCutoffs, limit: number): SweepResult;
|
|
350
|
+
private issAdd;
|
|
351
|
+
private dropInvite;
|
|
352
|
+
putInvite(i: StoredInvite): boolean;
|
|
353
|
+
takeInvite(lid: string, now: number): StoredInvite | undefined;
|
|
354
|
+
deleteInvite(lid: string, iss: string): boolean;
|
|
355
|
+
invitesOf(iss: string, now: number): string[];
|
|
356
|
+
evictedInvitesOf(iss: string, now: number): string[];
|
|
357
|
+
inviteBytes(): number;
|
|
358
|
+
inviteBytesOfIss(iss: string): number;
|
|
359
|
+
inviteBytesOfSrc(src: string): number;
|
|
360
|
+
evictInvites(bytes: number, now?: number): number;
|
|
361
|
+
sweepInvites(now: number, limit: number): {
|
|
362
|
+
invites: number;
|
|
363
|
+
evicted: number;
|
|
364
|
+
};
|
|
365
|
+
counts(): StoreCounts;
|
|
366
|
+
ping(): boolean;
|
|
367
|
+
close(): void;
|
|
368
|
+
}
|