@cero-base/cero 2.2.1 → 2.3.1
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 +91 -119
- package/src/lib/bluetooth.js +83 -19
- package/src/lib/constants.js +1 -0
- package/src/lib/operators.js +296 -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 +247 -205
- 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 +36 -53
- 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 +110 -93
- 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/rpc/client.js
CHANGED
|
@@ -1,39 +1,59 @@
|
|
|
1
1
|
import { Readable } from 'streamx'
|
|
2
|
+
import b4a from 'b4a'
|
|
2
3
|
import { RPCClient, bindCodec } from '@cero-base/core/rpc'
|
|
4
|
+
import { CeroError } from '@cero-base/core/errors'
|
|
5
|
+
import { checkFields, checkRequired } from '@cero-base/core/utils'
|
|
3
6
|
import c from 'compact-encoding'
|
|
4
7
|
import z32 from 'z32'
|
|
5
8
|
import { decodeId } from '@cero-base/core/blobs/codec'
|
|
6
9
|
|
|
7
10
|
import { Ref } from '../lib/refs.js'
|
|
8
|
-
import
|
|
11
|
+
import * as verbs from '../lib/operators.js'
|
|
9
12
|
import { t, schema } from '../lib/spec.js'
|
|
10
|
-
import { operatorsOf, bind } from '../extensions/index.js'
|
|
11
13
|
|
|
12
|
-
|
|
14
|
+
// hooks and batches take functions: they run where the data lives, never in the UI
|
|
15
|
+
const { before, after, tx, ...remote } = verbs
|
|
16
|
+
|
|
17
|
+
export const {
|
|
18
|
+
put,
|
|
19
|
+
set,
|
|
20
|
+
get,
|
|
21
|
+
del,
|
|
22
|
+
watch,
|
|
23
|
+
call,
|
|
24
|
+
open,
|
|
25
|
+
invite,
|
|
26
|
+
revoke,
|
|
27
|
+
rotate,
|
|
28
|
+
accept,
|
|
29
|
+
deny,
|
|
30
|
+
leave,
|
|
31
|
+
close,
|
|
32
|
+
cancel,
|
|
33
|
+
suspend,
|
|
34
|
+
resume,
|
|
35
|
+
activate,
|
|
36
|
+
deactivate,
|
|
37
|
+
phrase,
|
|
38
|
+
nearby
|
|
39
|
+
} = remote
|
|
40
|
+
export { t, schema }
|
|
13
41
|
|
|
14
42
|
/**
|
|
15
43
|
* @typedef {import('@cero-base/core/rpc').RPCClient} BaseRPCClient
|
|
16
44
|
*
|
|
17
|
-
* @typedef {
|
|
18
|
-
* @
|
|
19
|
-
* @
|
|
20
|
-
* @
|
|
21
|
-
* @
|
|
22
|
-
*
|
|
23
|
-
* @typedef {import('@cero-base/core/rpc').Spec & { meta: { ns?: string, refs: Record<string, RefInfo>, local?: { refs: Record<string, RefInfo> }, handles?: Record<string, Spec> }, handles: Record<string, Spec> }} Spec Built cero spec (schema + rpc + per-handle child specs).
|
|
24
|
-
*
|
|
25
|
-
* @typedef {{ data: any }} SingleResult
|
|
26
|
-
* @typedef {{ data: any[], total: number, size: number }} ListResult
|
|
27
|
-
* @typedef {{ data: any | null }} GetByIdResult
|
|
28
|
-
*
|
|
29
|
-
* @typedef {object} ClientIdentity
|
|
30
|
-
* @property {string} id
|
|
31
|
-
* @property {() => string|null} toPhrase
|
|
45
|
+
* @typedef {import('../lib/spec.js').RefInfo} RefInfo
|
|
46
|
+
* @typedef {import('../lib/spec.js').Spec} Spec
|
|
47
|
+
* @typedef {import('../lib/spec.js').Row} Row
|
|
48
|
+
* @typedef {import('../lib/operators.js').SingleResult} SingleResult
|
|
49
|
+
* @typedef {import('../lib/operators.js').ListResult} ListResult
|
|
32
50
|
*
|
|
33
51
|
* @typedef {object} HandleStub
|
|
34
52
|
* @property {string} id
|
|
35
53
|
* @property {string} type
|
|
36
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.
|
|
37
57
|
*/
|
|
38
58
|
|
|
39
59
|
const blobIdEnc = {
|
|
@@ -63,13 +83,19 @@ const blobIdEnc = {
|
|
|
63
83
|
const operators = {
|
|
64
84
|
_local: false,
|
|
65
85
|
|
|
66
|
-
/**
|
|
86
|
+
/**
|
|
87
|
+
* @param {string} name @returns {RefInfo|undefined}
|
|
88
|
+
* @private
|
|
89
|
+
*/
|
|
67
90
|
_refInfo(name) {
|
|
68
91
|
const refs = this._local ? this.spec.meta.local?.refs : this.spec.meta.refs
|
|
69
92
|
return refs?.[name]
|
|
70
93
|
},
|
|
71
94
|
|
|
72
|
-
/**
|
|
95
|
+
/**
|
|
96
|
+
* @returns {import('@cero-base/core/rpc').Codec}
|
|
97
|
+
* @private
|
|
98
|
+
*/
|
|
73
99
|
_codec() {
|
|
74
100
|
return this._local ? this.spec.local.codec : this.spec.codec
|
|
75
101
|
},
|
|
@@ -79,13 +105,9 @@ const operators = {
|
|
|
79
105
|
return this._refInfo(name)?.schema
|
|
80
106
|
},
|
|
81
107
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
* @param {string} id
|
|
86
|
-
* @returns {string}
|
|
87
|
-
*/
|
|
88
|
-
url(id) {
|
|
108
|
+
// the worker's file server, reached with the base and token learned at init
|
|
109
|
+
/** @private */
|
|
110
|
+
_link(id) {
|
|
89
111
|
const root = this.parent || this
|
|
90
112
|
const base = root._fileBase || ''
|
|
91
113
|
const token = root._fileToken || ''
|
|
@@ -102,8 +124,9 @@ const operators = {
|
|
|
102
124
|
* size, url }` objects.
|
|
103
125
|
*
|
|
104
126
|
* @param {string} name
|
|
105
|
-
* @param {
|
|
106
|
-
* @returns {
|
|
127
|
+
* @param {Row | Row[] | null} data
|
|
128
|
+
* @returns {Row | Row[] | null}
|
|
129
|
+
* @private
|
|
107
130
|
*/
|
|
108
131
|
_resolveFiles(name, data) {
|
|
109
132
|
if (data == null) return data
|
|
@@ -112,13 +135,14 @@ const operators = {
|
|
|
112
135
|
return this._resolveRow(name, info, data)
|
|
113
136
|
},
|
|
114
137
|
|
|
138
|
+
/** @private */
|
|
115
139
|
_resolveRow(name, info, row) {
|
|
116
140
|
if (!row || typeof row !== 'object') return row
|
|
117
141
|
if (info?.internal && info?.verb === 'file') {
|
|
118
142
|
if (!row.id) return row
|
|
119
143
|
try {
|
|
120
144
|
const { type, blobId } = decodeId(row.id)
|
|
121
|
-
return { ...row, type, size: blobId.byteLength, url: this.
|
|
145
|
+
return { ...row, type, size: blobId.byteLength, url: this._link(row.id) }
|
|
122
146
|
} catch {
|
|
123
147
|
return row
|
|
124
148
|
}
|
|
@@ -131,7 +155,7 @@ const operators = {
|
|
|
131
155
|
if (v == null) continue
|
|
132
156
|
try {
|
|
133
157
|
const { type, blobId } = decodeId(v)
|
|
134
|
-
out[f] = { id: v, type, size: blobId.byteLength, url: this.
|
|
158
|
+
out[f] = { id: v, type, size: blobId.byteLength, url: this._link(v) }
|
|
135
159
|
} catch {
|
|
136
160
|
// leave as-is if not a valid file id
|
|
137
161
|
}
|
|
@@ -143,7 +167,7 @@ const operators = {
|
|
|
143
167
|
* Insert a row over the wire.
|
|
144
168
|
*
|
|
145
169
|
* @param {string} name
|
|
146
|
-
* @param {
|
|
170
|
+
* @param {Row} row
|
|
147
171
|
* @returns {Promise<SingleResult>}
|
|
148
172
|
*/
|
|
149
173
|
async put(name, row) {
|
|
@@ -159,11 +183,10 @@ const operators = {
|
|
|
159
183
|
const decoded = codec.decodeRow(schema, res.data)
|
|
160
184
|
return { data: this._resolveRow(name, this._refInfo(name), decoded) }
|
|
161
185
|
}
|
|
162
|
-
const input = row?.id ? row : { id: '', ...row }
|
|
163
186
|
const res = await this.rpc.addRow({
|
|
164
187
|
handle: this.id,
|
|
165
188
|
ref: name,
|
|
166
|
-
data:
|
|
189
|
+
data: this._encode(name, row, true).data,
|
|
167
190
|
local: this._local
|
|
168
191
|
})
|
|
169
192
|
return { data: codec.decodeRow(schema, res.data) }
|
|
@@ -173,21 +196,23 @@ const operators = {
|
|
|
173
196
|
* Upsert a row over the wire. Pass `{ upsert: false }` to update-only.
|
|
174
197
|
*
|
|
175
198
|
* @param {string} name
|
|
176
|
-
* @param {
|
|
199
|
+
* @param {Row} row
|
|
177
200
|
* @param {{ upsert?: boolean }} [opts]
|
|
178
|
-
* @returns {Promise<SingleResult>}
|
|
201
|
+
* @returns {Promise<SingleResult | null>}
|
|
179
202
|
*/
|
|
180
203
|
async set(name, row, opts) {
|
|
181
204
|
const codec = this._codec()
|
|
182
205
|
const schema = this.schemaOf(name)
|
|
206
|
+
const { data, fields } = this._encode(name, row, false)
|
|
183
207
|
const res = await this.rpc.set({
|
|
184
208
|
handle: this.id,
|
|
185
209
|
ref: name,
|
|
186
|
-
data
|
|
210
|
+
data,
|
|
211
|
+
fields,
|
|
187
212
|
local: this._local,
|
|
188
213
|
noUpsert: opts?.upsert === false || undefined
|
|
189
214
|
})
|
|
190
|
-
return { data: codec.decodeRow(schema, res.data) }
|
|
215
|
+
return res.data ? { data: codec.decodeRow(schema, res.data) } : null
|
|
191
216
|
},
|
|
192
217
|
|
|
193
218
|
/**
|
|
@@ -195,35 +220,56 @@ const operators = {
|
|
|
195
220
|
* `Storage.get` contract.
|
|
196
221
|
*
|
|
197
222
|
* @param {string} name
|
|
198
|
-
* @param {string | Record<string,
|
|
199
|
-
* @returns {Promise<SingleResult | ListResult
|
|
223
|
+
* @param {string | Record<string, unknown>} [query]
|
|
224
|
+
* @returns {Promise<SingleResult | ListResult>}
|
|
200
225
|
*/
|
|
201
226
|
async get(name, query) {
|
|
202
|
-
const
|
|
203
|
-
const schema = this.schemaOf(name)
|
|
204
|
-
if (typeof query === 'string') {
|
|
205
|
-
const res = await this.rpc.getOne({
|
|
206
|
-
handle: this.id,
|
|
207
|
-
ref: name,
|
|
208
|
-
id: query,
|
|
209
|
-
local: this._local
|
|
210
|
-
})
|
|
211
|
-
const decoded = res.data ? codec.decodeRow(schema, res.data) : null
|
|
212
|
-
return { data: this._resolveFiles(name, decoded) }
|
|
213
|
-
}
|
|
227
|
+
const one = typeof query === 'string' || this._refInfo(name)?.kind === 'single'
|
|
214
228
|
const res = await this.rpc.get({
|
|
215
229
|
handle: this.id,
|
|
216
230
|
ref: name,
|
|
217
|
-
query:
|
|
231
|
+
query: toWire(query),
|
|
218
232
|
local: this._local
|
|
219
233
|
})
|
|
234
|
+
return this._read(name, one, res)
|
|
235
|
+
},
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* The typed row and the indexes of the declared fields it carries, checked as the worker
|
|
239
|
+
* would: the encoder drops an undeclared field and fails on a missing required one.
|
|
240
|
+
*
|
|
241
|
+
* @param {string} name
|
|
242
|
+
* @param {Row} row
|
|
243
|
+
* @param {boolean} whole A put's row; a set's may leave fields out.
|
|
244
|
+
* @returns {{ data: Uint8Array, fields: number[] }}
|
|
245
|
+
* @private
|
|
246
|
+
*/
|
|
247
|
+
_encode(name, row, whole) {
|
|
220
248
|
const info = this._refInfo(name)
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
249
|
+
checkFields(name, info, row)
|
|
250
|
+
if (whole) checkRequired(name, info, row)
|
|
251
|
+
const declared = info?.fields || []
|
|
252
|
+
const fields = Object.keys(row)
|
|
253
|
+
.map((k) => declared.indexOf(k))
|
|
254
|
+
.filter((i) => i !== -1)
|
|
255
|
+
return { data: this._codec().encodeRow(this.schemaOf(name), filled(row, info)), fields }
|
|
256
|
+
},
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Decode a result: one row or null when `one`, the list with its counts otherwise.
|
|
260
|
+
*
|
|
261
|
+
* @param {string} name
|
|
262
|
+
* @param {boolean} one
|
|
263
|
+
* @param {{ data: Uint8Array | null, total?: number, size?: number }} res
|
|
264
|
+
* @returns {SingleResult | ListResult}
|
|
265
|
+
* @private
|
|
266
|
+
*/
|
|
267
|
+
_read(name, one, res) {
|
|
268
|
+
const codec = this._codec()
|
|
269
|
+
const schema = this.schemaOf(name)
|
|
270
|
+
if (one) return { data: this._resolveFiles(name, codec.decodeRow(schema, res.data) ?? null) }
|
|
225
271
|
return {
|
|
226
|
-
data: this._resolveFiles(name,
|
|
272
|
+
data: this._resolveFiles(name, codec.decodeRows(schema, res.data)),
|
|
227
273
|
total: res.total === -1 ? null : res.total,
|
|
228
274
|
size: res.size
|
|
229
275
|
}
|
|
@@ -245,17 +291,15 @@ const operators = {
|
|
|
245
291
|
* underlying mutation. Destroy the stream to stop watching.
|
|
246
292
|
*
|
|
247
293
|
* @param {string} name
|
|
248
|
-
* @param {Record<string,
|
|
294
|
+
* @param {string | Record<string, unknown>} [query]
|
|
249
295
|
* @returns {import('streamx').Readable}
|
|
250
296
|
*/
|
|
251
297
|
watch(name, query) {
|
|
252
|
-
const
|
|
253
|
-
const codec = this._codec()
|
|
254
|
-
const schema = refInfo?.schema
|
|
298
|
+
const one = typeof query === 'string' || this._refInfo(name)?.kind === 'single'
|
|
255
299
|
const wire = this.rpc.watch({
|
|
256
300
|
handle: this.id,
|
|
257
301
|
ref: name,
|
|
258
|
-
query:
|
|
302
|
+
query: toWire(query),
|
|
259
303
|
local: this._local
|
|
260
304
|
})
|
|
261
305
|
const out = new Readable({
|
|
@@ -263,17 +307,7 @@ const operators = {
|
|
|
263
307
|
wire.destroy()
|
|
264
308
|
}
|
|
265
309
|
})
|
|
266
|
-
wire.on('data', (snap) =>
|
|
267
|
-
const raw =
|
|
268
|
-
refInfo.kind === 'single'
|
|
269
|
-
? (codec.decodeRow(schema, snap.data) ?? null)
|
|
270
|
-
: codec.decodeRows(schema, snap.data)
|
|
271
|
-
out.push({
|
|
272
|
-
data: this._resolveFiles(name, raw),
|
|
273
|
-
total: snap.total === -1 ? null : snap.total,
|
|
274
|
-
size: snap.size
|
|
275
|
-
})
|
|
276
|
-
})
|
|
310
|
+
wire.on('data', (snap) => out.push(this._read(name, one, snap)))
|
|
277
311
|
// end exactly once whether the wire ends or the server destroys it (handle close)
|
|
278
312
|
let ended = false
|
|
279
313
|
const end = () => {
|
|
@@ -289,52 +323,11 @@ const operators = {
|
|
|
289
323
|
return out
|
|
290
324
|
},
|
|
291
325
|
|
|
292
|
-
/**
|
|
293
|
-
* Delta subscription over the wire — same contract as the local operator: batches of `{
|
|
294
|
-
* prev, next }` with file fields resolved, `reset` marks a full replay.
|
|
295
|
-
*/
|
|
296
|
-
changes(name, query) {
|
|
297
|
-
const refInfo = this._refInfo(name)
|
|
298
|
-
const codec = this._codec()
|
|
299
|
-
const schema = refInfo?.schema
|
|
300
|
-
const wire = this.rpc.changes({
|
|
301
|
-
handle: this.id,
|
|
302
|
-
ref: name,
|
|
303
|
-
query: codec.encodeQuery(query),
|
|
304
|
-
local: this._local
|
|
305
|
-
})
|
|
306
|
-
const out = new Readable({
|
|
307
|
-
predestroy() {
|
|
308
|
-
wire.destroy()
|
|
309
|
-
}
|
|
310
|
-
})
|
|
311
|
-
const pump = async () => {
|
|
312
|
-
for await (const frame of wire) {
|
|
313
|
-
const changes = []
|
|
314
|
-
for (const { prev, next } of codec.decodeChanges(schema, frame.changes)) {
|
|
315
|
-
changes.push({
|
|
316
|
-
prev: prev && this._resolveFiles(name, prev),
|
|
317
|
-
next: next && this._resolveFiles(name, next)
|
|
318
|
-
})
|
|
319
|
-
}
|
|
320
|
-
out.push({ changes, reset: frame.reset === true })
|
|
321
|
-
}
|
|
322
|
-
if (!out.destroyed) out.push(null)
|
|
323
|
-
}
|
|
324
|
-
pump().catch((err) => {
|
|
325
|
-
if (out.destroyed) return
|
|
326
|
-
// both are ends, not failures
|
|
327
|
-
if (err.code === 'PREMATURE_CLOSE' || err.code === 'CHANNEL_CLOSED') out.push(null)
|
|
328
|
-
else out.destroy(err)
|
|
329
|
-
})
|
|
330
|
-
return out
|
|
331
|
-
},
|
|
332
|
-
|
|
333
326
|
/**
|
|
334
327
|
* Invoke a named action ref over the wire.
|
|
335
328
|
*
|
|
336
329
|
* @param {string} op
|
|
337
|
-
* @param {
|
|
330
|
+
* @param {Row} [data]
|
|
338
331
|
* @returns {Promise<void>}
|
|
339
332
|
*/
|
|
340
333
|
async call(op, data) {
|
|
@@ -344,51 +337,72 @@ const operators = {
|
|
|
344
337
|
await this.rpc.call({ handle: this.id, op, data: encoded })
|
|
345
338
|
},
|
|
346
339
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
* @returns {Promise<string>}
|
|
352
|
-
*/
|
|
353
|
-
async invite({ role, expiresIn, reuse } = {}) {
|
|
340
|
+
// the verbs in lib/operators.js land here, and cross to the worker
|
|
341
|
+
|
|
342
|
+
/** @private */
|
|
343
|
+
async _invite({ role, ttl, reuse, confirm, data } = {}) {
|
|
354
344
|
const { invite } = await this.rpc.invite({
|
|
355
345
|
handle: this.id,
|
|
356
346
|
role: role || '',
|
|
357
|
-
|
|
358
|
-
reuse: reuse === true
|
|
347
|
+
ttl: ttl ? String(ttl) : '',
|
|
348
|
+
reuse: reuse === true,
|
|
349
|
+
confirm: confirm === true,
|
|
350
|
+
data: data || null
|
|
359
351
|
})
|
|
360
352
|
return invite
|
|
361
353
|
},
|
|
362
354
|
|
|
363
|
-
/**
|
|
364
|
-
|
|
365
|
-
*
|
|
366
|
-
* @param {string} invite
|
|
367
|
-
* @returns {Promise<boolean>}
|
|
368
|
-
*/
|
|
369
|
-
async revoke(invite) {
|
|
355
|
+
/** @private */
|
|
356
|
+
async _revoke(invite) {
|
|
370
357
|
const { ok } = await this.rpc.revoke({ handle: this.id, invite })
|
|
371
358
|
return ok
|
|
372
359
|
},
|
|
373
360
|
|
|
374
|
-
/**
|
|
375
|
-
|
|
376
|
-
*
|
|
377
|
-
* @returns {Promise<{ epoch: number }>}
|
|
378
|
-
*/
|
|
379
|
-
async rotate() {
|
|
361
|
+
/** @private */
|
|
362
|
+
async _rotate() {
|
|
380
363
|
const { epoch } = await this.rpc.rotate({ handle: this.id })
|
|
381
364
|
return { epoch }
|
|
382
365
|
},
|
|
383
366
|
|
|
384
|
-
/**
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
async
|
|
391
|
-
await this.rpc.
|
|
367
|
+
/** @private */
|
|
368
|
+
async _answer(id, { accept, role, reason }) {
|
|
369
|
+
await this.rpc.answer({ handle: this.id, id, accept, role: role || '', reason: reason || '' })
|
|
370
|
+
},
|
|
371
|
+
|
|
372
|
+
/** @private */
|
|
373
|
+
async _leave() {
|
|
374
|
+
await this.rpc.leave({ handle: this.id })
|
|
375
|
+
},
|
|
376
|
+
|
|
377
|
+
/** @private */
|
|
378
|
+
async _cancel(invite) {
|
|
379
|
+
return (await this.rpc.cancel({ invite })).ok
|
|
380
|
+
},
|
|
381
|
+
|
|
382
|
+
/** @private */
|
|
383
|
+
async _suspend() {
|
|
384
|
+
await this.rpc.suspend({ handle: this.id })
|
|
385
|
+
},
|
|
386
|
+
|
|
387
|
+
/** @private */
|
|
388
|
+
async _resume() {
|
|
389
|
+
await this.rpc.resume({ handle: this.id })
|
|
390
|
+
},
|
|
391
|
+
|
|
392
|
+
/** @private */
|
|
393
|
+
async _active(on) {
|
|
394
|
+
await this.rpc.setActive({ handle: this.id, active: on })
|
|
395
|
+
},
|
|
396
|
+
|
|
397
|
+
/** @private */
|
|
398
|
+
async _phrase() {
|
|
399
|
+
return (await this.rpc.seed({})).phrase || null
|
|
400
|
+
},
|
|
401
|
+
|
|
402
|
+
/** @private */
|
|
403
|
+
async _nearby(mode) {
|
|
404
|
+
const invite = typeof mode === 'string' ? mode : ''
|
|
405
|
+
await this.rpc.nearby({ on: mode !== false, invite })
|
|
392
406
|
}
|
|
393
407
|
}
|
|
394
408
|
|
|
@@ -403,8 +417,7 @@ const operators = {
|
|
|
403
417
|
export async function restore(me, phrase) {
|
|
404
418
|
const res = await me.rpc.restore({ phrase })
|
|
405
419
|
me.id = res.id
|
|
406
|
-
me.
|
|
407
|
-
me.identity = { id: res.id, toPhrase: async () => (await me.rpc.seed({})).phrase || null }
|
|
420
|
+
me.device = device(res)
|
|
408
421
|
return me
|
|
409
422
|
}
|
|
410
423
|
|
|
@@ -418,6 +431,7 @@ class LocalRefs {
|
|
|
418
431
|
this.parent = client
|
|
419
432
|
this.spec = client.spec
|
|
420
433
|
this.store = this
|
|
434
|
+
/** @private */
|
|
421
435
|
this._local = true
|
|
422
436
|
const refs = client.spec.meta.local?.refs || {}
|
|
423
437
|
const exposed = Object.fromEntries(Object.entries(refs).filter(([, info]) => !info.internal))
|
|
@@ -429,7 +443,11 @@ class LocalRefs {
|
|
|
429
443
|
return this.parent.rpc
|
|
430
444
|
}
|
|
431
445
|
|
|
432
|
-
/**
|
|
446
|
+
/**
|
|
447
|
+
* The root's id: local ops resolve against the root's local store.
|
|
448
|
+
*
|
|
449
|
+
* @returns {string | null}
|
|
450
|
+
*/
|
|
433
451
|
get id() {
|
|
434
452
|
return this.parent.id
|
|
435
453
|
}
|
|
@@ -442,28 +460,38 @@ class LocalRefs {
|
|
|
442
460
|
*/
|
|
443
461
|
export class Client extends RPCClient {
|
|
444
462
|
/**
|
|
445
|
-
* @param {
|
|
463
|
+
* @param {import('streamx').Duplex} ipc Framed IPC stream (must be writable).
|
|
446
464
|
* @param {Spec} spec Compiled cero spec (schema + rpc + handles).
|
|
447
|
-
* @param {{
|
|
465
|
+
* @param {{ onerror?: (err: Error) => void }} [opts] Where the worker's background errors go; the console without one.
|
|
448
466
|
*/
|
|
449
|
-
constructor(ipc, spec,
|
|
467
|
+
constructor(ipc, spec, { onerror } = {}) {
|
|
450
468
|
super(ipc, spec)
|
|
451
469
|
if (spec.local?.schema && !spec.local.codec) bindCodec(spec.local)
|
|
452
470
|
Object.assign(this, operators)
|
|
453
|
-
|
|
471
|
+
/** @private */
|
|
472
|
+
this._onerror = onerror || ((err) => console.error(err))
|
|
473
|
+
/** @type {string | null} */
|
|
454
474
|
this.id = null
|
|
455
|
-
|
|
475
|
+
/** @type {{ id: string, name: string | null } | null} */
|
|
476
|
+
this.device = null
|
|
456
477
|
this.store = this
|
|
457
478
|
this.local = null
|
|
458
479
|
}
|
|
459
480
|
|
|
460
481
|
// the worker's background errors, delivered the way a local root delivers them
|
|
482
|
+
/** @private */
|
|
461
483
|
_pumpErrors() {
|
|
462
|
-
const report =
|
|
463
|
-
this.listenerCount('error') ? this.emit('error', err) : console.error(err)
|
|
484
|
+
const report = this._onerror
|
|
464
485
|
const pump = async () => {
|
|
465
|
-
for await (const { message, code, stack } of this.rpc.errors({})) {
|
|
466
|
-
report(
|
|
486
|
+
for await (const { message, code, stack, reason } of this.rpc.errors({})) {
|
|
487
|
+
report(
|
|
488
|
+
Object.assign(
|
|
489
|
+
new Error(message),
|
|
490
|
+
code && { code },
|
|
491
|
+
stack && { stack },
|
|
492
|
+
reason && { reason }
|
|
493
|
+
)
|
|
494
|
+
)
|
|
467
495
|
}
|
|
468
496
|
}
|
|
469
497
|
pump().catch((err) => {
|
|
@@ -471,44 +499,35 @@ export class Client extends RPCClient {
|
|
|
471
499
|
})
|
|
472
500
|
}
|
|
473
501
|
|
|
502
|
+
/** @private */
|
|
474
503
|
async _open() {
|
|
475
504
|
await super._open()
|
|
476
|
-
const
|
|
505
|
+
const res = await this.rpc.init({})
|
|
506
|
+
const { id, fileBase, fileToken } = res
|
|
477
507
|
this.id = id
|
|
478
|
-
this.
|
|
508
|
+
this.device = device(res)
|
|
509
|
+
/** @private */
|
|
479
510
|
this._fileBase = fileBase || ''
|
|
511
|
+
/** @private */
|
|
480
512
|
this._fileToken = fileToken || ''
|
|
481
|
-
this.identity = { id, toPhrase: async () => (await this.rpc.seed({})).phrase || null }
|
|
482
513
|
Ref.attach(this, /** @type {Spec} */ (this.spec).meta.refs)
|
|
483
|
-
bind(this, null, this.operators)
|
|
484
514
|
if (/** @type {Spec} */ (this.spec).meta.local?.refs) this.local = new LocalRefs(this)
|
|
485
515
|
this._pumpErrors()
|
|
486
516
|
}
|
|
487
517
|
|
|
488
|
-
/** Pause networking and storage on the server. Idempotent. */
|
|
489
|
-
async suspend() {
|
|
490
|
-
await this.rpc.suspend({})
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
/** Resume a suspended server. Idempotent. */
|
|
494
|
-
async resume() {
|
|
495
|
-
await this.rpc.resume({})
|
|
496
|
-
}
|
|
497
|
-
|
|
498
518
|
/**
|
|
499
519
|
* Create a new child handle of the given type.
|
|
500
520
|
*
|
|
501
521
|
* @param {string} type
|
|
502
|
-
* @param {
|
|
522
|
+
* @param {{ name?: string | null }} [opts]
|
|
503
523
|
* @returns {Promise<Handle>}
|
|
524
|
+
* @private
|
|
504
525
|
*/
|
|
505
526
|
async _create(type, opts = {}) {
|
|
506
|
-
// routes are functions and cannot cross the wire
|
|
507
|
-
const wire = { ...opts, noAccept: opts.accept === false || undefined }
|
|
508
527
|
const stub = await this.rpc.addHandle({
|
|
509
528
|
ref: type,
|
|
510
529
|
handle: this.id,
|
|
511
|
-
data: this.spec.codec.encodeCreate(
|
|
530
|
+
data: this.spec.codec.encodeCreate({ name: opts.name })
|
|
512
531
|
})
|
|
513
532
|
return new Handle(this, stub.id, stub.type, stub.name || null)
|
|
514
533
|
}
|
|
@@ -519,6 +538,7 @@ export class Client extends RPCClient {
|
|
|
519
538
|
* @param {string} type
|
|
520
539
|
* @param {string} id
|
|
521
540
|
* @returns {Promise<Handle>}
|
|
541
|
+
* @private
|
|
522
542
|
*/
|
|
523
543
|
async _load(type, id) {
|
|
524
544
|
const stub = await this.rpc.openHandle({ parent: this.id, row: id })
|
|
@@ -531,9 +551,11 @@ export class Client extends RPCClient {
|
|
|
531
551
|
* @param {string} invite
|
|
532
552
|
* @param {string} type
|
|
533
553
|
* @returns {Promise<Handle>}
|
|
554
|
+
* @private
|
|
534
555
|
*/
|
|
535
556
|
async _join(invite, type) {
|
|
536
557
|
const stub = await this.rpc.join({ parent: this.id, ref: type, invite })
|
|
558
|
+
if (stub.denied) throw CeroError.DENIED(stub.reason || null)
|
|
537
559
|
return new Handle(this, stub.id, stub.type, stub.name || null)
|
|
538
560
|
}
|
|
539
561
|
}
|
|
@@ -560,7 +582,6 @@ class Handle {
|
|
|
560
582
|
if (!this.spec.codec) bindCodec(this.spec)
|
|
561
583
|
this.store = this
|
|
562
584
|
Ref.attach(this, this.spec.meta.refs)
|
|
563
|
-
bind(this, this.type, parent.operators)
|
|
564
585
|
}
|
|
565
586
|
|
|
566
587
|
/** Underlying RPC channel borrowed from the parent. */
|
|
@@ -568,23 +589,17 @@ class Handle {
|
|
|
568
589
|
return this.parent.rpc
|
|
569
590
|
}
|
|
570
591
|
|
|
571
|
-
/** Tear down the remote handle without leaving the room. */
|
|
572
592
|
close() {
|
|
573
593
|
return this.parent.rpc.closeHandle({ handle: this.id })
|
|
574
594
|
}
|
|
575
|
-
|
|
576
|
-
/** Tear down the remote handle and drop membership. */
|
|
577
|
-
leave() {
|
|
578
|
-
return this.parent.rpc.leave({ handle: this.id })
|
|
579
|
-
}
|
|
580
595
|
}
|
|
581
596
|
|
|
582
597
|
/**
|
|
583
598
|
* Construct a `Client`, wait for `init` to complete, and return it.
|
|
584
599
|
*
|
|
585
|
-
* @param {
|
|
586
|
-
* @param {
|
|
587
|
-
* @param {{
|
|
600
|
+
* @param {import('streamx').Duplex} ipc
|
|
601
|
+
* @param {Spec} spec
|
|
602
|
+
* @param {{ onerror?: (err: Error) => void }} [opts]
|
|
588
603
|
* @returns {Promise<Client>}
|
|
589
604
|
*/
|
|
590
605
|
export async function connect(ipc, spec, opts) {
|
|
@@ -597,24 +612,51 @@ export async function connect(ipc, spec, opts) {
|
|
|
597
612
|
* Symmetric client entry. Mirrors the main `cero`, but `cero(ipc, spec)` connects to a
|
|
598
613
|
* server (via `connect`) instead of opening a local store.
|
|
599
614
|
*
|
|
600
|
-
* @param {
|
|
601
|
-
* @param {
|
|
602
|
-
* @param {{
|
|
603
|
-
* @returns {Promise<
|
|
615
|
+
* @param {import('streamx').Duplex} ipc Framed IPC duplex stream.
|
|
616
|
+
* @param {Spec} spec Built cero spec.
|
|
617
|
+
* @param {{ onerror?: (err: Error) => void }} [opts]
|
|
618
|
+
* @returns {Promise<Context>}
|
|
604
619
|
*/
|
|
605
|
-
|
|
606
|
-
return connect(ipc, spec, opts)
|
|
620
|
+
function start(ipc, spec, opts) {
|
|
621
|
+
return /** @type {Promise<Context>} */ (connect(ipc, spec, opts))
|
|
622
|
+
}
|
|
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 })
|
|
626
|
+
|
|
627
|
+
// the same shape a local root has
|
|
628
|
+
function device({ deviceId, deviceName }) {
|
|
629
|
+
return deviceId ? { id: deviceId, name: deviceName || null } : null
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// the typed row carries an id and every required field: what a row leaves out is filled, the
|
|
633
|
+
// worker keeps only the fields sent
|
|
634
|
+
function filled(row, info) {
|
|
635
|
+
let out = row.id === undefined ? { id: '', ...row } : row
|
|
636
|
+
for (const key in info?.required) {
|
|
637
|
+
if (out[key] == null) out = { ...out, [key]: ZERO[info.required[key]] }
|
|
638
|
+
}
|
|
639
|
+
return out
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
const ZERO = {
|
|
643
|
+
string: '',
|
|
644
|
+
file: '',
|
|
645
|
+
json: null,
|
|
646
|
+
uint: 0,
|
|
647
|
+
int: 0,
|
|
648
|
+
bool: false,
|
|
649
|
+
bytes: b4a.alloc(0),
|
|
650
|
+
fixed32: b4a.alloc(32),
|
|
651
|
+
fixed64: b4a.alloc(64)
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// c.any carries undefined as null, and in a query undefined is absent
|
|
655
|
+
function toWire(query) {
|
|
656
|
+
if (query === undefined) return null
|
|
657
|
+
const defined =
|
|
658
|
+
query && typeof query === 'object'
|
|
659
|
+
? Object.fromEntries(Object.entries(query).filter(([, v]) => v !== undefined))
|
|
660
|
+
: query
|
|
661
|
+
return c.encode(c.any, defined)
|
|
607
662
|
}
|
|
608
|
-
cero.connect = connect
|
|
609
|
-
cero.restore = restore
|
|
610
|
-
cero.t = t
|
|
611
|
-
cero.put = put
|
|
612
|
-
cero.set = set
|
|
613
|
-
cero.get = get
|
|
614
|
-
cero.del = del
|
|
615
|
-
cero.watch = watch
|
|
616
|
-
cero.changes = changes
|
|
617
|
-
cero.call = call
|
|
618
|
-
cero.open = open
|
|
619
|
-
cero.rotate = rotate
|
|
620
|
-
cero.schema = schema
|