@cero-base/cero 2.2.1 → 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/rpc/server.js
CHANGED
|
@@ -1,11 +1,29 @@
|
|
|
1
|
+
import c from 'compact-encoding'
|
|
1
2
|
import safetyCatch from 'safety-catch'
|
|
2
3
|
|
|
3
4
|
import { RPCServer, bindCodec } from '@cero-base/core/rpc'
|
|
4
5
|
import { CeroError } from '@cero-base/core/errors'
|
|
5
|
-
import { encodeId } from '@cero-base/core/blobs/codec'
|
|
6
6
|
|
|
7
|
-
import { cero, restore } from '../index.js'
|
|
8
|
-
import {
|
|
7
|
+
import { cero, restore, toSeed } from '../index.js'
|
|
8
|
+
import {
|
|
9
|
+
put,
|
|
10
|
+
set,
|
|
11
|
+
get,
|
|
12
|
+
del,
|
|
13
|
+
watch,
|
|
14
|
+
call,
|
|
15
|
+
invite,
|
|
16
|
+
revoke,
|
|
17
|
+
rotate,
|
|
18
|
+
cancel,
|
|
19
|
+
leave,
|
|
20
|
+
suspend,
|
|
21
|
+
resume,
|
|
22
|
+
activate,
|
|
23
|
+
deactivate,
|
|
24
|
+
phrase,
|
|
25
|
+
nearby
|
|
26
|
+
} from '../lib/operators.js'
|
|
9
27
|
|
|
10
28
|
/**
|
|
11
29
|
* @typedef {import('@cero-base/core/rpc').RPCServer} BaseRPCServer
|
|
@@ -20,21 +38,25 @@ import { put, set, get, del, watch, changes, call } from '../lib/operators.js'
|
|
|
20
38
|
* @typedef {object} Identity
|
|
21
39
|
* @property {string} id Long-lived cero identity id.
|
|
22
40
|
* @property {string} deviceId Per-device id (empty when no `local` spec).
|
|
41
|
+
* @property {string} deviceName This device's name, empty when it has none.
|
|
23
42
|
*
|
|
24
|
-
* @typedef {{ ref:
|
|
43
|
+
* @typedef {{ ref: import('../lib/refs.js').Ref, codec: import('@cero-base/core/rpc').Codec, info?: import('../lib/spec.js').RefInfo }} RefAndCodec
|
|
25
44
|
*
|
|
26
|
-
* @typedef {
|
|
45
|
+
* @typedef {import('../lib/spec.js').Spec} Spec
|
|
46
|
+
* @typedef {import('../handle/index.js').Context} Context
|
|
47
|
+
*
|
|
48
|
+
* @typedef {{ data: import('../lib/spec.js').Row | import('../lib/spec.js').Row[] | null, total?: number, size?: number }} GetResult Single-ref get omits `total`/`size`; list/handle refs include them.
|
|
27
49
|
*/
|
|
28
50
|
|
|
29
51
|
/**
|
|
30
|
-
* IPC-side RPC server for cero. Bridges an `hrpc` channel to a live `Handle` tree:
|
|
31
|
-
*
|
|
32
|
-
* pairing, and handle lifecycle.
|
|
52
|
+
* IPC-side RPC server for cero. Bridges an `hrpc` channel to a live `Handle` tree: boots the
|
|
53
|
+
* root via `cero()` as soon as it opens, so the network is up while the UI still loads, then
|
|
54
|
+
* exposes data ops, pairing, and handle lifecycle.
|
|
33
55
|
*/
|
|
34
56
|
export class Server extends RPCServer {
|
|
35
57
|
/**
|
|
36
|
-
* @param {
|
|
37
|
-
* @param {
|
|
58
|
+
* @param {import('streamx').Duplex} ipc Framed IPC stream (must be writable).
|
|
59
|
+
* @param {Spec} spec
|
|
38
60
|
* @param {Partial<ServerOpts>} [opts]
|
|
39
61
|
*/
|
|
40
62
|
constructor(ipc, spec, { storage, ...opts } = {}) {
|
|
@@ -43,19 +65,25 @@ export class Server extends RPCServer {
|
|
|
43
65
|
super(ipc, spec)
|
|
44
66
|
if (spec.local?.schema && !spec.local.codec) bindCodec(spec.local)
|
|
45
67
|
this.storage = storage
|
|
68
|
+
/** @private */
|
|
46
69
|
this._report = opts.onerror || ((err) => console.error(err))
|
|
70
|
+
/** @type {Omit<Partial<ServerOpts>, 'storage'> & { onerror: (err: Error) => void }} */
|
|
47
71
|
this.opts = { ...opts, onerror: (err) => this._onerror(err) }
|
|
48
72
|
/** @type {Set<object>} open error streams, one per connected client */
|
|
49
73
|
this._errors = new Set()
|
|
74
|
+
/** @type {Context | null} */
|
|
50
75
|
this.me = null
|
|
76
|
+
/** @type {Map<string, Context>} */
|
|
51
77
|
this.handles = new Map()
|
|
52
78
|
/** @type {Map<string, Set<object>>} handle id → its open watch streams */
|
|
53
79
|
this._watchStreams = new Map()
|
|
80
|
+
/** @private */
|
|
81
|
+
this._booting = null
|
|
54
82
|
this._wireInit()
|
|
55
83
|
}
|
|
56
84
|
|
|
57
85
|
/**
|
|
58
|
-
* Root cero id (
|
|
86
|
+
* Root cero id (undefined until booted).
|
|
59
87
|
*
|
|
60
88
|
* @returns {string|undefined}
|
|
61
89
|
*/
|
|
@@ -64,15 +92,37 @@ export class Server extends RPCServer {
|
|
|
64
92
|
}
|
|
65
93
|
|
|
66
94
|
/**
|
|
67
|
-
* Root identity object (
|
|
95
|
+
* Root identity object (undefined until booted).
|
|
68
96
|
*
|
|
69
|
-
* @returns {
|
|
97
|
+
* @returns {import('@cero-base/core/identity').Identity | undefined}
|
|
70
98
|
*/
|
|
71
99
|
get identity() {
|
|
72
100
|
return this.me?.identity
|
|
73
101
|
}
|
|
74
102
|
|
|
103
|
+
/** @private */
|
|
104
|
+
async _open() {
|
|
105
|
+
await super._open()
|
|
106
|
+
this._booting = this._boot()
|
|
107
|
+
// it surfaces on init
|
|
108
|
+
this._booting.catch(() => {})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** @private */
|
|
112
|
+
async _boot() {
|
|
113
|
+
this.me = await cero(this.storage, this.spec, this.opts)
|
|
114
|
+
this.handles.set(this.me.id, this.me)
|
|
115
|
+
await this.me.fileServer.listen()
|
|
116
|
+
this._wireData()
|
|
117
|
+
this._wirePairing()
|
|
118
|
+
this._wireHandles()
|
|
119
|
+
this._wireRestore()
|
|
120
|
+
this._wireSeed()
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** @private */
|
|
75
124
|
async _close() {
|
|
125
|
+
await this._booting?.catch(() => {})
|
|
76
126
|
if (this.me) {
|
|
77
127
|
try {
|
|
78
128
|
await this.me.close()
|
|
@@ -81,7 +131,10 @@ export class Server extends RPCServer {
|
|
|
81
131
|
await super._close()
|
|
82
132
|
}
|
|
83
133
|
|
|
84
|
-
/**
|
|
134
|
+
/**
|
|
135
|
+
* End every watch stream bound to a handle (e.g. when it closes or leaves).
|
|
136
|
+
* @private
|
|
137
|
+
*/
|
|
85
138
|
_endWatches(handle) {
|
|
86
139
|
const set = this._watchStreams.get(handle)
|
|
87
140
|
if (!set) return
|
|
@@ -89,17 +142,22 @@ export class Server extends RPCServer {
|
|
|
89
142
|
this._watchStreams.delete(handle)
|
|
90
143
|
}
|
|
91
144
|
|
|
92
|
-
/**
|
|
145
|
+
/** @private */
|
|
93
146
|
_onerror(err) {
|
|
94
147
|
if (!this._errors.size) return this._report(err)
|
|
95
148
|
const frame = {
|
|
96
149
|
message: err?.message || String(err),
|
|
97
150
|
code: err?.code || '',
|
|
98
|
-
stack: err?.stack || ''
|
|
151
|
+
stack: err?.stack || '',
|
|
152
|
+
reason: err?.reason || ''
|
|
99
153
|
}
|
|
100
154
|
for (const stream of this._errors) stream.write(frame)
|
|
101
155
|
}
|
|
102
156
|
|
|
157
|
+
/**
|
|
158
|
+
* Wire the `errors` and `init` handlers: init waits for the boot and attaches the client.
|
|
159
|
+
* @private
|
|
160
|
+
*/
|
|
103
161
|
_wireInit() {
|
|
104
162
|
this.rpc.onErrors((stream) => {
|
|
105
163
|
this._errors.add(stream)
|
|
@@ -107,75 +165,55 @@ export class Server extends RPCServer {
|
|
|
107
165
|
stream.on('close', () => this._errors.delete(stream))
|
|
108
166
|
})
|
|
109
167
|
this.rpc.onInit(async () => {
|
|
168
|
+
await this._booting
|
|
110
169
|
// a reloaded UI is a new client on the same worker: it re-attaches, its old streams end
|
|
111
|
-
|
|
112
|
-
for (const handle of this._watchStreams.keys()) this._endWatches(handle)
|
|
113
|
-
return this._identity()
|
|
114
|
-
}
|
|
115
|
-
this.me = await cero(this.storage, this.spec, this.opts)
|
|
116
|
-
this.handles.set(this.me.id, this.me)
|
|
117
|
-
await this.me.fileServer.listen()
|
|
118
|
-
this._wireData()
|
|
119
|
-
this._wirePairing()
|
|
120
|
-
this._wireHandles()
|
|
121
|
-
this._wireRestore()
|
|
122
|
-
this._wireSeed()
|
|
170
|
+
for (const handle of this._watchStreams.keys()) this._endWatches(handle)
|
|
123
171
|
return this._identity()
|
|
124
172
|
})
|
|
125
173
|
}
|
|
126
174
|
|
|
127
|
-
/**
|
|
175
|
+
/**
|
|
176
|
+
* Wire the `restore` handler. The phrase becomes a seed here: the UI cannot load the crypto it takes.
|
|
177
|
+
* @private
|
|
178
|
+
*/
|
|
128
179
|
_wireRestore() {
|
|
129
180
|
this.rpc.onRestore(async ({ phrase }) => {
|
|
130
181
|
if (!this.me) throw CeroError.NOT_READY('Server', 'server')
|
|
131
|
-
this.me = await restore(this.me, phrase)
|
|
182
|
+
this.me = await restore(this.me, toSeed(phrase))
|
|
132
183
|
this.handles = new Map([[this.me.id, this.me]])
|
|
133
184
|
return this._identity()
|
|
134
185
|
})
|
|
135
186
|
}
|
|
136
187
|
|
|
137
|
-
/**
|
|
188
|
+
/**
|
|
189
|
+
* Register the row-level RPC handlers (put/set/get/del/watch/call).
|
|
190
|
+
* @private
|
|
191
|
+
*/
|
|
138
192
|
_wireData() {
|
|
139
193
|
this.rpc.onAddFile(async ({ handle, data, name, type }) => {
|
|
140
194
|
const h = this._resolve(handle)
|
|
141
|
-
const
|
|
142
|
-
await
|
|
143
|
-
|
|
144
|
-
const id = encodeId(blobs.key, blobId, type || '')
|
|
145
|
-
const codec = h.spec.codec
|
|
146
|
-
await h.store.call('add-file', { id, name: name || null, stamp: blobs.stamp || 0 })
|
|
147
|
-
const { data: row } = await get(h.files, id)
|
|
148
|
-
return { data: codec.encodeRow(h.files.schema, row) }
|
|
195
|
+
const { data: file } = await put(h.files, { data, type: type || '', name: name || null })
|
|
196
|
+
const { data: row } = await get(h.files, file.id)
|
|
197
|
+
return { data: h.spec.codec.encodeRow(h.files.schema, row) }
|
|
149
198
|
})
|
|
150
199
|
|
|
151
200
|
this.rpc.onAddRow(async ({ handle, ref, data, local }) => {
|
|
152
|
-
const { ref: r, codec } = this._refOf(handle, ref, local)
|
|
153
|
-
const { data: row } = await put(r, codec.decodeRow(r.schema, data))
|
|
201
|
+
const { ref: r, codec, info } = this._refOf(handle, ref, local)
|
|
202
|
+
const { data: row } = await put(r, received(codec.decodeRow(r.schema, data), info))
|
|
154
203
|
return { data: codec.encodeRow(r.schema, row) }
|
|
155
204
|
})
|
|
156
205
|
|
|
157
|
-
this.rpc.onSet(async ({ handle, ref, data, local, noUpsert }) => {
|
|
158
|
-
const { ref: r, codec } = this._refOf(handle, ref, local)
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
return { data: codec.encodeRow(r.schema, result
|
|
206
|
+
this.rpc.onSet(async ({ handle, ref, data, fields, local, noUpsert }) => {
|
|
207
|
+
const { ref: r, codec, info } = this._refOf(handle, ref, local)
|
|
208
|
+
const row = received(codec.decodeRow(r.schema, data), info, fields)
|
|
209
|
+
const result = await set(r, row, { upsert: !noUpsert })
|
|
210
|
+
return { data: result ? codec.encodeRow(r.schema, result.data) : null }
|
|
162
211
|
})
|
|
163
212
|
|
|
164
213
|
this.rpc.onGet(async ({ handle, ref, query, local }) => {
|
|
165
214
|
const { ref: r, codec } = this._refOf(handle, ref, local)
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
r.kind === 'single'
|
|
169
|
-
? codec.encodeRow(r.schema, result.data)
|
|
170
|
-
: codec.encodeRows(r.schema, result.data)
|
|
171
|
-
// total is int on the wire: -1 encodes null
|
|
172
|
-
return { data, total: result.total ?? -1, size: result.size ?? 0 }
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
this.rpc.onGetOne(async ({ handle, ref, id, local }) => {
|
|
176
|
-
const { ref: r, codec } = this._refOf(handle, ref, local)
|
|
177
|
-
const { data } = await get(r, id)
|
|
178
|
-
return { data: data ? codec.encodeRow(r.schema, data) : null }
|
|
215
|
+
const q = fromWire(query)
|
|
216
|
+
return encodeGet(r, codec, q, await get(r, q))
|
|
179
217
|
})
|
|
180
218
|
|
|
181
219
|
this.rpc.onDel(async ({ handle, ref, id, local }) => {
|
|
@@ -184,11 +222,12 @@ export class Server extends RPCServer {
|
|
|
184
222
|
})
|
|
185
223
|
|
|
186
224
|
this.rpc.onWatch((stream) => {
|
|
187
|
-
const { handle, ref,
|
|
225
|
+
const { handle, ref, local } = stream.data
|
|
226
|
+
const query = fromWire(stream.data.query)
|
|
188
227
|
let r, codec, live
|
|
189
228
|
try {
|
|
190
229
|
;({ ref: r, codec } = this._refOf(handle, ref, local))
|
|
191
|
-
live = watch(r,
|
|
230
|
+
live = watch(r, query)
|
|
192
231
|
} catch (err) {
|
|
193
232
|
// forward the error over the wire by destroying the response stream with it
|
|
194
233
|
stream.once('error', () => {})
|
|
@@ -204,11 +243,7 @@ export class Server extends RPCServer {
|
|
|
204
243
|
pending = snap
|
|
205
244
|
return
|
|
206
245
|
}
|
|
207
|
-
|
|
208
|
-
r.kind === 'single'
|
|
209
|
-
? codec.encodeRow(r.schema, snap.data)
|
|
210
|
-
: codec.encodeRows(r.schema, snap.data)
|
|
211
|
-
blocked = stream.write({ data, total: snap.total ?? -1, size: snap.size ?? 0 }) === false
|
|
246
|
+
blocked = stream.write(encodeGet(r, codec, query, snap)) === false
|
|
212
247
|
if (blocked) {
|
|
213
248
|
stream.once('drain', () => {
|
|
214
249
|
blocked = false
|
|
@@ -232,39 +267,6 @@ export class Server extends RPCServer {
|
|
|
232
267
|
})
|
|
233
268
|
})
|
|
234
269
|
|
|
235
|
-
this.rpc.onChanges((stream) => {
|
|
236
|
-
const { handle, ref, query, local } = stream.data
|
|
237
|
-
let r, codec, live
|
|
238
|
-
try {
|
|
239
|
-
;({ ref: r, codec } = this._refOf(handle, ref, local))
|
|
240
|
-
live = changes(r, codec.decodeQuery(query))
|
|
241
|
-
} catch (err) {
|
|
242
|
-
stream.once('error', () => {})
|
|
243
|
-
stream.writeStream.destroy(err)
|
|
244
|
-
stream.destroy()
|
|
245
|
-
return
|
|
246
|
-
}
|
|
247
|
-
let set = this._watchStreams.get(handle)
|
|
248
|
-
if (!set) this._watchStreams.set(handle, (set = new Set()))
|
|
249
|
-
set.add(stream)
|
|
250
|
-
// deltas are not idempotent: hold the iteration on backpressure instead
|
|
251
|
-
const pump = async () => {
|
|
252
|
-
for await (const batch of live) {
|
|
253
|
-
const ok = stream.write({
|
|
254
|
-
changes: codec.encodeChanges(r.schema, batch.changes),
|
|
255
|
-
reset: batch.reset === true
|
|
256
|
-
})
|
|
257
|
-
if (ok === false) await new Promise((resolve) => stream.once('drain', resolve))
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
pump().catch(safetyCatch)
|
|
261
|
-
stream.on('error', safetyCatch)
|
|
262
|
-
stream.on('close', () => {
|
|
263
|
-
live.destroy()
|
|
264
|
-
this._watchStreams.get(handle)?.delete(stream)
|
|
265
|
-
})
|
|
266
|
-
})
|
|
267
|
-
|
|
268
270
|
this.rpc.onCall(async ({ handle, op, data }) => {
|
|
269
271
|
const h = this._resolve(handle)
|
|
270
272
|
const r = h[op]
|
|
@@ -274,61 +276,81 @@ export class Server extends RPCServer {
|
|
|
274
276
|
})
|
|
275
277
|
}
|
|
276
278
|
|
|
277
|
-
/**
|
|
279
|
+
/**
|
|
280
|
+
* Register invite/revoke/join RPC handlers.
|
|
281
|
+
* @private
|
|
282
|
+
*/
|
|
278
283
|
_wirePairing() {
|
|
279
|
-
this.rpc.onInvite(async ({ handle, role,
|
|
280
|
-
const
|
|
281
|
-
const invite = await h.invite({
|
|
284
|
+
this.rpc.onInvite(async ({ handle, role, ttl, reuse, confirm, data }) => {
|
|
285
|
+
const code = await invite(this._resolve(handle), {
|
|
282
286
|
role: role || undefined,
|
|
283
|
-
|
|
284
|
-
reuse: reuse === true
|
|
287
|
+
ttl: ttl || undefined,
|
|
288
|
+
reuse: reuse === true,
|
|
289
|
+
confirm: confirm === true,
|
|
290
|
+
data: data || null
|
|
285
291
|
})
|
|
286
|
-
return { invite }
|
|
292
|
+
return { invite: code }
|
|
287
293
|
})
|
|
288
294
|
|
|
289
|
-
this.rpc.onRevoke(async ({ handle, invite }) => {
|
|
290
|
-
|
|
291
|
-
return { ok }
|
|
295
|
+
this.rpc.onRevoke(async ({ handle, invite: code }) => {
|
|
296
|
+
return { ok: await revoke(this._resolve(handle), code) }
|
|
292
297
|
})
|
|
293
298
|
|
|
294
|
-
this.rpc.onRotate(
|
|
295
|
-
|
|
296
|
-
|
|
299
|
+
this.rpc.onRotate(({ handle }) => rotate(this._resolve(handle)))
|
|
300
|
+
|
|
301
|
+
this.rpc.onAnswer(async ({ handle, id, accept, role, reason }) => {
|
|
302
|
+
await this._resolve(handle)._answer(id, { accept, role: role || undefined, reason })
|
|
303
|
+
return {}
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
this.rpc.onSuspend(async ({ handle }) => {
|
|
307
|
+
await suspend(this._resolve(handle))
|
|
308
|
+
return {}
|
|
297
309
|
})
|
|
298
310
|
|
|
299
|
-
this.rpc.
|
|
300
|
-
this._resolve(handle)
|
|
311
|
+
this.rpc.onResume(async ({ handle }) => {
|
|
312
|
+
await resume(this._resolve(handle))
|
|
301
313
|
return {}
|
|
302
314
|
})
|
|
303
315
|
|
|
304
|
-
this.rpc.
|
|
305
|
-
await this.
|
|
316
|
+
this.rpc.onSetActive(async ({ handle, active: on }) => {
|
|
317
|
+
await (on ? activate : deactivate)(this._resolve(handle))
|
|
306
318
|
return {}
|
|
307
319
|
})
|
|
308
320
|
|
|
309
|
-
this.rpc.
|
|
310
|
-
await this.me
|
|
321
|
+
this.rpc.onNearby(async ({ on, invite }) => {
|
|
322
|
+
await nearby(this.me, invite || on)
|
|
311
323
|
return {}
|
|
312
324
|
})
|
|
313
325
|
|
|
326
|
+
this.rpc.onCancel(async ({ invite: code }) => ({ ok: await cancel(this.me, code) }))
|
|
327
|
+
|
|
314
328
|
this.rpc.onJoin(async ({ parent, ref, invite }) => {
|
|
315
329
|
if (this._resolve(parent) !== this.me) throw CeroError.UNSUPPORTED('nested handles')
|
|
316
|
-
|
|
330
|
+
let child
|
|
331
|
+
try {
|
|
332
|
+
child = await this.me._join(invite, ref)
|
|
333
|
+
} catch (err) {
|
|
334
|
+
if (err.code !== 'DENIED') throw err
|
|
335
|
+
return { id: '', type: ref, denied: true, reason: err.reason || '' }
|
|
336
|
+
}
|
|
317
337
|
const id = child.id
|
|
318
338
|
this.handles.set(id, child)
|
|
319
339
|
return { id, type: ref, name: '' }
|
|
320
340
|
})
|
|
321
341
|
}
|
|
322
342
|
|
|
323
|
-
/**
|
|
343
|
+
/**
|
|
344
|
+
* Register add/open/close/leave RPC handlers for child handles.
|
|
345
|
+
* @private
|
|
346
|
+
*/
|
|
324
347
|
_wireHandles() {
|
|
325
348
|
this.rpc.onAddHandle(async ({ handle, ref, data }) => {
|
|
326
349
|
const parent = this._resolve(handle)
|
|
327
350
|
const info = parent.spec.meta.refs?.[ref] || parent.spec.handles?.[ref]
|
|
328
351
|
if (!info) throw CeroError.UNKNOWN('handle type', ref)
|
|
329
352
|
const wire = parent.spec.codec.decodeCreate(data) || {}
|
|
330
|
-
|
|
331
|
-
const opts = { name: wire.name, role: wire.role, accept: wire.noAccept ? false : undefined }
|
|
353
|
+
const opts = { name: wire.name }
|
|
332
354
|
const child = await this.me._create(ref, opts)
|
|
333
355
|
const id = child.id
|
|
334
356
|
this.handles.set(id, child)
|
|
@@ -354,12 +376,9 @@ export class Server extends RPCServer {
|
|
|
354
376
|
})
|
|
355
377
|
|
|
356
378
|
this.rpc.onLeave(async ({ handle }) => {
|
|
357
|
-
|
|
358
|
-
if (!h || h === this.me) return {}
|
|
379
|
+
await leave(this._resolve(handle))
|
|
359
380
|
this._endWatches(handle)
|
|
360
|
-
await this.me.store.call('del-handle', { id: handle })
|
|
361
381
|
this.handles.delete(handle)
|
|
362
|
-
await h.close()
|
|
363
382
|
return {}
|
|
364
383
|
})
|
|
365
384
|
}
|
|
@@ -369,7 +388,8 @@ export class Server extends RPCServer {
|
|
|
369
388
|
* codec on first use.
|
|
370
389
|
*
|
|
371
390
|
* @param {string} id
|
|
372
|
-
* @returns {
|
|
391
|
+
* @returns {Context}
|
|
392
|
+
* @private
|
|
373
393
|
*/
|
|
374
394
|
_resolve(id) {
|
|
375
395
|
const h = this.handles.get(id)
|
|
@@ -385,6 +405,7 @@ export class Server extends RPCServer {
|
|
|
385
405
|
* @param {string} name
|
|
386
406
|
* @param {boolean} [local]
|
|
387
407
|
* @returns {RefAndCodec}
|
|
408
|
+
* @private
|
|
388
409
|
*/
|
|
389
410
|
_refOf(id, name, local) {
|
|
390
411
|
if (local) {
|
|
@@ -392,34 +413,39 @@ export class Server extends RPCServer {
|
|
|
392
413
|
if (!info || info.internal) throw CeroError.UNKNOWN('local ref', name)
|
|
393
414
|
const r = this.me.local?.[name]
|
|
394
415
|
if (!r) throw CeroError.UNKNOWN('local ref', name)
|
|
395
|
-
return { ref: r, codec: this.spec.local.codec }
|
|
416
|
+
return { ref: r, codec: this.spec.local.codec, info }
|
|
396
417
|
}
|
|
397
418
|
const h = this._resolve(id)
|
|
398
419
|
const r = h[name]
|
|
399
420
|
if (!r) throw CeroError.UNKNOWN('ref', name)
|
|
400
|
-
return { ref: r, codec: h.spec.codec }
|
|
421
|
+
return { ref: r, codec: h.spec.codec, info: h.spec.meta?.refs?.[name] }
|
|
401
422
|
}
|
|
402
423
|
|
|
403
424
|
/**
|
|
404
425
|
* Snapshot the current identity for return to the client.
|
|
405
426
|
*
|
|
406
427
|
* @returns {Identity}
|
|
428
|
+
* @private
|
|
407
429
|
*/
|
|
408
430
|
_identity() {
|
|
409
431
|
const fs = this.me.fileServer
|
|
410
432
|
return {
|
|
411
433
|
id: this.me.id,
|
|
412
434
|
deviceId: this.me.device?.id || '',
|
|
435
|
+
deviceName: this.me.device?.name || '',
|
|
413
436
|
fileBase: `http://127.0.0.1:${fs.port}`,
|
|
414
437
|
fileToken: fs.server.token || ''
|
|
415
438
|
}
|
|
416
439
|
}
|
|
417
440
|
|
|
418
|
-
/**
|
|
441
|
+
/**
|
|
442
|
+
* Wire the on-demand `seed` handler — surfaces the recovery phrase only when asked.
|
|
443
|
+
* @private
|
|
444
|
+
*/
|
|
419
445
|
_wireSeed() {
|
|
420
446
|
this.rpc.onSeed(async () => {
|
|
421
447
|
if (!this.me) throw CeroError.NOT_READY('Server', 'server')
|
|
422
|
-
return { phrase: this.me
|
|
448
|
+
return { phrase: await phrase(this.me) }
|
|
423
449
|
})
|
|
424
450
|
}
|
|
425
451
|
}
|
|
@@ -427,8 +453,8 @@ export class Server extends RPCServer {
|
|
|
427
453
|
/**
|
|
428
454
|
* Construct a `Server`, wait for it to be ready, and return it.
|
|
429
455
|
*
|
|
430
|
-
* @param {
|
|
431
|
-
* @param {
|
|
456
|
+
* @param {import('streamx').Duplex} ipc
|
|
457
|
+
* @param {Spec} spec
|
|
432
458
|
* @param {ServerOpts} opts
|
|
433
459
|
* @returns {Promise<Server>}
|
|
434
460
|
*/
|
|
@@ -437,3 +463,30 @@ export async function serve(ipc, spec, opts) {
|
|
|
437
463
|
await server.ready()
|
|
438
464
|
return server
|
|
439
465
|
}
|
|
466
|
+
|
|
467
|
+
// a query crosses as the client wrote it; no bytes is no query
|
|
468
|
+
function fromWire(buf) {
|
|
469
|
+
return buf ? c.decode(c.any, buf) : undefined
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* One row for an id or a single ref, the list otherwise; total is int on the wire: -1 encodes null.
|
|
474
|
+
* @param {GetResult} res
|
|
475
|
+
*/
|
|
476
|
+
function encodeGet(r, codec, query, { data, total, size }) {
|
|
477
|
+
const one = typeof query === 'string' || r.kind === 'single'
|
|
478
|
+
return {
|
|
479
|
+
data: one ? codec.encodeRow(r.schema, data) : codec.encodeRows(r.schema, data),
|
|
480
|
+
total: total ?? -1,
|
|
481
|
+
size: size ?? 0
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// a typed row decodes every field, the ones the client left out as defaults: keep what was sent
|
|
486
|
+
function received(row, info, sent = null) {
|
|
487
|
+
const declared = info?.fields
|
|
488
|
+
if (!declared) return row
|
|
489
|
+
const out = row.id ? { id: row.id } : {}
|
|
490
|
+
for (const i of sent ?? declared.keys()) out[declared[i]] = row[declared[i]]
|
|
491
|
+
return out
|
|
492
|
+
}
|
package/types/build/index.d.ts
CHANGED
|
@@ -9,13 +9,9 @@ export type BuildOpts = {
|
|
|
9
9
|
*/
|
|
10
10
|
ns?: string;
|
|
11
11
|
/**
|
|
12
|
-
* The extensions to fold in. A module specifier, relative to `specDir`, is imported here for its `extensions` export and written into the spec, so every process runs the same list. A list is folded in only. The bundled two by default, `[]` for none.
|
|
12
|
+
* The extensions to fold in. A module specifier, relative to `specDir`, is imported here for its `extensions` export and written into the spec, so every process runs the same list. A list is folded in only, and `cero()` then needs it in its options. The bundled two by default, `[]` for none.
|
|
13
13
|
*/
|
|
14
14
|
extensions?: string | import('../extensions/index.js').Extension[];
|
|
15
|
-
/**
|
|
16
|
-
* A module specifier, relative to `specDir`, written into the spec for its `operators` export, so every process binds the same map.
|
|
17
|
-
*/
|
|
18
|
-
operators?: string;
|
|
19
15
|
};
|
|
20
16
|
/**
|
|
21
17
|
* @typedef {import('@cero-base/core/schema').Schema} Schema
|
|
@@ -24,8 +20,7 @@ export type BuildOpts = {
|
|
|
24
20
|
*
|
|
25
21
|
* @typedef {object} BuildOpts
|
|
26
22
|
* @property {string} [ns] Namespace prefix for emitted schema ids. Defaults to `'cero'`.
|
|
27
|
-
* @property {string | import('../extensions/index.js').Extension[]} [extensions] The extensions to fold in. A module specifier, relative to `specDir`, is imported here for its `extensions` export and written into the spec, so every process runs the same list. A list is folded in only. The bundled two by default, `[]` for none.
|
|
28
|
-
* @property {string} [operators] A module specifier, relative to `specDir`, written into the spec for its `operators` export, so every process binds the same map.
|
|
23
|
+
* @property {string | import('../extensions/index.js').Extension[]} [extensions] The extensions to fold in. A module specifier, relative to `specDir`, is imported here for its `extensions` export and written into the spec, so every process runs the same list. A list is folded in only, and `cero()` then needs it in its options. The bundled two by default, `[]` for none.
|
|
29
24
|
*/
|
|
30
25
|
/**
|
|
31
26
|
* Compile a cero schema into wire-level artifacts and write them to disk.
|
|
@@ -35,4 +30,4 @@ export type BuildOpts = {
|
|
|
35
30
|
* @param {BuildOpts} [opts]
|
|
36
31
|
* @returns {Promise<void>}
|
|
37
32
|
*/
|
|
38
|
-
export declare function build(specDir: string, schema: SchemaInput, { ns, extensions
|
|
33
|
+
export declare function build(specDir: string, schema: SchemaInput, { ns, extensions }?: BuildOpts): Promise<void>;
|