@biolibs/bionet-client 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/dist/link.js ADDED
@@ -0,0 +1,228 @@
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 { entriesBeyond, haveOf, readMessage, signHello } from "@biolibs/bionet";
13
+ /** How many entries go in one message. */
14
+ const BATCH = 500;
15
+ /**
16
+ * The connection from one device to its lab's web peer.
17
+ *
18
+ * `Lab` makes and runs one of these; it is exported for anything that wants to drive the exchange
19
+ * itself. Give it the peer's address, the lab, the device's key and a way to read and add to what
20
+ * the device holds, then `start()` it. It reconnects on its own, waiting longer each time.
21
+ */
22
+ export class PeerLink {
23
+ options;
24
+ status = "offline";
25
+ /** What is in the way, said for a person, or `null`. */
26
+ problem = null;
27
+ /** When this device and the peer last had the same entries. */
28
+ lastSynced = null;
29
+ /** What the peer holds, as far as this knows. `null` until the peer has said. */
30
+ peerHas = null;
31
+ ws = null;
32
+ stopped = true;
33
+ delay = 1_000;
34
+ retry = null;
35
+ turn = Promise.resolve();
36
+ publishing = null;
37
+ pushNow = () => { };
38
+ sendInventory = () => { };
39
+ constructor(options) {
40
+ this.options = options;
41
+ }
42
+ /** How many entries held here the peer is not known to have. */
43
+ get pending() {
44
+ const held = this.options.held();
45
+ return this.peerHas === null ? held.length : entriesBeyond(held, this.peerHas).length;
46
+ }
47
+ /** Start, and keep trying. */
48
+ start() {
49
+ this.stopped = false;
50
+ this.open();
51
+ }
52
+ /** Stop, and stop trying. */
53
+ stop() {
54
+ this.stopped = true;
55
+ if (this.retry !== null)
56
+ clearTimeout(this.retry);
57
+ this.ws?.close();
58
+ this.ws = null;
59
+ this.pushNow = () => { };
60
+ this.sendInventory = () => { };
61
+ this.say("offline", null);
62
+ }
63
+ /** Send the peer whatever it lacks. Called whenever something is written on this device. */
64
+ push() {
65
+ this.pushNow();
66
+ }
67
+ /**
68
+ * Ask the peer to publish an inventory.
69
+ *
70
+ * @param signed - The inventory, signed by this device.
71
+ * @returns Whether it was published, and why not.
72
+ */
73
+ publish(signed) {
74
+ if (this.status !== "synced" && this.status !== "syncing") {
75
+ return Promise.resolve({ ok: false, reason: "Not connected to the lab's peer as a member." });
76
+ }
77
+ return new Promise((resolve) => {
78
+ this.publishing = resolve;
79
+ this.sendInventory(signed);
80
+ setTimeout(() => {
81
+ if (this.publishing === resolve) {
82
+ this.publishing = null;
83
+ resolve({ ok: false, reason: "The lab's peer did not answer." });
84
+ }
85
+ }, 15_000);
86
+ });
87
+ }
88
+ say(status, problem = this.problem) {
89
+ this.status = status;
90
+ this.problem = problem;
91
+ this.options.onChange();
92
+ }
93
+ settled() {
94
+ this.lastSynced = new Date().toISOString();
95
+ this.say("synced", null);
96
+ }
97
+ open() {
98
+ if (this.stopped)
99
+ return;
100
+ const { lab, key, held, take } = this.options;
101
+ const Socket = this.options.WebSocket ?? globalThis.WebSocket;
102
+ this.say("connecting", null);
103
+ let ws;
104
+ try {
105
+ ws = new Socket(this.options.url);
106
+ }
107
+ catch (cause) {
108
+ this.say("offline", `That is not an address a peer can be reached at: ${cause instanceof Error ? cause.message : String(cause)}`);
109
+ return;
110
+ }
111
+ this.ws = ws;
112
+ let nonce = "";
113
+ let welcomed = false;
114
+ let pushedEverything = false;
115
+ const send = (message) => { if (ws.readyState === ws.OPEN)
116
+ ws.send(JSON.stringify(message)); };
117
+ const sendEntries = (entries) => {
118
+ for (let at = 0; at < entries.length; at += BATCH)
119
+ send({ type: "entries", entries: entries.slice(at, at + BATCH) });
120
+ };
121
+ const hello = async () => send({ type: "hello", have: haveOf(held()), signature: await signHello(key, lab, nonce) });
122
+ this.pushNow = () => {
123
+ // Only over a connection that is open. Counting an entry as delivered because it was written
124
+ // into a socket that had already closed would leave it unsent and reported as sent.
125
+ if (!welcomed || this.peerHas === null || ws.readyState !== ws.OPEN)
126
+ return;
127
+ const lacking = entriesBeyond(held(), this.peerHas);
128
+ if (lacking.length === 0)
129
+ return;
130
+ this.peerHas = haveOf(held()); // what it will have, once it has kept these
131
+ this.say("syncing");
132
+ sendEntries(lacking);
133
+ };
134
+ this.sendInventory = (signed) => send({ type: "inventory", signed });
135
+ ws.addEventListener("message", (event) => {
136
+ this.turn = this.turn.then(async () => {
137
+ let message;
138
+ try {
139
+ message = readMessage(String(event.data));
140
+ }
141
+ catch {
142
+ return; // a peer that says something else is ignored, not obeyed
143
+ }
144
+ if (message.type === "challenge") {
145
+ if (message.lab !== lab) {
146
+ this.say("waiting", "That peer serves a different lab.");
147
+ return;
148
+ }
149
+ nonce = message.nonce;
150
+ await hello();
151
+ }
152
+ else if (message.type === "refused") {
153
+ if (message.code === "not-a-member") {
154
+ if (welcomed) {
155
+ welcomed = false;
156
+ this.say("waiting", message.reason);
157
+ }
158
+ else if (!pushedEverything && held().length > 0) {
159
+ pushedEverything = true;
160
+ sendEntries(held());
161
+ }
162
+ else {
163
+ this.say("waiting", "This device's key is not on the roster the lab's peer holds yet. A keeper has to add it and sync.");
164
+ // Ask again in a few seconds, on the same connection: the keeper may be doing it now.
165
+ setTimeout(() => { if (this.ws === ws && !welcomed)
166
+ void hello(); }, 4_000);
167
+ }
168
+ }
169
+ else if (message.code === "bad-inventory" && this.publishing !== null) {
170
+ this.publishing({ ok: false, reason: message.reason });
171
+ this.publishing = null;
172
+ }
173
+ else {
174
+ this.say(this.status, message.reason);
175
+ }
176
+ }
177
+ else if (message.type === "welcome") {
178
+ welcomed = true;
179
+ this.delay = 1_000;
180
+ this.peerHas = message.have;
181
+ this.say("syncing", null);
182
+ this.pushNow();
183
+ if (this.pending === 0)
184
+ this.settled();
185
+ }
186
+ else if (message.type === "entries") {
187
+ await take(message.entries);
188
+ if (welcomed && this.peerHas !== null) {
189
+ // Whatever the peer just sent, it has. Count it, so it is not offered straight back.
190
+ for (const { entry } of message.entries) {
191
+ this.peerHas[entry.author] = Math.max(this.peerHas[entry.author] ?? -1, entry.seq);
192
+ }
193
+ if (this.pending === 0)
194
+ this.settled();
195
+ }
196
+ }
197
+ else if (message.type === "kept") {
198
+ if (!welcomed)
199
+ await hello(); // the peer now knows the roster; ask again
200
+ else if (this.pending === 0)
201
+ this.settled();
202
+ }
203
+ else if (message.type === "published" && this.publishing !== null) {
204
+ this.publishing({ ok: true, reason: null });
205
+ this.publishing = null;
206
+ }
207
+ });
208
+ });
209
+ ws.addEventListener("close", () => {
210
+ if (this.ws !== ws)
211
+ return;
212
+ this.ws = null;
213
+ this.pushNow = () => { };
214
+ this.sendInventory = () => { };
215
+ if (this.publishing !== null) {
216
+ this.publishing({ ok: false, reason: "The connection to the lab's peer was lost." });
217
+ this.publishing = null;
218
+ }
219
+ if (this.stopped)
220
+ return;
221
+ this.say("offline", this.status === "waiting" ? this.problem : null);
222
+ this.retry = setTimeout(() => this.open(), this.delay);
223
+ this.delay = Math.min(this.delay * 2, 30_000);
224
+ });
225
+ ws.addEventListener("error", () => { }); // the close that follows is what is acted on
226
+ }
227
+ }
228
+ //# sourceMappingURL=link.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"link.js","sourceRoot":"","sources":["../src/link.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAC3B,MAAM,iBAAiB,CAAC;AA2BnD,0CAA0C;AAC1C,MAAM,KAAK,GAAG,GAAG,CAAC;AAElB;;;;;;GAMG;AACH,MAAM,OAAO,QAAQ;IAkBU;IAjB7B,MAAM,GAAe,SAAS,CAAC;IAC/B,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D,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC;QAChD,CAAC,CAAC,CAAC;QACH,EAAE,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC,CAAI,6CAA6C;IAC1F,CAAC;CACF"}
@@ -0,0 +1,52 @@
1
+ /**
2
+ * The narrow way a window reaches a lab.
3
+ *
4
+ * A window does not always share a process with the lab it shows. In a desktop app the lab lives
5
+ * where the key and the files are, and the window is deliberately given neither. So anything that
6
+ * draws a lab asks for it through this: the lab as plain data, to be told when it changes, and one
7
+ * of a fixed list of things to be done. A `Lab` is one of these directly (`sourceOf`), and so is a
8
+ * bridge to a lab somewhere else.
9
+ */
10
+ import type { Lab, LabSnapshot } from "./lab.js";
11
+ /** Everything a window may ask a lab to do. Nothing outside this list can be asked for. */
12
+ export declare const ACTIONS: readonly ["setPerson", "found", "join", "setPeer", "setOrigin", "addMember", "addPart", "addItem", "adjust", "recount", "retire", "offer", "setField", "publish"];
13
+ /** One thing a window may ask for. */
14
+ export type Action = (typeof ACTIONS)[number];
15
+ /** How asking went: what came back, or why it could not be done, said for a person. */
16
+ export type Answer = {
17
+ ok: true;
18
+ result: unknown;
19
+ } | {
20
+ ok: false;
21
+ error: string;
22
+ };
23
+ /** A lab, as a window reaches it. */
24
+ export interface LabSource {
25
+ /** Be told when the lab changes. Returns a function that stops the telling. */
26
+ subscribe(listener: () => void): () => void;
27
+ /** The lab as it stands, or `null` while it is still being opened. The same object until it changes. */
28
+ snapshot(): LabSnapshot | null;
29
+ /** Ask for one thing to be done. It never throws: a refusal comes back as an answer. */
30
+ act(action: Action, payload?: unknown): Promise<Answer>;
31
+ }
32
+ /**
33
+ * Do one of the things a window may ask for.
34
+ *
35
+ * This is the door between a window and a lab, and it is deliberately the only one: an action that
36
+ * is not on the list is refused whatever it is called, so a window that has been tricked can still
37
+ * only do what a person at it could.
38
+ *
39
+ * @param lab - The lab.
40
+ * @param action - What to do.
41
+ * @param payload - What to do it with.
42
+ * @returns What came back, or why it could not be done.
43
+ */
44
+ export declare function perform(lab: Lab, action: string, payload?: unknown): Promise<Answer>;
45
+ /**
46
+ * A lab in this process, as a window reaches it.
47
+ *
48
+ * @param lab - The lab, opened.
49
+ * @returns The source.
50
+ */
51
+ export declare function sourceOf(lab: Lab): LabSource;
52
+ //# sourceMappingURL=source.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"source.d.ts","sourceRoot":"","sources":["../src/source.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEjD,2FAA2F;AAC3F,eAAO,MAAM,OAAO,mKAC2E,CAAC;AAEhG,sCAAsC;AACtC,MAAM,MAAM,MAAM,GAAG,CAAC,OAAO,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC;AAE9C,uFAAuF;AACvF,MAAM,MAAM,MAAM,GAAG;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAElF,qCAAqC;AACrC,MAAM,WAAW,SAAS;IACxB,+EAA+E;IAC/E,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC;IAC5C,wGAAwG;IACxG,QAAQ,IAAI,WAAW,GAAG,IAAI,CAAC;IAC/B,wFAAwF;IACxF,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACzD;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAQ1F;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,SAAS,CAE5C"}
package/dist/source.js ADDED
@@ -0,0 +1,36 @@
1
+ /** Everything a window may ask a lab to do. Nothing outside this list can be asked for. */
2
+ export const ACTIONS = ["setPerson", "found", "join", "setPeer", "setOrigin", "addMember", "addPart", "addItem",
3
+ "adjust", "recount", "retire", "offer", "setField", "publish"];
4
+ /**
5
+ * Do one of the things a window may ask for.
6
+ *
7
+ * This is the door between a window and a lab, and it is deliberately the only one: an action that
8
+ * is not on the list is refused whatever it is called, so a window that has been tricked can still
9
+ * only do what a person at it could.
10
+ *
11
+ * @param lab - The lab.
12
+ * @param action - What to do.
13
+ * @param payload - What to do it with.
14
+ * @returns What came back, or why it could not be done.
15
+ */
16
+ export async function perform(lab, action, payload) {
17
+ if (!ACTIONS.includes(action))
18
+ return { ok: false, error: "That is not something this app does." };
19
+ try {
20
+ const method = lab[action];
21
+ return { ok: true, result: await method.call(lab, payload) };
22
+ }
23
+ catch (cause) {
24
+ return { ok: false, error: cause instanceof Error ? cause.message : String(cause) };
25
+ }
26
+ }
27
+ /**
28
+ * A lab in this process, as a window reaches it.
29
+ *
30
+ * @param lab - The lab, opened.
31
+ * @returns The source.
32
+ */
33
+ export function sourceOf(lab) {
34
+ return { subscribe: lab.subscribe, snapshot: lab.snapshot, act: (action, payload) => perform(lab, action, payload) };
35
+ }
36
+ //# sourceMappingURL=source.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"source.js","sourceRoot":"","sources":["../src/source.ts"],"names":[],"mappings":"AAWA,2FAA2F;AAC3F,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,WAAW,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS;IACvF,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,SAAS,CAAU,CAAC;AAkBhG;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,OAAO,CAAC,GAAQ,EAAE,MAAc,EAAE,OAAiB;IACvE,IAAI,CAAE,OAA6B,CAAC,QAAQ,CAAC,MAAM,CAAC;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,sCAAsC,EAAE,CAAC;IAC1H,IAAI,CAAC;QACH,MAAM,MAAM,GAAI,GAAwE,CAAC,MAAM,CAAE,CAAC;QAClG,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,CAAC;IAC/D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;IACtF,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAQ;IAC/B,OAAO,EAAE,SAAS,EAAE,GAAG,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;AACvH,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { LabStore } from "./store.js";
2
+ /**
3
+ * Open, or make, a device's store in a folder.
4
+ *
5
+ * @param dir - The folder. The file is `lab.db` inside it.
6
+ * @returns The store.
7
+ */
8
+ export declare function sqliteStore(dir: string): LabStore;
9
+ //# sourceMappingURL=sqlite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../src/sqlite.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAM3C;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,CAqCjD"}
package/dist/sqlite.js ADDED
@@ -0,0 +1,60 @@
1
+ /**
2
+ * A device's store as one SQLite file, using the SQLite built into Node.
3
+ *
4
+ * This is its own entry point, `@biolibs/bionet-client/sqlite`, because it needs Node: importing the
5
+ * rest of the package into a browser must not drag it in.
6
+ */
7
+ import { mkdirSync } from "node:fs";
8
+ import { createRequire } from "node:module";
9
+ import { join } from "node:path";
10
+ // Asked for at run time and not imported, because tools that bundle or transform code do not all
11
+ // know yet that `node:sqlite` is part of Node and has no name without its prefix.
12
+ const { DatabaseSync } = createRequire(import.meta.url)("node:sqlite");
13
+ /**
14
+ * Open, or make, a device's store in a folder.
15
+ *
16
+ * @param dir - The folder. The file is `lab.db` inside it.
17
+ * @returns The store.
18
+ */
19
+ export function sqliteStore(dir) {
20
+ mkdirSync(dir, { recursive: true });
21
+ const db = new DatabaseSync(join(dir, "lab.db"));
22
+ db.exec(`
23
+ pragma journal_mode = wal;
24
+ create table if not exists entries (id text primary key, author text not null, seq integer not null, json text not null);
25
+ create index if not exists entries_by_log on entries (author, seq);
26
+ create table if not exists settings (name text primary key, value text not null);
27
+ `);
28
+ const insert = db.prepare("insert or ignore into entries (id, author, seq, json) values (?, ?, ?, ?)");
29
+ const put = db.prepare("insert into settings (name, value) values (?, ?) on conflict(name) do update set value = excluded.value");
30
+ const get = db.prepare("select value from settings where name = ?");
31
+ const forget = db.prepare("delete from settings where name = ?");
32
+ const all = db.prepare("select json from entries order by author, seq");
33
+ return {
34
+ entries: () => all.all().map((row) => JSON.parse(String(row["json"]))),
35
+ add(entries) {
36
+ db.exec("begin");
37
+ try {
38
+ for (const { id, signed } of entries)
39
+ insert.run(id, signed.entry.author, signed.entry.seq, JSON.stringify(signed));
40
+ db.exec("commit");
41
+ }
42
+ catch (cause) {
43
+ db.exec("rollback");
44
+ throw cause;
45
+ }
46
+ },
47
+ setting(name) {
48
+ const row = get.get(name);
49
+ return row === undefined ? null : String(row["value"]);
50
+ },
51
+ set(name, value) {
52
+ if (value === null)
53
+ forget.run(name);
54
+ else
55
+ put.run(name, value);
56
+ },
57
+ close: () => db.close(),
58
+ };
59
+ }
60
+ //# sourceMappingURL=sqlite.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite.js","sourceRoot":"","sources":["../src/sqlite.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAIjC,iGAAiG;AACjG,kFAAkF;AAClF,MAAM,EAAE,YAAY,EAAE,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,aAAa,CAAiC,CAAC;AAEvG;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,GAAW;IACrC,SAAS,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACpC,MAAM,EAAE,GAAG,IAAI,YAAY,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC;IACjD,EAAE,CAAC,IAAI,CAAC;;;;;GAKP,CAAC,CAAC;IACH,MAAM,MAAM,GAAG,EAAE,CAAC,OAAO,CAAC,2EAA2E,CAAC,CAAC;IACvG,MAAM,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,yGAAyG,CAAC,CAAC;IAClI,MAAM,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,2CAA2C,CAAC,CAAC;IACpE,MAAM,MAAM,GAAG,EAAE,CAAC,OAAO,CAAC,qCAAqC,CAAC,CAAC;IACjE,MAAM,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,+CAA+C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/**
2
+ * What a device keeps: the lab's entries, and a few settings.
3
+ *
4
+ * The entries are the truth and everything shown is worked out from them, so a store has no table of
5
+ * items or amounts to fall out of step. Lose it, and the lab's web peer or any other member hands
6
+ * every entry back. Where it lives depends on the device, so it is an interface: a SQLite file on a
7
+ * desktop (`@biolibs/bionet-client/sqlite`), memory in a test, whatever a browser offers.
8
+ */
9
+ import type { SignedEntry } from "@biolibs/bionet";
10
+ /** Where a device keeps things. */
11
+ export interface LabStore {
12
+ /** Every entry held. */
13
+ entries(): SignedEntry[];
14
+ /** Keep more entries, each with its ID. Adding one already held does nothing. */
15
+ add(entries: readonly {
16
+ id: string;
17
+ signed: SignedEntry;
18
+ }[]): void;
19
+ /** A setting, or `null` if it was never set. */
20
+ setting(name: string): string | null;
21
+ /** Set a setting. `null` forgets it. */
22
+ set(name: string, value: string | null): void;
23
+ /** Finish with the store. */
24
+ close(): void;
25
+ }
26
+ /** Where a device's private key lives. A desktop app gives one backed by the system's keychain. */
27
+ export interface KeyKeeper {
28
+ /** The key as `exportSigningKey` wrote it, or `null` if this device has none yet. */
29
+ load(): Promise<string | null>;
30
+ /** Keep a new key. */
31
+ save(exported: string): Promise<void>;
32
+ }
33
+ /**
34
+ * A store that lives in memory and is gone when the process is.
35
+ *
36
+ * @returns The store.
37
+ */
38
+ export declare function memoryStore(): LabStore;
39
+ /**
40
+ * A key kept in memory, for tests and for trying things out. A real device keeps its key somewhere
41
+ * the operating system guards.
42
+ *
43
+ * @returns The keeper.
44
+ */
45
+ export declare function memoryKeys(): KeyKeeper;
46
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEnD,mCAAmC;AACnC,MAAM,WAAW,QAAQ;IACvB,wBAAwB;IACxB,OAAO,IAAI,WAAW,EAAE,CAAC;IACzB,iFAAiF;IACjF,GAAG,CAAC,OAAO,EAAE,SAAS;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,WAAW,CAAA;KAAE,EAAE,GAAG,IAAI,CAAC;IACnE,gDAAgD;IAChD,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IACrC,wCAAwC;IACxC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC;IAC9C,6BAA6B;IAC7B,KAAK,IAAI,IAAI,CAAC;CACf;AAED,mGAAmG;AACnG,MAAM,WAAW,SAAS;IACxB,qFAAqF;IACrF,IAAI,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAC/B,sBAAsB;IACtB,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACvC;AAED;;;;GAIG;AACH,wBAAgB,WAAW,IAAI,QAAQ,CAUtC;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,IAAI,SAAS,CAGtC"}
package/dist/store.js ADDED
@@ -0,0 +1,32 @@
1
+ /**
2
+ * A store that lives in memory and is gone when the process is.
3
+ *
4
+ * @returns The store.
5
+ */
6
+ export function memoryStore() {
7
+ const held = new Map();
8
+ const settings = new Map();
9
+ return {
10
+ entries: () => [...held.values()],
11
+ add: (entries) => { for (const { id, signed } of entries)
12
+ if (!held.has(id))
13
+ held.set(id, signed); },
14
+ setting: (name) => settings.get(name) ?? null,
15
+ set: (name, value) => { if (value === null)
16
+ settings.delete(name);
17
+ else
18
+ settings.set(name, value); },
19
+ close: () => { },
20
+ };
21
+ }
22
+ /**
23
+ * A key kept in memory, for tests and for trying things out. A real device keeps its key somewhere
24
+ * the operating system guards.
25
+ *
26
+ * @returns The keeper.
27
+ */
28
+ export function memoryKeys() {
29
+ let held = null;
30
+ return { load: async () => held, save: async (exported) => { held = exported; } };
31
+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1
  {
2
+ "bugs": {
3
+ "url": "https://github.com/matthewlovestocode/biolibs/issues"
4
+ },
5
+ "dependencies": {
6
+ "@biolibs/bionet": "^1.2.0",
7
+ "@biolibs/opencloning": "^2.0.0"
8
+ },
9
+ "description": "One device in one lab: its key, its copy of the lab's signed log, everything a member can do, and the connection to the lab's web peer. Works offline and syncs when it can. Storage and key keeping are plugged in, so it runs in Electron, Node or a browser",
10
+ "engines": {
11
+ "node": ">=20"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ },
18
+ "./sqlite": {
19
+ "types": "./dist/sqlite.d.ts",
20
+ "default": "./dist/sqlite.js"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "src"
26
+ ],
27
+ "homepage": "https://github.com/matthewlovestocode/biolibs/tree/main/packages/bionet-client#readme",
28
+ "keywords": [
29
+ "bionet",
30
+ "local-first",
31
+ "offline",
32
+ "sync",
33
+ "inventory",
34
+ "biology"
35
+ ],
36
+ "license": "MIT",
37
+ "main": "dist/index.js",
2
38
  "name": "@biolibs/bionet-client",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/matthewlovestocode/biolibs.git",
45
+ "directory": "packages/bionet-client"
46
+ },
47
+ "type": "module",
48
+ "types": "dist/index.d.ts",
49
+ "version": "0.1.0"
50
+ }
package/src/index.ts ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * `@biolibs/bionet-client`: one device in one lab.
4
+ *
5
+ * `@biolibs/bionet` says what a lab's signed log means. This is the thing that keeps one: a device's
6
+ * key, its copy of the log, every action a member can take, and the connection to the lab's web
7
+ * peer. It works with no network and syncs when there is one.
8
+ *
9
+ * It is an application with the edges left open. Where entries are kept, where the key lives and
10
+ * what opens a socket are handed in, so the same `Lab` runs behind a desktop window, in a browser or
11
+ * in a test:
12
+ *
13
+ * - `lab.ts` — the device: `Lab`, and the plain-data snapshot a window draws
14
+ * - `link.ts` — the connection to the web peer, which is allowed to fail
15
+ * - `source.ts` — the narrow way a window reaches a lab, in this process or another
16
+ * - `store.ts` — the interfaces for storage and key keeping, with in-memory versions
17
+ * - `sqlite.ts` — storage in a SQLite file, at `@biolibs/bionet-client/sqlite` because it needs Node
18
+ */
19
+ export * from "./lab.js";
20
+ export * from "./link.js";
21
+ export * from "./store.js";
22
+ export * from "./source.js";