@cero-base/cero 2.3.4 → 2.4.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.
- package/README.md +1 -1
- package/package.json +3 -2
- package/src/build/index.js +1 -7
- package/src/build/internal.js +14 -16
- package/src/build/schemas.js +13 -50
- package/src/extensions/profile-sync.js +8 -2
- package/src/handle/index.js +54 -140
- package/src/handle/join.js +7 -12
- package/src/index.js +43 -76
- package/src/lib/bluetooth.js +96 -84
- package/src/lib/operators.js +43 -36
- package/src/lib/refs.js +2 -2
- package/src/rpc/client.js +57 -50
- package/src/rpc/server.js +77 -88
- package/types/build/schemas.d.ts +8 -43
- package/types/extensions/index.d.ts +5 -3
- package/types/handle/index.d.ts +7 -63
- package/types/index.d.ts +17 -40
- package/types/lib/bluetooth.d.ts +29 -24
- package/types/lib/operators.d.ts +21 -17
- package/types/lib/refs.d.ts +2 -2
- package/types/rpc/client.d.ts +7 -3
- package/types/rpc/server.d.ts +7 -12
package/README.md
CHANGED
|
@@ -69,7 +69,7 @@ That is a working, encrypted, offline-first, peer-to-peer app. Your phrase, `awa
|
|
|
69
69
|
|
|
70
70
|
| Subpath | What it gives you |
|
|
71
71
|
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
72
|
-
| `@cero-base/cero` | `cero`, `t`, `schema`, `restore`, `
|
|
72
|
+
| `@cero-base/cero` | `cero`, `t`, `schema`, `restore`, `peek`, the 24 operators, `Handle`, `Ref`, `Local` |
|
|
73
73
|
| `@cero-base/cero/build` | `build(specDir, schema, opts)` |
|
|
74
74
|
| `@cero-base/cero/server` | `serve(ipc, spec, opts)` and `Server`, the worker that owns the data |
|
|
75
75
|
| `@cero-base/cero/client` | `cero(ipc, spec)` (also `connect`), `Client`, `restore`, `t`, `schema` and every operator but `before`, `after` and `tx`, for a UI process |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/cero",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
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,7 +99,7 @@
|
|
|
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.
|
|
102
|
+
"@cero-base/core": "^2.4.0",
|
|
103
103
|
"b4a": "^1.9.0",
|
|
104
104
|
"bare-abort-controller": "^1.1.2",
|
|
105
105
|
"bare-crypto": "^1.15.4",
|
|
@@ -116,6 +116,7 @@
|
|
|
116
116
|
"hyperdb": "^6.9.0",
|
|
117
117
|
"hyperdispatch": "^1.6.0",
|
|
118
118
|
"hyperschema": "^1.26.2",
|
|
119
|
+
"protomux": "^3.12.1",
|
|
119
120
|
"ready-resource": "^1.2.0",
|
|
120
121
|
"safety-catch": "^1.0.3",
|
|
121
122
|
"streamx": "^2.28.1",
|
package/src/build/index.js
CHANGED
|
@@ -9,7 +9,7 @@ import HRPCBuilder from 'hrpc'
|
|
|
9
9
|
|
|
10
10
|
import { CeroError } from '@cero-base/core/errors'
|
|
11
11
|
|
|
12
|
-
import { NS
|
|
12
|
+
import { NS } from '../lib/constants.js'
|
|
13
13
|
import { extensionsOf } from '../extensions/index.js'
|
|
14
14
|
import * as internal from './internal.js'
|
|
15
15
|
|
|
@@ -263,18 +263,12 @@ function emitMain(dir, ns, { types, collections, dispatches, indexes = [] }, { r
|
|
|
263
263
|
for (const desc of internal.collections(ns, 'main')) dns.collections.register(desc)
|
|
264
264
|
for (const desc of collections) dns.collections.register(desc)
|
|
265
265
|
for (const desc of indexes) dns.indexes.register(desc)
|
|
266
|
-
// after the app's: hyperdb numbers collections positionally and persists them
|
|
267
|
-
dns.collections.register({ name: REFUSALS, schema: `@${ns}/refusal`, key: ['id'] })
|
|
268
|
-
dns.collections.register({ name: SPENT, schema: `@${ns}/spent`, key: ['id'] })
|
|
269
266
|
HyperdbBuilder.toDisk(db, dbDir, { esm: true })
|
|
270
267
|
|
|
271
268
|
const d = Hyperdispatch.from(schemaDir, dispatchDir)
|
|
272
269
|
const xns = d.namespace(ns)
|
|
273
270
|
for (const desc of internal.dispatches(ns)) xns.register(desc)
|
|
274
271
|
for (const desc of dispatches) xns.register(desc)
|
|
275
|
-
// after the app's dispatches: hyperdispatch numbers routes positionally and persists them
|
|
276
|
-
xns.register({ name: 'rotate-key', requestType: `@${ns}/epoch` })
|
|
277
|
-
xns.register({ name: 'del-refusal', requestType: `@${ns}/del-by-id` })
|
|
278
272
|
Hyperdispatch.toDisk(d, dispatchDir, { esm: true })
|
|
279
273
|
|
|
280
274
|
if (rpc) {
|
package/src/build/internal.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CeroError } from '@cero-base/core/errors'
|
|
2
|
-
import { COUNTERS, EPOCHS, REMOVALS } from '../lib/constants.js'
|
|
2
|
+
import { COUNTERS, EPOCHS, REMOVALS, REFUSALS, SPENT } from '../lib/constants.js'
|
|
3
3
|
import * as schemas from './schemas.js'
|
|
4
4
|
|
|
5
5
|
// internal refs by scope; kind defaults to collection
|
|
@@ -134,6 +134,7 @@ export function declare(ref, fields) {
|
|
|
134
134
|
* @param {'main' | 'local'} scope
|
|
135
135
|
* @returns {Array<{ name: string, schema: string, key: string[] }>}
|
|
136
136
|
*/
|
|
137
|
+
// frozen once an app ships: collections number by position, so a new one goes after the app's
|
|
137
138
|
export function collections(ns, scope) {
|
|
138
139
|
const out = Object.entries(defs[scope]).map(([name, def]) => ({
|
|
139
140
|
name,
|
|
@@ -144,6 +145,8 @@ export function collections(ns, scope) {
|
|
|
144
145
|
out.push({ name: COUNTERS, schema: `@${ns}/counter`, key: ['name'] })
|
|
145
146
|
out.push({ name: EPOCHS, schema: `@${ns}/epoch`, key: ['epoch'] })
|
|
146
147
|
out.push({ name: REMOVALS, schema: `@${ns}/removal`, key: ['id'] })
|
|
148
|
+
out.push({ name: REFUSALS, schema: `@${ns}/refusal`, key: ['id'] })
|
|
149
|
+
out.push({ name: SPENT, schema: `@${ns}/spent`, key: ['id'] })
|
|
147
150
|
}
|
|
148
151
|
return out
|
|
149
152
|
}
|
|
@@ -152,18 +155,21 @@ export function collections(ns, scope) {
|
|
|
152
155
|
* @param {string} ns
|
|
153
156
|
* @returns {Array<{ name: string, requestType: string }>}
|
|
154
157
|
*/
|
|
158
|
+
// frozen once an app ships: routes number by position, so a new one goes after the app's
|
|
155
159
|
export function dispatches(ns) {
|
|
156
160
|
return [
|
|
157
161
|
{ name: 'add-writer', requestType: `@${ns}/writer` },
|
|
158
162
|
{ name: 'del-writer', requestType: `@${ns}/writer` },
|
|
159
|
-
{ name: 'claim-writer', requestType: `@${ns}/claim` },
|
|
160
163
|
...Object.values(defs.main).flatMap(({ type }) => [
|
|
161
164
|
{ name: `add-${type}`, requestType: `@${ns}/${type}` },
|
|
162
165
|
{ name: `set-${type}`, requestType: `@${ns}/${type}` },
|
|
163
166
|
{ name: `del-${type}`, requestType: `@${ns}/del-by-id` }
|
|
164
167
|
]),
|
|
165
168
|
{ name: 'join', requestType: `@${ns}/join` },
|
|
166
|
-
{ name: 'accept', requestType: `@${ns}/accept` }
|
|
169
|
+
{ name: 'accept', requestType: `@${ns}/accept` },
|
|
170
|
+
{ name: 'deny', requestType: `@${ns}/deny` },
|
|
171
|
+
{ name: 'rotate-key', requestType: `@${ns}/epoch` },
|
|
172
|
+
{ name: 'del-refusal', requestType: `@${ns}/del-by-id` }
|
|
167
173
|
]
|
|
168
174
|
}
|
|
169
175
|
|
|
@@ -171,29 +177,21 @@ export function dispatches(ns) {
|
|
|
171
177
|
const COMMANDS = [
|
|
172
178
|
['init', 'req-empty', 'res-identity'],
|
|
173
179
|
['restore', 'req-restore', 'res-identity'],
|
|
174
|
-
['
|
|
180
|
+
['errors', 'req-empty', 'res-error', true],
|
|
175
181
|
['add-row', 'req-row', 'res-data'],
|
|
176
182
|
['add-file', 'req-add-file', 'res-data'],
|
|
177
|
-
['add-handle', 'req-row', 'res-handle'],
|
|
178
183
|
['set', 'req-row', 'res-data'],
|
|
179
184
|
['get', 'req-query', 'res-rows'],
|
|
180
185
|
['del', 'req-id', 'res-ok'],
|
|
181
186
|
['watch', 'req-query', 'res-rows', true],
|
|
182
187
|
['call', 'req-call', 'res-data'],
|
|
183
|
-
['
|
|
184
|
-
['revoke', 'req-revoke', 'res-ok'],
|
|
185
|
-
['join', 'req-join', 'res-handle'],
|
|
186
|
-
['cancel', 'req-cancel', 'res-ok'],
|
|
188
|
+
['add-handle', 'req-row', 'res-handle'],
|
|
187
189
|
['open-handle', 'req-open', 'res-handle'],
|
|
190
|
+
['join', 'req-join', 'res-handle'],
|
|
188
191
|
['close-handle', 'req-handle', 'res-ok'],
|
|
192
|
+
// not a verb: it ends the handle on both sides
|
|
189
193
|
['leave', 'req-handle', 'res-ok'],
|
|
190
|
-
['
|
|
191
|
-
['set-active', 'req-set-active', 'res-ok'],
|
|
192
|
-
['suspend', 'req-handle', 'res-ok'],
|
|
193
|
-
['resume', 'req-handle', 'res-ok'],
|
|
194
|
-
['errors', 'req-empty', 'res-error', true],
|
|
195
|
-
['answer', 'req-answer', 'res-ok'],
|
|
196
|
-
['nearby', 'req-nearby', 'res-ok']
|
|
194
|
+
['verb', 'req-verb', 'res-data']
|
|
197
195
|
]
|
|
198
196
|
|
|
199
197
|
/**
|
package/src/build/schemas.js
CHANGED
|
@@ -70,12 +70,6 @@ export const main = {
|
|
|
70
70
|
// the file this one was copied from, so a sync copies it once
|
|
71
71
|
from: string
|
|
72
72
|
},
|
|
73
|
-
claim: {
|
|
74
|
-
identity: required(bytes),
|
|
75
|
-
writer: required(bytes),
|
|
76
|
-
sig: required(bytes),
|
|
77
|
-
ts: int
|
|
78
|
-
},
|
|
79
73
|
epoch: {
|
|
80
74
|
epoch: required(uint),
|
|
81
75
|
wrapped: required(bytes),
|
|
@@ -135,7 +129,7 @@ export const main = {
|
|
|
135
129
|
isMobile: bool,
|
|
136
130
|
device: string
|
|
137
131
|
},
|
|
138
|
-
// a join the
|
|
132
|
+
// a join the handle turned away, until the joiner read why. `id` is the joiner's writer
|
|
139
133
|
refusal: {
|
|
140
134
|
id: required(string),
|
|
141
135
|
reply: required(bytes),
|
|
@@ -147,6 +141,10 @@ export const main = {
|
|
|
147
141
|
id: required(string),
|
|
148
142
|
reason: required(string),
|
|
149
143
|
expires: int
|
|
144
|
+
},
|
|
145
|
+
deny: {
|
|
146
|
+
id: required(string),
|
|
147
|
+
reason: string
|
|
150
148
|
}
|
|
151
149
|
}
|
|
152
150
|
|
|
@@ -166,7 +164,7 @@ export const local = {
|
|
|
166
164
|
secretKey: required(bytes),
|
|
167
165
|
encryptionKey: bytes
|
|
168
166
|
},
|
|
169
|
-
// a
|
|
167
|
+
// a handle with invites, reopened at boot so its joins are answered
|
|
170
168
|
serving: {
|
|
171
169
|
id: required(string),
|
|
172
170
|
type: required(string)
|
|
@@ -179,7 +177,7 @@ export const local = {
|
|
|
179
177
|
invite: required(string),
|
|
180
178
|
publicKey: required(bytes),
|
|
181
179
|
secretKey: required(bytes),
|
|
182
|
-
// the reply, once it landed: the join then opens the
|
|
180
|
+
// the reply, once it landed: the join then opens the handle without joining again
|
|
183
181
|
key: bytes,
|
|
184
182
|
encryptionKey: bytes,
|
|
185
183
|
epochs: bytes
|
|
@@ -230,38 +228,25 @@ export const rpc = {
|
|
|
230
228
|
op: required(string),
|
|
231
229
|
data: bytes
|
|
232
230
|
},
|
|
233
|
-
|
|
231
|
+
// an operator by name, run on the handle as in the worker: the args and the result are c.any
|
|
232
|
+
'req-verb': {
|
|
234
233
|
handle: required(string),
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
ttl: string,
|
|
238
|
-
reuse: bool,
|
|
239
|
-
data: bytes,
|
|
240
|
-
confirm: bool
|
|
241
|
-
},
|
|
242
|
-
'req-revoke': {
|
|
243
|
-
handle: required(string),
|
|
244
|
-
invite: required(string)
|
|
234
|
+
verb: required(string),
|
|
235
|
+
args: required(bytes)
|
|
245
236
|
},
|
|
246
237
|
'req-join': {
|
|
247
238
|
parent: required(string),
|
|
248
239
|
ref: required(string),
|
|
249
240
|
invite: required(string)
|
|
250
241
|
},
|
|
251
|
-
'req-cancel': {
|
|
252
|
-
invite: required(string)
|
|
253
|
-
},
|
|
254
242
|
'req-open': {
|
|
255
243
|
parent: required(string),
|
|
256
|
-
row: required(string)
|
|
244
|
+
row: required(string),
|
|
245
|
+
type: string
|
|
257
246
|
},
|
|
258
247
|
'req-handle': {
|
|
259
248
|
handle: required(string)
|
|
260
249
|
},
|
|
261
|
-
'req-set-active': {
|
|
262
|
-
handle: required(string),
|
|
263
|
-
active: bool
|
|
264
|
-
},
|
|
265
250
|
'res-data': {
|
|
266
251
|
data: bytes
|
|
267
252
|
},
|
|
@@ -270,9 +255,6 @@ export const rpc = {
|
|
|
270
255
|
total: required(int),
|
|
271
256
|
size: required(int)
|
|
272
257
|
},
|
|
273
|
-
'res-invite': {
|
|
274
|
-
invite: required(string)
|
|
275
|
-
},
|
|
276
258
|
'res-handle': {
|
|
277
259
|
id: required(string),
|
|
278
260
|
type: required(string),
|
|
@@ -294,32 +276,13 @@ export const rpc = {
|
|
|
294
276
|
fileToken: string,
|
|
295
277
|
deviceName: string
|
|
296
278
|
},
|
|
297
|
-
'res-seed': {
|
|
298
|
-
phrase: string
|
|
299
|
-
},
|
|
300
279
|
'res-ok': {
|
|
301
280
|
ok: bool
|
|
302
281
|
},
|
|
303
|
-
'res-epoch': {
|
|
304
|
-
epoch: required(uint)
|
|
305
|
-
},
|
|
306
282
|
'res-error': {
|
|
307
283
|
message: required(string),
|
|
308
284
|
code: string,
|
|
309
285
|
stack: string,
|
|
310
286
|
reason: string
|
|
311
|
-
},
|
|
312
|
-
// accept, or deny with a reason
|
|
313
|
-
'req-answer': {
|
|
314
|
-
handle: required(string),
|
|
315
|
-
id: required(string),
|
|
316
|
-
accept: bool,
|
|
317
|
-
role: string,
|
|
318
|
-
reason: string
|
|
319
|
-
},
|
|
320
|
-
// the mesh when `on`, one invite's rendezvous when `invite` is set
|
|
321
|
-
'req-nearby': {
|
|
322
|
-
on: bool,
|
|
323
|
-
invite: string
|
|
324
287
|
}
|
|
325
288
|
}
|
|
@@ -16,8 +16,14 @@ export function profileSync({ fields = { avatar: t.string } } = {}) {
|
|
|
16
16
|
members: t.extend(fields)
|
|
17
17
|
},
|
|
18
18
|
setup(me) {
|
|
19
|
-
const publish = (child, profile) =>
|
|
20
|
-
|
|
19
|
+
const publish = async (child, profile) => {
|
|
20
|
+
try {
|
|
21
|
+
await mirror(me.profile, profile, fields, child.members, me.identity.id)
|
|
22
|
+
} catch (err) {
|
|
23
|
+
// a handle that closed meanwhile has no member row left to update
|
|
24
|
+
if (!child.closing) throw err
|
|
25
|
+
}
|
|
26
|
+
}
|
|
21
27
|
|
|
22
28
|
const onHandle = async (child) => {
|
|
23
29
|
const { data: profile } = await get(me.profile)
|