@cero-base/core 3.4.1 → 3.6.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.
@@ -0,0 +1,95 @@
1
+ import EventEmitter from 'events';
2
+ /**
3
+ * A signal as it crosses: `INVALID` for what is no offer or answer, or is over 64 KiB.
4
+ *
5
+ * @param {{ type: 'offer' | 'answer', sdp: string }} signal
6
+ * @returns {{ type: number, sdp: string }}
7
+ */
8
+ export declare function encode(signal: {
9
+ type: 'offer' | 'answer';
10
+ sdp: string;
11
+ }): {
12
+ type: number;
13
+ sdp: string;
14
+ };
15
+ /**
16
+ * A local UDP port for one call over one open connection: what is sent to `port` from this host
17
+ * reaches the bridge of the same `id` on the other end, which hands it to the local port that last
18
+ * sent to it. Media goes as it is, may be dropped and never waits behind other media. Signals are
19
+ * WebRTC descriptions, in order.
20
+ */
21
+ export declare class Bridge extends EventEmitter {
22
+ /** @type {import('@hyperswarm/secret-stream')} */
23
+ stream: import('@hyperswarm/secret-stream');
24
+ id: Uint8Array<ArrayBufferLike>;
25
+ /** This side's alias, at the head of each datagram it sends. */
26
+ alias: number;
27
+ /** The other side's, -1 until the channel opens. */
28
+ remote: number;
29
+ opened: boolean;
30
+ /** The other end routes this side's datagrams: media flows. */
31
+ routed: boolean;
32
+ closed: boolean;
33
+ /** The local port media last came from; 0 before any. */
34
+ sender: number;
35
+ /** @type {import('udx-native/lib/socket.js')} */
36
+ socket: import('udx-native/lib/socket.js');
37
+ /** @type {number} The port to send media to. */
38
+ port: number;
39
+ /** @private */
40
+ _key;
41
+ /** @private */
42
+ _link;
43
+ /** @private */
44
+ _prefix;
45
+ /** @private */
46
+ _out;
47
+ /** @private */
48
+ _in;
49
+ /** @private */
50
+ _signals;
51
+ /** @private */
52
+ _closing;
53
+ /** @private */
54
+ _channel;
55
+ /** @private */
56
+ _mux;
57
+ /** @private */
58
+ _onclose;
59
+ /**
60
+ * @param {import('@hyperswarm/secret-stream')} stream An open connection.
61
+ * @param {{ id: Uint8Array, udx: import('udx-native') }} opts `id` names the call on both ends; `udx` makes the socket, the DHT's.
62
+ */
63
+ constructor(stream: import('@hyperswarm/secret-stream'), { id, udx }: {
64
+ id: Uint8Array;
65
+ udx: import('udx-native');
66
+ });
67
+ /**
68
+ * Send a description to the other end, in order; kept until the channel opens.
69
+ *
70
+ * @param {{ type: 'offer' | 'answer', sdp: string }} signal
71
+ * @returns {void}
72
+ */
73
+ signal(signal: {
74
+ type: 'offer' | 'answer';
75
+ sdp: string;
76
+ }): void;
77
+ /** @returns {Promise<void>} Resolves once the socket is closed. */
78
+ close(): Promise<void>;
79
+ /** @private */
80
+ private _connect;
81
+ /** @private */
82
+ private _signal;
83
+ /** @private */
84
+ private _open;
85
+ /** @private */
86
+ private _routed;
87
+ /** @private */
88
+ private _outbound;
89
+ /** @private */
90
+ private _follow;
91
+ /** @private */
92
+ private _inbound;
93
+ /** @private */
94
+ private _forward;
95
+ }
@@ -0,0 +1,91 @@
1
+ import ReadyResource from 'ready-resource';
2
+ import { Peers } from './peers.js';
3
+ import { Bridge } from './bridge.js';
4
+ export { Peers, Bridge };
5
+ export type CallOpts = {
6
+ /**
7
+ * The DHT to connect on, the network's.
8
+ */
9
+ dht: import('hyperdht');
10
+ /**
11
+ * This device's call key pair: the key the others hold.
12
+ */
13
+ keyPair: {
14
+ publicKey: Uint8Array;
15
+ secretKey: Uint8Array;
16
+ };
17
+ /**
18
+ * Names the call on every device in it.
19
+ */
20
+ id: string | Uint8Array;
21
+ /**
22
+ * Blind relay keys, the network's.
23
+ */
24
+ relayThrough?: Array<string | Uint8Array> | null;
25
+ };
26
+ /**
27
+ * @typedef {object} CallOpts
28
+ * @property {import('hyperdht')} dht The DHT to connect on, the network's.
29
+ * @property {{ publicKey: Uint8Array, secretKey: Uint8Array }} keyPair This device's call key pair: the key the others hold.
30
+ * @property {string | Uint8Array} id Names the call on every device in it.
31
+ * @property {Array<string | Uint8Array> | null} [relayThrough] Blind relay keys, the network's.
32
+ */
33
+ /**
34
+ * The media transport of one call: a connection to each device in it and, over each, a local port
35
+ * for the app's WebRTC to send to. Events: `'peer'(key, port)` when a device's port opens or
36
+ * changes, also for the ones already open when a listener is added; `'peer-close'(key)` once no
37
+ * connection to it is open or opening; `'signal'(key, { type, sdp })`.
38
+ */
39
+ export declare class Call extends ReadyResource {
40
+ /** @private */
41
+ _peers;
42
+ /** @type {import('hyperdht')} */
43
+ dht: import('hyperdht');
44
+ id: Uint8Array<ArrayBufferLike>;
45
+ /** @type {Map<string, number>} The local port of each device, by key in hex, from its `'peer'` to its `'peer-close'`. */
46
+ peers: Map<string, number>;
47
+ /** @private @type {Map<Bridge, string>} Oldest first. */
48
+ _bridges;
49
+ /** @param {CallOpts} opts */
50
+ constructor({ dht, keyPair, id, relayThrough }?: CallOpts);
51
+ /** @returns {Map<string, import('@hyperswarm/secret-stream')>} The open connection to each device. */
52
+ get connections(): Map<string, import('@hyperswarm/secret-stream')>;
53
+ /** @private */
54
+ private _open;
55
+ /** @private */
56
+ private _close;
57
+ /**
58
+ * The devices to hold: the call's other participants, by their call keys.
59
+ *
60
+ * @param {Array<string | Uint8Array>} keys
61
+ * @returns {void}
62
+ */
63
+ set(keys: Array<string | Uint8Array>): void;
64
+ /**
65
+ * Send a WebRTC description to a device, on its newest connection; dropped when there is none.
66
+ *
67
+ * @param {string | Uint8Array} key
68
+ * @param {{ type: 'offer' | 'answer', sdp: string }} signal
69
+ * @returns {void}
70
+ */
71
+ signal(key: string | Uint8Array, signal: {
72
+ type: 'offer' | 'answer';
73
+ sdp: string;
74
+ }): void;
75
+ /** @returns {Promise<void>} */
76
+ suspend(): Promise<void>;
77
+ /** @returns {Promise<void>} */
78
+ resume(): Promise<void>;
79
+ /** @private */
80
+ private _bridge;
81
+ /** @private */
82
+ private _heard;
83
+ /** @private */
84
+ private _current;
85
+ /** @private */
86
+ private _newest;
87
+ /** @private */
88
+ private _announce;
89
+ /** @private */
90
+ private _replay;
91
+ }
@@ -0,0 +1,184 @@
1
+ import ReadyResource from 'ready-resource';
2
+ /** @type {(key: string | Uint8Array) => Uint8Array} */
3
+ export declare function toKey(key: string | Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>;
4
+ /** @type {(key: string | Uint8Array) => string} */
5
+ export declare function toId(key: string | Uint8Array<ArrayBufferLike>): string;
6
+ export type Conn = {
7
+ stream: import('@hyperswarm/secret-stream');
8
+ /**
9
+ * The timestamp this side sent.
10
+ */
11
+ local: number;
12
+ /**
13
+ * The timestamp the other side sent, 0 until it does.
14
+ */
15
+ remote: number;
16
+ established: boolean;
17
+ /**
18
+ * When a packet last came, 0 until it opens.
19
+ */
20
+ heard: number;
21
+ timer: ReturnType<typeof setTimeout> | null;
22
+ tick: ReturnType<typeof setInterval> | null;
23
+ };
24
+ export type Peer = {
25
+ id: string;
26
+ key: Uint8Array;
27
+ /**
28
+ * Whether this side holds the lower key, whose timestamps break ties.
29
+ */
30
+ lower: boolean;
31
+ attempts: number;
32
+ /**
33
+ * A dial goes through a relay.
34
+ */
35
+ forced: boolean;
36
+ /**
37
+ * When the open connection opened.
38
+ */
39
+ up: number;
40
+ /**
41
+ * When the last one closed.
42
+ */
43
+ down: number;
44
+ timer: ReturnType<typeof setTimeout> | null;
45
+ dialing: Conn | null;
46
+ current: Conn | null;
47
+ conns: Set<Conn>;
48
+ };
49
+ /**
50
+ * @typedef {object} Conn
51
+ * @property {import('@hyperswarm/secret-stream')} stream
52
+ * @property {number} local The timestamp this side sent.
53
+ * @property {number} remote The timestamp the other side sent, 0 until it does.
54
+ * @property {boolean} established
55
+ * @property {number} heard When a packet last came, 0 until it opens.
56
+ * @property {ReturnType<typeof setTimeout> | null} timer
57
+ * @property {ReturnType<typeof setInterval> | null} tick
58
+ *
59
+ * @typedef {object} Peer
60
+ * @property {string} id
61
+ * @property {Uint8Array} key
62
+ * @property {boolean} lower Whether this side holds the lower key, whose timestamps break ties.
63
+ * @property {number} attempts
64
+ * @property {boolean} forced A dial goes through a relay.
65
+ * @property {number} up When the open connection opened.
66
+ * @property {number} down When the last one closed.
67
+ * @property {ReturnType<typeof setTimeout> | null} timer
68
+ * @property {Conn | null} dialing
69
+ * @property {Conn | null} current
70
+ * @property {Set<Conn>} conns
71
+ */
72
+ /**
73
+ * One live connection to each key in a set, on a DHT the caller owns, under the caller's key pair.
74
+ * Both sides dial; of two connections opening at once, the newer by the lower key's clock stays,
75
+ * and one that still carries packets is never replaced. Events: `'connection'(stream)`,
76
+ * `'disconnection'(stream)`, `'gone'(key)` once no connection to the key is open or opening.
77
+ */
78
+ export declare class Peers extends ReadyResource {
79
+ /** @type {import('hyperdht')} */
80
+ dht: import('hyperdht');
81
+ keyPair: {
82
+ publicKey: Uint8Array;
83
+ secretKey: Uint8Array;
84
+ };
85
+ /** @type {Uint8Array[] | null} */
86
+ relayThrough: Uint8Array[] | null;
87
+ /** @type {Set<string>} The keys held, in hex. */
88
+ keys: Set<string>;
89
+ suspended: boolean;
90
+ /** @private */
91
+ _listening;
92
+ /** @private @type {Map<string, Peer>} */
93
+ _peers;
94
+ /** @private */
95
+ _clock;
96
+ /** @private */
97
+ _onupdate;
98
+ /** @private */
99
+ _server;
100
+ /**
101
+ * @param {import('hyperdht')} dht
102
+ * @param {{ publicKey: Uint8Array, secretKey: Uint8Array }} keyPair
103
+ * @param {{ relayThrough?: Array<string | Uint8Array> | null }} [opts] Blind relay keys, for a pair that cannot connect directly.
104
+ */
105
+ constructor(dht: import('hyperdht'), keyPair: {
106
+ publicKey: Uint8Array;
107
+ secretKey: Uint8Array;
108
+ }, { relayThrough }?: {
109
+ relayThrough?: Array<string | Uint8Array> | null;
110
+ });
111
+ /** @returns {Map<string, import('@hyperswarm/secret-stream')>} The open connection to each key, by key in hex. */
112
+ get connections(): Map<string, import('@hyperswarm/secret-stream')>;
113
+ /**
114
+ * Whether a connection to `key` is open or opening.
115
+ *
116
+ * @param {string | Uint8Array} key
117
+ * @returns {boolean}
118
+ */
119
+ connecting(key: string | Uint8Array): boolean;
120
+ /** @private */
121
+ private _open;
122
+ /** @private */
123
+ private _close;
124
+ /**
125
+ * Hold a connection to `key` until it is removed, dialed once the instance is open. Its own key
126
+ * is skipped.
127
+ *
128
+ * @param {string | Uint8Array} key
129
+ * @returns {void}
130
+ */
131
+ add(key: string | Uint8Array): void;
132
+ /**
133
+ * Drop `key`: its connection closes and it is not dialed again.
134
+ *
135
+ * @param {string | Uint8Array} key
136
+ * @returns {void}
137
+ */
138
+ remove(key: string | Uint8Array): void;
139
+ /**
140
+ * Hold exactly these keys.
141
+ *
142
+ * @param {Array<string | Uint8Array>} keys
143
+ * @returns {void}
144
+ */
145
+ set(keys: Array<string | Uint8Array>): void;
146
+ /**
147
+ * Close every connection and keep the keys: nothing dials or is let in until `resume()`.
148
+ *
149
+ * @returns {Promise<void>}
150
+ */
151
+ suspend(): Promise<void>;
152
+ /**
153
+ * Dial every key again at once.
154
+ *
155
+ * @returns {Promise<void>}
156
+ */
157
+ resume(): Promise<void>;
158
+ /** @private */
159
+ private _wanted;
160
+ /** @private */
161
+ private _relay;
162
+ /** @private */
163
+ private _now;
164
+ /** @private */
165
+ private _dial;
166
+ /** @private */
167
+ private _accept;
168
+ /** @private */
169
+ private _attach;
170
+ /** @private */
171
+ private _watch;
172
+ /** @private */
173
+ private _established;
174
+ /** @private */
175
+ private _pick;
176
+ /** @private */
177
+ private _closed;
178
+ /** @private */
179
+ private _retry;
180
+ /** @private */
181
+ private _update;
182
+ /** @private */
183
+ private _drop;
184
+ }
@@ -60,7 +60,7 @@ export type DatabaseOpts = {
60
60
  */
61
61
  key?: Uint8Array | null;
62
62
  /**
63
- * Always search and announce, outside the network's presence budget. The root.
63
+ * Always announce and look up, outside the network's presence budget. The root.
64
64
  */
65
65
  pinned?: boolean;
66
66
  /**
@@ -142,6 +142,14 @@ export type HookContext = {
142
142
  * The writer's role, null likewise.
143
143
  */
144
144
  role: string | null;
145
+ /**
146
+ * The writer's device, its id as `devices` holds it.
147
+ */
148
+ device: string | null;
149
+ /**
150
+ * The op's time, the same on every peer.
151
+ */
152
+ ts: number;
145
153
  /**
146
154
  * On removing a member or changing her rank: whether the writer's rank alone allows it. A rule answers `'grant'` to allow what rank refuses, `false` to refuse.
147
155
  */
@@ -153,7 +161,9 @@ export type HookContext = {
153
161
  }>;
154
162
  put: (ref: string | {
155
163
  name: string;
156
- }, row: Record<string, unknown>) => Promise<void>;
164
+ }, row: Record<string, unknown>) => Promise<{
165
+ data: Record<string, unknown>;
166
+ }>;
157
167
  set: (ref: string | {
158
168
  name: string;
159
169
  }, row: Record<string, unknown>) => Promise<void>;
