@cero-base/core 1.18.2 → 2.0.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.
Files changed (57) hide show
  1. package/README.md +5 -6
  2. package/package.json +14 -11
  3. package/src/blobs/index.js +17 -17
  4. package/src/blobs/server.js +0 -2
  5. package/src/database/changes.js +34 -60
  6. package/src/database/dispatch.js +156 -245
  7. package/src/database/encryption.js +22 -52
  8. package/src/database/envelope.js +1 -3
  9. package/src/database/index.js +514 -513
  10. package/src/database/rotation.js +191 -193
  11. package/src/identity/index.js +8 -12
  12. package/src/index.js +5 -9
  13. package/src/lib/constants.js +3 -12
  14. package/src/lib/errors.js +6 -10
  15. package/src/lib/schema.js +2 -5
  16. package/src/lib/utils.js +55 -38
  17. package/src/network/index.js +138 -166
  18. package/src/pairing/candidate.js +123 -0
  19. package/src/pairing/index.js +36 -267
  20. package/src/pairing/invite.js +3 -4
  21. package/src/pairing/request.js +96 -0
  22. package/src/rpc/index.js +4 -14
  23. package/src/rpc/peer.js +3 -7
  24. package/src/storage/index.js +36 -49
  25. package/types/blobs/codec.d.ts +8 -8
  26. package/types/blobs/index.d.ts +38 -37
  27. package/types/blobs/server.d.ts +8 -10
  28. package/types/database/changes.d.ts +3 -8
  29. package/types/database/dispatch.d.ts +28 -25
  30. package/types/database/encryption.d.ts +22 -30
  31. package/types/database/envelope.d.ts +2 -2
  32. package/types/database/index.d.ts +196 -221
  33. package/types/database/rotation.d.ts +47 -30
  34. package/types/identity/index.d.ts +83 -87
  35. package/types/index.d.ts +42 -55
  36. package/types/lib/constants.d.ts +34 -38
  37. package/types/lib/errors.d.ts +16 -20
  38. package/types/lib/schema.d.ts +49 -42
  39. package/types/lib/spec/index.d.ts +29 -55
  40. package/types/lib/utils.d.ts +26 -20
  41. package/types/network/discovery.d.ts +7 -7
  42. package/types/network/index.d.ts +64 -72
  43. package/types/pairing/candidate.d.ts +36 -0
  44. package/types/pairing/index.d.ts +91 -151
  45. package/types/pairing/invite.d.ts +91 -91
  46. package/types/pairing/request.d.ts +133 -0
  47. package/types/rpc/client.d.ts +2 -2
  48. package/types/rpc/index.d.ts +30 -33
  49. package/types/rpc/peer.d.ts +11 -11
  50. package/types/rpc/server.d.ts +2 -2
  51. package/types/storage/index.d.ts +70 -71
  52. package/src/database/bootstrap.js +0 -133
  53. package/src/database/hooks.js +0 -112
  54. package/src/database/membership.js +0 -67
  55. package/types/database/bootstrap.d.ts +0 -20
  56. package/types/database/hooks.d.ts +0 -8
  57. package/types/database/membership.d.ts +0 -7
@@ -3,22 +3,14 @@ import ReadyResource from 'ready-resource'
3
3
  import safetyCatch from 'safety-catch'
4
4
  import b4a from 'b4a'
5
5
  import c from 'compact-encoding'
6
- import { discoveryKey, keyPair, sign } from 'hypercore-crypto'
6
+ import { discoveryKey } from 'hypercore-crypto'
7
7
 
8
8
  import { Invite } from './invite.js'
9
+ import { Candidate } from './candidate.js'
10
+ import { Request } from './request.js'
9
11
  import { channelTopic } from '../network/index.js'
10
- import { getEncoding } from '../lib/spec/index.js'
11
12
  import { CeroError } from '../lib/errors.js'
12
13
 
