@cero-base/cero 2.4.1 → 2.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.
@@ -49,6 +49,13 @@ export declare const cero: {
49
49
  resume(me: operators.Context): Promise<void>;
50
50
  activate(ctx: operators.Context): Promise<void>;
51
51
  deactivate(ctx: operators.Context): Promise<void>;
52
+ receive(me: operators.Context, payload: Uint8Array): Promise<{
53
+ id: string;
54
+ type: string;
55
+ name: string | null;
56
+ newer: boolean;
57
+ ref: string | null;
58
+ } | null>;
52
59
  phrase(me: operators.Context): Promise<string>;
53
60
  nearby(ctx: operators.Context, on: boolean, opts?: {
54
61
  topic?: string;
@@ -64,12 +71,17 @@ export declare const cero: {
64
71
  fixed64: import("@cero-base/core").Prim;
65
72
  file: import("@cero-base/core").Prim;
66
73
  required: (marker: import("@cero-base/core").Prim) => import("@cero-base/core").Prim;
67
- single(fields: Record<string, import("@cero-base/core").Prim>): import("@cero-base/core").TypeDef;
74
+ single(fields: Record<string, import("@cero-base/core").Prim>, opts?: {
75
+ push?: boolean;
76
+ }): import("@cero-base/core").TypeDef;
68
77
  collection(fields: Record<string, import("@cero-base/core").Prim>, opts?: {
69
78
  indexes?: Record<string, string[]>;
70
79
  own?: boolean;
80
+ push?: boolean;
81
+ }): import("@cero-base/core").TypeDef;
82
+ action(fields: Record<string, import("@cero-base/core").Prim>, opts?: {
83
+ push?: boolean;
71
84
  }): import("@cero-base/core").TypeDef;
72
- action(fields: Record<string, import("@cero-base/core").Prim>): import("@cero-base/core").TypeDef;
73
85
  extend(fields: Record<string, import("@cero-base/core").Prim>): {
74
86
  kind: 'extend';
75
87
  fields: Record<string, import("@cero-base/core").Prim>;
@@ -209,6 +209,7 @@ export declare class Handle extends ReadyResource {
209
209
  _live;
210
210
  /** @private */
211
211
  _ac;
212
+ _indexing: any;
212
213
  /** @param {HandleOpts} [opts] */
213
214
  constructor(opts?: HandleOpts);
214
215
  /**
@@ -279,8 +280,6 @@ export declare class Handle extends ReadyResource {
279
280
  /** @private */
280
281
  private _tx;
281
282
  /** @private */
282
- private _pairing;
283
- /** @private */
284
283
  private _leave;
285
284
  /** @private */
286
285
  private _suspend;
@@ -307,7 +306,15 @@ export declare class Handle extends ReadyResource {
307
306
  /** @private */
308
307
  private _joins;
309
308
  /** @private */
310
- private _onJoins;
309
+ private _onLocal;
310
+ /** @private */
311
+ private _topics;
312
+ /** @private */
313
+ private _index;
314
+ /** @private */
315
+ private _reindex;
316
+ /** @private */
317
+ private _receive;
311
318
  /** @private */
312
319
  private _phrase;
313
320
  /**
@@ -407,6 +414,12 @@ export declare class Handle extends ReadyResource {
407
414
  /** @private */
408
415
  private _adopt;
409
416
  /** @private */
417
+ private _prune;
418
+ /** @private */
419
+ private _pruneAll;
420
+ /** @private */
421
+ private _carry;
422
+ /** @private */
410
423
  private _hookType;
411
424
  /** @private */
412
425
  private _serve;
package/types/index.d.ts CHANGED
@@ -13,6 +13,14 @@ export type CeroOpts = {
13
13
  * The identity's BIP-39 phrase, as `cero.phrase(me)` gives it: on a new `dir` it recovers that identity.
14
14
  */
15
15
  phrase?: string;
16
+ /**
17
+ * A device invite, as `cero.invite(me)` gives it on another device of yours: on a new `dir` this device joins that identity once a device of it accepts, with no phrase.
18
+ */
19
+ invite?: string;
20
+ /**
21
+ * With `invite`: called with the request's id as it is sent, the same id `me.requests` shows on the accepting device.
22
+ */
23
+ onrequest?: (id: string) => void;
16
24
  /**
17
25
  * Mnemonic length when generating a fresh identity.
18
26
  */
@@ -86,6 +94,8 @@ export type CeroOpts = {
86
94
  /**
87
95
  * @typedef {object} CeroOpts
88
96
  * @property {string} [phrase] The identity's BIP-39 phrase, as `cero.phrase(me)` gives it: on a new `dir` it recovers that identity.
97
+ * @property {string} [invite] A device invite, as `cero.invite(me)` gives it on another device of yours: on a new `dir` this device joins that identity once a device of it accepts, with no phrase.
98
+ * @property {(id: string) => void} [onrequest] With `invite`: called with the request's id as it is sent, the same id `me.requests` shows on the accepting device.
89
99
  * @property {12 | 24} [words] Mnemonic length when generating a fresh identity.
90
100
  * @property {string | null} [name] Friendly device name persisted on the identity claim.
91
101
  * @property {boolean} [isMobile] Marks this device as mobile.
@@ -50,6 +50,12 @@ export declare function call(ref: Ref, d?: Row): Promise<void>;
50
50
  * opens; register in an extension's `setup` to see every op the root applies too. The imported
51
51
  * operators throw inside a hook; use the ones on `ctx`. Not available over RPC.
52
52
  *
53
+ * On `members`, removing someone or changing her rank is the rank's call by default: the writer
54
+ * outranks whom she acts on, and `ctx.ranked` says whether she does. A rule there may return
55
+ * `'grant'` to allow what rank alone refuses (a bare `true` grants nothing), or `false` to refuse
56
+ * what it allows, and only a writer's grant counts. It is decided before anything moves. The
57
+ * last owner is never removed nor demoted.
58
+ *
53
59
  * @param {Ref} ref
54
60
  * @param {(ctx: HookContext) => unknown} fn
55
61
  * @param {{ signal?: AbortSignal }} [opts]
@@ -223,6 +229,21 @@ export declare function activate(ctx: Context): Promise<void>;
223
229
  * @returns {Promise<void>}
224
230
  */
225
231
  export declare function deactivate(ctx: Context): Promise<void>;
232
+ /**
233
+ * A push the platform delivered, as bytes: the handle it is about and the ref the write was on,
234
+ * verified against this device's own data, or `null` for a forged one or one about no handle here.
235
+ *
236
+ * @param {Context} me
237
+ * @param {Uint8Array} payload
238
+ * @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null } | null>}
239
+ */
240
+ export declare function receive(me: Context, payload: Uint8Array): Promise<{
241
+ id: string;
242
+ type: string;
243
+ name: string | null;
244
+ newer: boolean;
245
+ ref: string | null;
246
+ } | null>;
226
247
  /**
227
248
  * Reveal the recovery phrase.
228
249
  *
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Quickly check whether the on-disk directory at `dir` already holds an initialised cero
3
- * identity (i.e. a stored master seed).
3
+ * identity (i.e. a stored device key with its proof).
4
4
  *
5
5
  * @param {string} dir Cero data directory.
6
6
  * @param {import('./spec.js').Spec} spec Built spec, the value passed to `cero(dir, spec)`.
7
- * @returns {Promise<boolean>} `true` if a master seed exists on disk.
7
+ * @returns {Promise<boolean>} `true` if a device of an identity exists on disk.
8
8
  */
9
9
  export declare function peek(dir: string, spec: import('./spec.js').Spec): Promise<boolean>;
@@ -31,6 +31,10 @@ export type RefInfo = {
31
31
  */
32
32
  files?: string[];
33
33
  verb?: string;
34
+ /**
35
+ * Whether a write to this ref pushes a notification.
36
+ */
37
+ push?: boolean;
34
38
  };
35
39
  export type SpecMeta = {
36
40
  ns?: string;
@@ -70,6 +74,7 @@ export type Row = import('@cero-base/core/database').Row;
70
74
  * @property {Record<string, string>} [required] The required fields and their types.
71
75
  * @property {string[]} [files] The fields holding file ids.
72
76
  * @property {string} [verb]
77
+ * @property {boolean} [push] Whether a write to this ref pushes a notification.
73
78
  *
74
79
  * @typedef {object} SpecMeta
75
80
  * @property {string} [ns]
@@ -0,0 +1,12 @@
1
+ /**
2
+ * `@react-native-firebase/messaging` as the messaging service `connect` takes for push: pass `messaging()`. Pushes to the app in
3
+ * front; the push worklet answers the ones the OS delivers while it is not.
4
+ *
5
+ * @param {{ subscribeToTopic: (topic: string) => Promise<void>, unsubscribeFromTopic: (topic: string) => Promise<void>, onMessage: (fn: (message: object) => void) => () => void }} messaging
6
+ * @returns {import('../rpc/push.js').Messaging}
7
+ */
8
+ export declare function firebase(messaging: {
9
+ subscribeToTopic: (topic: string) => Promise<void>;
10
+ unsubscribeFromTopic: (topic: string) => Promise<void>;
11
+ onMessage: (fn: (message: object) => void) => () => void;
12
+ }): import('../rpc/push.js').Messaging;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * A push the platform delivered while the app is dead, verified from its storage opened
3
+ * read-only. `null` while the live app holds the storage, which shows the activity itself, and
4
+ * for a push that is forged or about no handle here.
5
+ *
6
+ * @param {string} dir The directory `cero()` opened.
7
+ * @param {import('../lib/spec.js').Spec} spec
8
+ * @param {Uint8Array} payload
9
+ * @param {{ storageKey?: Uint8Array }} [opts] The key `cero()` encrypts the local store with, if any.
10
+ * @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null } | null>}
11
+ */
12
+ export declare function receive(dir: string, spec: import('../lib/spec.js').Spec, payload: Uint8Array, { storageKey }?: {
13
+ storageKey?: Uint8Array;
14
+ }): Promise<{
15
+ id: string;
16
+ type: string;
17
+ name: string | null;
18
+ newer: boolean;
19
+ ref: string | null;
20
+ } | null>;
21
+ /**
22
+ * The bare-kit push entry: on each push the OS hands this worklet, verify it and, for one about
23
+ * new data on a handle this device holds, answer with what `show` returns: the notification,
24
+ * `{ title, body }`, or nothing. The request carries `storage`, `payload` (base64) and, when the
25
+ * app encrypts its local store, `storageKey` (base64).
26
+ *
27
+ * @param {import('../lib/spec.js').Spec} spec
28
+ * @param {(push: { id: string, type: string, name: string | null, newer: boolean, ref: string | null }) => { title: string, body: string } | null | undefined | Promise<{ title: string, body: string } | null | undefined>} show What to show for a verified push, as `cero.receive` would give it to the app.
29
+ * @returns {void}
30
+ */
31
+ export declare function push(spec: import('../lib/spec.js').Spec, show: (push: {
32
+ id: string;
33
+ type: string;
34
+ name: string | null;
35
+ newer: boolean;
36
+ ref: string | null;
37
+ }) => {
38
+ title: string;
39
+ body: string;
40
+ } | null | undefined | Promise<{
41
+ title: string;
42
+ body: string;
43
+ } | null | undefined>): void;
@@ -41,6 +41,13 @@ declare const remote: {
41
41
  resume(me: verbs.Context): Promise<void>;
42
42
  activate(ctx: verbs.Context): Promise<void>;
43
43
  deactivate(ctx: verbs.Context): Promise<void>;
44
+ receive(me: verbs.Context, payload: Uint8Array): Promise<{
45
+ id: string;
46
+ type: string;
47
+ name: string | null;
48
+ newer: boolean;
49
+ ref: string | null;
50
+ } | null>;
44
51
  phrase(me: verbs.Context): Promise<string>;
45
52
  nearby(ctx: verbs.Context, on: boolean, opts?: {
46
53
  topic?: string;
@@ -95,6 +102,8 @@ declare class LocalRefs {
95
102
  * across the wire.
96
103
  */
97
104
  export declare class Client extends RPCClient {
105
+ /** @private */
106
+ _push;
98
107
  /** @private */
99
108
  _onerror;
100
109
  /** @type {string | null} */
@@ -106,6 +115,7 @@ export declare class Client extends RPCClient {
106
115
  } | null;
107
116
  store: this;
108
117
  local: LocalRefs;
118
+ _unfollow: () => void;
109
119
  /** @private */
110
120
  _fileBase;
111
121
  /** @private */
@@ -113,16 +123,19 @@ export declare class Client extends RPCClient {
113
123
  /**
114
124
  * @param {import('streamx').Duplex} ipc Framed IPC stream (must be writable).
115
125
  * @param {Spec} spec Compiled cero spec (schema + rpc + handles).
116
- * @param {{ onerror?: (err: Error) => void }} [opts] Where the worker's background errors go; the console without one.
126
+ * @param {{ onerror?: (err: Error) => void, push?: import('./push.js').Messaging }} [opts] Where the worker's background errors go, the console without one; `push`: the platform's messaging service, to follow the handles' push topics.
117
127
  */
118
- constructor(ipc: import('streamx').Duplex, spec: Spec, { onerror }?: {
128
+ constructor(ipc: import('streamx').Duplex, spec: Spec, { onerror, push }?: {
119
129
  onerror?: (err: Error) => void;
130
+ push?: import('./push.js').Messaging;
120
131
  });
121
132
  /** @private */
122
133
  private _pumpErrors;
123
134
  /** @private */
124
135
  private _open;
125
136
  /** @private */
137
+ private _close;
138
+ /** @private */
126
139
  private _identify;
127
140
  /**
128
141
  * Create a new child handle of the given type.
@@ -180,11 +193,12 @@ declare class Handle {
180
193
  *
181
194
  * @param {import('streamx').Duplex} ipc
182
195
  * @param {Spec} spec
183
- * @param {{ onerror?: (err: Error) => void }} [opts]
196
+ * @param {{ onerror?: (err: Error) => void, push?: import('./push.js').Messaging }} [opts]
184
197
  * @returns {Promise<Client>}
185
198
  */
186
199
  export declare function connect(ipc: import('streamx').Duplex, spec: Spec, opts?: {
187
200
  onerror?: (err: Error) => void;
201
+ push?: import('./push.js').Messaging;
188
202
  }): Promise<Client>;
189
203
  /**
190
204
  * Symmetric client entry. Mirrors the main `cero`, but `cero(ipc, spec)` connects to a
@@ -192,11 +206,12 @@ export declare function connect(ipc: import('streamx').Duplex, spec: Spec, opts?
192
206
  *
193
207
  * @param {import('streamx').Duplex} ipc Framed IPC duplex stream.
194
208
  * @param {Spec} spec Built cero spec.
195
- * @param {{ onerror?: (err: Error) => void }} [opts]
209
+ * @param {{ onerror?: (err: Error) => void, push?: import('./push.js').Messaging }} [opts]
196
210
  * @returns {Promise<Context>}
197
211
  */
198
212
  declare function start(ipc: import('streamx').Duplex, spec: Spec, opts?: {
199
213
  onerror?: (err: Error) => void;
214
+ push?: import('./push.js').Messaging;
200
215
  }): Promise<Context>;
201
216
  /** @type {typeof start & typeof remote & { connect: typeof connect, restore: typeof restore, t: typeof t, schema: typeof schema }} */
202
217
  export declare const cero: typeof start & typeof remote & {
@@ -0,0 +1,20 @@
1
+ export type Messaging = {
2
+ subscribe: (topic: string) => Promise<void>;
3
+ unsubscribe: (topic: string) => Promise<void>;
4
+ on: (event: 'push', fn: (payload: Uint8Array) => void) => (() => void) | void;
5
+ };
6
+ /**
7
+ * @typedef {object} Messaging The platform's messaging service, as `connect` takes it for push.
8
+ * @property {(topic: string) => Promise<void>} subscribe
9
+ * @property {(topic: string) => Promise<void>} unsubscribe
10
+ * @property {(event: 'push', fn: (payload: Uint8Array) => void) => (() => void) | void} on
11
+ */
12
+ /**
13
+ * Keep the device on the push topics of its handles, one subscription per handle, dropped when it
14
+ * leaves; and act on a push: the handle it is about ranks first on the swarm.
15
+ *
16
+ * @param {import('./client.js').Client} me
17
+ * @param {Messaging} messaging
18
+ * @returns {() => void}
19
+ */
20
+ export declare function follow(me: import('./client.js').Client, messaging: Messaging): () => void;