@cero-base/cero 1.18.2 → 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 (44) hide show
  1. package/README.md +38 -644
  2. package/package.json +16 -12
  3. package/src/build/index.js +21 -50
  4. package/src/build/internal.js +121 -0
  5. package/src/build/schemas.js +2 -8
  6. package/src/extensions/handle-sync.js +3 -10
  7. package/src/extensions/index.js +6 -0
  8. package/src/extensions/profile-sync.js +0 -5
  9. package/src/handle/index.js +255 -300
  10. package/src/index.js +65 -112
  11. package/src/lib/bluetooth.js +25 -56
  12. package/src/lib/constants.js +0 -15
  13. package/src/lib/operators.js +24 -74
  14. package/src/lib/peek.js +4 -8
  15. package/src/lib/refs.js +46 -0
  16. package/src/lib/spec.js +2 -3
  17. package/src/local/index.js +4 -5
  18. package/src/rpc/client.js +27 -39
  19. package/src/rpc/index.js +3 -3
  20. package/src/rpc/server.js +29 -40
  21. package/types/build/index.d.ts +19 -7
  22. package/types/build/internal.d.ts +78 -0
  23. package/types/build/schemas.d.ts +3 -3
  24. package/types/extensions/handle-sync.d.ts +4 -9
  25. package/types/extensions/index.d.ts +24 -2
  26. package/types/extensions/profile-sync.d.ts +2 -6
  27. package/types/handle/index.d.ts +218 -254
  28. package/types/index.d.ts +78 -102
  29. package/types/lib/bluetooth.d.ts +24 -46
  30. package/types/lib/constants.d.ts +5 -16
  31. package/types/lib/operators.d.ts +49 -77
  32. package/types/lib/peek.d.ts +3 -4
  33. package/types/lib/refs.d.ts +36 -0
  34. package/types/lib/spec.d.ts +5 -1
  35. package/types/local/index.d.ts +24 -23
  36. package/types/rpc/client.d.ts +107 -127
  37. package/types/rpc/index.d.ts +7 -2
  38. package/types/rpc/server.d.ts +62 -76
  39. package/src/build/builtins.js +0 -174
  40. package/src/lib/internal.js +0 -9
  41. package/src/lib/utils.js +0 -67
  42. package/types/build/builtins.d.ts +0 -100
  43. package/types/lib/internal.d.ts +0 -24
  44. package/types/lib/utils.d.ts +0 -55
@@ -1,3 +1,19 @@
1
+ import ReadyResource from 'ready-resource';
2
+ import { Storage } from '@cero-base/core/storage';
3
+ export type LocalOpts = {
4
+ /**
5
+ * Pre-existing HypercoreStorage to reuse.
6
+ */
7
+ root?: any;
8
+ /**
9
+ * Pre-existing Corestore to reuse.
10
+ */
11
+ store?: any;
12
+ /**
13
+ * 32-byte key encrypting the local store at rest.
14
+ */
15
+ storageKey?: Uint8Array;
16
+ };
1
17
  /**
2
18
  * @typedef {object} LocalOpts
3
19
  * @property {any} [root] Pre-existing HypercoreStorage to reuse.
@@ -5,34 +21,19 @@
5
21
  * @property {Uint8Array} [storageKey] 32-byte key encrypting the local store at rest.
6
22
  */
7
23
  /**
8
- * Per-device, single-writer storage for cero — holds the master seed,
9
- * device keypair and any per-handle keypairs. Wraps a hyperbee-backed
10
- * `Storage` and exposes each local ref as a property of the instance.
24
+ * Per-device, single-writer storage for cero — holds the master seed, device keypair and
25
+ * any per-handle keypairs.
11
26
  */
