@cero-base/core 1.19.0 → 2.0.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 (43) hide show
  1. package/package.json +8 -5
  2. package/src/blobs/index.js +15 -15
  3. package/src/blobs/server.js +0 -2
  4. package/src/database/changes.js +3 -7
  5. package/src/database/dispatch.js +128 -203
  6. package/src/database/encryption.js +22 -52
  7. package/src/database/envelope.js +1 -3
  8. package/src/database/index.js +336 -387
  9. package/src/database/rotation.js +56 -70
  10. package/src/identity/index.js +5 -9
  11. package/src/index.js +5 -9
  12. package/src/lib/constants.js +3 -12
  13. package/src/lib/errors.js +6 -10
  14. package/src/lib/schema.js +2 -5
  15. package/src/lib/utils.js +9 -24
  16. package/src/network/index.js +138 -166
  17. package/src/pairing/candidate.js +2 -4
  18. package/src/pairing/index.js +41 -55
  19. package/src/pairing/invite.js +3 -4
  20. package/src/pairing/request.js +3 -9
  21. package/src/rpc/index.js +4 -14
  22. package/src/rpc/peer.js +3 -7
  23. package/src/storage/index.js +25 -31
  24. package/types/blobs/index.d.ts +5 -5
  25. package/types/blobs/server.d.ts +0 -2
  26. package/types/database/changes.d.ts +2 -5
  27. package/types/database/dispatch.d.ts +2 -6
  28. package/types/database/encryption.d.ts +8 -20
  29. package/types/database/index.d.ts +79 -96
  30. package/types/database/rotation.d.ts +14 -20
  31. package/types/identity/index.d.ts +5 -9
  32. package/types/index.d.ts +4 -3
  33. package/types/lib/errors.d.ts +6 -10
  34. package/types/lib/schema.d.ts +2 -5
  35. package/types/lib/utils.d.ts +3 -7
  36. package/types/network/index.d.ts +16 -26
  37. package/types/pairing/candidate.d.ts +1 -2
  38. package/types/pairing/index.d.ts +22 -30
  39. package/types/pairing/invite.d.ts +3 -3
  40. package/types/pairing/request.d.ts +1 -3
  41. package/types/rpc/index.d.ts +1 -4
  42. package/types/rpc/peer.d.ts +2 -4
  43. package/types/storage/index.d.ts +17 -20
@@ -1,7 +1,6 @@
1
1
  /**
2
- * Canonical error class. Every instance carries a stable `code` field which
3
- * is also prefixed to the message, so logs stay self-identifying. Static
4
- * factories produce errors with the right code + a sensible message.
2
+ * Canonical error class. Every instance carries a stable `code` field which is also
3
+ * prefixed to the message, so logs stay self-identifying.
5
4
  */
