@biolibs/bionet-client 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/dist/lab.js ADDED
@@ -0,0 +1,434 @@
1
+ /**
2
+ * One person's device in one lab: its key, what it holds, and everything it can do.
3
+ *
4
+ * This is a whole lab application without a window. It knows nothing of Electron, React or a
5
+ * browser, so the same class sits behind a desktop app, a web page or a test, and two of these and a
6
+ * web peer are a lab.
7
+ *
8
+ * Everything a member does becomes a signed entry in this device's own log, is kept, and is offered
9
+ * to the lab's web peer when there is one to offer it to. Nothing waits for the network. What the lab
10
+ * holds is worked out from the entries each time they change, by `mergeLog`, which is why two devices
11
+ * that have been apart agree the moment they have each other's entries.
12
+ */
13
+ import { addItem, addPart, addPlace, addToRoster, adjustItem, entryId, fetchInventory, foundLab, isMember, mergeLog, movePlace, newItemId, newPlaceId, nextEntry, offerPart, publicInventory, putItem, recountItem, retireItem, retirePlace, setField, signInventory, standPlace, takeEntries } from "@biolibs/bionet";
14
+ import { exportSigningKey, generateSigningKey, importSigningKey, keyFingerprint } from "@biolibs/opencloning";
15
+ import { openKey, sealKey } from "./backup.js";
16
+ import { PeerLink } from "./link.js";
17
+ /** @param peer - The peer's socket address. @returns The web address it serves from. */
18
+ export function originOf(peer) {
19
+ const url = new URL(peer);
20
+ return `${url.protocol === "ws:" ? "http:" : "https:"}//${url.host}`;
21
+ }
22
+ /**
23
+ * Read an invitation: the peer's address, then `#`, then the lab's ID.
24
+ *
25
+ * @param invite - What a keeper sent.
26
+ * @returns Where the lab's peer is, and which lab.
27
+ * @throws If it is not an invitation.
28
+ */
29
+ export function readInvite(invite) {
30
+ const [peer, lab] = invite.trim().split("#");
31
+ if (peer === undefined || lab === undefined || !/^[0-9a-f]{64}$/.test(lab)) {
32
+ throw new Error("That is not an invitation. One looks like wss://a-lab.example/sync#, followed by 64 letters and digits.");
33
+ }
34
+ const url = new URL(peer);
35
+ if (url.protocol !== "wss:" && url.protocol !== "ws:")
36
+ throw new Error("An invitation's address starts with wss://.");
37
+ return { peer, lab };
38
+ }
39
+ const peerAddress = (peer) => {
40
+ if (peer === null || peer === undefined || peer.trim() === "")
41
+ return null;
42
+ const url = new URL(peer.trim());
43
+ if (url.protocol !== "wss:" && url.protocol !== "ws:")
44
+ throw new Error("A peer's address starts with wss://.");
45
+ return peer.trim();
46
+ };
47
+ /**
48
+ * One device in one lab.
49
+ *
50
+ * Make one with somewhere to keep things and somewhere to keep the key, `open()` it, and it is a
51
+ * working lab application: it makes the device's key if there is none, works the lab out from what
52
+ * is held, and starts talking to the lab's peer if it has one.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * import { Lab, memoryKeys, memoryStore } from "@biolibs/bionet-client";
57
+ *
58
+ * const lab = await new Lab({ store: memoryStore(), keys: memoryKeys() }).open();
59
+ * await lab.setPerson("Ada");
60
+ * await lab.found({ name: "The Wren Lab" });
61
+ * lab.snapshot().lab?.keeper; // true
62
+ * ```
63
+ */
64
+ export class Lab {
65
+ options;
66
+ store;
67
+ held = [];
68
+ state = null;
69
+ link = null;
70
+ key;
71
+ fingerprint = "";
72
+ fingerprints = new Map();
73
+ listeners = new Set();
74
+ cached = null;
75
+ /** One change at a time, so two cannot both write the same place in the log. */
76
+ turn = Promise.resolve();
77
+ constructor(options) {
78
+ this.options = options;
79
+ this.store = options.store;
80
+ }
81
+ /**
82
+ * Load or make the device's key, and work the lab out from what is held.
83
+ *
84
+ * @returns This, ready.
85
+ */
86
+ async open() {
87
+ let exported = await this.options.keys.load();
88
+ if (exported === null) {
89
+ exported = await exportSigningKey(await generateSigningKey());
90
+ await this.options.keys.save(exported);
91
+ }
92
+ this.key = await importSigningKey(exported);
93
+ this.fingerprint = await keyFingerprint(this.key.publicKey);
94
+ this.held = this.store.entries();
95
+ await this.remerge();
96
+ this.connect();
97
+ return this;
98
+ }
99
+ /**
100
+ * Be told whenever anything a window would draw has changed.
101
+ *
102
+ * @param listener - Called with nothing; ask `snapshot()` for what is new.
103
+ * @returns A function that stops the telling.
104
+ */
105
+ subscribe = (listener) => {
106
+ this.listeners.add(listener);
107
+ return () => { this.listeners.delete(listener); };
108
+ };
109
+ /**
110
+ * Everything a window needs to draw, as plain data. The same object is returned until something
111
+ * changes, so it can be compared by identity.
112
+ */
113
+ snapshot = () => {
114
+ if (this.cached !== null)
115
+ return this.cached;
116
+ const state = this.state;
117
+ const mine = this.key.publicKey;
118
+ const me = state?.roster.find((one) => one.key === mine && one.removed === null);
119
+ const peer = this.store.setting("peer");
120
+ this.cached = {
121
+ device: { key: mine, fingerprint: this.fingerprint, person: this.person },
122
+ lab: state === null ? null : {
123
+ id: state.lab,
124
+ name: state.name,
125
+ arrived: state.roster.length > 0,
126
+ member: me !== undefined,
127
+ keeper: me?.role === "keeper",
128
+ roster: state.roster.map((one) => ({ ...one, fingerprint: this.fingerprints.get(one.key) ?? "", you: one.key === mine })),
129
+ parts: state.parts,
130
+ items: state.items,
131
+ places: state.places,
132
+ crowded: state.crowded,
133
+ overlaps: state.overlaps,
134
+ waiting: state.waiting.length,
135
+ entries: state.entries.map((one) => ({
136
+ id: one.id, author: one.signed.entry.author, seq: one.signed.entry.seq, op: one.signed.entry.op,
137
+ status: one.status, reason: one.reason, signer: one.signed.signature.signer, signedAt: one.signed.signature.signed_at,
138
+ })),
139
+ },
140
+ peer: {
141
+ url: peer,
142
+ origin: this.origin,
143
+ invite: this.invite,
144
+ status: this.link?.status ?? "offline",
145
+ problem: this.link?.problem ?? null,
146
+ pending: this.link?.pending ?? 0,
147
+ lastSynced: this.link?.lastSynced ?? null,
148
+ },
149
+ published: this.store.setting("published"),
150
+ };
151
+ return this.cached;
152
+ };
153
+ /** The lab this device is in, or `null`. */
154
+ get labId() { return this.store.setting("lab"); }
155
+ /** What the person at this device is called, or `null` if they have not said. */
156
+ get person() { return this.store.setting("person"); }
157
+ /** The web address the lab's inventory is published at. */
158
+ get origin() {
159
+ const peer = this.store.setting("peer");
160
+ return this.store.setting("origin") ?? (peer === null ? null : originOf(peer));
161
+ }
162
+ /** What to send somebody who should join, or `null` if the lab has no peer to join through. */
163
+ get invite() {
164
+ const peer = this.store.setting("peer");
165
+ return this.labId === null || peer === null ? null : `${peer}#${this.labId}`;
166
+ }
167
+ /** @param name - What to call the person at this device. */
168
+ async setPerson(name) {
169
+ if (name.trim() === "")
170
+ throw new Error("A name, so that the lab can see who made each change.");
171
+ this.store.set("person", name.trim().slice(0, 200));
172
+ this.changed();
173
+ }
174
+ /**
175
+ * Found a lab on this device. It works straight away, with or without a peer.
176
+ *
177
+ * @param about - What the lab is called, and its peer's address if it has one yet.
178
+ */
179
+ async found(about) {
180
+ if (this.labId !== null)
181
+ throw new Error("This device is already in a lab.");
182
+ if (this.person === null)
183
+ throw new Error("Say who you are first.");
184
+ if (about.name.trim() === "")
185
+ throw new Error("A lab needs a name.");
186
+ const peer = peerAddress(about.peer);
187
+ const founding = await foundLab(this.key, { name: about.name.trim().slice(0, 200), person: this.person });
188
+ const id = await entryId(founding.entry);
189
+ this.store.add([{ id, signed: founding }]);
190
+ this.held.push(founding);
191
+ this.store.set("lab", id);
192
+ this.store.set("peer", peer);
193
+ await this.remerge();
194
+ this.connect();
195
+ }
196
+ /**
197
+ * Join a lab somebody else founded. Nothing arrives until a keeper has added this device's key.
198
+ *
199
+ * @param invite - The invitation a keeper sent.
200
+ */
201
+ async join(invite) {
202
+ if (this.labId !== null)
203
+ throw new Error("This device is already in a lab.");
204
+ if (this.person === null)
205
+ throw new Error("Say who you are first.");
206
+ const { peer, lab } = readInvite(invite);
207
+ this.store.set("lab", lab);
208
+ this.store.set("peer", peer);
209
+ await this.remerge();
210
+ this.connect();
211
+ }
212
+ /** @param peer - The lab's peer, or `null` to work with none. */
213
+ async setPeer(peer) {
214
+ this.store.set("peer", peerAddress(peer));
215
+ this.connect();
216
+ this.changed();
217
+ }
218
+ /** @param origin - The web address the lab publishes from, if it is not the peer's own. */
219
+ async setOrigin(origin) {
220
+ this.store.set("origin", origin === null || origin.trim() === "" ? null : origin.trim());
221
+ this.changed();
222
+ }
223
+ /**
224
+ * Seal this device's key under a passphrase, to keep somewhere that is not this device.
225
+ *
226
+ * @param passphrase - What it will take to open it again. Nothing here remembers it.
227
+ * @returns The sealed key as text, to be written to a file. It holds nothing readable without the passphrase.
228
+ */
229
+ async backUp(passphrase) {
230
+ const exported = await this.options.keys.load();
231
+ if (exported === null)
232
+ throw new Error("This device has no key to back up.");
233
+ return JSON.stringify(await sealKey(exported, passphrase), null, 2);
234
+ }
235
+ /**
236
+ * Take up a key from a backup, in place of the one this device made for itself.
237
+ *
238
+ * This is for a new device standing in for a lost one, so it is refused once the device is in a
239
+ * lab: a key in use on two devices at once writes two histories under one name, and the lab
240
+ * cannot tell which is true.
241
+ *
242
+ * @param from - The text of the backup file, and the passphrase it was sealed under.
243
+ */
244
+ async restore(from) {
245
+ if (this.labId !== null)
246
+ throw new Error("This device is already in a lab, and keeps the key it has. A backup is restored on a new device, before it joins.");
247
+ const exported = await openKey(from.backup, from.passphrase);
248
+ await this.options.keys.save(exported);
249
+ this.key = await importSigningKey(exported);
250
+ this.fingerprint = await keyFingerprint(this.key.publicKey);
251
+ this.changed();
252
+ }
253
+ /** Stop talking to the peer, until `goOnline`. The device goes on working. */
254
+ goOffline() { this.link?.stop(); }
255
+ /** Start talking to the peer again. */
256
+ goOnline() { this.link?.start(); }
257
+ /** Add a device's key to the lab. Only a keeper may. */
258
+ addMember(who) {
259
+ return this.write(addToRoster(who.key.trim(), who.person.trim(), who.role ?? "member"));
260
+ }
261
+ /** Add a part, by its sequence. */
262
+ async addPart(part) {
263
+ const bases = part.sequence.replace(/\s+/g, "");
264
+ // The library underneath accepts any letters as a sequence. A freezer should not: a part named
265
+ // by a checksum of the wrong letters is a part nobody else will ever match.
266
+ if (!/^[ACGTURYSWKMBDHVN]+$/i.test(bases)) {
267
+ throw new Error("A sequence is written in the letters for bases: A, C, G, T, and the codes for an uncertain base.");
268
+ }
269
+ return this.write(addPart({ name: part.name.trim(), sequence: bases, circular: part.circular === true }));
270
+ }
271
+ /** Add one physical item of a part. @returns The new item's ID. */
272
+ async addItem(item) {
273
+ const id = newItemId();
274
+ await this.write(addItem({ item: id, part: item.part, amount: item.amount, unit: item.unit === "" ? null : item.unit ?? null }));
275
+ return id;
276
+ }
277
+ /** Add to or take from an item. `by` is a whole-number difference. */
278
+ adjust(change) { return this.write(adjustItem(change.item, change.by)); }
279
+ /** Say how much of an item is physically there. */
280
+ recount(count) { return this.write(recountItem(count.item, count.amount)); }
281
+ /** Retire an item. It stays in the history. */
282
+ retire(which) { return this.write(retireItem(which.item)); }
283
+ /** Say whether a part is on offer to other labs. */
284
+ offer(offer) { return this.write(offerPart(offer.part, offer.available)); }
285
+ /**
286
+ * Add a place: a room, a freezer, a shelf, a box.
287
+ *
288
+ * @param place - What kind it is, what it is called, the place it is in if any, and for a box how
289
+ * many rows and columns of positions it has.
290
+ * @returns The new place's ID.
291
+ */
292
+ async addPlace(place) {
293
+ const id = newPlaceId();
294
+ await this.write(addPlace({ place: id, kind: place.kind.trim().toLowerCase(), name: place.name.trim(), parent: place.parent ?? null, grid: place.grid ?? null }));
295
+ return id;
296
+ }
297
+ /** Put a place inside another, or with `null` inside nothing. */
298
+ movePlace(move) { return this.write(movePlace(move.place, move.parent)); }
299
+ /**
300
+ * Say where a place stands and how big it is, in metres, or with `null` that nobody has said.
301
+ * It is kept to the millimetre.
302
+ */
303
+ standPlace(stand) {
304
+ const mm = (list) => [Math.round(list[0] * 1000), Math.round(list[1] * 1000), Math.round(list[2] * 1000)];
305
+ return this.write(standPlace(stand.place, stand.standing === null ? null
306
+ : { at: mm(stand.standing.position), size: mm(stand.standing.size), turn: stand.standing.turn ?? 0 }));
307
+ }
308
+ /** Retire a place. It stays in the history. */
309
+ retirePlace(which) { return this.write(retirePlace(which.place)); }
310
+ /** Say where an item is: which place, and which position if the place is a box. `null` for nowhere recorded. */
311
+ putItem(put) {
312
+ return this.write(putItem(put.item, put.place, put.slot ?? null));
313
+ }
314
+ /** Set one field on a part (by checksum), an item or a place (by ID). */
315
+ setField(change) {
316
+ return this.write(setField(change.on, change.id, change.field, change.value));
317
+ }
318
+ /**
319
+ * Make one change: sign it into this device's log, keep it, and offer it to the peer.
320
+ *
321
+ * The change is tried before it is kept. A log is forever, so an entry the lab would refuse — a
322
+ * sequence that is not one, an item that does not exist — is turned down here, with the reason,
323
+ * and never written.
324
+ *
325
+ * @param op - The change.
326
+ * @throws If this device may not change the lab, or the lab would refuse the change.
327
+ */
328
+ write(op) {
329
+ return this.inTurn(async () => {
330
+ const state = this.state;
331
+ if (state === null || state.lab === null)
332
+ throw new Error("This device is not in a lab yet.");
333
+ if (!isMember(state, this.key.publicKey)) {
334
+ throw new Error("This device's key is not on the lab's roster, so it cannot change the lab.");
335
+ }
336
+ const signed = await nextEntry(state, this.key, op, { signer: this.person });
337
+ const id = await entryId(signed.entry);
338
+ const tried = (await mergeLog([...this.held, signed], { lab: state.lab })).entries.find((one) => one.id === id);
339
+ if (tried?.status !== "applied")
340
+ throw new Error(tried?.reason ?? "The lab would not accept that change.");
341
+ this.store.add([{ id, signed }]);
342
+ this.held.push(signed);
343
+ await this.remerge();
344
+ this.link?.push();
345
+ });
346
+ }
347
+ /**
348
+ * Take entries that arrived from somewhere else.
349
+ *
350
+ * @param incoming - What arrived.
351
+ * @returns How many were kept.
352
+ */
353
+ take(incoming) {
354
+ return this.inTurn(async () => {
355
+ const lab = this.labId;
356
+ if (lab === null)
357
+ return 0;
358
+ const { kept } = await takeEntries(lab, this.held, incoming);
359
+ if (kept.length === 0)
360
+ return 0;
361
+ this.store.add(await Promise.all(kept.map(async (signed) => ({ id: await entryId(signed.entry), signed }))));
362
+ this.held.push(...kept);
363
+ await this.remerge();
364
+ return kept.length;
365
+ });
366
+ }
367
+ /**
368
+ * Publish what the lab shows the world: sign the public inventory and hand it to the peer.
369
+ *
370
+ * The lab keeps pointing at whichever labs its published inventory already points at.
371
+ *
372
+ * @returns Whether it was published, why not if not, and how many parts it listed.
373
+ */
374
+ async publish() {
375
+ const origin = this.origin;
376
+ if (this.state === null || this.link === null || origin === null) {
377
+ return { ok: false, reason: "The lab has no peer to publish through.", parts: 0 };
378
+ }
379
+ let peers;
380
+ try {
381
+ peers = (await fetchInventory(origin, { timeoutMs: 6_000, ...(this.options.fetch === undefined ? {} : { fetch: this.options.fetch }) })).signed.inventory.peers;
382
+ }
383
+ catch {
384
+ peers = undefined; // nothing published yet, or unreachable: publish without pointers
385
+ }
386
+ const inventory = publicInventory(this.state, { name: this.state.name ?? "A lab", origin, contact: null, note: null }, peers === undefined ? {} : { peers });
387
+ const signed = await signInventory(inventory, this.key, { signer: this.person });
388
+ const answer = await this.link.publish(signed);
389
+ if (answer.ok) {
390
+ this.store.set("published", inventory.published_at);
391
+ this.changed();
392
+ }
393
+ return { ...answer, parts: inventory.parts.length };
394
+ }
395
+ /** Stop talking to the peer and finish with the store. */
396
+ close() {
397
+ this.link?.stop();
398
+ this.link = null;
399
+ this.store.close();
400
+ }
401
+ changed() {
402
+ this.cached = null;
403
+ for (const listener of this.listeners)
404
+ listener();
405
+ }
406
+ async remerge() {
407
+ const lab = this.labId;
408
+ this.state = lab === null ? null : await mergeLog(this.held, { lab });
409
+ for (const one of this.state?.roster ?? []) {
410
+ if (!this.fingerprints.has(one.key))
411
+ this.fingerprints.set(one.key, await keyFingerprint(one.key));
412
+ }
413
+ this.changed();
414
+ }
415
+ connect() {
416
+ this.link?.stop();
417
+ this.link = null;
418
+ const lab = this.labId;
419
+ const url = this.store.setting("peer");
420
+ if (lab === null || url === null)
421
+ return;
422
+ this.link = new PeerLink({
423
+ url, lab, key: this.key, held: () => this.held, take: (entries) => this.take(entries), onChange: () => this.changed(),
424
+ ...(this.options.WebSocket === undefined ? {} : { WebSocket: this.options.WebSocket }),
425
+ });
426
+ this.link.start();
427
+ }
428
+ inTurn(work) {
429
+ const next = this.turn.then(work, work);
430
+ this.turn = next.catch(() => { });
431
+ return next;
432
+ }
433
+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AE,IAAI,EAAE,CAAC;QAClB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC;QACvB,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACvC,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,IAAI;YAAE,OAAO;QACzC,IAAI,CAAC,IAAI,GAAG,IAAI,QAAQ,CAAC;YACvB,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE;YACrH,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;SACvF,CAAC,CAAC;QACH,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACpB,CAAC;IAEO,MAAM,CAAI,IAAsB;QACtC,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACxC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QACjC,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
package/dist/link.d.ts ADDED
@@ -0,0 +1,83 @@
1
+ /**
2
+ * A device's connection to the lab's web peer.
3
+ *
4
+ * It is allowed to fail. A device works with no peer at all; this only carries entries to and from
5
+ * the lab's address when it can, says how that is going, and keeps trying when it cannot.
6
+ *
7
+ * The exchange is the one `@biolibs/bionet`'s `sync.ts` describes. The peer sends a number, this
8
+ * signs it with the device's key, and if that key is on the lab's roster the two swap whatever the
9
+ * other lacks. A peer that holds nothing knows no members, so on being refused this sends what it
10
+ * holds and asks again: that is how a lab's first entries get there at all.
11
+ */
12
+ import { type Have, type SignedEntry, type SignedInventory } from "@biolibs/bionet";
13
+ import type { SigningKey } from "@biolibs/opencloning";
14
+ /**
15
+ * How the connection is going. `waiting` means the peer answered and this device's key is not on the
16
+ * roster it holds.
17
+ */
18
+ export type LinkStatus = "offline" | "connecting" | "waiting" | "syncing" | "synced";
19
+ /** What a link needs. */
20
+ export interface LinkOptions {
21
+ /** The peer's socket address. */
22
+ url: string;
23
+ /** The lab's ID. */
24
+ lab: string;
25
+ /** This device's key. */
26
+ key: SigningKey;
27
+ /** What this device holds, now. */
28
+ held: () => readonly SignedEntry[];
29
+ /** Hand over entries that arrived. Resolves to how many were kept. */
30
+ take: (entries: readonly unknown[]) => Promise<number>;
31
+ /** Called whenever the status, the problem or what is pending changes. */
32
+ onChange: () => void;
33
+ /** Something to open sockets with. Defaults to the runtime's own `WebSocket`. */
34
+ WebSocket?: typeof globalThis.WebSocket;
35
+ }
36
+ /**
37
+ * The connection from one device to its lab's web peer.
38
+ *
39
+ * `Lab` makes and runs one of these; it is exported for anything that wants to drive the exchange
40
+ * itself. Give it the peer's address, the lab, the device's key and a way to read and add to what
41
+ * the device holds, then `start()` it. It reconnects on its own, waiting longer each time.
42
+ */
43
+ export declare class PeerLink {
44
+ private readonly options;
45
+ status: LinkStatus;
46
+ /** What is in the way, said for a person, or `null`. */
47
+ problem: string | null;
48
+ /** When this device and the peer last had the same entries. */
49
+ lastSynced: string | null;
50
+ /** What the peer holds, as far as this knows. `null` until the peer has said. */
51
+ peerHas: Have | null;
52
+ private ws;
53
+ private stopped;
54
+ private delay;
55
+ private retry;
56
+ private turn;
57
+ private publishing;
58
+ private pushNow;
59
+ private sendInventory;
60
+ constructor(options: LinkOptions);
61
+ /** How many entries held here the peer is not known to have. */
62
+ get pending(): number;
63
+ /** Start, and keep trying. */
64
+ start(): void;
65
+ /** Stop, and stop trying. */
66
+ stop(): void;
67
+ /** Send the peer whatever it lacks. Called whenever something is written on this device. */
68
+ push(): void;
69
+ /**
70
+ * Ask the peer to publish an inventory.
71
+ *
72
+ * @param signed - The inventory, signed by this device.
73
+ * @returns Whether it was published, and why not.
74
+ */
75
+ publish(signed: SignedInventory): Promise<{
76
+ ok: boolean;
77
+ reason: string | null;
78
+ }>;
79
+ private say;
80
+ private settled;
81
+ private open;
82
+ }
83
+ //# sourceMappingURL=link.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"link.d.ts","sourceRoot":"","sources":["../src/link.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAiD,KAAK,IAAI,EAAE,KAAK,WAAW,EAAE,KAAK,eAAe,EAC9E,MAAM,iBAAiB,CAAC;AACnD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAEvD;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG,SAAS,GAAG,YAAY,GAAG,SAAS,GAAG,SAAS,GAAG,QAAQ,CAAC;AAErF,yBAAyB;AACzB,MAAM,WAAW,WAAW;IAC1B,iCAAiC;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,oBAAoB;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,yBAAyB;IACzB,GAAG,EAAE,UAAU,CAAC;IAChB,mCAAmC;IACnC,IAAI,EAAE,MAAM,SAAS,WAAW,EAAE,CAAC;IACnC,sEAAsE;IACtE,IAAI,EAAE,CAAC,OAAO,EAAE,SAAS,OAAO,EAAE,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IACvD,0EAA0E;IAC1E,QAAQ,EAAE,MAAM,IAAI,CAAC;IACrB,iFAAiF;IACjF,SAAS,CAAC,EAAE,OAAO,UAAU,CAAC,SAAS,CAAC;CACzC;AAKD;;;;;;GAMG;AACH,q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