@cero-base/core 1.17.0 → 1.18.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/package.json +10 -9
- package/src/blobs/codec.js +2 -1
- package/src/database/dispatch.js +41 -1
- package/src/database/encryption.js +5 -3
- package/src/database/hooks.js +112 -0
- package/src/database/index.js +71 -467
- package/src/database/membership.js +67 -0
- package/src/database/rotation.js +228 -0
- package/src/lib/utils.js +19 -6
- package/src/network/index.js +68 -2
- package/src/pairing/index.js +3 -3
- package/src/pairing/invite.js +4 -2
- package/src/storage/index.js +12 -6
- package/types/database/encryption.d.ts +1 -1
- package/types/database/hooks.d.ts +8 -0
- package/types/database/index.d.ts +8 -56
- package/types/database/membership.d.ts +7 -0
- package/types/database/rotation.d.ts +32 -0
- package/types/lib/utils.d.ts +6 -7
- package/types/network/index.d.ts +19 -0
- package/types/pairing/index.d.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.18.0",
|
|
4
4
|
"description": "cero p2p primitives — identity, storage, network, database, blobs, rpc, pairing.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -136,15 +136,15 @@
|
|
|
136
136
|
},
|
|
137
137
|
"dependencies": {
|
|
138
138
|
"@hyperswarm/secret-stream": "^6.9.1",
|
|
139
|
-
"autobee": "^2.0.0
|
|
139
|
+
"autobee": "^2.0.0",
|
|
140
140
|
"autobee-encryption": "0.1.3",
|
|
141
141
|
"b4a": "^1.8.1",
|
|
142
142
|
"bare-crypto": "^1.15.3",
|
|
143
143
|
"bare-fs": "^4.8.1",
|
|
144
|
-
"bare-path": "^3.1.
|
|
144
|
+
"bare-path": "^3.1.2",
|
|
145
145
|
"bip39-mnemonic": "^2.5.0",
|
|
146
146
|
"blind-pairing": "^2.3.1",
|
|
147
|
-
"blind-peering": "^2.
|
|
147
|
+
"blind-peering": "^2.7.0",
|
|
148
148
|
"compact-encoding": "^3.3.2",
|
|
149
149
|
"corestore": "^7.12.2",
|
|
150
150
|
"framed-stream": "^1.0.1",
|
|
@@ -157,9 +157,10 @@
|
|
|
157
157
|
"hypercore-storage": "^3.2.1",
|
|
158
158
|
"hyperdb": "^6.9.0",
|
|
159
159
|
"hyperdispatch": "^1.6.0",
|
|
160
|
-
"hyperschema": "^1.
|
|
160
|
+
"hyperschema": "^1.23.0",
|
|
161
161
|
"hyperswarm": "^4.17.0",
|
|
162
162
|
"keet-identity-key": "^3.2.0",
|
|
163
|
+
"protomux": "^3.11.0",
|
|
163
164
|
"protomux-wakeup": "^2.9.0",
|
|
164
165
|
"ready-resource": "^1.2.0",
|
|
165
166
|
"safety-catch": "^1.0.3",
|
|
@@ -171,11 +172,11 @@
|
|
|
171
172
|
"@hyperswarm/testnet": "^3.1.4",
|
|
172
173
|
"bare-abort-controller": "^1.1.2",
|
|
173
174
|
"bare-events": "^2.9.2",
|
|
174
|
-
"bare-fetch": "^3.2.
|
|
175
|
+
"bare-fetch": "^3.2.1",
|
|
175
176
|
"bare-process": "^4.5.1",
|
|
176
|
-
"bare-url": "^2.5.
|
|
177
|
-
"blind-peer": "^3.14.
|
|
178
|
-
"brittle": "^4.1.
|
|
177
|
+
"bare-url": "^2.5.4",
|
|
178
|
+
"blind-peer": "^3.14.1",
|
|
179
|
+
"brittle": "^4.1.1",
|
|
179
180
|
"typescript": "^5.9.3",
|
|
180
181
|
"which-runtime": "^1.4.0"
|
|
181
182
|
},
|
package/src/blobs/codec.js
CHANGED
|
@@ -33,8 +33,9 @@ export function encodeId(coreKey, blobId, type) {
|
|
|
33
33
|
* @returns {{ coreKey: Uint8Array, blobId: RawBlobId, type: string }}
|
|
34
34
|
*/
|
|
35
35
|
export function decodeId(id) {
|
|
36
|
-
if (typeof id !== 'string' || id.length === 0)
|
|
36
|
+
if (typeof id !== 'string' || id.length === 0) {
|
|
37
37
|
throw CeroError.INVALID('id must be a non-empty string')
|
|
38
|
+
}
|
|
38
39
|
let buf
|
|
39
40
|
try {
|
|
40
41
|
buf = z32.decode(id)
|
package/src/database/dispatch.js
CHANGED
|
@@ -295,8 +295,9 @@ export function makeDispatcher(
|
|
|
295
295
|
// reorder epochs freely without ever changing which key a block header
|
|
296
296
|
// points at. Uniqueness is enforced here, deterministically.
|
|
297
297
|
if (!Number.isInteger(op.stamp) || op.stamp <= 0 || op.stamp > 0xffffffff) return
|
|
298
|
-
if (!can(await getSignerRole(ctx.view, ctx.key), REMOVE))
|
|
298
|
+
if (!can(await getSignerRole(ctx.view, ctx.key), REMOVE)) {
|
|
299
299
|
throw CeroError.REFUSED('rotate', 'rotate requires the remove permission')
|
|
300
|
+
}
|
|
300
301
|
const rows = await ctx.view.find(`@${ns}/${EPOCHS}`, {}).toArray()
|
|
301
302
|
if (rows.some((r) => r.stamp === op.stamp)) return
|
|
302
303
|
// sequence numbers order epochs; concurrent rotations linearize as
|
|
@@ -369,6 +370,43 @@ export function makeDispatcher(
|
|
|
369
370
|
})
|
|
370
371
|
continue
|
|
371
372
|
}
|
|
373
|
+
if (b.verb === 'invite') {
|
|
374
|
+
// Invite rows are served by every replica, so they are shared state with
|
|
375
|
+
// teeth. Any WRITE member may mint one — the rank it can grant is capped
|
|
376
|
+
// at admission — but an existing row is never overwritten (that only
|
|
377
|
+
// breaks the invite for everyone), and altering or revoking one is a
|
|
378
|
+
// moderation act: it needs REMOVE.
|
|
379
|
+
add(`add-${b.verb}`, async (op, ctx) => {
|
|
380
|
+
await requireWrite(ctx)
|
|
381
|
+
if (await ctx.view.get(col, { id: op.id })) throw CeroError.REFUSED('invite')
|
|
382
|
+
await insert(ctx.view, b.name, col, op)
|
|
383
|
+
})
|
|
384
|
+
// like requireWrite, a signer with no resolvable role yet (genesis, or
|
|
385
|
+
// the optimistic-admission window) is not refused
|
|
386
|
+
const moderate = async (ctx) => {
|
|
387
|
+
const r = await getSignerRole(ctx.view, ctx.key)
|
|
388
|
+
if (r !== null && !can(r, REMOVE)) throw CeroError.REFUSED('invite')
|
|
389
|
+
}
|
|
390
|
+
add(`set-${b.verb}`, async (op, ctx) => {
|
|
391
|
+
await moderate(ctx)
|
|
392
|
+
const existing = await ctx.view.get(col, { id: op.id })
|
|
393
|
+
if (!existing) return
|
|
394
|
+
await insert(ctx.view, b.name, col, { ...existing, ...op })
|
|
395
|
+
})
|
|
396
|
+
// deleting is two different acts: revoking (moderation, REMOVE) and
|
|
397
|
+
// consuming a single-use invite — which is done by whichever replica
|
|
398
|
+
// served the join, so any member who may serve invites may do it
|
|
399
|
+
add(`del-${b.verb}`, async (op, ctx) => {
|
|
400
|
+
const existing = await ctx.view.get(col, { id: op.id })
|
|
401
|
+
const consume = existing && existing.reuse !== true
|
|
402
|
+
const r = await getSignerRole(ctx.view, ctx.key)
|
|
403
|
+
if (r !== null && !can(r, REMOVE) && !(consume && can(r, INVITE))) {
|
|
404
|
+
throw CeroError.REFUSED('invite')
|
|
405
|
+
}
|
|
406
|
+
return ctx.view.delete(col, op)
|
|
407
|
+
})
|
|
408
|
+
continue
|
|
409
|
+
}
|
|
372
410
|
if (b.verb === 'file') {
|
|
373
411
|
add('add-file', async (op, ctx) => {
|
|
374
412
|
if (!can(await getSignerRole(ctx.view, ctx.key), WRITE)) throw CeroError.REFUSED('write')
|
|
@@ -453,6 +491,8 @@ export function makeDispatcher(
|
|
|
453
491
|
onerror(refusal)
|
|
454
492
|
} else {
|
|
455
493
|
await tx.flush()
|
|
494
|
+
// replayed before apply() returns: autobee clears host.applying right
|
|
495
|
+
// after, and addWriter reads it (rc.31 derives coord/carrier from it)
|
|
456
496
|
for (const [fn, args] of calls) await host[fn](...args)
|
|
457
497
|
}
|
|
458
498
|
await view.flush()
|
|
@@ -224,9 +224,11 @@ export class EpochAutobee extends Autobee {
|
|
|
224
224
|
// haven't learned yet) parks the whole pass — the scheduled retry wakes the
|
|
225
225
|
// stalled cores once the announcement lands. Oplog blocks at unknown epochs
|
|
226
226
|
// never reach here: autobee's own reader freezes that writer.
|
|
227
|
-
|
|
227
|
+
// forwards its arguments: since autobee rc.31 the local drain passes
|
|
228
|
+
// { local: true }, and swallowing it would run the wrong branch
|
|
229
|
+
async _bumpPendingWriters(...args) {
|
|
228
230
|
try {
|
|
229
|
-
return await super._bumpPendingWriters()
|
|
231
|
+
return await super._bumpPendingWriters(...args)
|
|
230
232
|
} catch (err) {
|
|
231
233
|
if (err?.code !== 'UNKNOWN_EPOCH') throw err
|
|
232
234
|
this._scheduleEpochRetry()
|
|
@@ -242,7 +244,7 @@ export class EpochAutobee extends Autobee {
|
|
|
242
244
|
} catch (err) {
|
|
243
245
|
if (err?.code !== 'UNKNOWN_EPOCH') throw err
|
|
244
246
|
this._scheduleEpochRetry()
|
|
245
|
-
return
|
|
247
|
+
return []
|
|
246
248
|
}
|
|
247
249
|
}
|
|
248
250
|
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import safetyCatch from 'safety-catch'
|
|
2
|
+
|
|
3
|
+
import { ACTION } from '../lib/constants.js'
|
|
4
|
+
import { CeroError } from '../lib/errors.js'
|
|
5
|
+
|
|
6
|
+
export function before(db, op, fn) {
|
|
7
|
+
return addHook(db.beforeHooks, op, fn)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function after(db, op, fn) {
|
|
11
|
+
return addHook(db.afterHooks, op, fn)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function onUpdate(db, fn, scope) {
|
|
15
|
+
db.updaters.set(fn, scope || null)
|
|
16
|
+
return () => db.updaters.delete(fn)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function touch(db, nodes) {
|
|
20
|
+
if (!db._byVerb) {
|
|
21
|
+
db._byVerb = new Map()
|
|
22
|
+
for (const [name, info] of Object.entries(db.refs)) {
|
|
23
|
+
db._byVerb.set(info.verb || name, name)
|
|
24
|
+
db._byVerb.set(name, name)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
for (const node of nodes) {
|
|
28
|
+
try {
|
|
29
|
+
const { name } = db.spec.dispatch.decode(node.value)
|
|
30
|
+
const verb = name.slice(name.indexOf('/') + 1)
|
|
31
|
+
const dash = verb.indexOf('-')
|
|
32
|
+
const target = dash < 0 ? verb : verb.slice(dash + 1)
|
|
33
|
+
const ref = db._byVerb.get(target)
|
|
34
|
+
// an action's route handler writes wherever it wants, widen to all
|
|
35
|
+
if (!ref || db.refs[ref]?.kind === ACTION) db._touched.add('*')
|
|
36
|
+
else db._touched.add(ref)
|
|
37
|
+
} catch {
|
|
38
|
+
db._touched.add('*')
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function onApply(db, fn) {
|
|
44
|
+
if (typeof fn !== 'function') return () => {}
|
|
45
|
+
db.onApplyHooks.add(fn)
|
|
46
|
+
return () => db.onApplyHooks.delete(fn)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function observe(db, nodes) {
|
|
50
|
+
for (const node of nodes) {
|
|
51
|
+
let event
|
|
52
|
+
try {
|
|
53
|
+
const { name, value } = db.spec.dispatch.decode(node.value)
|
|
54
|
+
const verb = name.slice(name.indexOf('/') + 1) // '@cero/set-messages' → 'set-messages'
|
|
55
|
+
const dash = verb.indexOf('-')
|
|
56
|
+
event = {
|
|
57
|
+
op: dash < 0 ? verb : verb.slice(0, dash),
|
|
58
|
+
name: dash < 0 ? verb : verb.slice(dash + 1),
|
|
59
|
+
row: value,
|
|
60
|
+
writerKey: node.key,
|
|
61
|
+
seq: db._applySeq++
|
|
62
|
+
}
|
|
63
|
+
} catch (err) {
|
|
64
|
+
safetyCatch(err)
|
|
65
|
+
continue
|
|
66
|
+
}
|
|
67
|
+
for (const fn of db.onApplyHooks) {
|
|
68
|
+
try {
|
|
69
|
+
fn(event)
|
|
70
|
+
} catch (err) {
|
|
71
|
+
db._onerror(err)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function runBefore(db, op, ctx) {
|
|
78
|
+
const hooks = db.beforeHooks.get(op)
|
|
79
|
+
if (hooks) {
|
|
80
|
+
for (const fn of hooks) {
|
|
81
|
+
const out = await fn(ctx)
|
|
82
|
+
if (out === false) return false
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
db.emit(`before:${op}`, ctx)
|
|
86
|
+
return true
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function runAfter(db, op, ctx) {
|
|
90
|
+
const hooks = db.afterHooks.get(op)
|
|
91
|
+
if (hooks) {
|
|
92
|
+
for (const fn of hooks) {
|
|
93
|
+
try {
|
|
94
|
+
await fn(ctx)
|
|
95
|
+
} catch (err) {
|
|
96
|
+
db._onerror(err)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
db.emit(`after:${op}`, ctx)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function addHook(map, op, fn) {
|
|
104
|
+
if (typeof fn !== 'function') throw CeroError.INVALID('hook fn must be a function')
|
|
105
|
+
let arr = map.get(op)
|
|
106
|
+
if (!arr) map.set(op, (arr = []))
|
|
107
|
+
arr.push(fn)
|
|
108
|
+
return () => {
|
|
109
|
+
const i = arr.indexOf(fn)
|
|
110
|
+
if (i >= 0) arr.splice(i, 1)
|
|
111
|
+
}
|
|
112
|
+
}
|