@@ -172,7 +182,7 @@ export type HookFn = (ctx: HookContext) => unknown;
172
182
  * @property {Uint8Array} encryptionKey Encrypts it; a join hands it out, so never the identity's on a database you pair.
173
183
  * @property {Array<{ epoch: number, entropy: Uint8Array }> | null} [epochs] Rotation epochs to prime the keyring with (delivered at join).
174
184
  * @property {Uint8Array | null} [key] Existing autobee key to reopen.
175
- * @property {boolean} [pinned] Always search and announce, outside the network's presence budget. The root.
185
+ * @property {boolean} [pinned] Always announce and look up, outside the network's presence budget. The root.
176
186
  * @property {boolean} [root] The identity's own database: a device key past a removed one stands while a device of the identity is seated on it.
177
187
  * @property {boolean} [push] Push a notification through the mirrors after a local write to a ref whose meta declares `push`, naming the ref, so members learn of it while their app is dead.
178
188
  * @property {import('../identity/index.js').KeyPair} [keyPair] This device's writer keypair; a fresh random one by default. Never the identity's, never the database key.
@@ -196,9 +206,11 @@ export type HookFn = (ctx: HookContext) => unknown;
196
206
  * @property {string | null} id The row id for a `del`.
197
207
  * @property {string | null} memberId The writer's member id, null for a core no member owns yet (a join, a claim, genesis).