13
- const STATUS_ACCEPTED = 0
14
- const STATUS_DENIED = 1
15
-
16
- // Wire envelope carried in BlindPairing's `additional.data` on the confirm response.
17
- // We piggyback on the confirm path for *both* success and deny — blind-pairing's
18
- // native deny() has no payload field, and its discoveryKey check pins the wire
19
- // `key` to the invite, so the real resource key flows through this envelope.
20
- const Response = getEncoding('@cero/confirm')
21
-
22
14
  /**
23
15
  * @typedef {object} PairingOpts
24
16
  * @property {import('../network/index.js').Network} network Network to host the BlindPairing member.
@@ -36,30 +28,15 @@ const Response = getEncoding('@cero/confirm')
36
28
  * @property {boolean} [reuse] Reusable until expiry/revoke; default `false` (consumed on first settle).
37
29
  *
38
30
  * @typedef {object} JoinOpts
39
- * @property {any} [userData] Payload sent with the candidate request.
31
+ * @property {any} userData Payload sent with the candidate request. Required: blind-pairing derives the handshake token from it, so a candidate without one is never served.
40
32
  * @property {number} [timeout] Deadline for the handshake, in ms. Defaults to 30000.
41
33
  *
42
- * @typedef {object} ConfirmOpts
43
- * @property {Uint8Array} [key] 32-byte resource key delivered to the joiner; required at runtime.
44
- * @property {Uint8Array | null} [encryptionKey] Optional 32-byte symmetric key.
45
- * @property {Uint8Array | null} [additional] Extra opaque bytes piggybacked on the response.
46
- *
47
34
  * @typedef {{ key: Uint8Array, encryptionKey: Uint8Array | null, additional: Uint8Array | null }} JoinResult
48
- *
49
- * @typedef {object} RequestOpts
50
- * @property {Pairing} pairing Owning Pairing instance.
51
- * @property {any} req blind-pairing candidate request being answered.
52
- * @property {Invite} invite Invite the candidate paired against.
53
- * @property {Uint8Array} seed Per-invite seed used to sign the response.
54
- * @property {any} userData Decoded joiner payload (raw bytes when no encoding).
55
- * @property {() => void} onsettle Called once when the candidate is confirmed or denied.
56
35
  */
57
36
 
58
37
  /**
59
- * Blind-pairing membership wrapper. Hosts invites, dispatches incoming
60
- * candidates to the application for accept/deny, and provides the joiner
61
- * side of the handshake. All wire payloads are signed and routed through
62
- * the supplied network.
38
+ * Blind-pairing membership wrapper. Hosts invites, dispatches incoming candidates to the
39
+ * application for accept/deny, and provides the joiner side of the handshake.
63
40
  */
