@cero-base/core 3.5.0 → 3.6.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 +2 -1
- package/package.json +7 -2
- package/src/call/bridge.js +262 -0
- package/src/call/index.js +150 -0
- package/src/call/peers.js +426 -0
- package/src/database/dispatch.js +8 -3
- package/src/database/index.js +95 -1
- package/src/lib/spec/index.js +67 -2
- package/src/lib/spec/schema.json +55 -1
- package/src/mailbox/index.js +15 -6
- package/src/network/discovery.js +17 -6
- package/src/network/index.js +83 -4
- package/types/call/bridge.d.ts +95 -0
- package/types/call/index.d.ts +91 -0
- package/types/call/peers.d.ts +184 -0
- package/types/database/index.d.ts +34 -2
- package/types/lib/spec/index.d.ts +28 -0
- package/types/mailbox/index.d.ts +2 -0
- package/types/network/discovery.d.ts +8 -1
- package/types/network/index.d.ts +37 -3
|
@@ -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
|
+
}
|
|
@@ -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<
|
|
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>;
|
|
@@ -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<
|
|
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
|
|
@@ -314,6 +326,12 @@ export declare class Database extends ReadyResource {
|
|
|
314
326
|
};
|
|
315
327
|
/** @private */
|
|
316
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 */
|
|
@@ -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;
|
|
@@ -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;
|
package/types/mailbox/index.d.ts
CHANGED
|
@@ -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]
|
|
@@ -67,8 +67,15 @@ export declare class Discovery {
|
|
|
67
67
|
* @returns {Promise<void>}
|
|
68
68
|
*/
|
|
69
69
|
deactivate(): Promise<void>;
|
|
70
|
-
/**
|
|
70
|
+
/** The topic was just used: it ranks first, and announces from now until `idle` ms after its last use, slot or not. */
|
|
71
71
|
touch(): void;
|
|
72
|
+
/**
|
|
73
|
+
* The devices that held the topic before, by their network keys: each is dialed by its key whenever the
|
|
74
|
+
* topic is on the network, whether it announces the topic or not.
|
|
75
|
+
*
|
|
76
|
+
* @param {Uint8Array[]} keys
|
|
77
|
+
*/
|
|
78
|
+
know(keys: Uint8Array[]): void;
|
|
72
79
|
/** Rank the topic last: it keeps a slot only while one is spare. */
|
|
73
80
|
release(): void;
|
|
74
81
|
/**
|
package/types/network/index.d.ts
CHANGED
|
@@ -35,11 +35,13 @@ export type NetworkOpts = {
|
|
|
35
35
|
*/
|
|
36
36
|
onerror?: (err: Error) => void;
|
|
37
37
|
/**
|
|
38
|
-
* The budget for topics in the rotation: how many announce at once, and the ms
|
|
38
|
+
* The budget for topics in the rotation: how many announce at once besides the ones in use, and the ms a topic keeps announcing after its last use, or keeps a slot. `known` is how many devices a database remembers to dial by their keys, 0 for none. `connections` is how many connections the network keeps at once: a device that dials in past it is told to try later.
|
|
39
39
|
*/
|
|
40
40
|
presence?: {
|
|
41
41
|
active?: number;
|
|
42
42
|
idle?: number;
|
|
43
|
+
known?: number;
|
|
44
|
+
connections?: number;
|
|
43
45
|
};
|
|
44
46
|
};
|
|
45
47
|
export type Entry = {
|
|
@@ -57,6 +59,22 @@ export type Entry = {
|
|
|
57
59
|
* Joins pinning it.
|
|
58
60
|
*/
|
|
59
61
|
pins: number;
|
|
62
|
+
/**
|
|
63
|
+
* When it was last touched.
|
|
64
|
+
*/
|
|
65
|
+
used: number;
|
|
66
|
+
/**
|
|
67
|
+
* Set while a touch keeps it announcing.
|
|
68
|
+
*/
|
|
69
|
+
held: ReturnType<typeof setTimeout> | null;
|
|
70
|
+
/**
|
|
71
|
+
* Network keys of devices that held the topic before.
|
|
72
|
+
*/
|
|
73
|
+
known: Uint8Array[];
|
|
74
|
+
/**
|
|
75
|
+
* When they were last dialed.
|
|
76
|
+
*/
|
|
77
|
+
dialed: number;
|
|
60
78
|
};
|
|
61
79
|
/**
|
|
62
80
|
* @typedef {object} NetworkOpts
|
|
@@ -67,7 +85,7 @@ export type Entry = {
|
|
|
67
85
|
* @property {import('corestore')} store Corestore; every connection replicates it, so every core it holds rides every peer.
|
|
68
86
|
* @property {Array<string | Uint8Array>} [mirrors] Blind-peer public keys; each attached database/blob core is mirrored through them for offline sync.
|
|
69
87
|
* @property {(err: Error) => void} [onerror] Background-task error handler.
|
|
70
|
-
* @property {{ active?: number, idle?: number }} [presence]
|
|
88
|
+
* @property {{ active?: number, idle?: number, known?: number, connections?: number }} [presence] The budget for topics in the rotation: how many announce at once besides the ones in use, and the ms a topic keeps announcing after its last use, or keeps a slot. `known` is how many devices a database remembers to dial by their keys, 0 for none. `connections` is how many connections the network keeps at once: a device that dials in past it is told to try later.
|
|
71
89
|
*
|
|
72
90
|
* @typedef {object} Entry One replicator session per topic, shared by every join of it.
|
|
73
91
|
* @property {string} id
|
|
@@ -75,6 +93,10 @@ export type Entry = {
|
|
|
75
93
|
* @property {import('corestore')} store The store session the replicator session lives on.
|
|
76
94
|
* @property {number} refs Joins holding the topic.
|
|
77
95
|
* @property {number} pins Joins pinning it.
|
|
96
|
+
* @property {number} used When it was last touched.
|
|
97
|
+
* @property {ReturnType<typeof setTimeout> | null} held Set while a touch keeps it announcing.
|
|
98
|
+
* @property {Uint8Array[]} known Network keys of devices that held the topic before.
|
|
99
|
+
* @property {number} dialed When they were last dialed.
|
|
78
100
|
*/
|
|
79
101
|
/**
|
|
80
102
|
* Peer discovery and connections on hyperjoin, plus a shared wakeup channel. Every connection,
|
|
@@ -92,10 +114,12 @@ export declare class Network extends ReadyResource {
|
|
|
92
114
|
store: import('corestore');
|
|
93
115
|
/** @type {Uint8Array[]} */
|
|
94
116
|
mirrors: Uint8Array[];
|
|
95
|
-
/** @type {{ active: number, idle: number }} */
|
|
117
|
+
/** @type {{ active: number, idle: number, known: number, connections: number }} */
|
|
96
118
|
presence: {
|
|
97
119
|
active: number;
|
|
98
120
|
idle: number;
|
|
121
|
+
known: number;
|
|
122
|
+
connections: number;
|
|
99
123
|
};
|
|
100
124
|
/** @private */
|
|
101
125
|
_replicator;
|
|
@@ -230,6 +254,16 @@ export declare class Network extends ReadyResource {
|
|
|
230
254
|
/** @private */
|
|
231
255
|
private _leave;
|
|
232
256
|
/** @private */
|
|
257
|
+
private _use;
|
|
258
|
+
/** @private */
|
|
259
|
+
private _hold;
|
|
260
|
+
/** @private */
|
|
261
|
+
private _apply;
|
|
262
|
+
/** @private */
|
|
263
|
+
private _dial;
|
|
264
|
+
/** @private */
|
|
265
|
+
private _redial;
|
|
266
|
+
/** @private */
|
|
233
267
|
private _settle;
|
|
234
268
|
/** @private */
|
|
235
269
|
private _refresh;
|