@biolibs/bionet 1.1.0 → 1.2.1
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/README.md +95 -11
- package/dist/discover.d.ts +1 -1
- package/dist/discover.js +1 -1
- package/dist/fetch.d.ts +2 -2
- package/dist/fetch.js +2 -2
- package/dist/index.d.ts +7 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/inventory.d.ts +2 -2
- package/dist/inventory.js +2 -2
- package/dist/log.d.ts +196 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +246 -0
- package/dist/log.js.map +1 -0
- package/dist/merge.d.ts +171 -0
- package/dist/merge.d.ts.map +1 -0
- package/dist/merge.js +520 -0
- package/dist/merge.js.map +1 -0
- package/dist/model.d.ts +3 -3
- package/dist/model.js +2 -2
- package/dist/origin.d.ts +4 -4
- package/dist/origin.js +4 -4
- package/dist/sync.d.ts +157 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +203 -0
- package/dist/sync.js.map +1 -0
- package/package.json +1 -1
- package/src/discover.ts +1 -1
- package/src/fetch.ts +2 -2
- package/src/index.ts +7 -1
- package/src/inventory.ts +2 -2
- package/src/log.ts +325 -0
- package/src/merge.ts +605 -0
- package/src/model.ts +3 -3
- package/src/origin.ts +4 -4
- package/src/sync.ts +210 -0
package/dist/sync.d.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Getting entries from one machine to another, and deciding who may have them.
|
|
3
|
+
*
|
|
4
|
+
* `merge.ts` works a lab out from whatever entries it is given. This is how they are given: two
|
|
5
|
+
* machines say how far into each key's log they hold, and each sends the other what it lacks. It
|
|
6
|
+
* does not matter which machine is a member's laptop and which is the lab's web peer; the exchange
|
|
7
|
+
* is the same, and nothing here opens a connection — that is for whatever carries the messages.
|
|
8
|
+
*
|
|
9
|
+
* The one asymmetry is who may read. A lab's logs hold everything: every tube, every amount, who
|
|
10
|
+
* took what. The web peer relays them between members and must not hand them to anybody who asks,
|
|
11
|
+
* so before it sends an entry it makes the other side prove it holds a key on the roster, by signing
|
|
12
|
+
* a number the peer just made up. Writing needs no such proof. An entry is signed by its author and
|
|
13
|
+
* `mergeLog` already refuses what the roster does not allow, so a stranger who pushes entries gets
|
|
14
|
+
* nothing kept and learns nothing.
|
|
15
|
+
*/
|
|
16
|
+
import { type DocumentSignature, type SignOptions, type SigningKey } from "@biolibs/opencloning";
|
|
17
|
+
import { type SignedEntry } from "./log.js";
|
|
18
|
+
import { type LabState } from "./merge.js";
|
|
19
|
+
import type { SignedInventory } from "./model.js";
|
|
20
|
+
/** What the bytes of a signed hello are for. */
|
|
21
|
+
export declare const HELLO_DOMAIN = "biolibs/bionet peer hello v1";
|
|
22
|
+
/** The longest one message may be, as text. */
|
|
23
|
+
export declare const LONGEST_MESSAGE = 8000000;
|
|
24
|
+
/** How many entries one message may carry. */
|
|
25
|
+
export declare const MOST_ENTRIES = 2000;
|
|
26
|
+
/** How far into each key's log a machine holds: for each key, the last `seq` with no gap before it. */
|
|
27
|
+
export type Have = Record<string, number>;
|
|
28
|
+
/** One message between two machines. */
|
|
29
|
+
export type SyncMessage =
|
|
30
|
+
/** The peer's opening: which lab it serves, and a number to sign. */
|
|
31
|
+
{
|
|
32
|
+
type: "challenge";
|
|
33
|
+
lab: string;
|
|
34
|
+
nonce: string;
|
|
35
|
+
}
|
|
36
|
+
/** The answer: the signature over that number, and how much this side holds. */
|
|
37
|
+
| {
|
|
38
|
+
type: "hello";
|
|
39
|
+
signature: DocumentSignature;
|
|
40
|
+
have: Have;
|
|
41
|
+
}
|
|
42
|
+
/** The hello was from a member. How much the peer holds; what the member lacks follows. */
|
|
43
|
+
| {
|
|
44
|
+
type: "welcome";
|
|
45
|
+
have: Have;
|
|
46
|
+
}
|
|
47
|
+
/** Entries the other side lacks. */
|
|
48
|
+
| {
|
|
49
|
+
type: "entries";
|
|
50
|
+
entries: SignedEntry[];
|
|
51
|
+
}
|
|
52
|
+
/** How many of the entries just sent were kept. */
|
|
53
|
+
| {
|
|
54
|
+
type: "kept";
|
|
55
|
+
count: number;
|
|
56
|
+
}
|
|
57
|
+
/** A freshly signed public inventory, for the peer to publish. */
|
|
58
|
+
| {
|
|
59
|
+
type: "inventory";
|
|
60
|
+
signed: SignedInventory;
|
|
61
|
+
}
|
|
62
|
+
/** The inventory was published. */
|
|
63
|
+
| {
|
|
64
|
+
type: "published";
|
|
65
|
+
published_at: string;
|
|
66
|
+
}
|
|
67
|
+
/** Something was turned down. `code` is for programs, `reason` for people. */
|
|
68
|
+
| {
|
|
69
|
+
type: "refused";
|
|
70
|
+
code: "not-a-member" | "bad-message" | "bad-inventory";
|
|
71
|
+
reason: string;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* How far into each key's log a set of entries reaches without a gap.
|
|
75
|
+
*
|
|
76
|
+
* @param entries - Entries held.
|
|
77
|
+
* @returns For each key, the last `seq` such that every entry from 0 up to it is present.
|
|
78
|
+
*/
|
|
79
|
+
export declare function haveOf(entries: readonly SignedEntry[]): Have;
|
|
80
|
+
/**
|
|
81
|
+
* The entries another machine lacks.
|
|
82
|
+
*
|
|
83
|
+
* @param entries - Entries held here.
|
|
84
|
+
* @param have - How far the other machine says it holds.
|
|
85
|
+
* @returns What to send it, each key's log in order.
|
|
86
|
+
*/
|
|
87
|
+
export declare function entriesBeyond(entries: readonly SignedEntry[], have: Have): SignedEntry[];
|
|
88
|
+
/**
|
|
89
|
+
* Answer a peer's challenge: sign the number it made up, for the lab it named.
|
|
90
|
+
*
|
|
91
|
+
* @param key - This device's key.
|
|
92
|
+
* @param lab - The lab's ID, from the challenge.
|
|
93
|
+
* @param nonce - The number, from the challenge.
|
|
94
|
+
* @param options - A name to sign under, and when.
|
|
95
|
+
* @returns The signature to send back in a hello.
|
|
96
|
+
*/
|
|
97
|
+
export declare function signHello(key: SigningKey, lab: string, nonce: string, options?: SignOptions): Promise<DocumentSignature>;
|
|
98
|
+
/**
|
|
99
|
+
* Check the answer to a challenge.
|
|
100
|
+
*
|
|
101
|
+
* It settles one thing: whoever sent this holds the private half of the key it names, now. Whether
|
|
102
|
+
* that key may read the lab is `isMember`'s question.
|
|
103
|
+
*
|
|
104
|
+
* @param lab - The lab the challenge named.
|
|
105
|
+
* @param nonce - The number the challenge carried.
|
|
106
|
+
* @param signature - The answer.
|
|
107
|
+
* @returns The key that answered, or `null` if the signature does not hold.
|
|
108
|
+
*/
|
|
109
|
+
export declare function verifyHello(lab: string, nonce: string, signature: DocumentSignature): Promise<string | null>;
|
|
110
|
+
/**
|
|
111
|
+
* Whether a key may read the lab: it is on the roster and has not been removed.
|
|
112
|
+
*
|
|
113
|
+
* @param state - The lab.
|
|
114
|
+
* @param key - The key.
|
|
115
|
+
* @returns Whether to send it entries.
|
|
116
|
+
*/
|
|
117
|
+
export declare function isMember(state: LabState, key: string): boolean;
|
|
118
|
+
/**
|
|
119
|
+
* Take entries from another machine, keeping the ones that belong.
|
|
120
|
+
*
|
|
121
|
+
* An entry is kept if, merged with what is already held, it finds a place in the lab and its author
|
|
122
|
+
* is or was on the roster. Entries from keys the roster has never heard of are dropped, which is
|
|
123
|
+
* what stops a stranger filling a peer's disk, and so are entries still waiting on others — whoever
|
|
124
|
+
* sent them will send them again along with what they were waiting for.
|
|
125
|
+
*
|
|
126
|
+
* @param lab - The lab's ID.
|
|
127
|
+
* @param held - Entries already held.
|
|
128
|
+
* @param incoming - What arrived. Anything that is not an entry is ignored.
|
|
129
|
+
* @returns The entries worth keeping that were not already held, and the lab with them merged in.
|
|
130
|
+
*/
|
|
131
|
+
export declare function takeEntries(lab: string, held: readonly SignedEntry[], incoming: readonly unknown[]): Promise<{
|
|
132
|
+
kept: SignedEntry[];
|
|
133
|
+
state: LabState;
|
|
134
|
+
}>;
|
|
135
|
+
/**
|
|
136
|
+
* Decide whether a peer should publish an inventory a member sent it.
|
|
137
|
+
*
|
|
138
|
+
* The peer cannot sign, so the public inventory has to come from a member. It is published only if
|
|
139
|
+
* a key on the roster signed it, it names the address the peer serves it from, and it is newer than
|
|
140
|
+
* the one it would replace.
|
|
141
|
+
*
|
|
142
|
+
* @param state - The lab.
|
|
143
|
+
* @param signed - What the member sent.
|
|
144
|
+
* @param origin - The address this peer serves from.
|
|
145
|
+
* @param current - What is published now, if anything.
|
|
146
|
+
* @returns `null` if it should be published, or why not.
|
|
147
|
+
*/
|
|
148
|
+
export declare function inventoryProblem(state: LabState, signed: unknown, origin: string, current?: SignedInventory | null): Promise<string | null>;
|
|
149
|
+
/**
|
|
150
|
+
* Read a message that came off the wire.
|
|
151
|
+
*
|
|
152
|
+
* @param text - What arrived.
|
|
153
|
+
* @returns The message.
|
|
154
|
+
* @throws If it is too long, not JSON, or not a message this understands.
|
|
155
|
+
*/
|
|
156
|
+
export declare function readMessage(text: string): SyncMessage;
|
|
157
|
+
//# sourceMappingURL=sync.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["../src/sync.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAA0B,KAAK,iBAAiB,EAAE,KAAK,WAAW,EAChE,KAAK,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAEvD,OAAO,EAA0B,KAAK,WAAW,EAAE,MAAM,UAAU,CAAC;AACpE,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAGlD,gDAAgD;AAChD,eAAO,MAAM,YAAY,iCAAiC,CAAC;AAE3D,+CAA+C;AAC/C,eAAO,MAAM,eAAe,UAAY,CAAC;AAEzC,8CAA8C;AAC9C,eAAO,MAAM,YAAY,OAAQ,CAAC;AAElC,uGAAuG;AACvG,MAAM,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAE1C,w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iB,CAAC,CAE5H;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,iBAAiB,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAGlH;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAE9D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,WAAW,EAAE,EAAE,QAAQ,EAAE,SAAS,OAAO,EAAE,GAAG,OAAO,CAAC;IAClH,IAAI,EAAE,WAAW,EAAE,CAAC;IAAC,KAAK,EAAE,QAAQ,CAAC;CACtC,CAAC,CAOD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,gBAAgB,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAChD,OAAO,GAAE,eAAe,GAAG,IAAW,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAWrG;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,CA0BrD"}
|
package/dist/sync.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Getting entries from one machine to another, and deciding who may have them.
|
|
3
|
+
*
|
|
4
|
+
* `merge.ts` works a lab out from whatever entries it is given. This is how they are given: two
|
|
5
|
+
* machines say how far into each key's log they hold, and each sends the other what it lacks. It
|
|
6
|
+
* does not matter which machine is a member's laptop and which is the lab's web peer; the exchange
|
|
7
|
+
* is the same, and nothing here opens a connection — that is for whatever carries the messages.
|
|
8
|
+
*
|
|
9
|
+
* The one asymmetry is who may read. A lab's logs hold everything: every tube, every amount, who
|
|
10
|
+
* took what. The web peer relays them between members and must not hand them to anybody who asks,
|
|
11
|
+
* so before it sends an entry it makes the other side prove it holds a key on the roster, by signing
|
|
12
|
+
* a number the peer just made up. Writing needs no such proof. An entry is signed by its author and
|
|
13
|
+
* `mergeLog` already refuses what the roster does not allow, so a stranger who pushes entries gets
|
|
14
|
+
* nothing kept and learns nothing.
|
|
15
|
+
*/
|
|
16
|
+
import { signUnder, verifyUnder } from "@biolibs/opencloning";
|
|
17
|
+
import { isSignedInventory, verifyInventory } from "./inventory.js";
|
|
18
|
+
import { entryId, isSignedEntry } from "./log.js";
|
|
19
|
+
import { mergeLog } from "./merge.js";
|
|
20
|
+
import { originMatches } from "./origin.js";
|
|
21
|
+
/** What the bytes of a signed hello are for. */
|
|
22
|
+
export const HELLO_DOMAIN = "biolibs/bionet peer hello v1";
|
|
23
|
+
/** The longest one message may be, as text. */
|
|
24
|
+
export const LONGEST_MESSAGE = 8_000_000;
|
|
25
|
+
/** How many entries one message may carry. */
|
|
26
|
+
export const MOST_ENTRIES = 2_000;
|
|
27
|
+
/**
|
|
28
|
+
* How far into each key's log a set of entries reaches without a gap.
|
|
29
|
+
*
|
|
30
|
+
* @param entries - Entries held.
|
|
31
|
+
* @returns For each key, the last `seq` such that every entry from 0 up to it is present.
|
|
32
|
+
*/
|
|
33
|
+
export function haveOf(entries) {
|
|
34
|
+
const seqs = new Map();
|
|
35
|
+
for (const { entry } of entries)
|
|
36
|
+
seqs.set(entry.author, (seqs.get(entry.author) ?? new Set()).add(entry.seq));
|
|
37
|
+
const have = {};
|
|
38
|
+
for (const [author, held] of seqs) {
|
|
39
|
+
let last = -1;
|
|
40
|
+
while (held.has(last + 1))
|
|
41
|
+
last++;
|
|
42
|
+
if (last >= 0)
|
|
43
|
+
have[author] = last;
|
|
44
|
+
}
|
|
45
|
+
return have;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The entries another machine lacks.
|
|
49
|
+
*
|
|
50
|
+
* @param entries - Entries held here.
|
|
51
|
+
* @param have - How far the other machine says it holds.
|
|
52
|
+
* @returns What to send it, each key's log in order.
|
|
53
|
+
*/
|
|
54
|
+
export function entriesBeyond(entries, have) {
|
|
55
|
+
return entries.filter(({ entry }) => entry.seq > (have[entry.author] ?? -1))
|
|
56
|
+
.sort((a, b) => (a.entry.author === b.entry.author ? a.entry.seq - b.entry.seq
|
|
57
|
+
: a.entry.author < b.entry.author ? -1 : 1));
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Answer a peer's challenge: sign the number it made up, for the lab it named.
|
|
61
|
+
*
|
|
62
|
+
* @param key - This device's key.
|
|
63
|
+
* @param lab - The lab's ID, from the challenge.
|
|
64
|
+
* @param nonce - The number, from the challenge.
|
|
65
|
+
* @param options - A name to sign under, and when.
|
|
66
|
+
* @returns The signature to send back in a hello.
|
|
67
|
+
*/
|
|
68
|
+
export function signHello(key, lab, nonce, options = {}) {
|
|
69
|
+
return signUnder(HELLO_DOMAIN, { lab, nonce }, key, options);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check the answer to a challenge.
|
|
73
|
+
*
|
|
74
|
+
* It settles one thing: whoever sent this holds the private half of the key it names, now. Whether
|
|
75
|
+
* that key may read the lab is `isMember`'s question.
|
|
76
|
+
*
|
|
77
|
+
* @param lab - The lab the challenge named.
|
|
78
|
+
* @param nonce - The number the challenge carried.
|
|
79
|
+
* @param signature - The answer.
|
|
80
|
+
* @returns The key that answered, or `null` if the signature does not hold.
|
|
81
|
+
*/
|
|
82
|
+
export async function verifyHello(lab, nonce, signature) {
|
|
83
|
+
const [check] = await verifyUnder(HELLO_DOMAIN, { lab, nonce }, [signature]);
|
|
84
|
+
return check.ok ? signature.public_key : null;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Whether a key may read the lab: it is on the roster and has not been removed.
|
|
88
|
+
*
|
|
89
|
+
* @param state - The lab.
|
|
90
|
+
* @param key - The key.
|
|
91
|
+
* @returns Whether to send it entries.
|
|
92
|
+
*/
|
|
93
|
+
export function isMember(state, key) {
|
|
94
|
+
return state.roster.some((one) => one.key === key && one.removed === null);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Take entries from another machine, keeping the ones that belong.
|
|
98
|
+
*
|
|
99
|
+
* An entry is kept if, merged with what is already held, it finds a place in the lab and its author
|
|
100
|
+
* is or was on the roster. Entries from keys the roster has never heard of are dropped, which is
|
|
101
|
+
* what stops a stranger filling a peer's disk, and so are entries still waiting on others — whoever
|
|
102
|
+
* sent them will send them again along with what they were waiting for.
|
|
103
|
+
*
|
|
104
|
+
* @param lab - The lab's ID.
|
|
105
|
+
* @param held - Entries already held.
|
|
106
|
+
* @param incoming - What arrived. Anything that is not an entry is ignored.
|
|
107
|
+
* @returns The entries worth keeping that were not already held, and the lab with them merged in.
|
|
108
|
+
*/
|
|
109
|
+
export async function takeEntries(lab, held, incoming) {
|
|
110
|
+
const merged = await mergeLog([...held, ...incoming], { lab });
|
|
111
|
+
const known = new Set(merged.roster.map((one) => one.key));
|
|
112
|
+
const already = new Set(await Promise.all(held.map((one) => entryId(one.entry))));
|
|
113
|
+
const kept = merged.entries.filter((one) => known.has(one.signed.entry.author) && !already.has(one.id))
|
|
114
|
+
.map((one) => one.signed);
|
|
115
|
+
return { kept, state: await mergeLog([...held, ...kept], { lab }) };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Decide whether a peer should publish an inventory a member sent it.
|
|
119
|
+
*
|
|
120
|
+
* The peer cannot sign, so the public inventory has to come from a member. It is published only if
|
|
121
|
+
* a key on the roster signed it, it names the address the peer serves it from, and it is newer than
|
|
122
|
+
* the one it would replace.
|
|
123
|
+
*
|
|
124
|
+
* @param state - The lab.
|
|
125
|
+
* @param signed - What the member sent.
|
|
126
|
+
* @param origin - The address this peer serves from.
|
|
127
|
+
* @param current - What is published now, if anything.
|
|
128
|
+
* @returns `null` if it should be published, or why not.
|
|
129
|
+
*/
|
|
130
|
+
export async function inventoryProblem(state, signed, origin, current = null) {
|
|
131
|
+
if (!isSignedInventory(signed))
|
|
132
|
+
return "That is not a signed inventory.";
|
|
133
|
+
const checks = await verifyInventory(signed);
|
|
134
|
+
if (!checks.some((check) => check.ok && isMember(state, check.public_key))) {
|
|
135
|
+
return "No key on this lab's roster signed that inventory.";
|
|
136
|
+
}
|
|
137
|
+
if (!originMatches(signed.inventory.lab.origin, origin))
|
|
138
|
+
return "That inventory names a different address from this one.";
|
|
139
|
+
if (current !== null && !(signed.inventory.published_at > current.inventory.published_at)) {
|
|
140
|
+
return "That inventory is not newer than the one already published.";
|
|
141
|
+
}
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Read a message that came off the wire.
|
|
146
|
+
*
|
|
147
|
+
* @param text - What arrived.
|
|
148
|
+
* @returns The message.
|
|
149
|
+
* @throws If it is too long, not JSON, or not a message this understands.
|
|
150
|
+
*/
|
|
151
|
+
export function readMessage(text) {
|
|
152
|
+
if (text.length > LONGEST_MESSAGE)
|
|
153
|
+
throw new Error("That message is longer than will be read.");
|
|
154
|
+
let value;
|
|
155
|
+
try {
|
|
156
|
+
value = JSON.parse(text);
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
throw new Error("That message is not JSON.");
|
|
160
|
+
}
|
|
161
|
+
const held = value;
|
|
162
|
+
const have = (given) => typeof given === "object" && given !== null && !Array.isArray(given)
|
|
163
|
+
&& Object.values(given).every((seq) => Number.isInteger(seq) && seq >= 0);
|
|
164
|
+
const words = (given) => typeof given === "string" && given.length <= 2_000;
|
|
165
|
+
if (typeof held !== "object" || held === null)
|
|
166
|
+
throw new Error("That is not a message.");
|
|
167
|
+
switch (held["type"]) {
|
|
168
|
+
case "challenge":
|
|
169
|
+
if (words(held["lab"]) && words(held["nonce"]))
|
|
170
|
+
return held;
|
|
171
|
+
break;
|
|
172
|
+
case "hello":
|
|
173
|
+
if (typeof held["signature"] === "object" && held["signature"] !== null && have(held["have"]))
|
|
174
|
+
return held;
|
|
175
|
+
break;
|
|
176
|
+
case "welcome":
|
|
177
|
+
if (have(held["have"]))
|
|
178
|
+
return held;
|
|
179
|
+
break;
|
|
180
|
+
case "entries":
|
|
181
|
+
if (Array.isArray(held["entries"]) && held["entries"].length <= MOST_ENTRIES && held["entries"].every(isSignedEntry))
|
|
182
|
+
return held;
|
|
183
|
+
break;
|
|
184
|
+
case "kept":
|
|
185
|
+
if (Number.isInteger(held["count"]))
|
|
186
|
+
return held;
|
|
187
|
+
break;
|
|
188
|
+
case "inventory":
|
|
189
|
+
if (typeof held["signed"] === "object" && held["signed"] !== null)
|
|
190
|
+
return held;
|
|
191
|
+
break;
|
|
192
|
+
case "published":
|
|
193
|
+
if (words(held["published_at"]))
|
|
194
|
+
return held;
|
|
195
|
+
break;
|
|
196
|
+
case "refused":
|
|
197
|
+
if (words(held["code"]) && words(held["reason"]))
|
|
198
|
+
return held;
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
throw new Error("That is not a message this understands.");
|
|
202
|
+
}
|
|
203
|
+
//# sourceMappingURL=sync.js.map
|
package/dist/sync.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync.js","sourceRoot":"","sources":["../src/sync.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,SAAS,EAAE,WAAW,EACL,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACpE,OAAO,EAAE,OAAO,EAAE,aAAa,EAAoB,MAAM,UAAU,CAAC;AACpE,OAAO,EAAE,QAAQ,EAAiB,MAAM,YAAY,CAAC;AAErD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,gDAAgD;AAChD,MAAM,CAAC,MAAM,YAAY,GAAG,8BAA8B,CAAC;AAE3D,+CAA+C;AAC/C,MAAM,CAAC,MAAM,eAAe,GAAG,SAAS,CAAC;AAEzC,8CAA8C;AAC9C,MAAM,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,IAAmB,CAAC;YAAC,MAAM;IACtG,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;AAC7D,CAAC"}
|
package/package.json
CHANGED
package/src/discover.ts
CHANGED
|
@@ -106,7 +106,7 @@ interface Pending {
|
|
|
106
106
|
* ```ts
|
|
107
107
|
* import { discover, partsNeeded, settledLabs, whoHas } from "@biolibs/bionet";
|
|
108
108
|
*
|
|
109
|
-
* const walk = await discover(["
|
|
109
|
+
* const walk = await discover(["wrenlab.example"], { maxDepth: 2 });
|
|
110
110
|
* walk.reached.map((one) => `${one.path.join(" → ")}`);
|
|
111
111
|
*
|
|
112
112
|
* whoHas(partsNeeded(plan.document), settledLabs(walk));
|
package/src/fetch.ts
CHANGED
|
@@ -38,7 +38,7 @@ export interface FetchOptions {
|
|
|
38
38
|
/**
|
|
39
39
|
* Fetch a lab's inventory and check it.
|
|
40
40
|
*
|
|
41
|
-
* @param address - The lab's address, like `
|
|
41
|
+
* @param address - The lab's address, like `wrenlab.example`.
|
|
42
42
|
* @param options - What to fetch with, and how long to wait.
|
|
43
43
|
* @returns What came back, with the signatures checked and the address compared.
|
|
44
44
|
* @throws If the address is not usable, nothing answered, or what answered is not an inventory.
|
|
@@ -47,7 +47,7 @@ export interface FetchOptions {
|
|
|
47
47
|
* ```ts
|
|
48
48
|
* import { fetchInventory, settled } from "@biolibs/bionet";
|
|
49
49
|
*
|
|
50
|
-
* const found = await fetchInventory("
|
|
50
|
+
* const found = await fetchInventory("wrenlab.example");
|
|
51
51
|
* settled(found); // signed, and served from the address it claims
|
|
52
52
|
* found.signed.inventory.parts.length;
|
|
53
53
|
* ```
|
package/src/index.ts
CHANGED
|
@@ -20,12 +20,15 @@
|
|
|
20
20
|
* - `discover.ts` — finding labs you were never told about, by walking from ones you were
|
|
21
21
|
* - `trust.ts` — remembering which key answered last time, so a change gets noticed
|
|
22
22
|
* - `match.ts` — what a plan needs, and who has it
|
|
23
|
+
* - `log.ts` — one signed change inside a lab, and the log it belongs to
|
|
24
|
+
* - `merge.ts` — what a lab holds, worked out from every key's log together
|
|
25
|
+
* - `sync.ts` — getting entries from one machine to another, and deciding who may have them
|
|
23
26
|
*
|
|
24
27
|
* @example
|
|
25
28
|
* ```ts
|
|
26
29
|
* import { fetchInventory, partsNeeded, settled, whoHas } from "@biolibs/bionet";
|
|
27
30
|
*
|
|
28
|
-
* const found = await fetchInventory("
|
|
31
|
+
* const found = await fetchInventory("wrenlab.example");
|
|
29
32
|
* const sourcing = whoHas(partsNeeded(plan.document), settled(found) ? [found] : []);
|
|
30
33
|
*
|
|
31
34
|
* for (const { needed, holders } of sourcing) {
|
|
@@ -41,3 +44,6 @@ export * from "./fetch.js";
|
|
|
41
44
|
export * from "./discover.js";
|
|
42
45
|
export * from "./trust.js";
|
|
43
46
|
export * from "./match.js";
|
|
47
|
+
export * from "./log.js";
|
|
48
|
+
export * from "./merge.js";
|
|
49
|
+
export * from "./sync.js";
|
package/src/inventory.ts
CHANGED
|
@@ -54,9 +54,9 @@ export interface InventoryOptions {
|
|
|
54
54
|
* import { inventoryFrom, signInventory } from "@biolibs/bionet";
|
|
55
55
|
*
|
|
56
56
|
* const inventory = inventoryFrom([pUC19, insert], {
|
|
57
|
-
* name: "
|
|
57
|
+
* name: "Wren lab", origin: "https://wrenlab.example", contact: null, note: null,
|
|
58
58
|
* });
|
|
59
|
-
* const published = await signInventory(inventory, key, { signer: "
|
|
59
|
+
* const published = await signInventory(inventory, key, { signer: "Wren lab" });
|
|
60
60
|
* ```
|
|
61
61
|
*/
|
|
62
62
|
export function inventoryFrom(records: readonly Dseqrecord[], lab: LabModel,
|