@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.
- package/README.md +3 -2
- package/package.json +20 -14
- 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 +110 -18
- package/src/lib/constants.js +0 -3
- package/src/lib/spec/index.js +67 -2
- package/src/lib/spec/schema.json +55 -1
- package/src/lib/utils.js +16 -0
- package/src/mailbox/inbox.js +14 -1
- package/src/mailbox/index.js +15 -6
- package/src/network/discovery.js +95 -49
- package/src/network/index.js +306 -83
- package/src/pairing/index.js +2 -2
- 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 +39 -7
- package/types/lib/constants.d.ts +0 -2
- package/types/lib/spec/index.d.ts +28 -0
- package/types/lib/utils.d.ts +13 -1
- package/types/mailbox/inbox.d.ts +2 -0
- package/types/mailbox/index.d.ts +2 -0
- package/types/network/discovery.d.ts +60 -23
- package/types/network/index.d.ts +119 -30
- package/src/network/presence.js +0 -135
- package/types/network/presence.d.ts +0 -63
|
@@ -1,60 +1,97 @@
|
|
|
1
1
|
export type Session = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
state: 'inactive' | 'pinned' | 'autostarted' | 'discovering' | 'destroyed';
|
|
3
|
+
active: boolean;
|
|
4
|
+
priority: number;
|
|
5
|
+
closing: Promise<void> | null;
|
|
6
|
+
discovery: {
|
|
7
|
+
refresh(): void;
|
|
8
|
+
} | null;
|
|
9
|
+
pin: () => void;
|
|
10
|
+
unpin: () => void;
|
|
11
|
+
discover: () => void;
|
|
12
|
+
flush: () => Promise<void>;
|
|
13
|
+
destroy: () => Promise<void>;
|
|
14
|
+
on: (event: string, listener: (session: Session) => void) => void;
|
|
5
15
|
};
|
|
6
16
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
17
|
+
* @typedef {object} Session The hyperjoin session a topic has, as far as cero uses it.
|
|
18
|
+
* @property {'inactive' | 'pinned' | 'autostarted' | 'discovering' | 'destroyed'} state
|
|
19
|
+
* @property {boolean} active
|
|
20
|
+
* @property {number} priority
|
|
21
|
+
* @property {Promise<void> | null} closing
|
|
22
|
+
* @property {{ refresh(): void } | null} discovery
|
|
23
|
+
* @property {() => void} pin
|
|
24
|
+
* @property {() => void} unpin
|
|
25
|
+
* @property {() => void} discover
|
|
26
|
+
* @property {() => Promise<void>} flush
|
|
27
|
+
* @property {() => Promise<void>} destroy
|
|
28
|
+
* @property {(event: string, listener: (session: Session) => void) => void} on
|
|
9
29
|
*/
|
|
10
30
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
31
|
+
* Handle for one topic membership on a {@link Network}: a view on the session the topic has,
|
|
32
|
+
* shared by every join of it. Pinned, it announces and looks up until released; in the
|
|
33
|
+
* rotation, it announces while it holds a slot, ranked by its last touch.
|
|
13
34
|
*/
|
|
14
35
|
export declare class Discovery {
|
|
15
36
|
network: import("./index.js").Network;
|
|
16
|
-
/** @type {Session} */
|
|
17
|
-
session: Session;
|
|
18
37
|
/** @private */
|
|
19
|
-
|
|
38
|
+
entry;
|
|
20
39
|
/** @private */
|
|
21
|
-
|
|
40
|
+
_pinned;
|
|
22
41
|
/** @private */
|
|
23
|
-
|
|
42
|
+
_destroyed;
|
|
24
43
|
/**
|
|
25
44
|
* @param {import('./index.js').Network} network Owning network.
|
|
26
|
-
* @param {
|
|
27
|
-
* @param {
|
|
45
|
+
* @param {import('./index.js').Entry} entry The topic's session, shared by every join of it.
|
|
46
|
+
* @param {boolean} pin
|
|
28
47
|
*/
|
|
29
|
-
constructor(network: import('./index.js').Network,
|
|
30
|
-
/** @returns {
|
|
31
|
-
get
|
|
48
|
+
constructor(network: import('./index.js').Network, entry: import('./index.js').Entry, pin: boolean);
|
|
49
|
+
/** @returns {Session} */
|
|
50
|
+
get session(): Session;
|
|
51
|
+
/** @returns {'inactive' | 'pinned' | 'autostarted' | 'discovering' | 'destroyed'} */
|
|
52
|
+
get state(): 'inactive' | 'pinned' | 'autostarted' | 'discovering' | 'destroyed';
|
|
53
|
+
/** @returns {boolean} Whether the topic announces or looks up right now. */
|
|
54
|
+
get active(): boolean;
|
|
32
55
|
/** @returns {boolean} */
|
|
33
56
|
get destroyed(): boolean;
|
|
34
57
|
/**
|
|
35
|
-
*
|
|
58
|
+
* Pin the topic and look up now: it announces and looks up until `deactivate()`, outside the
|
|
59
|
+
* rotation's budget.
|
|
36
60
|
*
|
|
37
61
|
* @returns {Promise<void>}
|
|
38
62
|
*/
|
|
39
63
|
activate(): Promise<void>;
|
|
40
64
|
/**
|
|
41
|
-
*
|
|
65
|
+
* Unpin the topic: the rotation decides, by its last touch.
|
|
42
66
|
*
|
|
43
67
|
* @returns {Promise<void>}
|
|
44
68
|
*/
|
|
45
69
|
deactivate(): Promise<void>;
|
|
70
|
+
/** The topic was just used: it ranks first, and announces from now until `idle` ms after its last use, slot or not. */
|
|
71
|
+
touch(): void;
|
|
46
72
|
/**
|
|
47
|
-
*
|
|
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;
|
|
79
|
+
/** Rank the topic last: it keeps a slot only while one is spare. */
|
|
80
|
+
release(): void;
|
|
81
|
+
/**
|
|
82
|
+
* Wait for the current announce or lookup to settle.
|
|
48
83
|
*
|
|
49
84
|
* @returns {Promise<void>}
|
|
50
85
|
*/
|
|
51
86
|
flush(): Promise<void>;
|
|
52
|
-
/** @private */
|
|
53
|
-
private _requery;
|
|
54
87
|
/**
|
|
55
|
-
* Leave the topic
|
|
88
|
+
* Leave the topic, once every join of it has. Idempotent.
|
|
56
89
|
*
|
|
57
90
|
* @returns {Promise<void>}
|
|
58
91
|
*/
|
|
59
92
|
destroy(): Promise<void>;
|
|
93
|
+
/** @private */
|
|
94
|
+
private _pin;
|
|
95
|
+
/** @private */
|
|
96
|
+
private _rank;
|
|
60
97
|
}
|
package/types/network/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import ReadyResource from 'ready-resource';
|
|
2
2
|
import { Discovery } from './discovery.js';
|
|
3
|
-
import { Presence } from './presence.js';
|
|
4
3
|
/** @type {(topic: Uint8Array, channel: string | null) => Uint8Array} */
|
|
5
4
|
export declare function channelTopic(topic: Uint8Array<ArrayBufferLike>, channel: string): Uint8Array<ArrayBufferLike>;
|
|
6
5
|
export type NetworkOpts = {
|
|
@@ -19,10 +18,6 @@ export type NetworkOpts = {
|
|
|
19
18
|
* Blind relay public keys; a connection goes through one only when a direct one fails or this device sits behind a randomizing NAT.
|
|
20
19
|
*/
|
|
21
20
|
relayThrough?: Array<string | Uint8Array>;
|
|
22
|
-
/**
|
|
23
|
-
* Reconnect backoff tiers in ms; the default escalates to ~10min, far too slow for local nets.
|
|
24
|
-
*/
|
|
25
|
-
backoffs?: number[];
|
|
26
21
|
/**
|
|
27
22
|
* Optional network-isolation label; only same-channel peers meet.
|
|
28
23
|
*/
|
|
@@ -40,29 +35,72 @@ export type NetworkOpts = {
|
|
|
40
35
|
*/
|
|
41
36
|
onerror?: (err: Error) => void;
|
|
42
37
|
/**
|
|
43
|
-
*
|
|
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.
|
|
44
39
|
*/
|
|
45
40
|
presence?: {
|
|
46
41
|
active?: number;
|
|
47
|
-
announced?: number;
|
|
48
42
|
idle?: number;
|
|
43
|
+
known?: number;
|
|
44
|
+
connections?: number;
|
|
49
45
|
};
|
|
50
46
|
};
|
|
47
|
+
export type Entry = {
|
|
48
|
+
id: string;
|
|
49
|
+
session: import('./discovery.js').Session;
|
|
50
|
+
/**
|
|
51
|
+
* The store session the replicator session lives on.
|
|
52
|
+
*/
|
|
53
|
+
store: import('corestore');
|
|
54
|
+
/**
|
|
55
|
+
* Joins holding the topic.
|
|
56
|
+
*/
|
|
57
|
+
refs: number;
|
|
58
|
+
/**
|
|
59
|
+
* Joins pinning it.
|
|
60
|
+
*/
|
|
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;
|
|
78
|
+
};
|
|
51
79
|
/**
|
|
52
80
|
* @typedef {object} NetworkOpts
|
|
53
81
|
* @property {Array<{ host: string, port: number }>} [bootstrap] Custom DHT bootstrap nodes.
|
|
54
82
|
* @property {(remotePublicKey: Uint8Array, payload: unknown) => boolean} [firewall] Incoming-connection filter.
|
|
55
83
|
* @property {Array<string | Uint8Array>} [relayThrough] Blind relay public keys; a connection goes through one only when a direct one fails or this device sits behind a randomizing NAT.
|
|
56
|
-
* @property {number[]} [backoffs] Reconnect backoff tiers in ms; the default escalates to ~10min, far too slow for local nets.
|
|
57
84
|
* @property {string} [channel] Optional network-isolation label; only same-channel peers meet.
|
|
58
85
|
* @property {import('corestore')} store Corestore; every connection replicates it, so every core it holds rides every peer.
|
|
59
86
|
* @property {Array<string | Uint8Array>} [mirrors] Blind-peer public keys; each attached database/blob core is mirrored through them for offline sync.
|
|
60
87
|
* @property {(err: Error) => void} [onerror] Background-task error handler.
|
|
61
|
-
* @property {{ active?: number,
|
|
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.
|
|
89
|
+
*
|
|
90
|
+
* @typedef {object} Entry One replicator session per topic, shared by every join of it.
|
|
91
|
+
* @property {string} id
|
|
92
|
+
* @property {import('./discovery.js').Session} session
|
|
93
|
+
* @property {import('corestore')} store The store session the replicator session lives on.
|
|
94
|
+
* @property {number} refs Joins holding the topic.
|
|
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.
|
|
62
100
|
*/
|
|
63
101
|
/**
|
|
64
|
-
*
|
|
65
|
-
* replicates the store.
|
|
102
|
+
* Peer discovery and connections on hyperjoin, plus a shared wakeup channel. Every connection,
|
|
103
|
+
* the replicator's or injected, replicates the store.
|
|
66
104
|
*/
|
|
67
105
|
export declare class Network extends ReadyResource {
|
|
68
106
|
bootstrap: {
|
|
@@ -71,18 +109,27 @@ export declare class Network extends ReadyResource {
|
|
|
71
109
|
}[];
|
|
72
110
|
firewall: (remotePublicKey: Uint8Array, payload: unknown) => boolean;
|
|
73
111
|
relayThrough: any[];
|
|
74
|
-
backoffs: number[];
|
|
75
112
|
channel: string;
|
|
76
113
|
/** @type {import('corestore')} */
|
|
77
114
|
store: import('corestore');
|
|
78
115
|
/** @type {Uint8Array[]} */
|
|
79
116
|
mirrors: Uint8Array[];
|
|
117
|
+
/** @type {{ active: number, idle: number, known: number, connections: number }} */
|
|
118
|
+
presence: {
|
|
119
|
+
active: number;
|
|
120
|
+
idle: number;
|
|
121
|
+
known: number;
|
|
122
|
+
connections: number;
|
|
123
|
+
};
|
|
80
124
|
/** @private */
|
|
81
|
-
|
|
125
|
+
_replicator;
|
|
126
|
+
/** @private */
|
|
127
|
+
_dht;
|
|
82
128
|
/** @type {import('protomux-wakeup')} */
|
|
83
129
|
wakeup: import('protomux-wakeup');
|
|
84
|
-
|
|
85
|
-
|
|
130
|
+
/** @private @type {Map<string, Entry>} */
|
|
131
|
+
_sessions;
|
|
132
|
+
/** @private @type {Set<Discovery>} */
|
|
86
133
|
_discoveries;
|
|
87
134
|
/** @private */
|
|
88
135
|
_injected;
|
|
@@ -91,18 +138,24 @@ export declare class Network extends ReadyResource {
|
|
|
91
138
|
/** @private */
|
|
92
139
|
_peeringClosed;
|
|
93
140
|
/** @private */
|
|
94
|
-
|
|
141
|
+
_listening;
|
|
142
|
+
/** @private */
|
|
143
|
+
_refreshing;
|
|
144
|
+
/** @private */
|
|
145
|
+
_resuming;
|
|
95
146
|
/** @private */
|
|
96
147
|
_relookup;
|
|
97
148
|
/** @private */
|
|
98
149
|
_onerror;
|
|
99
150
|
/** @param {NetworkOpts} opts */
|
|
100
|
-
constructor({ bootstrap, firewall, relayThrough,
|
|
101
|
-
/** @returns {import('
|
|
102
|
-
get
|
|
103
|
-
/** @returns {
|
|
151
|
+
constructor({ bootstrap, firewall, relayThrough, channel, store, mirrors, presence, onerror }?: NetworkOpts);
|
|
152
|
+
/** @returns {import('hyperjoin') | null} The underlying hyperjoin replicator, or null before ready / after close. */
|
|
153
|
+
get replicator(): import('hyperjoin') | null;
|
|
154
|
+
/** @returns {import('hyperdht') | null} The DHT the replicator and the mirrors share. */
|
|
155
|
+
get dht(): import('hyperdht') | null;
|
|
156
|
+
/** @returns {Map<string, object>} Known peers keyed by public-key hex. */
|
|
104
157
|
get peers(): Map<string, object>;
|
|
105
|
-
/** @returns {Set<import('@hyperswarm/secret-stream')>} Live connection streams —
|
|
158
|
+
/** @returns {Set<import('@hyperswarm/secret-stream')>} Live connection streams — the replicator's and injected. */
|
|
106
159
|
get connections(): Set<import('@hyperswarm/secret-stream')>;
|
|
107
160
|
/** @returns {boolean} */
|
|
108
161
|
get suspended(): boolean;
|
|
@@ -139,7 +192,7 @@ export declare class Network extends ReadyResource {
|
|
|
139
192
|
*/
|
|
140
193
|
peering(): import('blind-peering');
|
|
141
194
|
/**
|
|
142
|
-
* Wait for pending DHT announces and
|
|
195
|
+
* Wait for pending DHT announces, lookups and dials to settle, bounded by timeout.
|
|
143
196
|
*
|
|
144
197
|
* @param {{ timeout?: number }} [opts]
|
|
145
198
|
* @returns {Promise<void>}
|
|
@@ -147,28 +200,40 @@ export declare class Network extends ReadyResource {
|
|
|
147
200
|
flush({ timeout }?: {
|
|
148
201
|
timeout?: number;
|
|
149
202
|
}): Promise<void>;
|
|
203
|
+
/** @private */
|
|
204
|
+
private _flush;
|
|
150
205
|
/**
|
|
151
|
-
* Pause the
|
|
206
|
+
* Pause the network — keeps state, drops sockets. Idempotent.
|
|
152
207
|
*
|
|
153
208
|
* @returns {Promise<void>}
|
|
154
209
|
*/
|
|
155
210
|
suspend(): Promise<void>;
|
|
156
211
|
/**
|
|
157
|
-
* Resume a suspended
|
|
212
|
+
* Resume a suspended network. Idempotent.
|
|
158
213
|
*
|
|
159
214
|
* @returns {Promise<void>}
|
|
160
215
|
*/
|
|
161
216
|
resume(): Promise<void>;
|
|
217
|
+
/** @private */
|
|
218
|
+
private _resume;
|
|
162
219
|
/**
|
|
163
|
-
*
|
|
220
|
+
* Join a 32-byte topic and return a Discovery handle. Pinned by default: it announces and
|
|
221
|
+
* looks up now; `pin: false` leaves it to the rotation.
|
|
164
222
|
*
|
|
165
223
|
* @param {Uint8Array} topic
|
|
166
|
-
* @param {{
|
|
224
|
+
* @param {{ pin?: boolean }} [opts]
|
|
167
225
|
* @returns {Discovery}
|
|
168
226
|
*/
|
|
169
|
-
join(topic: Uint8Array, {
|
|
170
|
-
|
|
227
|
+
join(topic: Uint8Array, { pin }?: {
|
|
228
|
+
pin?: boolean;
|
|
171
229
|
}): Discovery;
|
|
230
|
+
/**
|
|
231
|
+
* Where a topic stands on this network.
|
|
232
|
+
*
|
|
233
|
+
* @param {Uint8Array} topic
|
|
234
|
+
* @returns {'inactive' | 'pinned' | 'autostarted' | 'discovering' | null} Its session's state, null when not joined.
|
|
235
|
+
*/
|
|
236
|
+
state(topic: Uint8Array): 'inactive' | 'pinned' | 'autostarted' | 'discovering' | null;
|
|
172
237
|
/**
|
|
173
238
|
* Whether `store` replicates on this network's connections: the network's store or a session of it.
|
|
174
239
|
*
|
|
@@ -187,7 +252,31 @@ export declare class Network extends ReadyResource {
|
|
|
187
252
|
/** @private */
|
|
188
253
|
private _mirror;
|
|
189
254
|
/** @private */
|
|
190
|
-
private
|
|
255
|
+
private _leave;
|
|
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 */
|
|
267
|
+
private _settle;
|
|
268
|
+
/** @private */
|
|
269
|
+
private _refresh;
|
|
270
|
+
/** @private */
|
|
271
|
+
private _rotate;
|
|
272
|
+
/** @private */
|
|
273
|
+
private _active;
|
|
274
|
+
/** @private */
|
|
275
|
+
private _lookup;
|
|
276
|
+
/** @private */
|
|
277
|
+
private _watch;
|
|
278
|
+
/** @private */
|
|
279
|
+
private _retry;
|
|
191
280
|
/** @private */
|
|
192
|
-
private
|
|
281
|
+
private _errors;
|
|
193
282
|
}
|
package/src/network/presence.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import safetyCatch from 'safety-catch'
|
|
2
|
-
import b4a from 'b4a'
|
|
3
|
-
|
|
4
|
-
import { ACTIVE, PASSIVE } from '../lib/constants.js'
|
|
5
|
-
|
|
6
|
-
const rank = (mode) => (mode === ACTIVE ? 2 : mode === PASSIVE ? 1 : 0)
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Which handles swarm. Ranked by last touch: the latest `active` search and announce, the
|
|
10
|
-
* next `announced` announce only, the rest leave their topic. A handle moves up at once and
|
|
11
|
-
* down only after `idle` ms. A pinned handle always searches, outside the budget.
|
|
12
|
-
*
|
|
13
|
-
* @typedef {{ mode: 'active' | 'passive' | null, touch: () => void, off: () => void, remove: () => void }} Slot
|
|
14
|
-
*/
|
|
15
|
-
export class Presence {
|
|
16
|
-
/**
|
|
17
|
-
* @param {import('./index.js').Network} network
|
|
18
|
-
* @param {{ active?: number, announced?: number, idle?: number }} [opts]
|
|
19
|
-
*/
|
|
20
|
-
constructor(network, { active = 8, announced = 8, idle = 30_000 } = {}) {
|
|
21
|
-
this.network = network
|
|
22
|
-
this.active = active
|
|
23
|
-
this.announced = announced
|
|
24
|
-
this.idle = idle
|
|
25
|
-
/** @type {Map<string, { topic: Uint8Array, pinned: boolean, refs: number, touched: number, off: boolean, discovery: import('./discovery.js').Discovery | null, timer: ReturnType<typeof setTimeout> | null, pending: 'active' | 'passive' | null }>} */
|
|
26
|
-
this.entries = new Map()
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Register a topic. One entry per topic, shared by every slot on it until all are removed;
|
|
31
|
-
* a new entry starts untouched, at the bottom.
|
|
32
|
-
*
|
|
33
|
-
* @param {Uint8Array} topic
|
|
34
|
-
* @param {{ pinned?: boolean }} [opts]
|
|
35
|
-
* @returns {Slot}
|
|
36
|
-
*/
|
|
37
|
-
add(topic, { pinned = false } = {}) {
|
|
38
|
-
const id = b4a.toHex(topic)
|
|
39
|
-
let e = this.entries.get(id)
|
|
40
|
-
if (!e) {
|
|
41
|
-
e = {
|
|
42
|
-
topic,
|
|
43
|
-
pinned,
|
|
44
|
-
refs: 0,
|
|
45
|
-
touched: 0,
|
|
46
|
-
off: false,
|
|
47
|
-
discovery: null,
|
|
48
|
-
timer: null,
|
|
49
|
-
pending: null
|
|
50
|
-
}
|
|
51
|
-
this.entries.set(id, e)
|
|
52
|
-
}
|
|
53
|
-
e.refs++
|
|
54
|
-
this.refresh()
|
|
55
|
-
const presence = this
|
|
56
|
-
return {
|
|
57
|
-
get mode() {
|
|
58
|
-
return e.discovery ? e.discovery.mode : null
|
|
59
|
-
},
|
|
60
|
-
touch() {
|
|
61
|
-
e.touched = Date.now()
|
|
62
|
-
e.off = false
|
|
63
|
-
presence.refresh()
|
|
64
|
-
},
|
|
65
|
-
off() {
|
|
66
|
-
e.off = true
|
|
67
|
-
presence.refresh()
|
|
68
|
-
},
|
|
69
|
-
remove() {
|
|
70
|
-
if (--e.refs > 0) return
|
|
71
|
-
presence.entries.delete(id)
|
|
72
|
-
presence._leave(e)
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* @param {Uint8Array} topic
|
|
79
|
-
* @returns {'active' | 'passive' | null}
|
|
80
|
-
*/
|
|
81
|
-
mode(topic) {
|
|
82
|
-
const e = this.entries.get(b4a.toHex(topic))
|
|
83
|
-
return e?.discovery ? e.discovery.mode : null
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
refresh() {
|
|
87
|
-
if (this.network.closing || this.network.closed) return
|
|
88
|
-
const ranked = []
|
|
89
|
-
for (const e of this.entries.values()) {
|
|
90
|
-
if (e.pinned) this._want(e, ACTIVE)
|
|
91
|
-
else if (e.off) this._want(e, null)
|
|
92
|
-
else ranked.push(e)
|
|
93
|
-
}
|
|
94
|
-
ranked.sort((a, b) => b.touched - a.touched)
|
|
95
|
-
ranked.forEach((e, i) => {
|
|
96
|
-
this._want(e, i < this.active ? ACTIVE : i < this.active + this.announced ? PASSIVE : null)
|
|
97
|
-
})
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
close() {
|
|
101
|
-
for (const e of this.entries.values()) clearTimeout(e.timer)
|
|
102
|
-
this.entries.clear()
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// up at once, down after idle: a database at the edge of the budget must not churn the DHT
|
|
106
|
-
/** @private */
|
|
107
|
-
_want(e, mode) {
|
|
108
|
-
const current = e.discovery ? e.discovery.mode : null
|
|
109
|
-
if (rank(mode) < rank(current)) {
|
|
110
|
-
e.pending = mode
|
|
111
|
-
if (!e.timer) e.timer = setTimeout(() => this._settle(e), this.idle)
|
|
112
|
-
return
|
|
113
|
-
}
|
|
114
|
-
clearTimeout(e.timer)
|
|
115
|
-
e.timer = null
|
|
116
|
-
if (mode === current) return
|
|
117
|
-
if (!e.discovery) e.discovery = this.network.join(e.topic, { mode })
|
|
118
|
-
else e.discovery.activate().catch(safetyCatch)
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** @private */
|
|
122
|
-
_settle(e) {
|
|
123
|
-
e.timer = null
|
|
124
|
-
if (e.pending === PASSIVE) e.discovery.deactivate().catch(safetyCatch)
|
|
125
|
-
else this._leave(e)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** @private */
|
|
129
|
-
_leave(e) {
|
|
130
|
-
clearTimeout(e.timer)
|
|
131
|
-
e.timer = null
|
|
132
|
-
e.discovery?.destroy().catch(safetyCatch)
|
|
133
|
-
e.discovery = null
|
|
134
|
-
}
|
|
135
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
export type Slot = {
|
|
2
|
-
mode: 'active' | 'passive' | null;
|
|
3
|
-
touch: () => void;
|
|
4
|
-
off: () => void;
|
|
5
|
-
remove: () => void;
|
|
6
|
-
};
|
|
7
|
-
/**
|
|
8
|
-
* Which handles swarm. Ranked by last touch: the latest `active` search and announce, the
|
|
9
|
-
* next `announced` announce only, the rest leave their topic. A handle moves up at once and
|
|
10
|
-
* down only after `idle` ms. A pinned handle always searches, outside the budget.
|
|
11
|
-
*
|
|
12
|
-
* @typedef {{ mode: 'active' | 'passive' | null, touch: () => void, off: () => void, remove: () => void }} Slot
|
|
13
|
-
*/
|
|
14
|
-
export declare class Presence {
|
|
15
|
-
network: import("./index.js").Network;
|
|
16
|
-
active: number;
|
|
17
|
-
announced: number;
|
|
18
|
-
idle: number;
|
|
19
|
-
/** @type {Map<string, { topic: Uint8Array, pinned: boolean, refs: number, touched: number, off: boolean, discovery: import('./discovery.js').Discovery | null, timer: ReturnType<typeof setTimeout> | null, pending: 'active' | 'passive' | null }>} */
|
|
20
|
-
entries: Map<string, {
|
|
21
|
-
topic: Uint8Array;
|
|
22
|
-
pinned: boolean;
|
|
23
|
-
refs: number;
|
|
24
|
-
touched: number;
|
|
25
|
-
off: boolean;
|
|
26
|
-
discovery: import('./discovery.js').Discovery | null;
|
|
27
|
-
timer: ReturnType<typeof setTimeout> | null;
|
|
28
|
-
pending: 'active' | 'passive' | null;
|
|
29
|
-
}>;
|
|
30
|
-
/**
|
|
31
|
-
* @param {import('./index.js').Network} network
|
|
32
|
-
* @param {{ active?: number, announced?: number, idle?: number }} [opts]
|
|
33
|
-
*/
|
|
34
|
-
constructor(network: import('./index.js').Network, { active, announced, idle }?: {
|
|
35
|
-
active?: number;
|
|
36
|
-
announced?: number;
|
|
37
|
-
idle?: number;
|
|
38
|
-
});
|
|
39
|
-
/**
|
|
40
|
-
* Register a topic. One entry per topic, shared by every slot on it until all are removed;
|
|
41
|
-
* a new entry starts untouched, at the bottom.
|
|
42
|
-
*
|
|
43
|
-
* @param {Uint8Array} topic
|
|
44
|
-
* @param {{ pinned?: boolean }} [opts]
|
|
45
|
-
* @returns {Slot}
|
|
46
|
-
*/
|
|
47
|
-
add(topic: Uint8Array, { pinned }?: {
|
|
48
|
-
pinned?: boolean;
|
|
49
|
-
}): Slot;
|
|
50
|
-
/**
|
|
51
|
-
* @param {Uint8Array} topic
|
|
52
|
-
* @returns {'active' | 'passive' | null}
|
|
53
|
-
*/
|
|
54
|
-
mode(topic: Uint8Array): 'active' | 'passive' | null;
|
|
55
|
-
refresh(): void;
|
|
56
|
-
close(): void;
|
|
57
|
-
/** @private */
|
|
58
|
-
private _want;
|
|
59
|
-
/** @private */
|
|
60
|
-
private _settle;
|
|
61
|
-
/** @private */
|
|
62
|
-
private _leave;
|
|
63
|
-
}
|