@cero-base/cero 2.3.0 → 2.3.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cero-base/cero",
3
- "version": "2.3.0",
3
+ "version": "2.3.2",
4
4
  "description": "The ideal p2p API — everything is a handle, handles contain refs, refs contain rows.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -99,11 +99,11 @@
99
99
  "test:node": "find test -name '*.test.js' | sort | xargs -P4 -n1 brittle-node"
100
100
  },
101
101
  "dependencies": {
102
- "@cero-base/core": "^2.3.0",
102
+ "@cero-base/core": "^2.3.2",
103
103
  "b4a": "^1.9.0",
104
104
  "bare-abort-controller": "^1.1.2",
105
105
  "bare-crypto": "^1.15.3",
106
- "bare-fs": "^4.8.1",
106
+ "bare-fs": "^4.8.2",
107
107
  "bare-path": "^3.1.2",
108
108
  "ble-swarm": "^2.3.2",
109
109
  "compact-encoding": "^3.5.2",
package/src/index.js CHANGED
@@ -55,7 +55,7 @@ export { t, schema } from './lib/spec.js'
55
55
  * @param {CeroOpts} [opts]
56
56
  * @returns {Promise<Context>}
57
57
  */
58
- export async function cero(dir, spec, opts = {}) {
58
+ async function start(dir, spec, opts = {}) {
59
59
  if (typeof dir !== 'string' || !dir) throw CeroError.INVALID('dir must be a non-empty string')
60
60
  if (!spec) throw CeroError.REQUIRED('spec')
61
61
 
@@ -237,7 +237,7 @@ export async function restore(me, seed) {
237
237
  await me.close()
238
238
  await fs.promises.rm(`${dir}/main`, { recursive: true, force: true })
239
239
 
240
- return cero(dir, spec, { ...opts, seed })
240
+ return start(dir, spec, { ...opts, seed })
241
241
  }
242
242
 
243
243
  /**
@@ -251,7 +251,8 @@ export function toSeed(phrase) {
251
251
  }
252
252
 
253
253
  // the facade: cero.put and import { put } are the same function
254
- Object.assign(cero, verbs, { t, schema, peek, restore, toSeed })
254
+ /** @type {typeof start & typeof verbs & { t: typeof t, schema: typeof schema, peek: typeof peek, restore: typeof restore, toSeed: typeof toSeed }} */
255
+ export const cero = Object.assign(start, verbs, { t, schema, peek, restore, toSeed })
255
256
 
256
257
  function pointerManifest(store, identity) {
257
258
  return { version: store.manifestVersion, signers: [{ publicKey: identity.publicKey }] }
@@ -6,7 +6,7 @@ import { onAbort, subscribe } from '@cero-base/core/utils'
6
6
  /**
7
7
  * @typedef {import('./refs.js').Ref} Ref
8
8
  * @typedef {import('@cero-base/core/database').HookContext} HookContext
9
- * @typedef {import('../handle/index.js').Context} Context
9
+ * @typedef {import('../handle/index.js').Context | import('../rpc/client.js').Context} Context
10
10
  * @typedef {import('@cero-base/core/database').Row} Row
11
11
  * @typedef {import('@cero-base/core/database').SingleResult} SingleResult
12
12
  * @typedef {import('@cero-base/core/database').ListResult} ListResult
@@ -36,7 +36,10 @@ function resolveFile(handle, id, name) {
36
36
  * @returns {Promise<SingleResult>}
37
37
  */
38
38
  export async function put(ref, row) {
39
- await opened(ref.handle)
39
+ // an extension's setup runs while the root opens, so a call there waits for the open; on an open
40
+ // handle nothing is awaited, so the call reaches the database in the caller's run, where a hook's
41
+ // guard sees it
42
+ if (ref.handle.opened === false) await ref.handle.ready()
40
43
  return ref.name === 'files' ? putFile(ref, row) : ref.handle.store.put(ref.name, row)
41
44
  }
42
45
 
@@ -61,7 +64,7 @@ async function putFile(ref, row) {
61
64
  * @returns {Promise<SingleResult | null>}
62
65
  */
63
66
  export async function set(ref, row, opts) {
64
- await opened(ref.handle)
67
+ if (ref.handle.opened === false) await ref.handle.ready()
65
68
  return ref.handle.store.set(ref.name, row, opts)
66
69
  }
67
70
 
@@ -73,7 +76,7 @@ export async function set(ref, row, opts) {
73
76
  * @returns {Promise<void>}
74
77
  */
75
78
  export async function del(ref, id) {
76
- await opened(ref.handle)
79
+ if (ref.handle.opened === false) await ref.handle.ready()
77
80
  return ref.handle.store.del(ref.name, id)
78
81
  }
79
82
 
@@ -85,15 +88,10 @@ export async function del(ref, id) {
85
88
  * @returns {Promise<void>}
86
89
  */
87
90
  export async function call(ref, d) {
88
- await opened(ref.handle)
91
+ if (ref.handle.opened === false) await ref.handle.ready()
89
92
  return ref.handle.store.call(ref.name, d)
90
93
  }
91
94
 
92
- // an extension's setup runs while the root opens: what it reads or writes waits for the open
93
- function opened(handle) {
94
- return handle.opened === false ? handle.ready() : null
95
- }
96
-
97
95
  const WRITES = { single: ['set', 'del'], collection: ['put', 'set', 'del'] }
98
96
 
99
97
  /**
@@ -199,7 +197,7 @@ function resolveRow(ref, row) {
199
197
  * @returns {Promise<SingleResult | ListResult>}
200
198
  */
201
199
  export async function get(ref, q) {
202
- await opened(ref.handle)
200
+ if (ref.handle.opened === false) await ref.handle.ready()
203
201
  const live = ref.handle._live?.[ref.name]
204
202
  if (live) return live.get(q)
205
203
  if (ref.kind === 'handle') {
package/src/rpc/client.js CHANGED
@@ -52,6 +52,8 @@ export { t, schema }
52
52
  * @property {string} id
53
53
  * @property {string} type
54
54
  * @property {string|null} name
55
+ *
56
+ * @typedef {(Client | Handle) & Record<string, Ref>} Context A UI's root or room: the verbs take it like an in-process one.
55
57
  */
56
58
 
57
59
  const blobIdEnc = {
@@ -613,12 +615,14 @@ export async function connect(ipc, spec, opts) {
613
615
  * @param {import('streamx').Duplex} ipc Framed IPC duplex stream.
614
616
  * @param {Spec} spec Built cero spec.
615
617
  * @param {{ onerror?: (err: Error) => void }} [opts]
616
- * @returns {Promise<Client>}
618
+ * @returns {Promise<Context>}
617
619
  */
618
- export function cero(ipc, spec, opts) {
619
- return connect(ipc, spec, opts)
620
+ function start(ipc, spec, opts) {
621
+ return /** @type {Promise<Context>} */ (connect(ipc, spec, opts))
620
622
  }
621
- Object.assign(cero, remote, { connect, restore, t, schema })
623
+
624
+ /** @type {typeof start & typeof remote & { connect: typeof connect, restore: typeof restore, t: typeof t, schema: typeof schema }} */
625
+ export const cero = Object.assign(start, remote, { connect, restore, t, schema })
622
626
 
623
627
  // the same shape a local root has
624
628
  function device({ deviceId, deviceName }) {
package/types/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import { Identity } from '@cero-base/core/identity';
2
2
  import { Handle, Ref } from './handle/index.js';
3
3
  import { Local } from './local/index.js';
4
+ import * as verbs from './lib/operators.js';
5
+ import { peek } from './lib/peek.js';
6
+ import { t, schema } from './lib/spec.js';
4
7
  export { Handle, Ref, Local };
5
8
  export * from './lib/operators.js';
6
9
  export { peek } from './lib/peek.js';
@@ -120,7 +123,7 @@ export type CeroOpts = {
120
123
  * @param {CeroOpts} [opts]
121
124
  * @returns {Promise<Context>}
122
125
  */
123
- export declare function cero(dir: string, spec: import('./lib/spec.js').Spec, opts?: CeroOpts): Promise<Context>;
126
+ declare function start(dir: string, spec: import('./lib/spec.js').Spec, opts?: CeroOpts): Promise<Context>;
124
127
  /**
125
128
  * Restore a cero instance from a seed.
126
129
  *
@@ -136,3 +139,11 @@ export declare function restore(me: Context, seed: Uint8Array): Promise<Handle>;
136
139
  * @returns {Uint8Array}
137
140
  */
138
141
  export declare function toSeed(phrase: string): Uint8Array;
142
+ /** @type {typeof start & typeof verbs & { t: typeof t, schema: typeof schema, peek: typeof peek, restore: typeof restore, toSeed: typeof toSeed }} */
143
+ export declare const cero: typeof start & typeof verbs & {
144
+ t: typeof t;
145
+ schema: typeof schema;
146
+ peek: typeof peek;
147
+ restore: typeof restore;
148
+ toSeed: typeof toSeed;
149
+ };
@@ -1,6 +1,6 @@
1
1
  export type Ref = import('./refs.js').Ref;
2
2
  export type HookContext = import('@cero-base/core/database').HookContext;
3
- export type Context = import('../handle/index.js').Context;
3
+ export type Context = import('../handle/index.js').Context | import('../rpc/client.js').Context;
4
4
  export type Row = import('@cero-base/core/database').Row;
5
5
  export type SingleResult = import('@cero-base/core/database').SingleResult;
6
6
  export type ListResult = import('@cero-base/core/database').ListResult;
@@ -1,6 +1,49 @@
1
+ import { Readable } from 'streamx';
1
2
  import { RPCClient } from '@cero-base/core/rpc';
3
+ import { Ref } from '../lib/refs.js';
2
4
  import * as verbs from '../lib/operators.js';
3
5
  import { t, schema } from '../lib/spec.js';
6
+ declare const remote: {
7
+ put(ref: verbs.Ref, row: verbs.Row): Promise<verbs.SingleResult>;
8
+ set(ref: verbs.Ref, row: verbs.Row, opts?: {
9
+ upsert?: boolean;
10
+ }): Promise<verbs.SingleResult | null>;
11
+ del(ref: verbs.Ref, id?: string): Promise<void>;
12
+ call(ref: verbs.Ref, d?: verbs.Row): Promise<void>;
13
+ get(ref: verbs.Ref, q?: string | Record<string, unknown>): Promise<verbs.SingleResult | verbs.ListResult>;
14
+ watch(ref: verbs.Ref, query?: string | (Record<string, unknown> & {
15
+ changes?: boolean;
16
+ }), opts?: {
17
+ signal?: AbortSignal;
18
+ }): import('streamx').Readable;
19
+ open(ref: verbs.Ref, arg?: string | {
20
+ invite?: string;
21
+ id?: string;
22
+ name?: string;
23
+ } | undefined): Promise<verbs.Context>;
24
+ invite(ctx: verbs.Context, opts?: import("@cero-base/core").InviteOpts): Promise<string>;
25
+ revoke(ctx: verbs.Context, code: string): Promise<boolean>;
26
+ rotate(ctx: verbs.Context): Promise<{
27
+ epoch: number;
28
+ }>;
29
+ accept(ctx: verbs.Context, request: {
30
+ id: string;
31
+ }, { role }?: {
32
+ role?: string;
33
+ }): Promise<void>;
34
+ deny(ctx: verbs.Context, request: {
35
+ id: string;
36
+ }, reason?: string): Promise<void>;
37
+ leave(ctx: verbs.Context): Promise<void>;
38
+ close(ctx: verbs.Context): Promise<void>;
39
+ cancel(me: verbs.Context, code: string): Promise<boolean>;
40
+ suspend(me: verbs.Context): Promise<void>;
41
+ resume(me: verbs.Context): Promise<void>;
42
+ activate(room: verbs.Context): Promise<void>;
43
+ deactivate(room: verbs.Context): Promise<void>;
44
+ phrase(me: verbs.Context): Promise<string>;
45
+ nearby(ctx: verbs.Context, mode: boolean | string): Promise<void>;
46
+ };
4
47
  export declare const put: typeof verbs.put, set: typeof verbs.set, get: typeof verbs.get, del: typeof verbs.del, watch: typeof verbs.watch, call: typeof verbs.call, open: typeof verbs.open, invite: typeof verbs.invite, revoke: typeof verbs.revoke, rotate: typeof verbs.rotate, accept: typeof verbs.accept, deny: typeof verbs.deny, leave: typeof verbs.leave, close: typeof verbs.close, cancel: typeof verbs.cancel, suspend: typeof verbs.suspend, resume: typeof verbs.resume, activate: typeof verbs.activate, deactivate: typeof verbs.deactivate, phrase: typeof verbs.phrase, nearby: typeof verbs.nearby;
5
48
  export { t, schema };
6
49
  export type BaseRPCClient = import('@cero-base/core/rpc').RPCClient;
@@ -14,6 +57,7 @@ export type HandleStub = {
14
57
  type: string;
15
58
  name: string | null;
16
59
  };
60
+ export type Context = (Client | Handle) & Record<string, Ref>;
17
61
  /**
18
62
  * Re-initialize a `Client` from a recovery phrase. Closes the existing
19
63
  * local state and reseeds identity from the phrase.
@@ -104,6 +148,29 @@ export declare class Client extends RPCClient {
104
148
  */
105
149
  private _join;
106
150
  }
151
+ /**
152
+ * Client-side proxy for a remote handle. Exposes the same row-ops surface as `Client` but
153
+ * scoped to a single child handle id, and routes every call through the parent's RPC
154
+ * channel.
155
+ */
156
+ declare class Handle {
157
+ parent: Client;
158
+ id: string;
159
+ type: string;
160
+ name: string;
161
+ spec: import("../lib/spec.js").Spec;
162
+ store: this;
163
+ /**
164
+ * @param {Client} parent
165
+ * @param {string} id
166
+ * @param {string} type
167
+ * @param {string|null} name
168
+ */
169
+ constructor(parent: Client, id: string, type: string, name: string | null);
170
+ /** Underlying RPC channel borrowed from the parent. */
171
+ get rpc(): object;
172
+ close(): any;
173
+ }
107
174
  /**
108
175
  * Construct a `Client`, wait for `init` to complete, and return it.
109
176
  *
@@ -122,8 +189,15 @@ export declare function connect(ipc: import('streamx').Duplex, spec: Spec, opts?
122
189
  * @param {import('streamx').Duplex} ipc Framed IPC duplex stream.
123
190
  * @param {Spec} spec Built cero spec.
124
191
  * @param {{ onerror?: (err: Error) => void }} [opts]
125
- * @returns {Promise<Client>}
192
+ * @returns {Promise<Context>}
126
193
  */
127
- export declare function cero(ipc: import('streamx').Duplex, spec: Spec, opts?: {
194
+ declare function start(ipc: import('streamx').Duplex, spec: Spec, opts?: {
128
195
  onerror?: (err: Error) => void;
129
- }): Promise<Client>;
196
+ }): Promise<Context>;
197
+ /** @type {typeof start & typeof remote & { connect: typeof connect, restore: typeof restore, t: typeof t, schema: typeof schema }} */
198
+ export declare const cero: typeof start & typeof remote & {
199
+ connect: typeof connect;
200
+ restore: typeof restore;
201
+ t: typeof t;
202
+ schema: typeof schema;
203
+ };