@cero-base/cero 2.2.0 → 2.3.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 +24 -16
- package/package.json +7 -6
- package/src/build/index.js +38 -34
- package/src/build/internal.js +96 -11
- package/src/build/schemas.js +119 -19
- package/src/extensions/handle-sync.js +8 -12
- package/src/extensions/index.js +17 -63
- package/src/extensions/mirror.js +44 -0
- package/src/extensions/profile-sync.js +10 -12
- package/src/handle/index.js +499 -374
- package/src/handle/join.js +144 -0
- package/src/index.js +89 -118
- package/src/lib/bluetooth.js +83 -19
- package/src/lib/constants.js +1 -0
- package/src/lib/operators.js +298 -128
- package/src/lib/peek.js +1 -1
- package/src/lib/refs.js +7 -7
- package/src/lib/spec.js +32 -0
- package/src/local/index.js +27 -3
- package/src/rpc/client.js +240 -202
- package/src/rpc/server.js +186 -133
- package/types/build/index.d.ts +3 -8
- package/types/build/internal.d.ts +86 -27
- package/types/build/schemas.d.ts +91 -12
- package/types/extensions/handle-sync.d.ts +4 -12
- package/types/extensions/index.d.ts +59 -59
- package/types/extensions/mirror.d.ts +13 -0
- package/types/extensions/profile-sync.d.ts +4 -13
- package/types/handle/index.d.ts +229 -198
- package/types/handle/join.d.ts +63 -0
- package/types/index.d.ts +27 -55
- package/types/lib/bluetooth.d.ts +57 -19
- package/types/lib/constants.d.ts +1 -0
- package/types/lib/operators.d.ts +166 -81
- package/types/lib/peek.d.ts +2 -2
- package/types/lib/refs.d.ts +14 -15
- package/types/lib/spec.d.ts +87 -0
- package/types/local/index.d.ts +19 -9
- package/types/rpc/client.d.ts +55 -112
- package/types/rpc/server.d.ts +91 -50
- package/src/rpc/index.js +0 -8
- package/types/rpc/index.d.ts +0 -7
package/src/lib/operators.js
CHANGED
|
@@ -1,31 +1,29 @@
|
|
|
1
1
|
import { Readable } from 'streamx'
|
|
2
|
-
import b4a from 'b4a'
|
|
3
2
|
|
|
4
3
|
import { encodeId, decodeId } from '@cero-base/core/blobs/codec'
|
|
5
|
-
import {
|
|
6
|
-
import { onAbort } from '@cero-base/core/utils'
|
|
4
|
+
import { onAbort, subscribe } from '@cero-base/core/utils'
|
|
7
5
|
|
|
8
6
|
/**
|
|
9
7
|
* @typedef {import('./refs.js').Ref} Ref
|
|
10
8
|
* @typedef {import('@cero-base/core/database').HookContext} HookContext
|
|
11
|
-
* @typedef {import('../handle/index.js').
|
|
12
|
-
* @typedef {
|
|
13
|
-
* @typedef {
|
|
14
|
-
* @typedef {
|
|
9
|
+
* @typedef {import('../handle/index.js').Context} Context
|
|
10
|
+
* @typedef {import('@cero-base/core/database').Row} Row
|
|
11
|
+
* @typedef {import('@cero-base/core/database').SingleResult} SingleResult
|
|
12
|
+
* @typedef {import('@cero-base/core/database').ListResult} ListResult
|
|
15
13
|
*/
|
|
16
14
|
|
|
17
15
|
/**
|
|
18
16
|
* Resolve a durable file id (+ optional name) to the read shape returned
|
|
19
17
|
* everywhere: `{ id, name?, type, size, url }`.
|
|
20
18
|
*
|
|
21
|
-
* @param {
|
|
19
|
+
* @param {Context} handle
|
|
22
20
|
* @param {string} id
|
|
23
21
|
* @param {string} [name]
|
|
24
22
|
* @returns {{ id: string, type: string, size: number, url: string, name?: string }}
|
|
25
23
|
*/
|
|
26
|
-
|
|
24
|
+
function resolveFile(handle, id, name) {
|
|
27
25
|
const { type, blobId } = decodeId(id)
|
|
28
|
-
const file = { id, type, size: blobId.byteLength, url: handle.
|
|
26
|
+
const file = { id, type, size: blobId.byteLength, url: handle._link(id) }
|
|
29
27
|
if (name != null) file.name = name
|
|
30
28
|
return file
|
|
31
29
|
}
|
|
@@ -34,22 +32,23 @@ export function resolveFile(handle, id, name) {
|
|
|
34
32
|
* Insert (or overwrite by id) a row on `ref`.
|
|
35
33
|
*
|
|
36
34
|
* @param {Ref} ref
|
|
37
|
-
* @param {
|
|
35
|
+
* @param {Row} row
|
|
38
36
|
* @returns {Promise<SingleResult>}
|
|
39
37
|
*/
|
|
40
|
-
export function put(ref, row) {
|
|
38
|
+
export async function put(ref, row) {
|
|
39
|
+
await opened(ref.handle)
|
|
41
40
|
return ref.name === 'files' ? putFile(ref, row) : ref.handle.store.put(ref.name, row)
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
async function putFile(ref, row) {
|
|
45
44
|
const handle = ref.handle
|
|
46
45
|
if (handle.rpc) return handle.put('files', row)
|
|
47
|
-
const { data, type, name = null } = row
|
|
46
|
+
const { data, type, name = null, from = null } = row
|
|
48
47
|
const blobs = handle.blobs // captured once — the instance carries its epoch stamp
|
|
49
48
|
await blobs.ready()
|
|
50
49
|
const blobId = await blobs.put(data)
|
|
51
50
|
const id = encodeId(blobs.key, blobId, type)
|
|
52
|
-
await handle.store.call('add-file', { id, name, stamp: blobs.stamp || 0 })
|
|
51
|
+
await handle.store.call('add-file', { id, name, stamp: blobs.stamp || 0, from })
|
|
53
52
|
return { data: resolveFile(handle, id, name) }
|
|
54
53
|
}
|
|
55
54
|
|
|
@@ -57,11 +56,12 @@ async function putFile(ref, row) {
|
|
|
57
56
|
* Upsert a row on `ref` — merges with the existing row and preserves `createdAt`.
|
|
58
57
|
*
|
|
59
58
|
* @param {Ref} ref
|
|
60
|
-
* @param {
|
|
59
|
+
* @param {Row} row
|
|
61
60
|
* @param {{ upsert?: boolean }} [opts]
|
|
62
|
-
* @returns {Promise<SingleResult>}
|
|
61
|
+
* @returns {Promise<SingleResult | null>}
|
|
63
62
|
*/
|
|
64
|
-
export function set(ref, row, opts) {
|
|
63
|
+
export async function set(ref, row, opts) {
|
|
64
|
+
await opened(ref.handle)
|
|
65
65
|
return ref.handle.store.set(ref.name, row, opts)
|
|
66
66
|
}
|
|
67
67
|
|
|
@@ -72,7 +72,8 @@ export function set(ref, row, opts) {
|
|
|
72
72
|
* @param {string} [id]
|
|
73
73
|
* @returns {Promise<void>}
|
|
74
74
|
*/
|
|
75
|
-
export function del(ref, id) {
|
|
75
|
+
export async function del(ref, id) {
|
|
76
|
+
await opened(ref.handle)
|
|
76
77
|
return ref.handle.store.del(ref.name, id)
|
|
77
78
|
}
|
|
78
79
|
|
|
@@ -80,24 +81,31 @@ export function del(ref, id) {
|
|
|
80
81
|
* Invoke an `action`-kind ref (a custom mutation declared in the schema).
|
|
81
82
|
*
|
|
82
83
|
* @param {Ref} ref
|
|
83
|
-
* @param {
|
|
84
|
-
* @returns {Promise<
|
|
84
|
+
* @param {Row} [d]
|
|
85
|
+
* @returns {Promise<void>}
|
|
85
86
|
*/
|
|
86
|
-
export function call(ref, d) {
|
|
87
|
+
export async function call(ref, d) {
|
|
88
|
+
await opened(ref.handle)
|
|
87
89
|
return ref.handle.store.call(ref.name, d)
|
|
88
90
|
}
|
|
89
91
|
|
|
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
|
+
|
|
90
97
|
const WRITES = { single: ['set', 'del'], collection: ['put', 'set', 'del'] }
|
|
91
98
|
|
|
92
99
|
/**
|
|
93
|
-
* Rule that runs before a write to `ref` lands
|
|
94
|
-
* transaction. Return `false` to refuse it: the writer's own call rejects
|
|
95
|
-
* `ctx` is `{ op, name, row, existing, id, memberId, role, get, put, set, del }`;
|
|
96
|
-
* `ctx.row` to rewrite what is stored. `op` is the op as it applies, so an upsert on a
|
|
97
|
-
* collection is a `put`. The four operators on `ctx` read and write the room as it stands at
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
100
|
+
* Rule that runs before a write to `ref` lands, or before an action does: at apply, on every
|
|
101
|
+
* peer, inside the op's transaction. Return `false` to refuse it: the writer's own call rejects
|
|
102
|
+
* with `REFUSED`. `ctx` is `{ op, name, row, existing, id, memberId, role, get, put, set, del }`;
|
|
103
|
+
* mutate `ctx.row` to rewrite what is stored. `op` is the op as it applies, so an upsert on a
|
|
104
|
+
* collection is a `put`. The four operators on `ctx` read and write the room as it stands at this
|
|
105
|
+
* op, inside the transaction. Must be deterministic: read only `ctx`, never a clock or local
|
|
106
|
+
* state. On a type ref (`me.room.notes`) it reaches every room of the type before the room
|
|
107
|
+
* opens; register in an extension's `setup` to see every op the root applies too. The imported
|
|
108
|
+
* operators throw inside a hook; use the ones on `ctx`. Not available over RPC.
|
|
101
109
|
*
|
|
102
110
|
* @param {Ref} ref
|
|
103
111
|
* @param {(ctx: HookContext) => unknown} fn
|
|
@@ -105,26 +113,14 @@ const WRITES = { single: ['set', 'del'], collection: ['put', 'set', 'del'] }
|
|
|
105
113
|
* @returns {() => void}
|
|
106
114
|
*/
|
|
107
115
|
export function before(ref, fn, opts) {
|
|
108
|
-
|
|
109
|
-
const db = ref.handle.store
|
|
110
|
-
const ops = WRITES[ref.kind] || ['set']
|
|
111
|
-
const offs = ops.map((op) =>
|
|
112
|
-
db.before(op, (ctx) => (ctx.name === ref.name ? fn(ctx) : undefined))
|
|
113
|
-
)
|
|
114
|
-
let stopAbort
|
|
115
|
-
const off = () => {
|
|
116
|
-
offs.forEach((unsub) => unsub())
|
|
117
|
-
stopAbort?.()
|
|
118
|
-
}
|
|
119
|
-
stopAbort = onAbort(opts?.signal, off)
|
|
120
|
-
return off
|
|
116
|
+
return hook('before', ref, fn, opts)
|
|
121
117
|
}
|
|
122
118
|
|
|
123
119
|
/**
|
|
124
|
-
* Rule that runs after a write to `ref` lands
|
|
125
|
-
* transaction. Write derived rows through `ctx.put` /
|
|
126
|
-
* the whole op. Same `ctx
|
|
127
|
-
* `
|
|
120
|
+
* Rule that runs after a write to `ref` lands, at apply, on every peer, inside the op's
|
|
121
|
+
* transaction. On an action it is what the action does. Write derived rows through `ctx.put` /
|
|
122
|
+
* `ctx.set` / `ctx.del`; a throw refuses the whole op. Same `ctx`, determinism and reach as
|
|
123
|
+
* `before`. Use `watch(ref)` instead to observe writes locally.
|
|
128
124
|
*
|
|
129
125
|
* @param {Ref} ref
|
|
130
126
|
* @param {(ctx: HookContext) => unknown} fn
|
|
@@ -132,10 +128,21 @@ export function before(ref, fn, opts) {
|
|
|
132
128
|
* @returns {() => void}
|
|
133
129
|
*/
|
|
134
130
|
export function after(ref, fn, opts) {
|
|
135
|
-
|
|
131
|
+
return hook('after', ref, fn, opts)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// a write hook runs on each write op and keeps to its ref; an action is an op of its own
|
|
135
|
+
function hook(phase, ref, fn, opts) {
|
|
136
|
+
if (ref.type) {
|
|
137
|
+
return ref.handle._hookType(ref, (room) => hook(phase, room[ref.name], fn), opts)
|
|
138
|
+
}
|
|
136
139
|
const db = ref.handle.store
|
|
137
|
-
const
|
|
138
|
-
|
|
140
|
+
const offs =
|
|
141
|
+
ref.kind === 'action'
|
|
142
|
+
? [db[phase](ref.name, fn)]
|
|
143
|
+
: (WRITES[ref.kind] || ['set']).map((op) =>
|
|
144
|
+
db[phase](op, (ctx) => (ctx.name === ref.name ? fn(ctx) : undefined))
|
|
145
|
+
)
|
|
139
146
|
let stopAbort
|
|
140
147
|
const off = () => {
|
|
141
148
|
offs.forEach((unsub) => unsub())
|
|
@@ -149,7 +156,8 @@ export function after(ref, fn, opts) {
|
|
|
149
156
|
const parentStore = (ref) => (ref.handle.root ? ref.handle.root.store : ref.handle.store)
|
|
150
157
|
|
|
151
158
|
const normalize = (rows, name) => {
|
|
152
|
-
|
|
159
|
+
if (!Array.isArray(rows)) return { data: rows?.type === name ? rows : null }
|
|
160
|
+
const data = rows.filter((r) => r.type === name)
|
|
153
161
|
return { data, total: data.length, size: data.length }
|
|
154
162
|
}
|
|
155
163
|
|
|
@@ -187,10 +195,13 @@ function resolveRow(ref, row) {
|
|
|
187
195
|
* Read from `ref`. For data refs, dispatches to the underlying store.
|
|
188
196
|
*
|
|
189
197
|
* @param {Ref} ref
|
|
190
|
-
* @param {string | Record<string,
|
|
191
|
-
* @returns {Promise<SingleResult | ListResult
|
|
198
|
+
* @param {string | Record<string, unknown>} [q]
|
|
199
|
+
* @returns {Promise<SingleResult | ListResult>}
|
|
192
200
|
*/
|
|
193
201
|
export async function get(ref, q) {
|
|
202
|
+
await opened(ref.handle)
|
|
203
|
+
const live = ref.handle._live?.[ref.name]
|
|
204
|
+
if (live) return live.get(q)
|
|
194
205
|
if (ref.kind === 'handle') {
|
|
195
206
|
const { data } = await parentStore(ref).get('handles', q)
|
|
196
207
|
return normalize(data, ref.name)
|
|
@@ -208,78 +219,44 @@ const bindStream = (owner, stream, opts) => {
|
|
|
208
219
|
}
|
|
209
220
|
|
|
210
221
|
/**
|
|
211
|
-
*
|
|
212
|
-
*
|
|
222
|
+
* Read a ref as it changes. Each item is what `get(ref, query)` returns now; a slow reader gets
|
|
223
|
+
* only the newest. With `changes: true` in the query each item also carries `changes`, the
|
|
224
|
+
* `{ prev, next }` rows that changed since the item before, so a slow reader gets fewer items,
|
|
225
|
+
* never fewer changes, and `reset`, set on the first item, whose changes list every row with
|
|
226
|
+
* `prev: null`.
|
|
213
227
|
*
|
|
214
228
|
* @param {Ref} ref
|
|
215
|
-
* @param {Record<string,
|
|
229
|
+
* @param {string | Record<string, unknown> & { changes?: boolean }} [query]
|
|
216
230
|
* @param {{ signal?: AbortSignal }} [opts]
|
|
217
231
|
* @returns {import('streamx').Readable}
|
|
218
232
|
*/
|
|
219
|
-
export function watch(ref,
|
|
233
|
+
export function watch(ref, query, opts) {
|
|
234
|
+
const id = typeof query === 'string'
|
|
235
|
+
const { changes, ...rest } = (!id && query) || {}
|
|
236
|
+
const q = id ? query : rest
|
|
220
237
|
const owner = ref.handle
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return bindStream(owner, out, opts)
|
|
225
|
-
}
|
|
226
|
-
const source = parentStore(ref).watch('handles', q)
|
|
227
|
-
const out = snapshotStream(source, (snap) => normalize(snap?.data, ref.name))
|
|
228
|
-
return bindStream(owner, out, opts)
|
|
238
|
+
const from = owner.opened === false ? owner.ready().then(() => source(ref, q)) : source(ref, q)
|
|
239
|
+
const diff = changes ? differ(ref.kind === 'single') : null
|
|
240
|
+
return bindStream(owner, snapshotStream(from, diff), opts)
|
|
229
241
|
}
|
|
230
242
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
*/
|
|
236
|
-
export function changes(ref, q, opts) {
|
|
237
|
-
const owner = ref.handle
|
|
238
|
-
if (ref.kind === 'handle') throw CeroError.INVALID('changes does not support handle refs')
|
|
239
|
-
const src = owner.store.changes(ref.name, q)
|
|
240
|
-
let resume = null
|
|
241
|
-
const wake = () => {
|
|
242
|
-
const r = resume
|
|
243
|
-
resume = null
|
|
244
|
-
if (r) r()
|
|
243
|
+
function source(ref, q) {
|
|
244
|
+
const live = ref.handle._live?.[ref.name]
|
|
245
|
+
if (live) {
|
|
246
|
+
return { src: subscribe({ get: () => live.get(q), watch: live.watch }), map: (res) => res }
|
|
245
247
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
},
|
|
251
|
-
destroy(cb) {
|
|
252
|
-
wake()
|
|
253
|
-
src.destroy()
|
|
254
|
-
cb(null)
|
|
255
|
-
}
|
|
256
|
-
})
|
|
257
|
-
const pump = async () => {
|
|
258
|
-
for await (const batch of src) {
|
|
259
|
-
const changes = []
|
|
260
|
-
for (const { prev, next } of batch.changes) {
|
|
261
|
-
changes.push({
|
|
262
|
-
prev: prev && resolveRow(ref, prev),
|
|
263
|
-
next: next && resolveRow(ref, next)
|
|
264
|
-
})
|
|
265
|
-
}
|
|
266
|
-
if (out.push({ ...batch, changes }) === false) {
|
|
267
|
-
await new Promise((r) => {
|
|
268
|
-
resume = r
|
|
269
|
-
})
|
|
270
|
-
if (out.destroyed) return
|
|
271
|
-
}
|
|
248
|
+
if (ref.kind === 'handle') {
|
|
249
|
+
return {
|
|
250
|
+
src: parentStore(ref).watch('handles', q),
|
|
251
|
+
map: (snap) => normalize(snap?.data, ref.name)
|
|
272
252
|
}
|
|
273
|
-
out.push(null)
|
|
274
253
|
}
|
|
275
|
-
|
|
276
|
-
if (!out.destroyed) out.destroy(err)
|
|
277
|
-
})
|
|
278
|
-
return bindStream(owner, out, opts)
|
|
254
|
+
return { src: ref.handle.store.watch(ref.name, q), map: (res) => resolveResult(ref, res) }
|
|
279
255
|
}
|
|
280
256
|
|
|
281
|
-
// snapshots are idempotent: a slow
|
|
282
|
-
function snapshotStream(
|
|
257
|
+
// snapshots are idempotent: a slow reader gets only the newest
|
|
258
|
+
function snapshotStream(from, diff) {
|
|
259
|
+
let src = null
|
|
283
260
|
let pending
|
|
284
261
|
let wanted = false
|
|
285
262
|
const out = new Readable({
|
|
@@ -289,7 +266,7 @@ function snapshotStream(src, map) {
|
|
|
289
266
|
cb(null)
|
|
290
267
|
},
|
|
291
268
|
destroy(cb) {
|
|
292
|
-
src
|
|
269
|
+
src?.destroy()
|
|
293
270
|
cb(null)
|
|
294
271
|
}
|
|
295
272
|
})
|
|
@@ -297,39 +274,232 @@ function snapshotStream(src, map) {
|
|
|
297
274
|
if (!wanted || pending === undefined || out.destroyed) return
|
|
298
275
|
const snap = pending
|
|
299
276
|
pending = undefined
|
|
300
|
-
wanted = out.push(snap) !== false
|
|
277
|
+
wanted = out.push(diff ? diff(snap) : snap) !== false
|
|
301
278
|
}
|
|
302
|
-
src
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
279
|
+
const attach = ({ src: s, map }) => {
|
|
280
|
+
if (out.destroyed) return s.destroy()
|
|
281
|
+
src = s
|
|
282
|
+
src.on('data', (res) => {
|
|
283
|
+
pending = map(res)
|
|
284
|
+
flush()
|
|
285
|
+
})
|
|
286
|
+
src.on('end', () => out.push(null))
|
|
287
|
+
src.on('error', (err) => out.destroy(err))
|
|
288
|
+
}
|
|
289
|
+
if (from.then) from.then(attach, (err) => out.destroy(err))
|
|
290
|
+
else attach(from)
|
|
308
291
|
return out
|
|
309
292
|
}
|
|
310
293
|
|
|
294
|
+
// each item against the last one this reader got, so skipped snapshots fold into the next
|
|
295
|
+
function differ(single) {
|
|
296
|
+
let last
|
|
297
|
+
return (snap) => {
|
|
298
|
+
const prev = last
|
|
299
|
+
last = snap.data
|
|
300
|
+
const changes = single ? diffOne(prev ?? null, snap.data) : diffRows(prev || [], snap.data)
|
|
301
|
+
return { ...snap, changes, reset: prev === undefined }
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function diffOne(prev, next) {
|
|
306
|
+
return same(prev, next) ? [] : [{ prev, next }]
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function diffRows(prev, next) {
|
|
310
|
+
const was = new Map(prev.map((row) => [row.id, row]))
|
|
311
|
+
const changes = []
|
|
312
|
+
for (const row of next) {
|
|
313
|
+
const before = was.get(row.id) ?? null
|
|
314
|
+
was.delete(row.id)
|
|
315
|
+
if (!same(before, row)) changes.push({ prev: before, next: row })
|
|
316
|
+
}
|
|
317
|
+
for (const row of was.values()) changes.push({ prev: row, next: null })
|
|
318
|
+
return changes
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function same(a, b) {
|
|
322
|
+
return JSON.stringify(a) === JSON.stringify(b)
|
|
323
|
+
}
|
|
324
|
+
|
|
311
325
|
/**
|
|
312
326
|
* Open (or create / join / load) a child handle through a `handle`-kind ref.
|
|
313
327
|
*
|
|
314
328
|
* @param {Ref} ref
|
|
315
|
-
* @param {string | { invite?: string, id?: string, name?: string
|
|
316
|
-
* @returns {Promise<
|
|
329
|
+
* @param {string | { invite?: string, id?: string, name?: string } | undefined} [arg]
|
|
330
|
+
* @returns {Promise<Context>} The resolved child handle.
|
|
317
331
|
*/
|
|
318
332
|
export function open(ref, arg) {
|
|
319
333
|
if (typeof arg === 'string') return ref.handle._join(arg, ref.name)
|
|
320
334
|
if (arg && typeof arg.invite === 'string') return ref.handle._join(arg.invite, ref.name)
|
|
321
|
-
if (arg && typeof arg.id === 'string') return ref.handle._load(ref.name, arg.id
|
|
335
|
+
if (arg && typeof arg.id === 'string') return ref.handle._load(ref.name, arg.id)
|
|
322
336
|
return ref.handle._create(ref.name, arg)
|
|
323
337
|
}
|
|
324
338
|
|
|
325
339
|
/**
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
340
|
+
* Mint an invite code into a room.
|
|
341
|
+
*
|
|
342
|
+
* @param {Context} ctx
|
|
343
|
+
* @param {import('@cero-base/core/pairing').InviteOpts} [opts]
|
|
344
|
+
* @returns {Promise<string>}
|
|
345
|
+
*/
|
|
346
|
+
export function invite(ctx, opts) {
|
|
347
|
+
return ctx._invite(opts)
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Kill an invite, for every member.
|
|
352
|
+
*
|
|
353
|
+
* @param {Context} ctx
|
|
354
|
+
* @param {string} code
|
|
355
|
+
* @returns {Promise<boolean>} Whether it was live.
|
|
356
|
+
*/
|
|
357
|
+
export function revoke(ctx, code) {
|
|
358
|
+
return ctx._revoke(code)
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Re-key a room now: a new epoch sealed to its members, so one removed before it reads nothing
|
|
363
|
+
* written after. Needs the remove permission. A removal also re-keys on its own shortly after it
|
|
364
|
+
* lands; this is the one to await.
|
|
329
365
|
*
|
|
330
|
-
* @param {
|
|
366
|
+
* @param {Context} ctx
|
|
331
367
|
* @returns {Promise<{ epoch: number }>}
|
|
332
368
|
*/
|
|
333
|
-
export function rotate(
|
|
334
|
-
return
|
|
369
|
+
export function rotate(ctx) {
|
|
370
|
+
return ctx._rotate()
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Write atomically: every write `fn` makes through `tx`, the context it is handed, lands as one
|
|
375
|
+
* batch, or none does. Reads through `tx` see the room as it was before the batch. Not available
|
|
376
|
+
* over RPC.
|
|
377
|
+
*
|
|
378
|
+
* @template T
|
|
379
|
+
* @param {Context} ctx
|
|
380
|
+
* @param {(tx: Context) => Promise<T>} fn
|
|
381
|
+
* @returns {Promise<T>}
|
|
382
|
+
*/
|
|
383
|
+
export function tx(ctx, fn) {
|
|
384
|
+
return ctx._tx(fn)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Let in a join waiting on a `confirm` invite. `role` is the invite's by default, never above it.
|
|
389
|
+
*
|
|
390
|
+
* @param {Context} ctx
|
|
391
|
+
* @param {{ id: string }} request A row of `ctx.requests`.
|
|
392
|
+
* @param {{ role?: string }} [opts]
|
|
393
|
+
* @returns {Promise<void>}
|
|
394
|
+
*/
|
|
395
|
+
export function accept(ctx, request, { role } = {}) {
|
|
396
|
+
return ctx._answer(request.id, { accept: true, role })
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Turn away a join waiting on a `confirm` invite. The joiner's `open` rejects with `DENIED`.
|
|
401
|
+
*
|
|
402
|
+
* @param {Context} ctx
|
|
403
|
+
* @param {{ id: string }} request A row of `ctx.requests`.
|
|
404
|
+
* @param {string} [reason]
|
|
405
|
+
* @returns {Promise<void>}
|
|
406
|
+
*/
|
|
407
|
+
export function deny(ctx, request, reason = '') {
|
|
408
|
+
return ctx._answer(request.id, { accept: false, reason })
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Quit a room for good: it leaves your list and closes on this device.
|
|
413
|
+
*
|
|
414
|
+
* @param {Context} ctx
|
|
415
|
+
* @returns {Promise<void>}
|
|
416
|
+
*/
|
|
417
|
+
export function leave(ctx) {
|
|
418
|
+
return ctx._leave()
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Stop using a context on this device; everything stays. Closing the root closes it all.
|
|
423
|
+
*
|
|
424
|
+
* @param {Context} ctx
|
|
425
|
+
* @returns {Promise<void>}
|
|
426
|
+
*/
|
|
427
|
+
export function close(ctx) {
|
|
428
|
+
return ctx.close()
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Give up a join still waiting for an answer, for good: it is not resumed on the next boot.
|
|
433
|
+
*
|
|
434
|
+
* @param {Context} me
|
|
435
|
+
* @param {string} code
|
|
436
|
+
* @returns {Promise<boolean>} Whether a join was waiting.
|
|
437
|
+
*/
|
|
438
|
+
export function cancel(me, code) {
|
|
439
|
+
return me._cancel(code)
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* The app goes to the background: networking, storage and the Bluetooth radio pause together,
|
|
444
|
+
* and every context reports `status.suspended`. On `me` only.
|
|
445
|
+
*
|
|
446
|
+
* @param {Context} me
|
|
447
|
+
* @returns {Promise<void>}
|
|
448
|
+
*/
|
|
449
|
+
export function suspend(me) {
|
|
450
|
+
return me._suspend()
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* The app is back in the foreground: everything `suspend` paused resumes. On `me` only.
|
|
455
|
+
*
|
|
456
|
+
* @param {Context} me
|
|
457
|
+
* @returns {Promise<void>}
|
|
458
|
+
*/
|
|
459
|
+
export function resume(me) {
|
|
460
|
+
return me._resume()
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Mark a room as the one in use: it ranks first on the swarm, searching and announcing. Rooms are
|
|
465
|
+
* otherwise ranked by their last update.
|
|
466
|
+
*
|
|
467
|
+
* @param {Context} room
|
|
468
|
+
* @returns {Promise<void>}
|
|
469
|
+
*/
|
|
470
|
+
export async function activate(room) {
|
|
471
|
+
return room._active(true)
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Take a room off the swarm until something lands in it.
|
|
476
|
+
*
|
|
477
|
+
* @param {Context} room
|
|
478
|
+
* @returns {Promise<void>}
|
|
479
|
+
*/
|
|
480
|
+
export async function deactivate(room) {
|
|
481
|
+
return room._active(false)
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Reveal the recovery phrase.
|
|
486
|
+
*
|
|
487
|
+
* @param {Context} me
|
|
488
|
+
* @returns {Promise<string>}
|
|
489
|
+
*/
|
|
490
|
+
export function phrase(me) {
|
|
491
|
+
return me._phrase()
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Choose what the device's Bluetooth radio does: find the mesh (`true`), nothing (`false`), or
|
|
496
|
+
* hold one invite's rendezvous (its code) so a joiner in range finds this device with no internet,
|
|
497
|
+
* until the next call or the invite expires.
|
|
498
|
+
*
|
|
499
|
+
* @param {Context} ctx
|
|
500
|
+
* @param {boolean | string} mode
|
|
501
|
+
* @returns {Promise<void>}
|
|
502
|
+
*/
|
|
503
|
+
export function nearby(ctx, mode) {
|
|
504
|
+
return ctx._nearby(mode)
|
|
335
505
|
}
|
package/src/lib/peek.js
CHANGED
|
@@ -13,7 +13,7 @@ import { Local } from '../local/index.js'
|
|
|
13
13
|
* identity (i.e. a stored master seed).
|
|
14
14
|
*
|
|
15
15
|
* @param {string} dir Cero data directory.
|
|
16
|
-
* @param {
|
|
16
|
+
* @param {import('./spec.js').Spec} spec Built spec, the value passed to `cero(dir, spec)`.
|
|
17
17
|
* @returns {Promise<boolean>} `true` if a master seed exists on disk.
|
|
18
18
|
*/
|
|
19
19
|
export async function peek(dir, spec) {
|
package/src/lib/refs.js
CHANGED
|
@@ -2,10 +2,9 @@ import { CeroError } from '@cero-base/core/errors'
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @typedef {'collection' | 'single' | 'action' | 'handle'} RefKind
|
|
5
|
-
* @typedef {
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* from a generated spec.
|
|
5
|
+
* @typedef {import('./spec.js').RefInfo} RefInfo
|
|
6
|
+
* @typedef {import('./spec.js').Spec} Spec
|
|
7
|
+
* @typedef {import('../handle/index.js').Context | import('../local/index.js').Local} Owner
|
|
9
8
|
*/
|
|
10
9
|
|
|
11
10
|
/**
|
|
@@ -13,10 +12,11 @@ import { CeroError } from '@cero-base/core/errors'
|
|
|
13
12
|
*/
|
|
14
13
|
export class Ref {
|
|
15
14
|
/**
|
|
16
|
-
* @param {
|
|
15
|
+
* @param {Owner} handle The root, a room or the local store the ref lives on.
|
|
17
16
|
* @param {string} name Ref name as declared in the schema.
|
|
18
17
|
* @param {string} kind Ref kind: `'collection'`, `'single'`, `'action'`, or `'handle'`.
|
|
19
18
|
* @param {string | null} [schema] Fully-qualified schema id, if any.
|
|
19
|
+
* @param {string | null} [type] On a ref under a handle type (`me.room.notes`), that type.
|
|
20
20
|
*/
|
|
21
21
|
constructor(handle, name, kind, schema = null, type = null) {
|
|
22
22
|
this.handle = handle
|
|
@@ -30,9 +30,9 @@ export class Ref {
|
|
|
30
30
|
* Attach a `Ref` property to `target` for every entry in `refs`, so callers
|
|
31
31
|
* write `handle.someRef` instead of looking refs up by name.
|
|
32
32
|
*
|
|
33
|
-
* @param {
|
|
33
|
+
* @param {Owner} target
|
|
34
34
|
* @param {Record<string, RefInfo>} refs
|
|
35
|
-
* @param {Record<string,
|
|
35
|
+
* @param {Record<string, Spec>} [handles] The handle types, so `target.room.notes` names every room's notes.
|
|
36
36
|
*/
|
|
37
37
|
static attach(target, refs, handles) {
|
|
38
38
|
for (const [name, info] of Object.entries(refs || {})) {
|
package/src/lib/spec.js
CHANGED
|
@@ -3,3 +3,35 @@
|
|
|
3
3
|
* so cero apps can describe their tables without pulling in the core package directly.
|
|
4
4
|
*/
|
|
5
5
|
export { t, schema } from '@cero-base/core/schema'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} RefInfo One entry of a spec's `meta.refs`.
|
|
9
|
+
* @property {'single' | 'collection' | 'action' | 'handle'} [kind]
|
|
10
|
+
* @property {string} [schema]
|
|
11
|
+
* @property {string} [type] The handle type a `handle` ref opens.
|
|
12
|
+
* @property {boolean} [internal] A builtin: members, devices, files and the like.
|
|
13
|
+
* @property {boolean} [live] Computed on the device, never stored: status, joins, nearby.
|
|
14
|
+
* @property {string[]} [fields] The declared fields, in schema order.
|
|
15
|
+
* @property {Record<string, string>} [required] The required fields and their types.
|
|
16
|
+
* @property {string[]} [files] The fields holding file ids.
|
|
17
|
+
* @property {string} [verb]
|
|
18
|
+
*
|
|
19
|
+
* @typedef {object} SpecMeta
|
|
20
|
+
* @property {string} [ns]
|
|
21
|
+
* @property {string} [type] The handle type, on a handle's spec.
|
|
22
|
+
* @property {number} [version]
|
|
23
|
+
* @property {Record<string, RefInfo>} refs
|
|
24
|
+
* @property {{ refs: Record<string, RefInfo> }} [local]
|
|
25
|
+
* @property {Record<string, SpecMeta>} [handles]
|
|
26
|
+
*
|
|
27
|
+
* @typedef {import('@cero-base/core/rpc').Spec & {
|
|
28
|
+
* meta: SpecMeta,
|
|
29
|
+
* database?: object,
|
|
30
|
+
* dispatch?: object,
|
|
31
|
+
* local?: { database: object, schema: object, meta: SpecMeta, codec?: import('@cero-base/core/rpc').Codec },
|
|
32
|
+
* extensions?: import('../extensions/index.js').Extension[] | null,
|
|
33
|
+
* handles?: Record<string, Spec>
|
|
34
|
+
* }} Spec What `build` writes and `cero()` opens.
|
|
35
|
+
*
|
|
36
|
+
* @typedef {import('@cero-base/core/database').Row} Row
|
|
37
|
+
*/
|