@cero-base/core 1.19.0 → 2.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.
Files changed (46) hide show
  1. package/README.md +1 -1
  2. package/package.json +9 -6
  3. package/src/blobs/index.js +15 -15
  4. package/src/blobs/server.js +0 -2
  5. package/src/database/changes.js +4 -8
  6. package/src/database/dispatch.js +296 -227
  7. package/src/database/encryption.js +22 -52
  8. package/src/database/envelope.js +1 -3
  9. package/src/database/index.js +406 -476
  10. package/src/database/rotation.js +56 -70
  11. package/src/identity/index.js +5 -9
  12. package/src/index.js +5 -9
  13. package/src/lib/constants.js +3 -12
  14. package/src/lib/errors.js +6 -10
  15. package/src/lib/schema.js +2 -5
  16. package/src/lib/utils.js +9 -24
  17. package/src/network/index.js +144 -167
  18. package/src/network/presence.js +131 -0
  19. package/src/pairing/candidate.js +2 -4
  20. package/src/pairing/index.js +41 -55
  21. package/src/pairing/invite.js +3 -4
  22. package/src/pairing/request.js +3 -9
  23. package/src/rpc/index.js +4 -14
  24. package/src/rpc/peer.js +3 -7
  25. package/src/storage/index.js +29 -50
  26. package/types/blobs/index.d.ts +5 -5
  27. package/types/blobs/server.d.ts +0 -2
  28. package/types/database/changes.d.ts +2 -5
  29. package/types/database/dispatch.d.ts +11 -7
  30. package/types/database/encryption.d.ts +8 -20
  31. package/types/database/index.d.ts +164 -141
  32. package/types/database/rotation.d.ts +14 -20
  33. package/types/identity/index.d.ts +5 -9
  34. package/types/index.d.ts +4 -3
  35. package/types/lib/errors.d.ts +6 -10
  36. package/types/lib/schema.d.ts +2 -5
  37. package/types/lib/utils.d.ts +3 -7
  38. package/types/network/index.d.ts +28 -27
  39. package/types/network/presence.d.ts +50 -0
  40. package/types/pairing/candidate.d.ts +1 -2
  41. package/types/pairing/index.d.ts +22 -30
  42. package/types/pairing/invite.d.ts +3 -3
  43. package/types/pairing/request.d.ts +1 -3
  44. package/types/rpc/index.d.ts +1 -4
  45. package/types/rpc/peer.d.ts +2 -4
  46. package/types/storage/index.d.ts +17 -30
@@ -1,5 +1,6 @@
1
1
  import ReadyResource from 'ready-resource';
2
2
  import { Discovery } from './discovery.js';
3
+ import { Presence } from './presence.js';
3
4
  export declare function channelTopic(topic: any, channel: any): any;
4
5
  export type NetworkOpts = {
5
6
  /**
@@ -37,6 +38,14 @@ export type NetworkOpts = {
37
38
  * Blind-peer public keys; each attached room/blob core is mirrored through them for offline sync.
38
39
  */
39
40
  mirrors?: Array<string | Uint8Array>;
41
+ /**
42
+ * Swarm budget for attached databases: how many search, how many only announce, and the idle ms before the rest leave.
43
+ */
44
+ presence?: {
45
+ active?: number;
46
+ announced?: number;
47
+ idle?: number;
48
+ };
40
49
  };