198
208
  * @property {string | null} role The writer's role, null likewise.
209
+ * @property {string | null} device The writer's device, its id as `devices` holds it.
210
+ * @property {number} ts The op's time, the same on every peer.
199
211
  * @property {boolean} [ranked] On removing a member or changing her rank: whether the writer's rank alone allows it. A rule answers `'grant'` to allow what rank refuses, `false` to refuse.
200
212
  * @property {(ref: string | { name: string }, query?: string | Record<string, unknown>) => Promise<{ data: unknown }>} get
201
- * @property {(ref: string | { name: string }, row: Record<string, unknown>) => Promise<void>} put
213
+ * @property {(ref: string | { name: string }, row: Record<string, unknown>) => Promise<{ data: Record<string, unknown> }>} put
202
214
  * @property {(ref: string | { name: string }, row: Record<string, unknown>) => Promise<void>} set
203
215
  * @property {(ref: string | { name: string }, id?: string) => Promise<void>} del
204
216
  * @typedef {(ctx: HookContext) => unknown} HookFn
@@ -313,7 +325,13 @@ export declare class Database extends ReadyResource {
313
325
  }>;
314
326
  };
315
327
  /** @private */
316
- _presence;
328
+ _discovery;
329
+ /** @private @type {Uint8Array[]} */
330
+ _known;
331
+ /** @private */
332
+ _saved;
333
+ /** @private */
334
+ _meeting;
317
335
  /** @private */