12
- export class Local extends ReadyResource {
27
+ export declare class Local extends ReadyResource {
28
+ dir: string;
29
+ spec: any;
30
+ store: Storage;
13
31
  /**
14
32
  * @param {string | null} dir Directory for the local store, or `null` when reusing an external `store`.
15
33
  * @param {any} spec Built cero spec — must include `spec.local.database` and `spec.meta.local`.
16
34
  * @param {LocalOpts} [opts]
17
35
  */
18
36
  constructor(dir: string | null, spec: any, { root, store, storageKey }?: LocalOpts);
19
- dir: string;
20
- spec: any;
21
- store: Storage;
37
+ _open(): Promise<void>;
38
+ _close(): Promise<void>;
22
39
  }
23
- export type LocalOpts = {
24
- /**
25
- * Pre-existing HypercoreStorage to reuse.
26
- */
27
- root?: any;
28
- /**
29
- * Pre-existing Corestore to reuse.
30
- */
31
- store?: any;
32
- /**
33
- * 32-byte key encrypting the local store at rest.
34
- */
35
- storageKey?: Uint8Array;
36
- };
37
- import ReadyResource from 'ready-resource';
38
- import { Storage } from '@cero-base/core/storage';
@@ -1,3 +1,45 @@
1
+ import { RPCClient } from '@cero-base/core/rpc';
2
+ import { put, set, get, del, count, watch, changes, call, open, rotate, bind, define } from '../lib/operators.js';
3
+ import { t, schema } from '../lib/spec.js';
4
+ export { put, set, get, del, count, watch, changes, call, open, rotate, bind, define, t, schema };
5
+ export type BaseRPCClient = import('@cero-base/core/rpc').RPCClient;
6
+ export type RefInfo = {
7
+ kind?: 'single' | 'collection' | 'action' | 'handle';
8
+ schema?: string;
9
+ type?: string;
10
+ internal?: boolean;
11
+ };
12
+ export type Spec = import('@cero-base/core/rpc').Spec & {
13
+ meta: {
14
+ ns?: string;
15
+ refs: Record<string, RefInfo>;
16
+ local?: {
17
+ refs: Record<string, RefInfo>;
18
+ };
19
+ handles?: Record<string, Spec>;
20
+ };
21
+ handles: Record<string, Spec>;
22
+ };
23
+ export type SingleResult = {
24
+ data: any;
25
+ };
26
+ export type ListResult = {
27
+ data: any[];
28
+ total: number;
29
+ size: number;
30
+ };
31
+ export type GetByIdResult = {
32
+ data: any | null;
33
+ };
34
+ export type ClientIdentity = {
35
+ id: string;
36
+ toPhrase: () => string | null;
37
+ };
38
+ export type HandleStub = {
39
+ id: string;
40
+ type: string;
41
+ name: string | null;
42
+ };
1
43
  /**
2
44
  * Re-initialize a `Client` from a recovery phrase. Closes the existing
3
45
  * local state and reseeds identity from the phrase.
@@ -6,57 +48,28 @@
6
48
  * @param {string} phrase
7
49
  * @returns {Promise<Client>}
8
50
  */
9
- export function restore(me: Client, phrase: string): Promise<Client>;
10
- /**
11
- * Construct a `Client`, wait for `init` to complete, and return it.
12
- *
13
- * @param {any} ipc
14
- * @param {object} spec
15
- * @returns {Promise<Client>}
16
- */
17
- export function connect(ipc: any, spec: object): Promise<Client>;
51
+ export declare function restore(me: Client, phrase: string): Promise<Client>;
18
52
  /**
19
- * Symmetric client entry. Mirrors the main `cero`, but `cero(ipc, spec)` connects
20
- * to a server (via `connect`) instead of opening a local store. The same operator
21
- * surface is attached, so `import { cero } from '@cero-base/cero/client'` works
22
- * just like `import { cero } from '@cero-base/cero'`.
23
- *
24
- * Note: `before`/`after`/`peek` are intentionally not exposed here — they hook the
25
- * local write path / probe a local store, which a client proxy has no notion of.
26
- *
27
- * @param {any} ipc Framed IPC duplex stream.
28
- * @param {any} spec Built cero spec.
29
- * @returns {Promise<Client>}
53
+ * Per-device `local`-namespace surface on a Client.
30
54
  */
31
- export function cero(ipc: any, spec: any): Promise<Client>;
32
- export namespace cero {
33
- export { connect };
34
- export { restore };
35
- export { t };
36
- export { put };
37
- export { set };
38
- export { get };
39
- export { del };
40
- export { count };
41
- export { watch };
42
- export { call };
43
- export { open };
44
- export { rotate };
45
- export { bind };
46
- export { define };
47
- export { schema };
55
+ declare class LocalRefs {
56
+ parent: Client;
57
+ spec: import("@cero-base/core").Spec;
58
+ store: this;
59
+ _local: boolean;
60
+ /** @param {Client} client */
61
+ constructor(client: Client);
62
+ /** Underlying RPC channel borrowed from the parent. */
63
+ get rpc(): any;
64
+ /** Root handle id (local ops are resolved against the root's local store). */
65
+ get id(): any;
48
66
  }
49
67
  /**
50
- * IPC-side RPC client for cero. Wraps an `hrpc` channel and exposes the
51
- * same handle/ref/row API as a local cero instance, transparently
52
- * routing every operation across the wire.
68
+ * IPC-side RPC client for cero. Wraps an `hrpc` channel and exposes the same
69
+ * handle/ref/row API as a local cero instance, transparently routing every operation
70
+ * across the wire.
53
71
  */
54
- export class Client extends RPCClient {
55
- /**
56
- * @param {any} ipc Framed IPC stream (must be writable).
57
- * @param {Spec} spec Compiled cero spec (schema + rpc + handles).
58
- */
59
- constructor(ipc: any, spec: Spec);
72
+ export declare class Client extends RPCClient {
60
73
  id: any;
61
74
  deviceId: any;
62
75
  store: this;
@@ -67,6 +80,12 @@ export class Client extends RPCClient {
67
80
  id: any;
68
81
  toPhrase: () => Promise<any>;
69
82
  };
83
+ /**
84
+ * @param {any} ipc Framed IPC stream (must be writable).
85
+ * @param {Spec} spec Compiled cero spec (schema + rpc + handles).
86
+ */
87
+ constructor(ipc: any, spec: Spec);
88
+ _open(): Promise<void>;
70
89
  /**
71
90
  * Create a new child handle of the given type.
72
91
  *
@@ -92,85 +111,18 @@ export class Client extends RPCClient {
92
111
  */
93
112
  _join(invite: string, type: string): Promise<Handle>;
94
113
  }
95
- export type BaseRPCClient = import("@cero-base/core/rpc").RPCClient;
96
- export type RefInfo = {
97
- kind?: "single" | "collection" | "action" | "handle";
98
- schema?: string;
99
- type?: string;
100
- builtin?: boolean;
101
- };
102
114
  /**
103
- * Built cero spec (schema + rpc + per-handle child specs).
115
+ * Client-side proxy for a remote handle. Exposes the same row-ops surface as `Client` but
116
+ * scoped to a single child handle id, and routes every call through the parent's RPC
117
+ * channel.
104
118
  */
105
- export type Spec = import("@cero-base/core/rpc").Spec & {
106
- meta: {
107
- ns?: string;
108
- refs: Record<string, RefInfo>;
109
- local?: {
110
- refs: Record<string, RefInfo>;
111
- };
112
- handles?: Record<string, Spec>;
113
- };
114
- handles: Record<string, Spec>;
115
- };
116
- export type SingleResult = {
117
- data: any;
118
- };
119
- export type ListResult = {
120
- data: any[];
121
- total: number;
122
- size: number;
123
- };
124
- export type GetByIdResult = {
125
- data: any | null;
126
- };
127
- export type ClientIdentity = {
128
- id: string;
129
- toPhrase: () => string | null;
130
- };
131
- export type HandleStub = {
119
+ declare class Handle {
120
+ parent: Client;
132
121
  id: string;
133
122
  type: string;
134
- name: string | null;
135
- };
136
- import { t } from '../lib/spec.js';
137
- import { put } from '../lib/operators.js';
138
- import { set } from '../lib/operators.js';
139
- import { get } from '../lib/operators.js';
140
- import { del } from '../lib/operators.js';
141
- import { count } from '../lib/operators.js';
142
- import { watch } from '../lib/operators.js';
143
- import { call } from '../lib/operators.js';
144
- import { open } from '../lib/operators.js';
145
- import { rotate } from '../lib/operators.js';
146
- import { bind } from '../lib/operators.js';
147
- import { define } from '../lib/operators.js';
148
- import { schema } from '../lib/spec.js';
149
- import { RPCClient } from '@cero-base/core/rpc';
150
- /**
151
- * Per-device `local`-namespace surface on a Client. Exposes each app-defined
152
- * local ref (e.g. `client.local.settings`) and routes ops over RPC with
153
- * `local: true`, so they hit the server's per-device store and never
154
- * replicate. Built-in local refs (identity master/keypair) are not exposed.
155
- */
156
- declare class LocalRefs {
157
- /** @param {Client} client */
158
- constructor(client: Client);
159
- parent: Client;
160
- spec: import("@cero-base/core").Spec;
123
+ name: string;
124
+ spec: Spec;
161
125
  store: this;
162
- _local: boolean;
163
- /** Underlying RPC channel borrowed from the parent. */
164
- get rpc(): any;
165
- /** Root handle id (local ops are resolved against the root's local store). */
166
- get id(): any;
167
- }
168
- /**
169
- * Client-side proxy for a remote handle. Exposes the same row-ops surface
170
- * as `Client` but scoped to a single child handle id, and routes every
171
- * call through the parent's RPC channel.
172
- */
173
- declare class Handle {
174
126
  /**
175
127
  * @param {Client} parent
176
128
  * @param {string} id
@@ -178,12 +130,6 @@ declare class Handle {
178
130
  * @param {string|null} name
179
131
  */
180
132
  constructor(parent: Client, id: string, type: string, name: string | null);
181
- parent: Client;
182
- id: string;
183
- type: string;
184
- name: string;
185
- spec: Spec;
186
- store: this;
187
133
  /** Underlying RPC channel borrowed from the parent. */
188
134
  get rpc(): any;
189
135
  /** Tear down the remote handle without leaving the room. */
@@ -191,4 +137,38 @@ declare class Handle {
191
137
  /** Tear down the remote handle and drop membership. */
192
138
  leave(): any;
193
139
  }
194
- export { put, set, get, del, count, watch, call, open, rotate, bind, define, t, schema };
140
+ /**
141
+ * Construct a `Client`, wait for `init` to complete, and return it.
142
+ *
143
+ * @param {any} ipc
144
+ * @param {object} spec
145
+ * @returns {Promise<Client>}
146
+ */
147
+ export declare function connect(ipc: any, spec: object): Promise<Client>;
148
+ /**
149
+ * Symmetric client entry. Mirrors the main `cero`, but `cero(ipc, spec)` connects to a
150
+ * server (via `connect`) instead of opening a local store.
151
+ *
152
+ * @param {any} ipc Framed IPC duplex stream.
153
+ * @param {any} spec Built cero spec.
154
+ * @returns {Promise<Client>}
155
+ */
156
+ export declare function cero(ipc: any, spec: any): Promise<Client>;
157
+ export declare namespace cero {
158
+ export { connect };
159
+ export { restore };
160
+ export { t };
161
+ export { put };
162
+ export { set };
163
+ export { get };
164
+ export { del };
165
+ export { count };
166
+ export { watch };
167
+ export { changes };
168
+ export { call };
169
+ export { open };
170
+ export { rotate };
171
+ export { bind };
172
+ export { define };
173
+ export { schema };
174
+ }
@@ -1,2 +1,7 @@
1
- export { Server, serve } from "./server.js";
2
- export { Client, connect } from "./client.js";
1
+ /**
2
+ * RPC barrel — re-exports the `Server`/`Client` classes and their `serve()`/`connect()`
3
+ * helpers so consumers can spin up either side of the cero IPC bridge from a single
4
+ * import.
5
+ */
6
+ export { Server, serve } from './server.js';
7
+ export { Client, connect } from './client.js';
@@ -1,17 +1,48 @@
1
- /**
2
- * Construct a `Server`, wait for it to be ready, and return it.
3
- *
4
- * @param {any} ipc
5
- * @param {ServerOpts} opts
6
- * @returns {Promise<Server>}
7
- */
8
- export function serve(ipc: any, opts: ServerOpts): Promise<Server>;
1
+ import { RPCServer } from '@cero-base/core/rpc';
2
+ export type BaseRPCServer = import('@cero-base/core/rpc').RPCServer;
3
+ export type ServerOpts = {
4
+ /**
5
+ * Directory passed to `cero()` for the local store.
6
+ */
7
+ storage: string;
8
+ /**
9
+ * Optional display name forwarded to `cero()`.
10
+ */
11
+ name?: string;
12
+ /**
13
+ * Custom DHT bootstrap.
14
+ */
15
+ bootstrap?: Array<{
16
+ host: string;
17
+ port: number;
18
+ }>;
19
+ isMobile?: boolean;
20
+ onerror?: (err: Error) => void;
21
+ };
22
+ export type Identity = {
23
+ /**
24
+ * Long-lived cero identity id.
25
+ */
26
+ id: string;
27
+ /**
28
+ * Per-device id (empty when no `local` spec).
29
+ */
30
+ deviceId: string;
31
+ };
32
+ export type RefAndCodec = {
33
+ ref: any;
34
+ codec: any;
35
+ };
36
+ export type GetResult = {
37
+ data: any;
38
+ total?: number;
39
+ size?: number;
40
+ };
9
41
  /**
10
42
  * @typedef {import('@cero-base/core/rpc').RPCServer} BaseRPCServer
11
43
  *
12
44
  * @typedef {object} ServerOpts
13
45
  * @property {string} storage Directory passed to `cero()` for the local store.
14
- * @property {object} spec Compiled cero spec (schema + rpc + handles).
15
46
  * @property {string} [name] Optional display name forwarded to `cero()`.
16
47
  * @property {Array<{ host: string, port: number }>} [bootstrap] Custom DHT bootstrap.
17
48
  * @property {boolean} [isMobile]
@@ -26,26 +57,14 @@ export function serve(ipc: any, opts: ServerOpts): Promise<Server>;
26
57
  * @typedef {{ data: any, total?: number, size?: number }} GetResult Single-ref get omits `total`/`size`; list/handle refs include them.
27
58
  */
28
59
  /**
29
- * IPC-side RPC server for cero. Bridges an `hrpc` channel to a live
30
- * `Handle` tree: lazy-initializes the root via `cero()` on the first
31
- * `init` call, then exposes data ops, pairing, and handle lifecycle
32
- * over the wire using the spec-bound codec.
60
+ * IPC-side RPC server for cero. Bridges an `hrpc` channel to a live `Handle` tree:
61
+ * lazy-initializes the root via `cero()` on the first `init` call, then exposes data ops,
62
+ * pairing, and handle lifecycle.
33
63
  */
34
- export class Server extends RPCServer {
35
- /**
36
- * @param {any} ipc Framed IPC stream (must be writable).
37
- * @param {Partial<ServerOpts>} [opts]
38
- */
39
- constructor(ipc: any, { storage, spec, ...opts }?: Partial<ServerOpts>);
64
+ export declare class Server extends RPCServer {
40
65
  storage: string;
41
66
  opts: {
42
- /**
43
- * Optional display name forwarded to `cero()`.
44
- */
45
67
  name?: string;
46
- /**
47
- * Custom DHT bootstrap.
48
- */
49
68
  bootstrap?: Array<{
50
69
  host: string;
51
70
  port: number;
@@ -53,12 +72,16 @@ export class Server extends RPCServer {
53
72
  isMobile?: boolean;
54
73
  onerror?: (err: Error) => void;
55
74
  };
56
- me: any;
75
+ me: import("../handle/index.js").CeroHandle;
57
76
  handles: Map<any, any>;
58
77
  /** @type {Map<string, Set<object>>} handle id → its open watch streams */
59
78
  _watchStreams: Map<string, Set<object>>;
60
- /** End every watch stream bound to a handle (e.g. when it closes or leaves). */
61
- _endWatches(handle: any): void;
79
+ /**
80
+ * @param {any} ipc Framed IPC stream (must be writable).
81
+ * @param {object} spec
82
+ * @param {Partial<ServerOpts>} [opts]
83
+ */
84
+ constructor(ipc: any, spec: object, { storage, ...opts }?: Partial<ServerOpts>);
62
85
  /**
63
86
  * Root cero id (null until `init` has run).
64
87
  *
@@ -71,6 +94,9 @@ export class Server extends RPCServer {
71
94
  * @returns {any}
72
95
  */
73
96
  get identity(): any;
97
+ _close(): Promise<void>;
98
+ /** End every watch stream bound to a handle (e.g. when it closes or leaves). */
99
+ _endWatches(handle: any): void;
74
100
  /** Wire the `init` handler that lazily constructs the root cero handle. */
75
101
  _wireInit(): void;
76
102
  /** Wire the `restore` handler that rebuilds the local store from a phrase. */
@@ -90,9 +116,7 @@ export class Server extends RPCServer {
90
116
  */
91
117
  _resolve(id: string): any;
92
118
  /**
93
- * Resolve a `{ handle, ref }` pair to its `Ref` and codec. When `local` is
94
- * set, the ref is resolved against the root's per-device `local` store and
95
- * paired with the codec bound from the local schema.
119
+ * Resolve a `{ handle, ref }` pair to its `Ref` and codec.
96
120
  *
97
121
  * @param {string} id
98
122
  * @param {string} name
@@ -109,50 +133,12 @@ export class Server extends RPCServer {
109
133
  /** Wire the on-demand `seed` handler — surfaces the recovery phrase only when asked. */
110
134
  _wireSeed(): void;
111
135
  }
112
- export type BaseRPCServer = import("@cero-base/core/rpc").RPCServer;
113
- export type ServerOpts = {
114
- /**
115
- * Directory passed to `cero()` for the local store.
116
- */
117
- storage: string;
118
- /**
119
- * Compiled cero spec (schema + rpc + handles).
120
- */
121
- spec: object;
122
- /**
123
- * Optional display name forwarded to `cero()`.
124
- */
125
- name?: string;
126
- /**
127
- * Custom DHT bootstrap.
128
- */
129
- bootstrap?: Array<{
130
- host: string;
131
- port: number;
132
- }>;
133
- isMobile?: boolean;
134
- onerror?: (err: Error) => void;
135
- };
136
- export type Identity = {
137
- /**
138
- * Long-lived cero identity id.
139
- */
140
- id: string;
141
- /**
142
- * Per-device id (empty when no `local` spec).
143
- */
144
- deviceId: string;
145
- };
146
- export type RefAndCodec = {
147
- ref: any;
148
- codec: any;
149
- };
150
136
  /**
151
- * Single-ref get omits `total`/`size`; list/handle refs include them.
137
+ * Construct a `Server`, wait for it to be ready, and return it.
138
+ *
139
+ * @param {any} ipc
140
+ * @param {object} spec
141
+ * @param {ServerOpts} opts
142
+ * @returns {Promise<Server>}
152
143
  */
153
- export type GetResult = {
154
- data: any;
155
- total?: number;
156
- size?: number;
157
- };
158
- import { RPCServer } from '@cero-base/core/rpc';
144
+ export declare function serve(ipc: any, spec: object, opts: ServerOpts): Promise<Server>;