41
50
  export type Replicable = {
42
51
  replicate: (stream: any) => any;
@@ -51,6 +60,7 @@ export type Replicable = {
51
60
  * @property {string} [channel] Optional network-isolation label; only same-channel peers meet.
52
61
  * @property {any} [store] Corestore; required for mirrors (blind peers replicate its cores).
53
62
  * @property {Array<string | Uint8Array>} [mirrors] Blind-peer public keys; each attached room/blob core is mirrored through them for offline sync.
63
+ * @property {{ active?: number, announced?: number, idle?: number }} [presence] Swarm budget for attached databases: how many search, how many only announce, and the idle ms before the rest leave.
54
64
  *
55
65
  * @typedef {{ replicate: (stream: any) => any }} Replicable
56
66
  */
@@ -72,6 +82,7 @@ export declare class Network extends ReadyResource {
72
82
  mirrors: any[];
73
83
  _swarm: any;
74
84
  wakeup: any;
85
+ presence: Presence;
75
86
  info: object;
76
87
  _peerInfo: Map<any, any>;
77
88
  _infoSenders: Set<any>;
@@ -81,16 +92,20 @@ export declare class Network extends ReadyResource {
81
92
  _blind: any;
82
93
  _blindPeering: any;
83
94
  /** @param {NetworkOpts} [opts] */
84
- constructor({ identity, bootstrap, firewall, relayThrough, backoffs, channel, store, mirrors }?: NetworkOpts);
95
+ constructor({ identity, bootstrap, firewall, relayThrough, backoffs, channel, store, mirrors, presence }?: NetworkOpts);
85
96
  /** @returns {any} The underlying hyperswarm, or null before ready / after close. */
86
97
  get swarm(): any;
98
+ /** @returns {Map<string, any>} Known peers keyed by public-key string. */
99
+ get peers(): Map<string, any>;
100
+ /** @returns {Set<any>} Live connection streams — swarm and injected. */
101
+ get connections(): Set<any>;
102
+ /** @returns {boolean} */
103
+ get suspended(): boolean;
104
+ _open(): Promise<void>;
105
+ _close(): Promise<void>;
87
106
  /**
88
- * Feed an externally-established connection — a Bluetooth L2CAP channel, a
89
- * serial link, an in-process pair, any duplex — into the network. A raw
90
- * duplex is wrapped in NoiseSecretStream (pass `isInitiator`); a stream
91
- * that already IS one is used as-is. From here it gets the exact same
92
- * treatment as a swarm connection: wakeup, replication of every attached
93
- * core, pairing, and the 'connection' event.
107
+ * Feed an externally-established connection — a Bluetooth L2CAP channel, a serial link, an
108
+ * in-process pair, any duplex — into the network.
94
109
  *
95
110
  * @param {any} stream Duplex transport, or a ready NoiseSecretStream.
96
111
  * @param {{ isInitiator?: boolean }} [opts] Which side initiates the noise handshake (raw duplexes only).
@@ -100,27 +115,21 @@ export declare class Network extends ReadyResource {
100
115
  isInitiator?: boolean;
101
116
  }): any;
102
117
  /**
103
- * Lazily create the network-shared BlindPairing. One instance serves every
104
- * handle's pairing member — per-handle instances each added their own swarm
105
- * and DHT listeners plus a protomux channel per connection.
118
+ * Lazily create the network-shared BlindPairing.
106
119
  *
107
120
  * @returns {Promise<any>}
108
121
  */
109
122
  blind(): Promise<any>;
110
123
  /**
111
- * Re-attach pairing channels on injected connections. blind-pairing only
112
- * auto-attaches refs that existed when a connection arrived — swarm peers
113
- * meet again over topic joins, injected links (Bluetooth, pipes) don't, so
114
- * a member/candidate added later must re-run the attach. Idempotent:
115
- * protomux refuses duplicate channels.
124
+ * Re-attach pairing channels on injected connections. blind-pairing only auto-attaches
125
+ * refs that existed when a connection arrived — swarm peers meet again over topic joins,
126
+ * injected links (Bluetooth,.
116
127
  *
117
128
  * @returns {Promise<void>}
118
129
  */
119
130
  refreshInjected(): Promise<void>;
120
131
  /**
121
- * Declare this peer's self-reported info ({ name, ... }). Sent over an
122
- * encrypted channel on every live and future connection — never on the
123
- * radio advertisement or the DHT.
132
+ * Declare this peer's self-reported info ({ name, ... }).
124
133
  *
125
134
  * @param {object | null} info
126
135
  */
@@ -132,14 +141,6 @@ export declare class Network extends ReadyResource {
132
141
  * @returns {object | null}
133
142
  */
134
143
  getInfo(key: Uint8Array | string): object | null;
135
- _attachInfo(conn: any): void;
136
- /** @returns {Map<string, any>} Known peers keyed by public-key string. */
137
- get peers(): Map<string, any>;
138
- /** @returns {Set<any>} Live connection streams — swarm and injected. */
139
- get connections(): Set<any>;
140
- /** @returns {boolean} */
141
- get suspended(): boolean;
142
- _open(): Promise<void>;
143
144
  /**
144
145
  * Wait for pending DHT announces and lookups to settle, bounded by timeout.
145
146
  *
@@ -161,7 +162,6 @@ export declare class Network extends ReadyResource {
161
162
  * @returns {Promise<void>}
162
163
  */
163
164
  resume(): Promise<void>;
164
- _close(): Promise<void>;
165
165
  /**
166
166
  * Announce/lookup on a 32-byte topic and return a Discovery handle.
167
167
  *
@@ -196,4 +196,5 @@ export declare class Network extends ReadyResource {
196
196
  * @returns {void}
197
197
  */
198
198
  replicate(target: Replicable): void;
199
+ _attachInfo(conn: any): void;
199
200
  }
@@ -0,0 +1,50 @@
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
+ entries: Map<any, any>;
20
+ /**
21
+ * @param {import('./index.js').Network} network
22
+ * @param {{ active?: number, announced?: number, idle?: number }} [opts]
23
+ */
24
+ constructor(network: import('./index.js').Network, { active, announced, idle }?: {
25
+ active?: number;
26
+ announced?: number;
27
+ idle?: number;
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: Uint8Array, { pinned }?: {
38
+ pinned?: boolean;
39
+ }): Slot;
40
+ /**
41
+ * @param {Uint8Array} topic
42
+ * @returns {'active' | 'passive' | null}
43
+ */
44
+ mode(topic: Uint8Array): 'active' | 'passive' | null;
45
+ refresh(): void;
46
+ close(): void;
47
+ _want(e: any, mode: any): void;
48
+ _settle(e: any): void;
49
+ _leave(e: any): void;
50
+ }
@@ -1,7 +1,6 @@
1
1
  /**
2
2
  * Internal — the joiner side: a candidate's handshake state machine (this *is* the
3
- * blind-pairing `addCandidate`). One-shot: created by `Pairing.join()`, settled
4
- * exactly once via `_done` or `_fail`.
3
+ * blind-pairing `addCandidate`).
5
4
  *
6
5
  * @property {any} _candidate blind-pairing addCandidate handle (null until started).
7
6
  * @property {ReturnType<typeof setTimeout> | null} _timer Pending timeout, if any.
@@ -85,10 +85,8 @@ export type JoinResult = {
85
85
  * @typedef {{ key: Uint8Array, encryptionKey: Uint8Array | null, additional: Uint8Array | null }} JoinResult
86
86
  */
87
87
  /**
88
- * Blind-pairing membership wrapper. Hosts invites, dispatches incoming
89
- * candidates to the application for accept/deny, and provides the joiner
90
- * side of the handshake. All wire payloads are signed and routed through
91
- * the supplied network.
88
+ * Blind-pairing membership wrapper. Hosts invites, dispatches incoming candidates to the
89
+ * application for accept/deny, and provides the joiner side of the handshake.
92
90
  */
93
91
  export declare class Pairing extends ReadyResource {
94
92
  network: import("../index.js").Network;
@@ -105,17 +103,14 @@ export declare class Pairing extends ReadyResource {
105
103
  _candidates: Set<any>;
106
104
  /** @param {PairingOpts} [opts] */
107
105
  constructor({ network, identity, topic, host, inviteEncoding, joinerEncoding, onerror, onconsume }?: PairingOpts);
108
- _open(): Promise<void>;
109
- _close(): Promise<void>;
110
106
  /**
111
- * Resolve the resource discovery key an invite targets — without pairing.
112
- * Lets callers detect "already joined" before any handshake. Returns `null`
113
- * for an unparseable invite.
107
+ * Whether the underlying blind-pairing layer is suspended.
114
108
  *
115
- * @param {string} invite
116
- * @returns {Uint8Array | null}
109
+ * @returns {boolean}
117
110
  */
118
- static inviteTopic(invite: string): Uint8Array | null;
111
+ get suspended(): boolean;
112
+ _open(): Promise<void>;
113
+ _close(): Promise<void>;
119
114
  /**
120
115
  * Mint a new pairing invite. Returns its canonical wire form (z32 string).
121
116
  *
@@ -137,11 +132,7 @@ export declare class Pairing extends ReadyResource {
137
132
  reuse: boolean;
138
133
  } | null;
139
134
  /**
140
- * Reconcile the served-invite set with persisted rows (the room's `invites`
141
- * collection). Adds unknown rows so ANY member replica can serve them across
142
- * restarts; drops replicated records whose row disappeared (revoked or
143
- * consumed elsewhere). Locally-minted records not yet visible as rows are
144
- * kept.
135
+ * Reconcile the served-invite set with persisted rows (the room's `invites` collection).
145
136
  *
146
137
  * @param {Array<{ id: string, invite: Uint8Array, publicKey: Uint8Array, seed: Uint8Array, reuse?: boolean }>} rows
147
138
  */
@@ -160,13 +151,6 @@ export declare class Pairing extends ReadyResource {
160
151
  * @returns {boolean}
161
152
  */
162
153
  revoke(inviteStr: string): boolean;
163
- /**
164
- * Cheap structural test for a candidate invite string.
165
- *
166
- * @param {unknown} str
167
- * @returns {boolean}
168
- */
169
- static isInvite(str: unknown): boolean;
170
154
  /**
171
155
  * Joiner side — start a pairing handshake against `inviteStr` and resolve
172
156
  * with the host's response when the host confirms.
@@ -176,12 +160,6 @@ export declare class Pairing extends ReadyResource {
176
160
  * @returns {Promise<JoinResult>}
177
161
  */
178
162
  join(inviteStr: string, { userData, timeout }?: JoinOpts): Promise<JoinResult>;
179
- /**
180
- * Whether the underlying blind-pairing layer is suspended.
181
- *
182
- * @returns {boolean}
183
- */
184
- get suspended(): boolean;
185
163
  /**
186
164
  * Pause blind-pairing — keeps state, drops sockets. Idempotent.
187
165
  *
@@ -195,4 +173,18 @@ export declare class Pairing extends ReadyResource {
195
173
  */
196
174
  resume(): Promise<void>;
197
175
  _oncandidate(req: any): Promise<void>;
176
+ /**
177
+ * Resolve the resource discovery key an invite targets — without pairing.
178
+ *
179
+ * @param {string} invite
180
+ * @returns {Uint8Array | null}
181
+ */
182
+ static inviteTopic(invite: string): Uint8Array | null;
183
+ /**
184
+ * Cheap structural test for a candidate invite string.
185
+ *
186
+ * @param {unknown} str
187
+ * @returns {boolean}
188
+ */
189
+ static isInvite(str: unknown): boolean;
198
190
  }
@@ -87,9 +87,9 @@ export type ParseInviteOpts = {
87
87
  * @property {any} [encoding] compact-encoding type used to decode the embedded payload.
88
88
  */
89
89
  /**
90
- * Signed, expirable pairing invite. Wraps a blind-pairing invite with a
91
- * role, optional payload and an Ed25519 signature so the host can be
92
- * authenticated by the joiner before any handshake happens.
90
+ * Signed, expirable pairing invite. Wraps a blind-pairing invite with a role, optional
91
+ * payload and an Ed25519 signature so the host can be authenticated by the joiner before
92
+ * any handshake happens.
93
93
  *
94
94
  * @property {any} [_rawData] Decoded payload kept alongside the encoded `data` for convenience.
95
95
  * @property {Uint8Array} _discoveryKey Cached discovery key of the wrapped blind invite.
@@ -100,9 +100,7 @@ export type RequestOpts = {
100
100
  */
101
101
  /**
102
102
  * Internal — a pairing request from an incoming candidate, awaiting the host's
103
- * accept/deny. Built in `_oncandidate` and emitted on the `'candidate'` event so
104
- * the host can decide via `confirm` / `deny`. (The candidate is the joiner; this
105
- * is the host's handle to answer it.)
103
+ * accept/deny.
106
104
  *
107
105
  * @property {boolean} _settled Whether confirm/deny has already run.
108
106
  */
@@ -66,10 +66,7 @@ export type Codec = {
66
66
  * @property {(handle: any, op: string, buf: Uint8Array) => any} decodeAction
67
67
  */
68
68
  /**
69
- * Attach a `codec` namespace to a hyperschema-shaped `spec`. Looks up
70
- * envelope types using the spec's namespace (`@<ns>/rows` etc.) and falls
71
- * back to the bundled `@cero/*` envelopes when the spec has not declared
72
- * its own. Returns the spec for chaining.
69
+ * Attach a `codec` namespace to a hyperschema-shaped `spec`.
73
70
  *
74
71
  * @param {Spec} spec
75
72
  * @returns {Spec}
@@ -1,9 +1,7 @@
1
1
  import ReadyResource from 'ready-resource';
2
2
  /**
3
- * Shared RPC peer: wraps an IPC duplex in a length-framed stream and constructs
4
- * the spec's hrpc binding. Frames are paused until `_open` runs so handlers
5
- * registered before `ready()` see a clean slate. `RPCClient` and `RPCServer`
6
- * are thin named subclasses — identical wiring, distinct roles.
3
+ * Shared RPC peer: wraps an IPC duplex in a length-framed stream and constructs the spec's
4
+ * hrpc binding.
7
5
  */
8
6
  export declare class RPCPeer extends ReadyResource {
9
7
  ipc: any;
@@ -59,9 +59,8 @@ export type GetByIdResult = {
59
59
  * @typedef {{ data: any | null }} GetByIdResult
60
60
  */
61
61
  /**
62
- * Local, single-writer storage. Backed by either RocksDB (`rocks`) or a
63
- * Hyperbee on top of corestore (`bee`). Exposes the same `put/get/...`
64
- * surface as `Database` but without networking or multi-writer apply.
62
+ * Local, single-writer storage. Backed by either RocksDB (`rocks`) or a Hyperbee on top of
63
+ * corestore (`bee`).
65
64
  */
66
65
  export declare class Storage extends ReadyResource {
67
66
  dir: string;
@@ -91,20 +90,6 @@ export declare class Storage extends ReadyResource {
91
90
  * @param {StorageOpts} [opts]
92
91
  */
93
92
  constructor(dir: string, { spec, backend, root, store, storageKey }?: StorageOpts);
94
- /**
95
- * Construct a RocksDB-backed Storage.
96
- *
97
- * @param {string} dir
98
- * @param {Omit<StorageOpts, 'backend'>} opts
99
- */
100
- static rocks(dir: string, opts: Omit<StorageOpts, 'backend'>): Storage;
101
- /**
102
- * Construct a Hyperbee-backed Storage.
103
- *
104
- * @param {string} dir
105
- * @param {Omit<StorageOpts, 'backend'>} opts
106
- */
107
- static bee(dir: string, opts: Omit<StorageOpts, 'backend'>): Storage;
108
93
  _open(): Promise<void>;
109
94
  _close(): Promise<void>;
110
95
  /**
@@ -116,9 +101,7 @@ export declare class Storage extends ReadyResource {
116
101
  */
117
102
  put(name: string, row: Record<string, any>): Promise<SingleResult>;
118
103
  /**
119
- * Upsert by merging with the existing row, preserving `createdAt`. Pass
120
- * `{ upsert: false }` to update-only — a missing row is left untouched
121
- * (returns `null`) instead of created. Mirrors `Database.set`.
104
+ * Upsert by merging with the existing row, preserving `createdAt`.
122
105
  *
123
106
  * @param {string} name
124
107
  * @param {Record<string, any>} row
@@ -145,16 +128,6 @@ export declare class Storage extends ReadyResource {
145
128
  * @returns {Promise<SingleResult | ListResult | GetByIdResult>}
146
129
  */
147
130
  get(name: string, query?: string | Record<string, any>): Promise<SingleResult | ListResult | GetByIdResult>;
148
- /**
149
- * Number of rows that match `query` (or total if omitted).
150
- *
151
- * @param {string} name
152
- * @param {Record<string, any>} [query]
153
- * @returns {Promise<{ data: number }>}
154
- */
155
- count(name: string, query?: Record<string, any>): Promise<{
156
- data: number;
157
- }>;
158
131
  /**
159
132
  * Live snapshot stream — re-emits the latest `get()` result on every
160
133
  * underlying mutation. Destroy the stream to stop watching.
@@ -173,4 +146,18 @@ export declare class Storage extends ReadyResource {
173
146
  _read(ref: Ref, id?: string): Promise<any | null>;
174
147
  /** @param {Ref} ref @param {Record<string, any>} row @returns {Promise<void>} */
175
148
  _write(ref: Ref, row: Record<string, any>): Promise<void>;
149
+ /**
150
+ * Construct a RocksDB-backed Storage.
151
+ *
152
+ * @param {string} dir
153
+ * @param {Omit<StorageOpts, 'backend'>} opts
154
+ */
155
+ static rocks(dir: string, opts: Omit<StorageOpts, 'backend'>): Storage;
156
+ /**
157
+ * Construct a Hyperbee-backed Storage.
158
+ *
159
+ * @param {string} dir
160
+ * @param {Omit<StorageOpts, 'backend'>} opts
161
+ */
162
+ static bee(dir: string, opts: Omit<StorageOpts, 'backend'>): Storage;
176
163
  }