@biolibs/bionet 0.0.0-stage → 1.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 +21 -0
- package/README.md +230 -2
- package/dist/discover.d.ts +121 -0
- package/dist/discover.d.ts.map +1 -0
- package/dist/discover.js +143 -0
- package/dist/discover.js.map +1 -0
- package/dist/fetch.d.ts +60 -0
- package/dist/fetch.d.ts.map +1 -0
- package/dist/fetch.js +85 -0
- package/dist/fetch.js.map +1 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/dist/inventory.d.ts +103 -0
- package/dist/inventory.d.ts.map +1 -0
- package/dist/inventory.js +157 -0
- package/dist/inventory.js.map +1 -0
- package/dist/match.d.ts +88 -0
- package/dist/match.d.ts.map +1 -0
- package/dist/match.js +92 -0
- package/dist/match.js.map +1 -0
- package/dist/model.d.ts +114 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +23 -0
- package/dist/model.js.map +1 -0
- package/dist/origin.d.ts +51 -0
- package/dist/origin.d.ts.map +1 -0
- package/dist/origin.js +77 -0
- package/dist/origin.js.map +1 -0
- package/dist/trust.d.ts +65 -0
- package/dist/trust.d.ts.map +1 -0
- package/dist/trust.js +59 -0
- package/dist/trust.js.map +1 -0
- package/package.json +47 -4
- package/src/discover.ts +214 -0
- package/src/fetch.ts +107 -0
- package/src/index.ts +43 -0
- package/src/inventory.ts +191 -0
- package/src/match.ts +128 -0
- package/src/model.ts +119 -0
- package/src/origin.ts +79 -0
- package/src/trust.ts +88 -0
package/dist/fetch.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fetching a lab's inventory, and reporting exactly what came back.
|
|
3
|
+
*
|
|
4
|
+
* Nothing here returns a bare "trusted" flag, because there is no single question being answered. A
|
|
5
|
+
* fetch establishes three separate things, and they are reported separately: whether the document is
|
|
6
|
+
* an inventory at all, whether its signatures hold, and whether it was served from the address it
|
|
7
|
+
* claims. A caller that wants a yes-or-no can ask for `settled`, which is all three at once and says
|
|
8
|
+
* so in its own documentation.
|
|
9
|
+
*/
|
|
10
|
+
import { verifyInventory, readInventory, LONGEST } from "./inventory.js";
|
|
11
|
+
import { inventoryUrl, labOrigin, originMatches } from "./origin.js";
|
|
12
|
+
/** How long to wait for a lab's web server. */
|
|
13
|
+
const PATIENCE = 10_000;
|
|
14
|
+
/**
|
|
15
|
+
* Fetch a lab's inventory and check it.
|
|
16
|
+
*
|
|
17
|
+
* @param address - The lab's address, like `endylab.example`.
|
|
18
|
+
* @param options - What to fetch with, and how long to wait.
|
|
19
|
+
* @returns What came back, with the signatures checked and the address compared.
|
|
20
|
+
* @throws If the address is not usable, nothing answered, or what answered is not an inventory.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { fetchInventory, settled } from "@biolibs/bionet";
|
|
25
|
+
*
|
|
26
|
+
* const found = await fetchInventory("endylab.example");
|
|
27
|
+
* settled(found); // signed, and served from the address it claims
|
|
28
|
+
* found.signed.inventory.parts.length;
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export async function fetchInventory(address, options = {}) {
|
|
32
|
+
const origin = labOrigin(address);
|
|
33
|
+
const url = inventoryUrl(origin);
|
|
34
|
+
const get = options.fetch ?? globalThis.fetch;
|
|
35
|
+
const response = await get(url, {
|
|
36
|
+
headers: { accept: "application/json" },
|
|
37
|
+
redirect: "error",
|
|
38
|
+
signal: AbortSignal.timeout(options.timeoutMs ?? PATIENCE),
|
|
39
|
+
});
|
|
40
|
+
if (!response.ok)
|
|
41
|
+
throw new Error(`${origin} has no inventory to read (${response.status}).`);
|
|
42
|
+
const text = await response.text();
|
|
43
|
+
if (text.length > LONGEST)
|
|
44
|
+
throw new Error(`${origin} published more inventory than this will read.`);
|
|
45
|
+
let parsed;
|
|
46
|
+
try {
|
|
47
|
+
parsed = JSON.parse(text);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
throw new Error(`What ${origin} published is not JSON.`);
|
|
51
|
+
}
|
|
52
|
+
const signed = readInventory(parsed);
|
|
53
|
+
return {
|
|
54
|
+
origin,
|
|
55
|
+
signed,
|
|
56
|
+
checks: await verifyInventory(signed),
|
|
57
|
+
fromItsOwnAddress: originMatches(signed.inventory.lab.origin, origin),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The fingerprints of the keys whose signatures held.
|
|
62
|
+
*
|
|
63
|
+
* These, not the lab's name, are what a lab is recognised by from one visit to the next.
|
|
64
|
+
*
|
|
65
|
+
* @param found - A fetched inventory.
|
|
66
|
+
* @returns The fingerprints, sorted, with duplicates dropped.
|
|
67
|
+
*/
|
|
68
|
+
export function signingKeys(found) {
|
|
69
|
+
return [...new Set(found.checks.filter((check) => check.ok).map((check) => check.fingerprint))].sort();
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Whether everything a fetch can settle came out right: at least one signature holds, and the document
|
|
73
|
+
* was served from the address it claims.
|
|
74
|
+
*
|
|
75
|
+
* This is not "this lab is who it says it is". It is "this document is internally consistent and came
|
|
76
|
+
* from the address you asked for". Whether that address is the lab you mean is something you knew
|
|
77
|
+
* before you typed it, or do not know at all.
|
|
78
|
+
*
|
|
79
|
+
* @param found - A fetched inventory.
|
|
80
|
+
* @returns Whether it is consistent and from its own address.
|
|
81
|
+
*/
|
|
82
|
+
export function settled(found) {
|
|
83
|
+
return found.fromItsOwnAddress && found.checks.some((check) => check.ok);
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=fetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,gBAAgB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;QACN,MAAM,EAAE,MAAM,eAAe,CAAC,MAAM,CAAC;QACrC,iBAAiB,EAAE,aAAa,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;KACtE,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,WAAW,CAAC,KAAuB;IACjD,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;AACzG,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,OAAO,CAAC,KAAuB;IAC7C,OAAO,KAAK,CAAC,iBAAiB,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;AAC3E,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* `@biolibs/bionet`: labs publishing what they hold, so a cloning plan can be acted on and not just read.
|
|
4
|
+
*
|
|
5
|
+
* This package is **not a port**. Everything else in biolibs is measured against a Python library
|
|
6
|
+
* case by case; this has no upstream to be measured against. What it has instead is a question it is
|
|
7
|
+
* trying to answer honestly: a plan that replays is a plan you can believe, but belief is not a
|
|
8
|
+
* plasmid. Somebody has to have the parts.
|
|
9
|
+
*
|
|
10
|
+
* So a lab writes down what it holds, names each part by checksum rather than by its own name for it,
|
|
11
|
+
* signs the list with its key, and publishes it at a fixed path on its own web address. Fetch one and
|
|
12
|
+
* three separate things are settled: that the document is an inventory, that its signatures hold, and
|
|
13
|
+
* that it came from the address it claims. None of those is "this lab is who it says it is" — that you
|
|
14
|
+
* knew before you typed the address, or do not know at all. See `origin.ts`, which is blunt about it.
|
|
15
|
+
*
|
|
16
|
+
* - `model.ts` — what an inventory is
|
|
17
|
+
* - `inventory.ts` — writing one, signing one, reading a stranger's
|
|
18
|
+
* - `origin.ts` — where one lives, and what fetching it proves
|
|
19
|
+
* - `fetch.ts` — fetching and checking one
|
|
20
|
+
* - `discover.ts` — finding labs you were never told about, by walking from ones you were
|
|
21
|
+
* - `trust.ts` — remembering which key answered last time, so a change gets noticed
|
|
22
|
+
* - `match.ts` — what a plan needs, and who has it
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { fetchInventory, partsNeeded, settled, whoHas } from "@biolibs/bionet";
|
|
27
|
+
*
|
|
28
|
+
* const found = await fetchInventory("endylab.example");
|
|
29
|
+
* const sourcing = whoHas(partsNeeded(plan.document), settled(found) ? [found] : []);
|
|
30
|
+
*
|
|
31
|
+
* for (const { needed, holders } of sourcing) {
|
|
32
|
+
* console.log(needed.name, holders.map((holder) => holder.lab));
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export * from "./model.js";
|
|
37
|
+
export * from "./inventory.js";
|
|
38
|
+
export * from "./origin.js";
|
|
39
|
+
export * from "./fetch.js";
|
|
40
|
+
export * from "./discover.js";
|
|
41
|
+
export * from "./trust.js";
|
|
42
|
+
export * from "./match.js";
|
|
43
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* `@biolibs/bionet`: labs publishing what they hold, so a cloning plan can be acted on and not just read.
|
|
4
|
+
*
|
|
5
|
+
* This package is **not a port**. Everything else in biolibs is measured against a Python library
|
|
6
|
+
* case by case; this has no upstream to be measured against. What it has instead is a question it is
|
|
7
|
+
* trying to answer honestly: a plan that replays is a plan you can believe, but belief is not a
|
|
8
|
+
* plasmid. Somebody has to have the parts.
|
|
9
|
+
*
|
|
10
|
+
* So a lab writes down what it holds, names each part by checksum rather than by its own name for it,
|
|
11
|
+
* signs the list with its key, and publishes it at a fixed path on its own web address. Fetch one and
|
|
12
|
+
* three separate things are settled: that the document is an inventory, that its signatures hold, and
|
|
13
|
+
* that it came from the address it claims. None of those is "this lab is who it says it is" — that you
|
|
14
|
+
* knew before you typed the address, or do not know at all. See `origin.ts`, which is blunt about it.
|
|
15
|
+
*
|
|
16
|
+
* - `model.ts` — what an inventory is
|
|
17
|
+
* - `inventory.ts` — writing one, signing one, reading a stranger's
|
|
18
|
+
* - `origin.ts` — where one lives, and what fetching it proves
|
|
19
|
+
* - `fetch.ts` — fetching and checking one
|
|
20
|
+
* - `discover.ts` — finding labs you were never told about, by walking from ones you were
|
|
21
|
+
* - `trust.ts` — remembering which key answered last time, so a change gets noticed
|
|
22
|
+
* - `match.ts` — what a plan needs, and who has it
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { fetchInventory, partsNeeded, settled, whoHas } from "@biolibs/bionet";
|
|
27
|
+
*
|
|
28
|
+
* const found = await fetchInventory("endylab.example");
|
|
29
|
+
* const sourcing = whoHas(partsNeeded(plan.document), settled(found) ? [found] : []);
|
|
30
|
+
*
|
|
31
|
+
* for (const { needed, holders } of sourcing) {
|
|
32
|
+
* console.log(needed.name, holders.map((holder) => holder.lab));
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export * from "./model.js";
|
|
37
|
+
export * from "./inventory.js";
|
|
38
|
+
export * from "./origin.js";
|
|
39
|
+
export * from "./fetch.js";
|
|
40
|
+
export * from "./discover.js";
|
|
41
|
+
export * from "./trust.js";
|
|
42
|
+
export * from "./match.js";
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writing an inventory, signing one, and reading one a stranger published.
|
|
3
|
+
*
|
|
4
|
+
* Reading is the careful half. What arrives is a file from a web address, so `readInventory` checks
|
|
5
|
+
* its shape and its size before anything else looks at it, and says plainly what is wrong when it is
|
|
6
|
+
* wrong. Nothing here decides whether to believe an inventory — `fetch.ts` and `trust.ts` do that, and
|
|
7
|
+
* they have less to go on than you might like.
|
|
8
|
+
*/
|
|
9
|
+
import type { Dseqrecord } from "@biolibs/dseqrecord";
|
|
10
|
+
import { type SignatureCheck, type SignOptions, type SigningKey } from "@biolibs/opencloning";
|
|
11
|
+
import { type InventoryDocument, type LabModel, type PeerModel, type SignedInventory } from "./model.js";
|
|
12
|
+
/** What the bytes of a signed inventory are for. */
|
|
13
|
+
export declare const INVENTORY_DOMAIN = "biolibs/bionet signed inventory v1";
|
|
14
|
+
/** How many parts one inventory may list. */
|
|
15
|
+
export declare const MOST_PARTS = 5000;
|
|
16
|
+
/** The longest an inventory may be, as text. */
|
|
17
|
+
export declare const LONGEST = 4000000;
|
|
18
|
+
/** How many labs one inventory may point at. */
|
|
19
|
+
export declare const MOST_PEERS = 200;
|
|
20
|
+
/** What `inventoryFrom` needs to know that the molecules cannot say. */
|
|
21
|
+
export interface InventoryOptions {
|
|
22
|
+
/** When to say it was published. Defaults to now. */
|
|
23
|
+
publishedAt?: string;
|
|
24
|
+
/** Whether the parts are on offer. Defaults to true. */
|
|
25
|
+
available?: boolean;
|
|
26
|
+
/** A note per part, by the part's name. */
|
|
27
|
+
notes?: Record<string, string>;
|
|
28
|
+
/**
|
|
29
|
+
* Other labs to point at, so that whoever reads this can go on to find them.
|
|
30
|
+
*
|
|
31
|
+
* Left out, the inventory carries no list at all rather than an empty one, which is what every
|
|
32
|
+
* inventory written before there were peers looks like.
|
|
33
|
+
*/
|
|
34
|
+
peers?: readonly PeerModel[];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Write an inventory from the molecules a lab holds.
|
|
38
|
+
*
|
|
39
|
+
* @param records - The molecules.
|
|
40
|
+
* @param lab - Who is publishing, and from where.
|
|
41
|
+
* @param options - When, whether the parts are offered, and any notes.
|
|
42
|
+
* @returns The inventory, ready to sign.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { inventoryFrom, signInventory } from "@biolibs/bionet";
|
|
47
|
+
*
|
|
48
|
+
* const inventory = inventoryFrom([pUC19, insert], {
|
|
49
|
+
* name: "Endy lab", origin: "https://endylab.example", contact: null, note: null,
|
|
50
|
+
* });
|
|
51
|
+
* const published = await signInventory(inventory, key, { signer: "Endy lab" });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare function inventoryFrom(records: readonly Dseqrecord[], lab: LabModel, options?: InventoryOptions): InventoryDocument;
|
|
55
|
+
/**
|
|
56
|
+
* Point at another lab, for an inventory's `peers`.
|
|
57
|
+
*
|
|
58
|
+
* @param address - Their address, however it was written.
|
|
59
|
+
* @param about - What to call them, the key you saw, and why you are pointing at them.
|
|
60
|
+
* @returns The pointer.
|
|
61
|
+
* @throws If the address is not one an inventory can be fetched from.
|
|
62
|
+
*/
|
|
63
|
+
export declare function peer(address: string, about?: {
|
|
64
|
+
name?: string;
|
|
65
|
+
key?: string;
|
|
66
|
+
note?: string;
|
|
67
|
+
}): PeerModel;
|
|
68
|
+
/**
|
|
69
|
+
* Sign an inventory, or add a signature to one already signed.
|
|
70
|
+
*
|
|
71
|
+
* A second signature on someone else's inventory is a lab vouching for it — that they have seen these
|
|
72
|
+
* parts, say. It is not required, and nothing treats it as more than one more key's claim.
|
|
73
|
+
*
|
|
74
|
+
* @param inventory - The inventory, signed or not.
|
|
75
|
+
* @param key - The key to sign with.
|
|
76
|
+
* @param options - A name to sign under, and when.
|
|
77
|
+
* @returns The signed inventory.
|
|
78
|
+
*/
|
|
79
|
+
export declare function signInventory(inventory: InventoryDocument | SignedInventory, key: SigningKey, options?: SignOptions): Promise<SignedInventory>;
|
|
80
|
+
/**
|
|
81
|
+
* Check every signature on an inventory.
|
|
82
|
+
*
|
|
83
|
+
* @param signed - The signed inventory.
|
|
84
|
+
* @returns One check per signature, in the same order. Never throws over a bad one.
|
|
85
|
+
*/
|
|
86
|
+
export declare function verifyInventory(signed: SignedInventory): Promise<SignatureCheck[]>;
|
|
87
|
+
/** @param value - Anything. @returns Whether it is an inventory with signatures beside it. */
|
|
88
|
+
export declare function isSignedInventory(value: unknown): value is SignedInventory;
|
|
89
|
+
/**
|
|
90
|
+
* Read a signed inventory somebody else published.
|
|
91
|
+
*
|
|
92
|
+
* What comes back is the document **exactly as it arrived**, not a tidied copy of it. That matters:
|
|
93
|
+
* the signature covers the bytes the lab wrote, so trimming a long note or filling in a missing field
|
|
94
|
+
* here would leave a document that no longer matches its own signature. Anything out of bounds is
|
|
95
|
+
* therefore refused rather than repaired, and anything over-long is a display problem for whatever
|
|
96
|
+
* shows it.
|
|
97
|
+
*
|
|
98
|
+
* @param value - The parsed JSON.
|
|
99
|
+
* @returns The inventory and its signatures, untouched.
|
|
100
|
+
* @throws If it is not an inventory, or holds a field this will not carry.
|
|
101
|
+
*/
|
|
102
|
+
export declare function readInventory(value: unknown): SignedInventory;
|
|
103
|
+
//# sourceMappingURL=inventory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inventory.d.ts","sourceRoot":"","sources":["../src/inventory.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAA0B,KAAK,cAAc,EAAE,KAAK,WAAW,EAC7D,KAAK,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAqB,KAAK,iBAAiB,EAAsB,KAAK,QAAQ,EAC5E,KAAK,SAAS,EAAE,KAAK,eAAe,EAAE,MAAM,YAAY,CAAC;AAGlE,oDAAoD;AACpD,eAAO,MAAM,gBAAgB,uCAAuC,CAAC;AAErE,6CAA6C;AAC7C,eAAO,MAAM,UAAU,OAAQ,CAAC;AAChC,gDAAgD;AAChD,eAAO,MAAM,OAAO,UAAY,CAAC;AAEjC,gDAAgD;AAChD,eAAO,MAAM,UAAU,MAAM,CAAC;AAE9B,wEAAwE;AACxE,MAAM,WAAW,gBAAgB;IAC/B,qDAAqD;IACrD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,wDAAwD;IACxD,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,2CAA2C;IAC3C,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B;;;;;OAKG;IACH,KAAK,CAAC,EAAE,SAAS,SAAS,EAAE,CAAC;CAC9B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,SAAS,UAAU,EAAE,EAAE,GAAG,EAAE,QAAQ,EAC7C,OAAO,GAAE,gBAAqB,GAAG,iBAAiB,CAoB/E;AAED;;;;;;;GAOG;AACH,wBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,GAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,SAAS,CAO3G;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,aAAa,CAAC,SAAS,EAAE,iBAAiB,GAAG,eAAe,EAAE,GAAG,EAAE,UAAU,EAC/D,OAAO,GAAE,WAAgB,GAAG,OAAO,CAAC,eAAe,CAAC,CAIvF;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC,CAExF;AAED,8FAA8F;AAC9F,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,eAAe,CAI1E;AAYD;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,eAAe,CA+B7D"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { signUnder, verifyUnder } from "@biolibs/opencloning";
|
|
2
|
+
import { INVENTORY_VERSION } from "./model.js";
|
|
3
|
+
import { labOrigin } from "./origin.js";
|
|
4
|
+
/** What the bytes of a signed inventory are for. */
|
|
5
|
+
export const INVENTORY_DOMAIN = "biolibs/bionet signed inventory v1";
|
|
6
|
+
/** How many parts one inventory may list. */
|
|
7
|
+
export const MOST_PARTS = 5_000;
|
|
8
|
+
/** The longest an inventory may be, as text. */
|
|
9
|
+
export const LONGEST = 4_000_000;
|
|
10
|
+
/** How many labs one inventory may point at. */
|
|
11
|
+
export const MOST_PEERS = 200;
|
|
12
|
+
/**
|
|
13
|
+
* Write an inventory from the molecules a lab holds.
|
|
14
|
+
*
|
|
15
|
+
* @param records - The molecules.
|
|
16
|
+
* @param lab - Who is publishing, and from where.
|
|
17
|
+
* @param options - When, whether the parts are offered, and any notes.
|
|
18
|
+
* @returns The inventory, ready to sign.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { inventoryFrom, signInventory } from "@biolibs/bionet";
|
|
23
|
+
*
|
|
24
|
+
* const inventory = inventoryFrom([pUC19, insert], {
|
|
25
|
+
* name: "Endy lab", origin: "https://endylab.example", contact: null, note: null,
|
|
26
|
+
* });
|
|
27
|
+
* const published = await signInventory(inventory, key, { signer: "Endy lab" });
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function inventoryFrom(records, lab, options = {}) {
|
|
31
|
+
const parts = records.map((record, at) => {
|
|
32
|
+
const name = record.name === "" ? `part${at + 1}` : record.name;
|
|
33
|
+
return {
|
|
34
|
+
id: record.id === "" || record.id === null ? String(at + 1) : record.id,
|
|
35
|
+
name,
|
|
36
|
+
checksum: record.seguid(),
|
|
37
|
+
length: record.length,
|
|
38
|
+
circular: record.circular,
|
|
39
|
+
available: options.available ?? true,
|
|
40
|
+
note: options.notes?.[name] ?? null,
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
return {
|
|
44
|
+
bionet_inventory_version: INVENTORY_VERSION,
|
|
45
|
+
lab,
|
|
46
|
+
parts,
|
|
47
|
+
...(options.peers === undefined ? {} : { peers: [...options.peers] }),
|
|
48
|
+
published_at: options.publishedAt ?? new Date().toISOString(),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Point at another lab, for an inventory's `peers`.
|
|
53
|
+
*
|
|
54
|
+
* @param address - Their address, however it was written.
|
|
55
|
+
* @param about - What to call them, the key you saw, and why you are pointing at them.
|
|
56
|
+
* @returns The pointer.
|
|
57
|
+
* @throws If the address is not one an inventory can be fetched from.
|
|
58
|
+
*/
|
|
59
|
+
export function peer(address, about = {}) {
|
|
60
|
+
return {
|
|
61
|
+
origin: labOrigin(address),
|
|
62
|
+
name: about.name ?? null,
|
|
63
|
+
key: about.key ?? null,
|
|
64
|
+
note: about.note ?? null,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Sign an inventory, or add a signature to one already signed.
|
|
69
|
+
*
|
|
70
|
+
* A second signature on someone else's inventory is a lab vouching for it — that they have seen these
|
|
71
|
+
* parts, say. It is not required, and nothing treats it as more than one more key's claim.
|
|
72
|
+
*
|
|
73
|
+
* @param inventory - The inventory, signed or not.
|
|
74
|
+
* @param key - The key to sign with.
|
|
75
|
+
* @param options - A name to sign under, and when.
|
|
76
|
+
* @returns The signed inventory.
|
|
77
|
+
*/
|
|
78
|
+
export async function signInventory(inventory, key, options = {}) {
|
|
79
|
+
const held = isSignedInventory(inventory) ? inventory.inventory : inventory;
|
|
80
|
+
const already = isSignedInventory(inventory) ? inventory.signatures : [];
|
|
81
|
+
return { inventory: held, signatures: [...already, await signUnder(INVENTORY_DOMAIN, held, key, options)] };
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Check every signature on an inventory.
|
|
85
|
+
*
|
|
86
|
+
* @param signed - The signed inventory.
|
|
87
|
+
* @returns One check per signature, in the same order. Never throws over a bad one.
|
|
88
|
+
*/
|
|
89
|
+
export async function verifyInventory(signed) {
|
|
90
|
+
return verifyUnder(INVENTORY_DOMAIN, signed.inventory, signed.signatures);
|
|
91
|
+
}
|
|
92
|
+
/** @param value - Anything. @returns Whether it is an inventory with signatures beside it. */
|
|
93
|
+
export function isSignedInventory(value) {
|
|
94
|
+
const held = value;
|
|
95
|
+
return held !== null && typeof held === "object"
|
|
96
|
+
&& Array.isArray(held.signatures) && typeof held.inventory === "object" && held.inventory !== null;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Say whether a field is absent, or text no longer than it is allowed to be.
|
|
100
|
+
*
|
|
101
|
+
* @param value - The field.
|
|
102
|
+
* @param most - How long it may be.
|
|
103
|
+
* @returns Whether it will do.
|
|
104
|
+
*/
|
|
105
|
+
const sayable = (value, most) => value === null || value === undefined || (typeof value === "string" && value.length <= most);
|
|
106
|
+
/**
|
|
107
|
+
* Read a signed inventory somebody else published.
|
|
108
|
+
*
|
|
109
|
+
* What comes back is the document **exactly as it arrived**, not a tidied copy of it. That matters:
|
|
110
|
+
* the signature covers the bytes the lab wrote, so trimming a long note or filling in a missing field
|
|
111
|
+
* here would leave a document that no longer matches its own signature. Anything out of bounds is
|
|
112
|
+
* therefore refused rather than repaired, and anything over-long is a display problem for whatever
|
|
113
|
+
* shows it.
|
|
114
|
+
*
|
|
115
|
+
* @param value - The parsed JSON.
|
|
116
|
+
* @returns The inventory and its signatures, untouched.
|
|
117
|
+
* @throws If it is not an inventory, or holds a field this will not carry.
|
|
118
|
+
*/
|
|
119
|
+
export function readInventory(value) {
|
|
120
|
+
if (!isSignedInventory(value))
|
|
121
|
+
throw new Error("This is not a signed inventory.");
|
|
122
|
+
const held = value.inventory;
|
|
123
|
+
const lab = held.lab;
|
|
124
|
+
if (typeof lab?.name !== "string" || typeof lab.origin !== "string") {
|
|
125
|
+
throw new Error("This inventory does not say which lab published it, or from where.");
|
|
126
|
+
}
|
|
127
|
+
if (lab.name.length > 200 || lab.origin.length > 500 || !sayable(lab.contact, 200) || !sayable(lab.note, 2_000)) {
|
|
128
|
+
throw new Error("This inventory says more about the lab than will be carried.");
|
|
129
|
+
}
|
|
130
|
+
if (!Array.isArray(held.parts))
|
|
131
|
+
throw new Error("This inventory has no list of parts.");
|
|
132
|
+
if (held.parts.length > MOST_PARTS)
|
|
133
|
+
throw new Error(`An inventory may list at most ${MOST_PARTS} parts.`);
|
|
134
|
+
for (const part of held.parts) {
|
|
135
|
+
if (typeof part?.checksum !== "string" || !Number.isInteger(part.length) || part.length < 0
|
|
136
|
+
|| part.checksum.length > 120 || !sayable(part.id, 200) || !sayable(part.name, 200)
|
|
137
|
+
|| !sayable(part.note, 2_000)) {
|
|
138
|
+
throw new Error("This inventory lists something that is not a part.");
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (held.peers !== undefined) {
|
|
142
|
+
if (!Array.isArray(held.peers))
|
|
143
|
+
throw new Error("This inventory's list of other labs is not a list.");
|
|
144
|
+
if (held.peers.length > MOST_PEERS)
|
|
145
|
+
throw new Error(`An inventory may point at most ${MOST_PEERS} labs.`);
|
|
146
|
+
for (const one of held.peers) {
|
|
147
|
+
if (typeof one?.origin !== "string" || one.origin.length > 500
|
|
148
|
+
|| !sayable(one.name, 200) || !sayable(one.key, 120) || !sayable(one.note, 2_000)) {
|
|
149
|
+
throw new Error("This inventory points at something that is not a lab.");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (!sayable(held.published_at, 40))
|
|
154
|
+
throw new Error("This inventory's publication time is not a time.");
|
|
155
|
+
return value;
|
|
156
|
+
}
|
|
157
|
+
//# sourceMappingURL=inventory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inventory.js","sourceRoot":"","sources":["../src/inventory.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,SAAS,EAAE,WAAW,EACL,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EACqB,MAAM,YAAY,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,oDAAoD;AACpD,MAAM,CAAC,MAAM,gBAAgB,GAAG,oCAAoC,CAAC;AAErE,6CAA6C;AAC7C,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,CAAC;AAChC,gDAAgD;AAChD,MAAM,CAAC,MAAM,OAAO,GAAG,SAAS,CAAC;AAEjC,gDAAgD;AAChD,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,CAAC;AAmB9B;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,aAAa,CAAC,OAA8B,EAAE,GAAa,EAC7C,UAA4B,EAAE;IAC1D,MAAM,KAAK,GAAoB,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,EAAE,EAAE;QACxD,MAAM,IAAI,G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AAC,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC;eAChF,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,CAAC;YAClC,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;QACxE,CAAC;IACH,CAAC;IACD,IAAI,IAAI,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;QAC7B,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;QACtG,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,UAAU;YAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,UAAU,QAAQ,CAAC,CAAC;QAC1G,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,KAA6B,EAAE,CAAC;YACrD,IAAI,OAAO,GAAG,EAAE,MAAM,KAAK,QAAQ,IAAI,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,GAAG;mBACvD,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,CAAC;gBACtF,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;YAC3E,CAAC;QACH,CAAC;IACH,CAAC;IACD,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,EAAE,EAAE,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;IACzG,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/dist/match.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a plan needs before anyone can run it, and which labs have it.
|
|
3
|
+
*
|
|
4
|
+
* A cloning plan bottoms out in molecules nobody made: the parts that were simply had. Replaying the
|
|
5
|
+
* plan proves the product follows from those parts, which is worth a great deal and still leaves the
|
|
6
|
+
* reader holding nothing. This is the step that turns a checked plan into something a lab can act on —
|
|
7
|
+
* take the plan's leaves, and ask who has them.
|
|
8
|
+
*
|
|
9
|
+
* Parts are matched by checksum, never by name. A SEGUID is the same whichever strand the molecule was
|
|
10
|
+
* written from and, for a circle, wherever it was cut open, so two labs that have never spoken publish
|
|
11
|
+
* the same checksum for the same molecule. A name agrees with nothing.
|
|
12
|
+
*/
|
|
13
|
+
import { type CloningStrategyDocument } from "@biolibs/opencloning";
|
|
14
|
+
import type { FetchedInventory } from "./fetch.js";
|
|
15
|
+
import type { InventoryPart } from "./model.js";
|
|
16
|
+
/** One molecule a plan starts from. */
|
|
17
|
+
export interface NeededPart {
|
|
18
|
+
/** Its identifier within the plan. */
|
|
19
|
+
id: number;
|
|
20
|
+
/** What the plan calls it. */
|
|
21
|
+
name: string;
|
|
22
|
+
/** Its SEGUID checksum, which is what labs are asked about. */
|
|
23
|
+
checksum: string;
|
|
24
|
+
/** How many base pairs. */
|
|
25
|
+
length: number;
|
|
26
|
+
/** Whether it is a circle. */
|
|
27
|
+
circular: boolean;
|
|
28
|
+
}
|
|
29
|
+
/** A lab that has a part. */
|
|
30
|
+
export interface Holder {
|
|
31
|
+
/** The lab's address. */
|
|
32
|
+
origin: string;
|
|
33
|
+
/** What the lab calls itself. */
|
|
34
|
+
lab: string;
|
|
35
|
+
/** The part as that lab lists it. */
|
|
36
|
+
part: InventoryPart;
|
|
37
|
+
}
|
|
38
|
+
/** A part a plan needs, and who has it. */
|
|
39
|
+
export interface Sourcing {
|
|
40
|
+
/** The part. */
|
|
41
|
+
needed: NeededPart;
|
|
42
|
+
/** The labs listing a molecule with this checksum, in the order the inventories were given. */
|
|
43
|
+
holders: Holder[];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The molecules a plan starts from: the ones no step in it makes.
|
|
47
|
+
*
|
|
48
|
+
* @param document - The plan.
|
|
49
|
+
* @returns One entry per starting molecule, with its checksum.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { partsNeeded } from "@biolibs/bionet";
|
|
54
|
+
*
|
|
55
|
+
* partsNeeded(plan.document).map((part) => part.name); // [ "f1", "f2", "f3" ]
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function partsNeeded(document: CloningStrategyDocument): NeededPart[];
|
|
59
|
+
/**
|
|
60
|
+
* Ask a set of inventories who has each of a plan's starting molecules.
|
|
61
|
+
*
|
|
62
|
+
* Inventories whose signatures do not hold, or that were not served from the address they claim, are
|
|
63
|
+
* the caller's to filter out first — `settled` in `fetch.ts` says which those are. This matches what
|
|
64
|
+
* it is given, and says nothing about whether the labs are to be believed.
|
|
65
|
+
*
|
|
66
|
+
* @param needed - What the plan needs.
|
|
67
|
+
* @param inventories - The inventories to ask.
|
|
68
|
+
* @returns One entry per needed part, with the labs that list it.
|
|
69
|
+
*/
|
|
70
|
+
export declare function whoHas(needed: readonly NeededPart[], inventories: readonly FetchedInventory[]): Sourcing[];
|
|
71
|
+
/**
|
|
72
|
+
* The parts nobody asked has.
|
|
73
|
+
*
|
|
74
|
+
* @param sourcing - What `whoHas` worked out.
|
|
75
|
+
* @returns The parts with no holder.
|
|
76
|
+
*/
|
|
77
|
+
export declare function unsourced(sourcing: readonly Sourcing[]): NeededPart[];
|
|
78
|
+
/**
|
|
79
|
+
* The parts a lab has but will not send.
|
|
80
|
+
*
|
|
81
|
+
* Listing a part without offering it is a normal thing for a lab to do, and a reader is better off
|
|
82
|
+
* knowing the molecule exists somewhere than being told nobody has it.
|
|
83
|
+
*
|
|
84
|
+
* @param sourcing - What `whoHas` worked out.
|
|
85
|
+
* @returns The parts whose every holder has them marked unavailable.
|
|
86
|
+
*/
|
|
87
|
+
export declare function heldButNotOffered(sourcing: readonly Sourcing[]): NeededPart[];
|
|
88
|
+
//# sourceMappingURL=match.d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;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,SAAS,QAAQ,EAAE,GAAG,UAAU,EAAE,CAI7E"}
|
package/dist/match.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a plan needs before anyone can run it, and which labs have it.
|
|
3
|
+
*
|
|
4
|
+
* A cloning plan bottoms out in molecules nobody made: the parts that were simply had. Replaying the
|
|
5
|
+
* plan proves the product follows from those parts, which is worth a great deal and still leaves the
|
|
6
|
+
* reader holding nothing. This is the step that turns a checked plan into something a lab can act on —
|
|
7
|
+
* take the plan's leaves, and ask who has them.
|
|
8
|
+
*
|
|
9
|
+
* Parts are matched by checksum, never by name. A SEGUID is the same whichever strand the molecule was
|
|
10
|
+
* written from and, for a circle, wherever it was cut open, so two labs that have never spoken publish
|
|
11
|
+
* the same checksum for the same molecule. A name agrees with nothing.
|
|
12
|
+
*/
|
|
13
|
+
import { recordFromSequenceModel } from "@biolibs/opencloning";
|
|
14
|
+
/**
|
|
15
|
+
* The molecules a plan starts from: the ones no step in it makes.
|
|
16
|
+
*
|
|
17
|
+
* @param document - The plan.
|
|
18
|
+
* @returns One entry per starting molecule, with its checksum.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { partsNeeded } from "@biolibs/bionet";
|
|
23
|
+
*
|
|
24
|
+
* partsNeeded(plan.document).map((part) => part.name); // [ "f1", "f2", "f3" ]
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function partsNeeded(document) {
|
|
28
|
+
const made = new Set(document.sources.filter((source) => source.input.length > 0).map((source) => source.id));
|
|
29
|
+
const names = new Map(document.sources.map((source) => [source.id, source.output_name]));
|
|
30
|
+
const needed = [];
|
|
31
|
+
for (const sequence of document.sequences) {
|
|
32
|
+
if (made.has(sequence.id))
|
|
33
|
+
continue;
|
|
34
|
+
const record = recordFromSequenceModel(sequence);
|
|
35
|
+
needed.push({
|
|
36
|
+
id: sequence.id,
|
|
37
|
+
name: names.get(sequence.id) ?? record.name,
|
|
38
|
+
checksum: record.seguid(),
|
|
39
|
+
length: record.length,
|
|
40
|
+
circular: record.circular,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return needed;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Ask a set of inventories who has each of a plan's starting molecules.
|
|
47
|
+
*
|
|
48
|
+
* Inventories whose signatures do not hold, or that were not served from the address they claim, are
|
|
49
|
+
* the caller's to filter out first — `settled` in `fetch.ts` says which those are. This matches what
|
|
50
|
+
* it is given, and says nothing about whether the labs are to be believed.
|
|
51
|
+
*
|
|
52
|
+
* @param needed - What the plan needs.
|
|
53
|
+
* @param inventories - The inventories to ask.
|
|
54
|
+
* @returns One entry per needed part, with the labs that list it.
|
|
55
|
+
*/
|
|
56
|
+
export function whoHas(needed, inventories) {
|
|
57
|
+
return needed.map((part) => {
|
|
58
|
+
const holders = [];
|
|
59
|
+
for (const found of inventories) {
|
|
60
|
+
for (const listed of found.signed.inventory.parts) {
|
|
61
|
+
if (listed.checksum === part.checksum) {
|
|
62
|
+
holders.push({ origin: found.origin, lab: found.signed.inventory.lab.name, part: listed });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return { needed: part, holders };
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The parts nobody asked has.
|
|
71
|
+
*
|
|
72
|
+
* @param sourcing - What `whoHas` worked out.
|
|
73
|
+
* @returns The parts with no holder.
|
|
74
|
+
*/
|
|
75
|
+
export function unsourced(sourcing) {
|
|
76
|
+
return sourcing.filter((one) => one.holders.length === 0).map((one) => one.needed);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The parts a lab has but will not send.
|
|
80
|
+
*
|
|
81
|
+
* Listing a part without offering it is a normal thing for a lab to do, and a reader is better off
|
|
82
|
+
* knowing the molecule exists somewhere than being told nobody has it.
|
|
83
|
+
*
|
|
84
|
+
* @param sourcing - What `whoHas` worked out.
|
|
85
|
+
* @returns The parts whose every holder has them marked unavailable.
|
|
86
|
+
*/
|
|
87
|
+
export function heldButNotOffered(sourcing) {
|
|
88
|
+
return sourcing
|
|
89
|
+
.filter((one) => one.holders.length > 0 && one.holders.every((holder) => !holder.part.available))
|
|
90
|
+
.map((one) => one.needed);
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=match.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"match.js","sourceRoot":"","sources":["../src/match.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,uBAAuB,EAAgC,MAAM,sBAAsB,CAAC;AAoC7F;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,WAAW,CAAC,QAAiC;IAC3D,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC;IAC9G,MAAM,KAAK,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IACzF,MAAM,MAAM,GAAiB,EAAE,CAAC;IAChC,KAAK,MAAM,QAAQ,IAAI,QAAQ,CAAC,SAAS,EAAE,CAAC;QAC1C,IAAI,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;YAAE,SAAS;QACpC,MAAM,MAAM,GAAG,uBAAuB,CAAC,QAAQ,CAAC,CAAC;QACjD,MAAM,CAAC,IAAI,CAAC;YACV,EAAE,EAAE,QAA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