6
5
  export declare class CeroError extends Error {
7
6
  isCeroError: boolean;
@@ -39,9 +38,7 @@ export declare class CeroError extends Error {
39
38
  */
40
39
  static CLOSED(resource: string): CeroError;
41
40
  /**
42
- * A channel-stamped storage was opened under a different channel. Deliberately
43
- * does NOT name the stored channel — that label gates the network, so leaking it
44
- * in an error would hint at a realm the caller isn't on.
41
+ * A channel-stamped storage was opened under a different channel.
45
42
  */
46
43
  static CHANNEL_MISMATCH(): CeroError;
47
44
  /**
@@ -108,10 +105,9 @@ export declare class CeroError extends Error {
108
105
  */
109
106
  static DENIED(reason?: string | null, msg?: string): CeroError;
110
107
  /**
111
- * An apply-time rule refused the op. Every peer reaches this verdict from the
112
- * same node and the same view, so it aborts the writer's whole batch — which
113
- * is what makes a transaction atomic. Reserve it for deterministic decisions:
114
- * anything that could differ between peers must not abort, or they fork.
108
+ * An apply-time rule refused the op. Every peer reaches this verdict from the same node
109
+ * and the same view, so it aborts the writer's whole batch — which is what makes a
110
+ * transaction atomic.
115
111
  *
116
112
  * @param {string} rule Which rule refused, e.g. 'write' or 'own'.
117
113
  * @param {string} [msg]
@@ -43,11 +43,8 @@ export declare const t: {
43
43
  */
44
44
  single(fields: Record<string, Prim>): TypeDef;
45
45
  /**
46
- * Multi-row table keyed by `id`.
47
- *
48
- * `own` restricts a row to the member who wrote it — anyone may add rows, but
49
- * only the author changes or deletes theirs. Members holding REMOVE can still
50
- * moderate. Without it a writer may edit any row in the collection.
46
+ * Multi-row table keyed by `id`. `own` restricts a row to the member who wrote it — anyone
47
+ * may add rows, but only the author changes or deletes theirs.
51
48
  *
52
49
  * @param {Record<string, Prim>} fields
53
50
  * @param {{ indexes?: Record<string, string[]>, own?: boolean }} [opts]
@@ -21,9 +21,8 @@ export declare function isRank(role: any): boolean;
21
21
  export declare function grants(a: any, b: any): boolean;
22
22
  export declare function outranks(a: any, b: any): boolean;
23
23
  /**
24
- * Stream-of-snapshots primitive. Couples a `get` (returns the latest value)
25
- * with a `watch` (re-fires on change) and emits the newest snapshot every
26
- * time `watch` ticks.
24
+ * Stream-of-snapshots primitive. Couples a `get` (returns the latest value) with a `watch`
25
+ * (re-fires on change) and emits the newest snapshot every time `watch` ticks.
27
26
  *
28
27
  * @template T
29
28
  * @param {object} args
@@ -46,10 +45,7 @@ export declare function searchHit(row: any, term: any, fields: any): boolean;
46
45
  */
47
46
  export declare function filter(rows: any[], query?: Record<string, any>): any[];
48
47
  /**
49
- * Run `cb` when `signal` aborts — or immediately if it already has. No-op
50
- * without a signal. The listener removes itself on fire. Returns a disposer that
51
- * detaches the listener early, so a manual unsubscribe doesn't leave it lingering
52
- * on a long-lived signal.
48
+ * Run `cb` when `signal` aborts — or immediately if it already has.
53
49
  *
54
50
  * @param {AbortSignal | undefined} signal
55
51
  * @param {() => void} cb
@@ -84,13 +84,17 @@ export declare class Network extends ReadyResource {
84
84
  constructor({ identity, bootstrap, firewall, relayThrough, backoffs, channel, store, mirrors }?: NetworkOpts);
85
85
  /** @returns {any} The underlying hyperswarm, or null before ready / after close. */
86
86
  get swarm(): any;
87
+ /** @returns {Map<string, any>} Known peers keyed by public-key string. */
88
+ get peers(): Map<string, any>;
89
+ /** @returns {Set<any>} Live connection streams — swarm and injected. */
90
+ get connections(): Set<any>;
91
+ /** @returns {boolean} */
92
+ get suspended(): boolean;
93
+ _open(): Promise<void>;
94
+ _close(): Promise<void>;
87
95
  /**
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.
96
+ * Feed an externally-established connection — a Bluetooth L2CAP channel, a serial link, an
97
+ * in-process pair, any duplex — into the network.
94
98
  *
95
99
  * @param {any} stream Duplex transport, or a ready NoiseSecretStream.
96
100
  * @param {{ isInitiator?: boolean }} [opts] Which side initiates the noise handshake (raw duplexes only).
@@ -100,27 +104,21 @@ export declare class Network extends ReadyResource {
100
104
  isInitiator?: boolean;
101
105
  }): any;
102
106
  /**
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.
107
+ * Lazily create the network-shared BlindPairing.
106
108
  *
107
109
  * @returns {Promise<any>}
108
110
  */
109
111
  blind(): Promise<any>;
110
112
  /**
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.
113
+ * Re-attach pairing channels on injected connections. blind-pairing only auto-attaches
114
+ * refs that existed when a connection arrived — swarm peers meet again over topic joins,
115
+ * injected links (Bluetooth,.
116
116
  *
117
117
  * @returns {Promise<void>}
118
118
  */
119
119
  refreshInjected(): Promise<void>;
120
120
  /**
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.
121
+ * Declare this peer's self-reported info ({ name, ... }).
124
122
  *
125
123
  * @param {object | null} info
126
124
  */
@@ -132,14 +130,6 @@ export declare class Network extends ReadyResource {
132
130
  * @returns {object | null}
133
131
  */
134
132
  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
133
  /**
144
134
  * Wait for pending DHT announces and lookups to settle, bounded by timeout.
145
135
  *
@@ -161,7 +151,6 @@ export declare class Network extends ReadyResource {
161
151
  * @returns {Promise<void>}
162
152
  */
163
153
  resume(): Promise<void>;
164
- _close(): Promise<void>;
165
154
  /**
166
155
  * Announce/lookup on a 32-byte topic and return a Discovery handle.
167
156
  *
@@ -196,4 +185,5 @@ export declare class Network extends ReadyResource {
196
185
  * @returns {void}
197
186
  */
198
187
  replicate(target: Replicable): void;
188
+ _attachInfo(conn: any): void;
199
189
  }
@@ -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
@@ -173,4 +156,18 @@ export declare class Storage extends ReadyResource {
173
156
  _read(ref: Ref, id?: string): Promise<any | null>;
174
157
  /** @param {Ref} ref @param {Record<string, any>} row @returns {Promise<void>} */
175
158
  _write(ref: Ref, row: Record<string, any>): Promise<void>;
159
+ /**
160
+ * Construct a RocksDB-backed Storage.
161
+ *
162
+ * @param {string} dir
163
+ * @param {Omit<StorageOpts, 'backend'>} opts
164
+ */
165
+ static rocks(dir: string, opts: Omit<StorageOpts, 'backend'>): Storage;
166
+ /**
167
+ * Construct a Hyperbee-backed Storage.
168
+ *
169
+ * @param {string} dir
170
+ * @param {Omit<StorageOpts, 'backend'>} opts
171
+ */
172
+ static bee(dir: string, opts: Omit<StorageOpts, 'backend'>): Storage;
176
173
  }