318
336
  _pushing;
319
337
  /** @private */
@@ -352,6 +370,8 @@ export declare class Database extends ReadyResource {
352
370
  get writerKey(): Uint8Array | null;
353
371
  /** @returns {number | null} the database's version, or its parent's, while it is past this build's contract */
354
372
  get behind(): number | null;
373
+ /** @returns {Uint8Array[]} the network keys of the devices this database last replicated with, the latest first */
374
+ get known(): Uint8Array[];
355
375
  /** @returns {boolean} whether the bee accepts local writes */
356
376
  get writable(): boolean;
357
377
  /** @returns {number} number of ops in the local writer */
@@ -365,8 +385,8 @@ export declare class Database extends ReadyResource {
365
385
  /** @private */
366
386
  private _close;
367
387
  /**
368
- * `true` ranks this database as just touched, `false` takes it out of the swarm until the
369
- * next update lands in it.
388
+ * `true` ranks this database as just touched, `false` ranks it last: it keeps its slot on the
389
+ * swarm only while one is spare, until the next update lands in it.
370
390
  *
371
391
  * @param {boolean} active
372
392
  */
@@ -677,6 +697,18 @@ export declare class Database extends ReadyResource {
677
697
  /** @private */
678
698
  private _joinSwarm;
679
699
  /** @private */
700
+ private _recall;
701
+ /** @private */
702
+ private _meet;
703
+ /** @private */
704
+ private _met;
705
+ /** @private */
706
+ private _keep;
707
+ /** @private */
708
+ private _store;
709
+ /** @private */
710
+ private _forget;
711
+ /** @private */
680
712
  private _holdRoom;
681
713
  /** @private */
682
714
  private _hold;
@@ -2,8 +2,6 @@ export declare const SINGLE = "single";
2
2
  export declare const COLLECTION = "collection";
3
3
  export declare const HANDLE = "handle";
4
4
  export declare const ACTION = "action";
5
- export declare const ACTIVE = "active";
6
- export declare const PASSIVE = "passive";
7
5
  export declare const ROCKS = "rocks";
8
6
  export declare const BEE = "bee";
9
7
  export declare const OWNER = "owner";
@@ -70,6 +70,26 @@ declare function getEncoding(name: any): {
70
70
  byteLength: any;
71
71
  type: any;
72
72
  };
73
+ } | {
74
+ preencode(state: any, m: any): void;
75
+ encode(state: any, m: any): void;
76
+ decode(state: any): {
77
+ version: any;
78
+ timestamp: any;
79
+ };
80
+ } | {
81
+ preencode(state: any, m: any): void;
82
+ encode(state: any, m: any): void;
83
+ decode(state: any): {
84
+ alias: any;
85
+ };
86
+ } | {
87
+ preencode(state: any, m: any): void;
88
+ encode(state: any, m: any): void;
89
+ decode(state: any): {
90
+ type: any;
91
+ sdp: any;
92
+ };
73
93
  };
74
94
  declare function getStruct(name: any, v?: number): {
75
95
  preencode(state: any, m: any): void;
@@ -109,6 +129,14 @@ declare function getStruct(name: any, v?: number): {
109
129
  byteOffset: any;
110
130
  byteLength: any;
111
131
  type: any;
132
+ } | {
133
+ version: any;
134
+ timestamp: any;
135
+ } | {
136
+ alias: any;
137
+ } | {
138
+ type: any;
139
+ sdp: any;
112
140
  };
113
141
  };
114
142
  declare const resolveStruct: typeof getStruct;
@@ -51,7 +51,19 @@ export declare function filter<T extends Record<string, unknown>>(rows: T[], que
51
51
  * @param {() => void} cb
52
52
  * @returns {(() => void) | undefined}
53
53
  */
54
- export declare function onAbort(signal: AbortSignal | undefined, cb: () => void): (() => void) | undefined;
54
+ /**
55
+ * Run `fn` once `prev` has ended, however it ended: one after the other. `fn` is awaited as it starts, so when
56
+ * it fails at once, as a write on a database that closed while it waited, the promise that rejects is the one the
57
+ * caller already holds. Returned from a `then`, it would be rejected for a turn with nobody holding it, and Bare
58
+ * reports that as uncaught when a stream is destroyed in that turn.
59
+ *
60
+ * @template T
61
+ * @param {Promise<unknown> | null | undefined} prev
62
+ * @param {() => Promise<T> | T} fn
63
+ * @returns {Promise<T>}
64
+ */
65
+ export declare function inTurn<T>(prev: Promise<unknown> | null | undefined, fn: () => Promise<T> | T): Promise<T>;
66
+ export declare function onAbort(signal: any, cb: any): () => any;
55
67
  /** @type {<T>(row: T, createdAt?: number | null, ts?: number) => T & { createdAt: number, updatedAt: number }} */
56
68
  export declare function stamp<T>(row: T, createdAt: number, ts?: number): T & {
57
69
  createdAt: number;
@@ -77,6 +77,8 @@ export declare class Inbox extends ReadyResource {
77
77
  _session;
78
78
  /** @private */
79
79
  _discovery;
80
+ /** @private */
81
+ _core;
80
82
  /**
81
83
  * @param {import('../network/index.js').Network} network
82
84
  * @param {Uint8Array} secret
@@ -97,6 +97,8 @@ export declare class Mailbox extends ReadyResource {
97
97
  onerror: (err: Error) => void;
98
98
  /** @private */
99
99
  _resources;
100
+ /** @private */
101
+ _erasing;
100
102
  /**
101
103
  * @param {import('../network/index.js').Network} network Messages travel as cores in its store.
102
104
  * @param {MailboxOpts} [opts]