64
41
  export class Pairing extends ReadyResource {
65
42
  /** @param {PairingOpts} [opts] */
@@ -84,8 +61,7 @@ export class Pairing extends ReadyResource {
84
61
  this.inviteEncoding = inviteEncoding
85
62
  this.joinerEncoding = joinerEncoding
86
63
  this._onerror = onerror
87
- // Fired with an invite's hex id when this instance stops serving it
88
- // (single-use settled, or expired) — lets the owner drop its persisted row.
64
+ // fired when this instance stops serving an invite, so the owner drops its row
89
65
  this.onconsume = onconsume
90
66
 
91
67
  this._blind = null
@@ -94,14 +70,21 @@ export class Pairing extends ReadyResource {
94
70
  this._candidates = new Set() // active joiner candidates
95
71
  }
96
72
 
73
+ /**
74
+ * Whether the underlying blind-pairing layer is suspended.
75
+ *
76
+ * @returns {boolean}
77
+ */
78
+ get suspended() {
79
+ return this._blind?.suspended === true
80
+ }
81
+
97
82
  async _open() {
98
83
  await this.network.ready()
99
84
  // shared instance — one set of swarm/DHT listeners for every handle
100
85
  this._blind = await this.network.blind()
101
86
 
102
- // blind-pairing allows ONE member listener per discovery key — a join-only
103
- // pairing must not register one, or concurrent joins under the same
104
- // identity throw 'Active member already exist'
87
+ // blind-pairing allows one member listener per discovery key
105
88
  if (this.host) {
106
89
  this._member = this._blind.addMember({
107
90
  // channel-scoped; the candidate hashes identically (no channel → identity)
@@ -125,22 +108,6 @@ export class Pairing extends ReadyResource {
125
108
  this._invites.clear()
126
109
  }
127
110
 
128
- /**
129
- * Resolve the resource discovery key an invite targets — without pairing.
130
- * Lets callers detect "already joined" before any handshake. Returns `null`
131
- * for an unparseable invite.
132
- *
133
- * @param {string} invite
134
- * @returns {Uint8Array | null}
135
- */
136
- static inviteTopic(invite) {
137
- try {
138
- return Invite.parse(invite).discoveryKey
139
- } catch {
140
- return null
141
- }
142
- }
143
-
144
111
  /**
145
112
  * Mint a new pairing invite. Returns its canonical wire form (z32 string).
146
113
  *
@@ -149,13 +116,11 @@ export class Pairing extends ReadyResource {
149
116
  */
150
117
  async createInvite({ role = '', expiresIn = 0, data = null, reuse = false } = {}) {
151
118
  if (this.closing || this.closed) throw CeroError.CLOSED('Pairing')
152
- // a join-only pairing has no member listener — an invite it mints would
153
- // never be served, so fail loudly instead of handing out a dead invite
119
+ // a join-only pairing serves no invites
154
120
  if (!this.host) throw CeroError.INVALID('cannot create invites on a join-only Pairing')
155
121
  if (!this.opened) await this.ready()
156
122
 
157
- // Rendezvous key matches the Member topic so candidates land on the right
158
- // Pairing instance (per-handle keys avoid identity-wide collisions).
123
+ // the rendezvous key matches the member topic
159
124
  const blind = BlindPairing.createInvite(this.topic)
160
125
 
161
126
  const invite = Invite.create({
@@ -175,8 +140,7 @@ export class Pairing extends ReadyResource {
175
140
  publicKey: blind.publicKey,
176
141
  invite,
177
142
  reuse,
178
- // minted here and possibly not yet persisted/replicated — syncRows must
179
- // never drop it for being absent from the store
143
+ // minted here, maybe not persisted yet: syncRows must keep it
180
144
  local: true
181
145
  })
182
146
 
@@ -197,11 +161,7 @@ export class Pairing extends ReadyResource {
197
161
  }
198
162
 
199
163
  /**
200
- * Reconcile the served-invite set with persisted rows (the room's `invites`
201
- * collection). Adds unknown rows so ANY member replica can serve them across
202
- * restarts; drops replicated records whose row disappeared (revoked or
203
- * consumed elsewhere). Locally-minted records not yet visible as rows are
204
- * kept.
164
+ * Reconcile the served-invite set with persisted rows (the room's `invites` collection).
205
165
  *
206
166
  * @param {Array<{ id: string, invite: Uint8Array, publicKey: Uint8Array, seed: Uint8Array, reuse?: boolean }>} rows
207
167
  */
@@ -253,16 +213,6 @@ export class Pairing extends ReadyResource {
253
213
  return false
254
214
  }
255
215
 
256
- /**
257
- * Cheap structural test for a candidate invite string.
258
- *
259
- * @param {unknown} str
260
- * @returns {boolean}
261
- */
262
- static isInvite(str) {
263
- return Invite.isInvite(str)
264
- }
265
-
266
216
  /**
267
217
  * Joiner side — start a pairing handshake against `inviteStr` and resolve
268
218
  * with the host's response when the host confirms.
@@ -271,7 +221,7 @@ export class Pairing extends ReadyResource {
271
221
  * @param {JoinOpts} [opts]
272
222
  * @returns {Promise<JoinResult>}
273
223
  */
274
- async join(inviteStr, { userData = null, timeout = 30000 } = {}) {
224
+ async join(inviteStr, { userData, timeout = 30000 } = {}) {
275
225
  if (this.closing || this.closed) throw CeroError.CLOSED('Pairing')
276
226
  if (!this.opened) await this.ready()
277
227
 
@@ -290,15 +240,6 @@ export class Pairing extends ReadyResource {
290
240
  return candidate.start()
291
241
  }
292
242
 
293
- /**
294
- * Whether the underlying blind-pairing layer is suspended.
295
- *
296
- * @returns {boolean}
297
- */
298
- get suspended() {
299
- return this._blind?.suspended === true
300
- }
301
-
302
243
  /**
303
244
  * Pause blind-pairing — keeps state, drops sockets. Idempotent.
304
245
  *
@@ -364,190 +305,34 @@ export class Pairing extends ReadyResource {
364
305
 
365
306
  this.emit('candidate', request)
366
307
  }
367
- }
368
-
369
- /**
370
- * Internal — a pairing request from an incoming candidate, awaiting the host's
371
- * accept/deny. Built in `_oncandidate` and emitted on the `'candidate'` event so
372
- * the host can decide via `confirm` / `deny`. (The candidate is the joiner; this
373
- * is the host's handle to answer it.)
374
- *
375
- * @property {boolean} _settled Whether confirm/deny has already run.
376
- */
377
- class Request {
378
- /** @param {RequestOpts} opts */
379
- constructor({ pairing, req, invite, seed, userData, onsettle }) {
380
- this.pairing = pairing
381
- this._req = req
382
- this._seed = seed
383
- this._settled = false
384
- this._onsettle = onsettle
385
-
386
- this.invite = invite
387
- this.userData = userData
388
- this.publicKey = req.publicKey
389
- }
390
308
 
391
309
  /**
392
- * Accept the candidate and reveal the resource key. Idempotent.
310
+ * Resolve the resource discovery key an invite targets — without pairing.
393
311
  *
394
- * @param {ConfirmOpts} opts
395
- * @returns {Promise<void>}
312
+ * @param {string} invite
313
+ * @returns {Uint8Array | null}
396
314
  */
397
- async confirm({ key, encryptionKey = null, additional = null } = {}) {
398
- if (this._settled) return
399
- if (!key || key.length !== 32) throw CeroError.INVALID('key must be a 32-byte buffer')
400
- if (encryptionKey && encryptionKey.length !== 32) {
401
- throw CeroError.INVALID('encryptionKey must be a 32-byte buffer')
402
- }
403
- if (additional && !b4a.isBuffer(additional)) {
404
- throw CeroError.INVALID('additional must be a buffer')
315
+ static inviteTopic(invite) {
316
+ try {
317
+ return Invite.parse(invite).discoveryKey
318
+ } catch {
319
+ return null
405
320
  }
406
- this._respond({ status: STATUS_ACCEPTED, reason: '', key, encryptionKey, extra: additional })
407
321
  }
408
322
 
409
323
  /**
410
- * Reject the candidate with an optional reason. Idempotent.
411
- *
412
- * @param {string} [reason]
413
- * @returns {Promise<void>}
414
- */
415
- async deny(reason = '') {
416
- if (this._settled) return
417
- this._respond({
418
- status: STATUS_DENIED,
419
- reason,
420
- key: null,
421
- encryptionKey: null,
422
- extra: null
423
- })
424
- }
425
-
426
- // sign the envelope with the per-invite seed and answer the candidate
427
- // (callers guard idempotency before this runs)
428
- _respond(envelope) {
429
- this._settled = true
430
- this._onsettle()
431
- const data = c.encode(Response, envelope)
432
- const signature = sign(data, keyPair(this._seed).secretKey)
433
- this._req.confirm({
434
- key: this.pairing.topic,
435
- encryptionKey: undefined,
436
- additional: { data, signature }
437
- })
438
- }
439
- }
440
-
441
- /**
442
- * Internal — the joiner side: a candidate's handshake state machine (this *is* the
443
- * blind-pairing `addCandidate`). One-shot: created by `Pairing.join()`, settled
444
- * exactly once via `_done` or `_fail`.
445
- *
446
- * @property {any} _candidate blind-pairing addCandidate handle (null until started).
447
- * @property {ReturnType<typeof setTimeout> | null} _timer Pending timeout, if any.
448
- * @property {((result: JoinResult) => void) | null} _resolve Promise resolver, set in `start`.
449
- * @property {((err: Error) => void) | null} _reject Promise rejecter, set in `start`.
450
- * @property {boolean} _settled Whether the handshake has already settled.
451
- */
452
- class Candidate {
453
- /**
454
- * @param {Pairing} pairing
455
- * @param {Invite} invite
456
- * @param {Uint8Array | null} encodedUserData
457
- * @param {number} timeout
458
- */
459
- constructor(pairing, invite, encodedUserData, timeout) {
460
- this.pairing = pairing
461
- this.invite = invite
462
- this.userData = encodedUserData
463
- this.timeout = timeout
464
- this._candidate = null
465
- this._timer = null
466
- this._resolve = null
467
- this._reject = null
468
- this._settled = false
469
- }
470
-
471
- /**
472
- * Kick off the handshake; returns a promise that settles with the host response.
324
+ * Cheap structural test for a candidate invite string.
473
325
  *
474
- * @returns {Promise<JoinResult>}
326
+ * @param {unknown} str
327
+ * @returns {boolean}
475
328
  */
476
- start() {
477
- this.pairing._candidates.add(this)
478
-
479
- return new Promise((resolve, reject) => {
480
- this._resolve = resolve
481
- this._reject = reject
482
-
483
- if (this.timeout > 0) {
484
- this._timer = setTimeout(() => this._fail(CeroError.TIMEOUT()), this.timeout)
485
- }
486
-
487
- try {
488
- this._candidate = this.pairing._blind.addCandidate({
489
- invite: this.invite.blind,
490
- userData: this.userData,
491
- // must hash exactly like the member side — see Pairing._open
492
- discoveryKey: channelTopic(this.invite.discoveryKey, this.pairing.network.channel),
493
- onadd: (result) => this._done(result)
494
- })
495
- this._candidate.request.on('rejected', (err) => this._fail(fromBlindError(err)))
496
- this.pairing.network.refreshInjected().catch(this._onerror ?? (() => {}))
497
- } catch (err) {
498
- this._fail(err instanceof CeroError ? err : CeroError.NETWORK_ERROR(err.message))
499
- }
500
- })
501
- }
502
-
503
- _done(result) {
504
- if (this._settled) return
505
-
506
- let envelope = null
507
- const buf = result?.data ?? null
508
- if (buf) {
509
- try {
510
- envelope = c.decode(Response, buf)
511
- } catch {
512
- envelope = null
513
- }
514
- }
515
-
516
- if (envelope && envelope.status === STATUS_DENIED) {
517
- this._fail(CeroError.DENIED(envelope.reason || null))
518
- return
519
- }
520
-
521
- if (!envelope) {
522
- this._fail(CeroError.NETWORK_ERROR('malformed pairing response'))
523
- return
524
- }
525
-
526
- this._settled = true
527
- this.pairing._candidates.delete(this)
528
- clearTimeout(this._timer)
529
-
530
- this._resolve({
531
- key: envelope.key,
532
- encryptionKey: envelope.encryptionKey,
533
- additional: envelope.extra
534
- })
535
-
536
- this._candidate.close().catch(safetyCatch)
537
- }
538
-
539
- _fail(err) {
540
- if (this._settled) return
541
- this._settled = true
542
- this.pairing._candidates.delete(this)
543
- clearTimeout(this._timer)
544
- if (this._candidate) this._candidate.close().catch(safetyCatch)
545
- this._reject(err)
329
+ static isInvite(str) {
330
+ return Invite.isInvite(str)
546
331
  }
547
332
  }
548
333
 
549
334
  function encodeUserData(data, encoding) {
550
- if (data == null) return null
335
+ if (data == null) throw CeroError.REQUIRED('userData')
551
336
  if (encoding) return c.encode(encoding, data)
552
337
  if (b4a.isBuffer(data)) return data
553
338
  throw CeroError.INVALID('userData must be a buffer when no joinerEncoding is provided')
@@ -562,19 +347,3 @@ function decodeUserData(buf, encoding) {
562
347
  return buf
563
348
  }
564
349
  }
565
-
566
- function fromBlindError(err) {
567
- // code-less rejections are protocol failures (undecodable reply, key
568
- // mismatch, bad signature) — never a host decision, don't report DENIED
569
- if (!err || !err.code) return CeroError.NETWORK_ERROR(err?.message || 'pairing failed')
570
- switch (err.code) {
571
- case 'INVITE_EXPIRED':
572
- return CeroError.EXPIRED(err.message)
573
- case 'INVITE_USED':
574
- return CeroError.DENIED('used', err.message)
575
- case 'PAIRING_REJECTED':
576
- return CeroError.DENIED(null, err.message)
577
- default:
578
- return CeroError.DENIED(err.message)
579
- }
580
- }
@@ -35,9 +35,9 @@ const SignBody = getEncoding('@cero/invite-body')
35
35
  */
36
36
 
37
37
  /**
38
- * Signed, expirable pairing invite. Wraps a blind-pairing invite with a
39
- * role, optional payload and an Ed25519 signature so the host can be
40
- * authenticated by the joiner before any handshake happens.
38
+ * Signed, expirable pairing invite. Wraps a blind-pairing invite with a role, optional
39
+ * payload and an Ed25519 signature so the host can be authenticated by the joiner before
40
+ * any handshake happens.
41
41
  *
42
42
  * @property {any} [_rawData] Decoded payload kept alongside the encoded `data` for convenience.
43
43
  * @property {Uint8Array} _discoveryKey Cached discovery key of the wrapped blind invite.
@@ -54,7 +54,6 @@ export class Invite {
54
54
  this.sig = fields.sig
55
55
  this._str = fields._str || null
56
56
 
57
- // cached discovery key of the wrapped blind invite
58
57
  this._discoveryKey = null
59
58
  }
60
59
 
@@ -0,0 +1,96 @@
1
+ import b4a from 'b4a'
2
+ import c from 'compact-encoding'
3
+ import { keyPair, sign } from 'hypercore-crypto'
4
+
5
+ import { getEncoding } from '../lib/spec/index.js'
6
+ import { CeroError } from '../lib/errors.js'
7
+
8
+ export const STATUS_ACCEPTED = 0
9
+ export const STATUS_DENIED = 1
10
+
11
+ // success and deny both ride the confirm response
12
+ export const Response = getEncoding('@cero/confirm')
13
+
14
+ /**
15
+ * @typedef {object} ConfirmOpts
16
+ * @property {Uint8Array} [key] 32-byte resource key delivered to the joiner; required at runtime.
17
+ * @property {Uint8Array | null} [encryptionKey] Optional 32-byte symmetric key.
18
+ * @property {Uint8Array | null} [additional] Extra opaque bytes piggybacked on the response.
19
+ *
20
+ * @typedef {object} RequestOpts
21
+ * @property {import('./index.js').Pairing} pairing Owning Pairing instance.
22
+ * @property {any} req blind-pairing candidate request being answered.
23
+ * @property {import('./invite.js').Invite} invite Invite the candidate paired against.
24
+ * @property {Uint8Array} seed Per-invite seed used to sign the response.
25
+ * @property {any} userData Decoded joiner payload (raw bytes when no encoding).
26
+ * @property {() => void} onsettle Called once when the candidate is confirmed or denied.
27
+ */
28
+
29
+ /**
30
+ * Internal — a pairing request from an incoming candidate, awaiting the host's
31
+ * accept/deny.
32
+ *
33
+ * @property {boolean} _settled Whether confirm/deny has already run.
34
+ */
35
+ export class Request {
36
+ /** @param {RequestOpts} opts */
37
+ constructor(opts) {
38
+ this.pairing = opts.pairing
39
+ this._req = opts.req
40
+ this._seed = opts.seed
41
+ this._settled = false
42
+ this._onsettle = opts.onsettle
43
+
44
+ this.invite = opts.invite
45
+ this.userData = opts.userData
46
+ this.publicKey = opts.req.publicKey
47
+ }
48
+
49
+ /**
50
+ * Accept the candidate and reveal the resource key. Idempotent.
51
+ *
52
+ * @param {ConfirmOpts} opts
53
+ * @returns {Promise<void>}
54
+ */
55
+ async confirm({ key, encryptionKey = null, additional = null } = {}) {
56
+ if (this._settled) return
57
+ if (!key || key.length !== 32) throw CeroError.INVALID('key must be a 32-byte buffer')
58
+ if (encryptionKey && encryptionKey.length !== 32) {
59
+ throw CeroError.INVALID('encryptionKey must be a 32-byte buffer')
60
+ }
61
+ if (additional && !b4a.isBuffer(additional)) {
62
+ throw CeroError.INVALID('additional must be a buffer')
63
+ }
64
+ this._respond({ status: STATUS_ACCEPTED, reason: '', key, encryptionKey, extra: additional })
65
+ }
66
+
67
+ /**
68
+ * Reject the candidate with an optional reason. Idempotent.
69
+ *
70
+ * @param {string} [reason]
71
+ * @returns {Promise<void>}
72
+ */
73
+ async deny(reason = '') {
74
+ if (this._settled) return
75
+ this._respond({
76
+ status: STATUS_DENIED,
77
+ reason,
78
+ key: null,
79
+ encryptionKey: null,
80
+ extra: null
81
+ })
82
+ }
83
+
84
+ // signed with the per-invite seed
85
+ _respond(envelope) {
86
+ this._settled = true
87
+ this._onsettle()
88
+ const data = c.encode(Response, envelope)
89
+ const signature = sign(data, keyPair(this._seed).secretKey)
90
+ this._req.confirm({
91
+ key: this.pairing.topic,
92
+ encryptionKey: undefined,
93
+ additional: { data, signature }
94
+ })
95
+ }
96
+ }
package/src/rpc/index.js CHANGED
@@ -9,8 +9,7 @@ export { RPCClient } from './client.js'
9
9
 
10
10
  const EMPTY = b4a.alloc(0)
11
11
 
12
- // Default envelope encodings for the @cero namespace, used when the supplied
13
- // spec does not provide its own rows/query/create types in its schema.
12
+ // fallback envelope encodings when the spec has none
14
13
  const DEFAULT_ROWS = getEncoding('@cero/rows')
15
14
  const DEFAULT_CHANGES = getEncoding('@cero/changes')
16
15
  const DEFAULT_QUERY = getEncoding('@cero/query')
@@ -39,10 +38,7 @@ const DEFAULT_CREATE = getEncoding('@cero/create')
39
38
  */
40
39
 
41
40
  /**
42
- * Attach a `codec` namespace to a hyperschema-shaped `spec`. Looks up
43
- * envelope types using the spec's namespace (`@<ns>/rows` etc.) and falls
44
- * back to the bundled `@cero/*` envelopes when the spec has not declared
45
- * its own. Returns the spec for chaining.
41
+ * Attach a `codec` namespace to a hyperschema-shaped `spec`.
46
42
  *
47
43
  * @param {Spec} spec
48
44
  * @returns {Spec}
@@ -107,11 +103,7 @@ export function bindCodec(spec) {
107
103
  if (!buf || buf.length === 0) return undefined
108
104
  const env = decodeEnvelope(schema, QUERY, DEFAULT_QUERY, buf)
109
105
  const out = {}
110
- // Hyperschema fills optional uint/bool with 0/false defaults, so we can only
111
- // recover fields the encoder actually wrote (truthy). KNOWN LIMITATION: a
112
- // `limit: 0` query (degenerate — "return nothing") is indistinguishable from
113
- // "no limit" over the wire and is treated as unset. Falsy cursors (gt: '')
114
- // are harmless — an empty-string bound matches everything anyway.
106
+ // hyperschema fills optional fields with 0/false, so only truthy fields are recovered; limit: 0 is lost
115
107
  if (env.gt) out.gt = env.gt
116
108
  if (env.gte) out.gte = env.gte
117
109
  if (env.lt) out.lt = env.lt
@@ -145,9 +137,7 @@ export function bindCodec(spec) {
145
137
  return spec
146
138
  }
147
139
 
148
- // Tries the spec's own envelope type first; falls back to the built-in @cero
149
- // encoding bundled with this primitive. This lets callers point a foreign spec
150
- // at the codec without re-declaring the rows/query/create envelopes.
140
+ // the spec's own envelope type first, the built-in @cero encoding as fallback
151
141
  function encodeEnvelope(schema, fqn, fallback, value) {
152
142
  if (fqn && schemaHas(schema, fqn)) return schema.encode(fqn, value)
153
143
  return c.encode(fallback, value)
package/src/rpc/peer.js CHANGED
@@ -6,10 +6,8 @@ import { bindCodec } from './index.js'
6
6
  import { CeroError } from '../lib/errors.js'
7
7
 
8
8
  /**
9
- * Shared RPC peer: wraps an IPC duplex in a length-framed stream and constructs
10
- * the spec's hrpc binding. Frames are paused until `_open` runs so handlers
11
- * registered before `ready()` see a clean slate. `RPCClient` and `RPCServer`
12
- * are thin named subclasses — identical wiring, distinct roles.
9
+ * Shared RPC peer: wraps an IPC duplex in a length-framed stream and constructs the spec's
10
+ * hrpc binding.
13
11
  */
14
12
  export class RPCPeer extends ReadyResource {
15
13
  /**
@@ -26,9 +24,7 @@ export class RPCPeer extends ReadyResource {
26
24
  this.spec = spec
27
25
  this.framed = new FramedStream(ipc)
28
26
  this.rpc = new spec.rpc(this.framed)
29
- // pause AFTER hrpc attaches its 'data' listener — streamx re-resumes a
30
- // paused stream the moment a listener attaches, so pausing first holds
31
- // nothing and early frames would dispatch before the codec is bound
27
+ // pause after hrpc attaches its listener, streamx resumes on attach
32
28
  this.framed.pause()
33
29
  }
34
30