@cero-base/core 1.18.1 → 1.19.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 +5 -6
- package/package.json +9 -9
- package/src/blobs/index.js +2 -2
- package/src/database/changes.js +37 -59
- package/src/database/dispatch.js +31 -45
- package/src/database/index.js +395 -343
- package/src/database/rotation.js +201 -189
- package/src/identity/index.js +3 -3
- package/src/lib/utils.js +50 -18
- package/src/pairing/candidate.js +125 -0
- package/src/pairing/index.js +6 -223
- package/src/pairing/request.js +102 -0
- package/src/storage/index.js +11 -18
- package/types/blobs/codec.d.ts +8 -8
- package/types/blobs/index.d.ts +34 -33
- package/types/blobs/server.d.ts +8 -8
- package/types/database/changes.d.ts +5 -7
- package/types/database/dispatch.d.ts +30 -23
- package/types/database/encryption.d.ts +14 -10
- package/types/database/envelope.d.ts +2 -2
- package/types/database/index.d.ts +173 -181
- package/types/database/rotation.d.ts +53 -30
- package/types/identity/index.d.ts +81 -81
- package/types/index.d.ts +37 -51
- package/types/lib/constants.d.ts +34 -38
- package/types/lib/errors.d.ts +10 -10
- package/types/lib/schema.d.ts +47 -37
- package/types/lib/spec/index.d.ts +29 -55
- package/types/lib/utils.d.ts +27 -17
- package/types/network/discovery.d.ts +7 -7
- package/types/network/index.d.ts +50 -48
- package/types/pairing/candidate.d.ts +37 -0
- package/types/pairing/index.d.ts +87 -139
- package/types/pairing/invite.d.ts +88 -88
- package/types/pairing/request.d.ts +135 -0
- package/types/rpc/client.d.ts +2 -2
- package/types/rpc/index.d.ts +33 -33
- package/types/rpc/peer.d.ts +9 -7
- package/types/rpc/server.d.ts +2 -2
- package/types/storage/index.d.ts +70 -68
- package/src/database/bootstrap.js +0 -133
- package/src/database/hooks.js +0 -112
- package/src/database/membership.js +0 -67
- package/types/database/bootstrap.d.ts +0 -20
- package/types/database/hooks.d.ts +0 -8
- package/types/database/membership.d.ts +0 -7
package/README.md
CHANGED
|
@@ -86,7 +86,7 @@ db.before('put', (ctx) => {
|
|
|
86
86
|
})
|
|
87
87
|
```
|
|
88
88
|
|
|
89
|
-
`db.bootstrap(...)`
|
|
89
|
+
`db.bootstrap(...)` provisions this device. Its writer core was minted when the database opened and this device is its only author, ever. For a **second device** sharing the same identity, use `db.bootstrap({ recovering: true })` — it waits for the other devices' state, admits itself with a master-signed add-writer appended optimistically, then records the device.
|
|
90
90
|
|
|
91
91
|
`db.claim()` is an alternative path: a second device claims its existing local writer; the host admits it via `add-writer` once it sees the claim.
|
|
92
92
|
|
|
@@ -146,14 +146,13 @@ await client.rpc.ping('hi') // → 'pong:hi'
|
|
|
146
146
|
## Utils
|
|
147
147
|
|
|
148
148
|
```js
|
|
149
|
-
import {
|
|
149
|
+
import { genId, subscribe } from '@cero-base/core/utils'
|
|
150
150
|
|
|
151
|
-
toId(bee.key) // 32-byte buffer → z32 string (validates length)
|
|
152
|
-
toKey('a1b2…') // string → 32-byte buffer
|
|
153
|
-
isKeyId('foo') // → false
|
|
154
151
|
genId() // short z32 of 16 random bytes (for ids)
|
|
155
152
|
```
|
|
156
153
|
|
|
154
|
+
Keys travel as z32 strings through `hypercore-id-encoding` (`hid.encode(bee.key)` / `hid.decode(id)`).
|
|
155
|
+
|
|
157
156
|
`subscribe({ get, watch })` builds a Readable that re-emits the latest `get()` on every `watch(fn)` tick — the primitive `Database.watch` is built on.
|
|
158
157
|
|
|
159
158
|
## Errors
|
|
@@ -218,7 +217,7 @@ Every factory produces a `CeroError` with `name === 'CeroError'`, `isCeroError =
|
|
|
218
217
|
cd packages/core && npm test
|
|
219
218
|
```
|
|
220
219
|
|
|
221
|
-
Runs `
|
|
220
|
+
Runs every `test/**/*.test.js` under brittle-node (brittle v4). `npm run build:test` regenerates the test fixture spec.
|
|
222
221
|
|
|
223
222
|
## Types
|
|
224
223
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.19.0",
|
|
4
4
|
"description": "cero p2p primitives — identity, storage, network, database, blobs, rpc, pairing.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -125,18 +125,18 @@
|
|
|
125
125
|
},
|
|
126
126
|
"scripts": {
|
|
127
127
|
"build": "node schema/build.js",
|
|
128
|
-
"build:test": "rm -rf test/
|
|
128
|
+
"build:test": "rm -rf test/fixtures/spec && node test/fixtures/build.js",
|
|
129
129
|
"build:types": "rm -rf types && tsc -p .",
|
|
130
130
|
"pretest": "npm run build:test",
|
|
131
131
|
"prepublishOnly": "npm run build:types",
|
|
132
132
|
"test": "npm run test:bare",
|
|
133
|
-
"test:bare": "brittle-bare test/bare.js test
|
|
133
|
+
"test:bare": "brittle-bare test/bare.js $(find test -name '*.test.js' | sort)",
|
|
134
134
|
"pretest:bare": "npm run build:test",
|
|
135
|
-
"test:node": "brittle-node test
|
|
135
|
+
"test:node": "brittle-node $(find test -name '*.test.js' | sort)"
|
|
136
136
|
},
|
|
137
137
|
"dependencies": {
|
|
138
138
|
"@hyperswarm/secret-stream": "^6.9.1",
|
|
139
|
-
"autobee": "^2.0.
|
|
139
|
+
"autobee": "^2.0.1",
|
|
140
140
|
"autobee-encryption": "0.1.3",
|
|
141
141
|
"b4a": "^1.8.1",
|
|
142
142
|
"bare-crypto": "^1.15.3",
|
|
@@ -145,12 +145,12 @@
|
|
|
145
145
|
"bip39-mnemonic": "^2.5.0",
|
|
146
146
|
"blind-pairing": "^2.3.1",
|
|
147
147
|
"blind-peering": "^2.7.0",
|
|
148
|
-
"compact-encoding": "^3.
|
|
149
|
-
"corestore": "^7.12.
|
|
148
|
+
"compact-encoding": "^3.4.0",
|
|
149
|
+
"corestore": "^7.12.3",
|
|
150
150
|
"framed-stream": "^1.0.1",
|
|
151
151
|
"hrpc": "^4.3.1",
|
|
152
152
|
"hyperblobs": "^2.12.1",
|
|
153
|
-
"hypercore": "^11.35.
|
|
153
|
+
"hypercore": "^11.35.4",
|
|
154
154
|
"hypercore-blob-server": "^1.15.0",
|
|
155
155
|
"hypercore-crypto": "^3.7.0",
|
|
156
156
|
"hypercore-id-encoding": "^1.3.0",
|
|
@@ -177,7 +177,7 @@
|
|
|
177
177
|
"bare-url": "^2.5.4",
|
|
178
178
|
"blind-peer": "^3.14.1",
|
|
179
179
|
"brittle": "^4.1.1",
|
|
180
|
-
"typescript": "^
|
|
180
|
+
"typescript": "^7.0.2",
|
|
181
181
|
"which-runtime": "^1.4.0"
|
|
182
182
|
},
|
|
183
183
|
"license": "Apache-2.0"
|
package/src/blobs/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import Hyperblobs from 'hyperblobs'
|
|
2
2
|
import ReadyResource from 'ready-resource'
|
|
3
3
|
import b4a from 'b4a'
|
|
4
|
+
import hid from 'hypercore-id-encoding'
|
|
4
5
|
|
|
5
6
|
import { NAMESPACE } from '../lib/constants.js'
|
|
6
|
-
import { toId } from '../lib/utils.js'
|
|
7
7
|
import { CeroError } from '../lib/errors.js'
|
|
8
8
|
import { encodeId, decodeId } from './codec.js'
|
|
9
9
|
|
|
@@ -46,7 +46,7 @@ export class Blobs extends ReadyResource {
|
|
|
46
46
|
* @returns {string | null}
|
|
47
47
|
*/
|
|
48
48
|
get id() {
|
|
49
|
-
return this.key ?
|
|
49
|
+
return this.key ? hid.encode(this.key) : null
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
/**
|
package/src/database/changes.js
CHANGED
|
@@ -3,12 +3,10 @@ import b4a from 'b4a'
|
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Pull-driven delta stream over a collection: batches of `{ prev, next }`
|
|
6
|
-
* pairs diffed
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* fast-forwarded to a new core, carries the full matching state as inserts
|
|
11
|
-
* with `reset: true`.
|
|
6
|
+
* pairs diffed from the head this stream last reported. Update ticks only
|
|
7
|
+
* mark the stream dirty, the diff runs when the reader asks, so bursts fold
|
|
8
|
+
* into one batch and nothing is dropped. The first batch, and any batch after
|
|
9
|
+
* the view moved to another core, is the full matching state with `reset: true`.
|
|
12
10
|
*
|
|
13
11
|
* @param {import('./index.js').Database} db
|
|
14
12
|
* @param {string} name Ref name (scopes the update ticks).
|
|
@@ -17,74 +15,54 @@ import b4a from 'b4a'
|
|
|
17
15
|
* @returns {import('streamx').Readable}
|
|
18
16
|
*/
|
|
19
17
|
export function makeChanges(db, name, col, matches) {
|
|
20
|
-
let
|
|
21
|
-
let cursorView = null
|
|
18
|
+
let at = null
|
|
22
19
|
let dirty = true
|
|
23
|
-
let
|
|
24
|
-
let off = null
|
|
25
|
-
|
|
26
|
-
const stream = new Readable({
|
|
27
|
-
open(cb) {
|
|
28
|
-
off = db.onUpdate(mark, name)
|
|
29
|
-
pump()
|
|
30
|
-
cb(null)
|
|
31
|
-
},
|
|
32
|
-
read(cb) {
|
|
33
|
-
pump()
|
|
34
|
-
cb(null)
|
|
35
|
-
},
|
|
36
|
-
predestroy() {
|
|
37
|
-
if (off) off()
|
|
38
|
-
}
|
|
39
|
-
})
|
|
20
|
+
let wake = null
|
|
40
21
|
|
|
41
22
|
const mark = () => {
|
|
42
23
|
dirty = true
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const pump = () => {
|
|
47
|
-
if (busy || !dirty || stream.destroyed) return
|
|
48
|
-
busy = true
|
|
49
|
-
run()
|
|
50
|
-
.catch((err) => stream.destroy(err))
|
|
51
|
-
.finally(() => {
|
|
52
|
-
busy = false
|
|
53
|
-
})
|
|
24
|
+
if (wake !== null) wake()
|
|
54
25
|
}
|
|
26
|
+
const off = db.onUpdate(mark, name)
|
|
55
27
|
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
28
|
+
const stream = new Readable({
|
|
29
|
+
async read(cb) {
|
|
30
|
+
try {
|
|
31
|
+
let batch = null
|
|
32
|
+
while (batch === null && !stream.destroying) {
|
|
33
|
+
if (!dirty) await new Promise((resolve) => (wake = resolve))
|
|
34
|
+
dirty = false
|
|
35
|
+
batch = await diff()
|
|
36
|
+
}
|
|
37
|
+
if (batch !== null) stream.push(batch)
|
|
38
|
+
cb(null)
|
|
39
|
+
} catch (err) {
|
|
40
|
+
cb(err)
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
predestroy() {
|
|
44
|
+
off()
|
|
45
|
+
mark()
|
|
61
46
|
}
|
|
62
|
-
}
|
|
47
|
+
})
|
|
63
48
|
|
|
64
49
|
const diff = async () => {
|
|
65
50
|
const view = db.view
|
|
66
|
-
// snapshot() and
|
|
67
|
-
//
|
|
51
|
+
// snapshot() and head() are synchronous, so back to back they describe
|
|
52
|
+
// the same state and the cursor is exactly-once
|
|
68
53
|
const snap = view.snapshot()
|
|
69
54
|
const head = view.engine.head()
|
|
55
|
+
const reset =
|
|
56
|
+
at === null || at.view !== view || head === null || !b4a.equals(at.head.key, head.key)
|
|
70
57
|
try {
|
|
71
|
-
const fresh = !cursor || cursorView !== view || !head || !b4a.equals(cursor.key, head.key)
|
|
72
58
|
const changes = []
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
} else {
|
|
78
|
-
for await (const { left, right } of snap.diff(col, { from: cursor })) {
|
|
79
|
-
const prev = left && matches(left) ? left : null
|
|
80
|
-
const next = right && matches(right) ? right : null
|
|
81
|
-
if (prev || next) changes.push({ prev, next })
|
|
82
|
-
}
|
|
59
|
+
for await (const { left, right } of snap.diff(col, reset ? {} : { from: at.head })) {
|
|
60
|
+
const prev = left && matches(left) ? left : null
|
|
61
|
+
const next = right && matches(right) ? right : null
|
|
62
|
+
if (prev || next) changes.push({ prev, next })
|
|
83
63
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (fresh) return { changes, reset: true }
|
|
87
|
-
return changes.length ? { changes, reset: false } : null
|
|
64
|
+
at = head && { head, view }
|
|
65
|
+
return reset || changes.length ? { changes, reset } : null
|
|
88
66
|
} finally {
|
|
89
67
|
await snap.close().catch(() => {})
|
|
90
68
|
}
|
package/src/database/dispatch.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import b4a from 'b4a'
|
|
2
|
-
import
|
|
2
|
+
import hid from 'hypercore-id-encoding'
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
5
|
SINGLE,
|
|
@@ -12,15 +12,7 @@ import {
|
|
|
12
12
|
ASSIGN,
|
|
13
13
|
WRITE
|
|
14
14
|
} from '../lib/constants.js'
|
|
15
|
-
import {
|
|
16
|
-
toId,
|
|
17
|
-
toKey,
|
|
18
|
-
can,
|
|
19
|
-
grants,
|
|
20
|
-
outranks,
|
|
21
|
-
addWriterPayload,
|
|
22
|
-
claimWriterPayload
|
|
23
|
-
} from '../lib/utils.js'
|
|
15
|
+
import { can, grants, outranks, admission, ownership } from '../lib/utils.js'
|
|
24
16
|
import { CeroError } from '../lib/errors.js'
|
|
25
17
|
import { Identity } from '../identity/index.js'
|
|
26
18
|
|
|
@@ -34,7 +26,7 @@ export const BUILTINS = [
|
|
|
34
26
|
]
|
|
35
27
|
|
|
36
28
|
// Remote op fields are variable-length buffers on the wire — reject wrong sizes
|
|
37
|
-
// before they reach sodium/
|
|
29
|
+
// before they reach sodium/hid.encode, which throw instead of returning false.
|
|
38
30
|
const isKey = (b) => b?.byteLength === 32
|
|
39
31
|
const isSig = (b) => b?.byteLength === 64
|
|
40
32
|
|
|
@@ -42,22 +34,16 @@ const isSig = (b) => b?.byteLength === 64
|
|
|
42
34
|
* Build the hyperdispatch router for a spec: wires membership, builtin, and
|
|
43
35
|
* spec-defined collection/action ops, returning the router plus an apply loop.
|
|
44
36
|
*
|
|
45
|
-
* @param {
|
|
46
|
-
* @param {
|
|
47
|
-
* @param {
|
|
48
|
-
* @param {
|
|
49
|
-
* @param {() =>
|
|
50
|
-
* @param {
|
|
37
|
+
* @param {object} opts
|
|
38
|
+
* @param {{ dispatch: { Router: Function }, meta?: { refs?: Record<string, { kind?: string, builtin?: boolean, verb?: string }> } }} opts.spec Generated hyperdispatch spec.
|
|
39
|
+
* @param {string} opts.ns Namespace prefix for collection and op names.
|
|
40
|
+
* @param {Record<string, Function>} opts.routes Custom action handlers keyed by route name.
|
|
41
|
+
* @param {(err: Error) => void} opts.onerror Called when a malformed node is skipped.
|
|
42
|
+
* @param {() => Uint8Array | null} opts.key The database key, null until the bee booted.
|
|
43
|
+
* @param {(row: { epoch: number, stamp: number, wrapped: Uint8Array, commit: Uint8Array }) => Promise<void>} opts.onepoch Per-peer side of an applied rotation (skipped in dry runs).
|
|
51
44
|
* @returns {{ dispatcher: object, apply: (nodes: Array<{ value: Buffer, key: Buffer }>, view: object, host: object) => Promise<void> }}
|
|
52
45
|
*/
|
|
53
|
-
export function makeDispatcher(
|
|
54
|
-
spec,
|
|
55
|
-
ns,
|
|
56
|
-
routes,
|
|
57
|
-
onerror = safetyCatch,
|
|
58
|
-
getDbKey = () => null,
|
|
59
|
-
hooks = {}
|
|
60
|
-
) {
|
|
46
|
+
export function makeDispatcher({ spec, ns, routes, onerror, key, onepoch }) {
|
|
61
47
|
const dispatcher = new spec.dispatch.Router()
|
|
62
48
|
|
|
63
49
|
const countersCol = `@${ns}/${COUNTERS}`
|
|
@@ -65,16 +51,16 @@ export function makeDispatcher(
|
|
|
65
51
|
const getDevice = (view, id) => view.get(`@${ns}/devices`, { id })
|
|
66
52
|
|
|
67
53
|
const getRole = async (view, identityKey) =>
|
|
68
|
-
identityKey ? ((await getMember(view,
|
|
54
|
+
identityKey ? ((await getMember(view, hid.encode(identityKey)))?.role ?? null) : null
|
|
69
55
|
|
|
70
56
|
const getSignerRole = async (view, writerKey) => {
|
|
71
57
|
if (!writerKey) return null
|
|
72
|
-
const d = await getDevice(view,
|
|
58
|
+
const d = await getDevice(view, hid.encode(writerKey))
|
|
73
59
|
return d?.memberId ? ((await getMember(view, d.memberId))?.role ?? null) : null
|
|
74
60
|
}
|
|
75
61
|
const isGenesis = async (view) => !(await view.findOne(`@${ns}/members`, {}))
|
|
76
62
|
const getSignerMember = async (view, key) =>
|
|
77
|
-
(key ? (await getDevice(view,
|
|
63
|
+
(key ? (await getDevice(view, hid.encode(key)))?.memberId : null) ?? null
|
|
78
64
|
const canRemoveMember = async (view, signerKey, targetMemberId) => {
|
|
79
65
|
const r = await getSignerRole(view, signerKey)
|
|
80
66
|
if (!can(r, REMOVE)) return false
|
|
@@ -120,7 +106,7 @@ export function makeDispatcher(
|
|
|
120
106
|
|
|
121
107
|
const upsert = (name, col, kind, own) => async (op, ctx) => {
|
|
122
108
|
await requireWrite(ctx)
|
|
123
|
-
const d = ctx.key && (await getDevice(ctx.view,
|
|
109
|
+
const d = ctx.key && (await getDevice(ctx.view, hid.encode(ctx.key)))
|
|
124
110
|
op.memberId = d?.memberId || null
|
|
125
111
|
if (kind !== COLLECTION) return ctx.view.insert(col, op)
|
|
126
112
|
// `add` on an id that already exists is an overwrite, so it answers to the
|
|
@@ -135,7 +121,7 @@ export function makeDispatcher(
|
|
|
135
121
|
if (own) await requireOwn(ctx, existing)
|
|
136
122
|
// memberId is attribution, so it is derived here and never merged off the
|
|
137
123
|
// wire — otherwise an edit could leave the row credited to its victim
|
|
138
|
-
const d = ctx.key && (await getDevice(ctx.view,
|
|
124
|
+
const d = ctx.key && (await getDevice(ctx.view, hid.encode(ctx.key)))
|
|
139
125
|
await insert(ctx.view, name, col, { ...existing, ...op, memberId: d?.memberId || null })
|
|
140
126
|
}
|
|
141
127
|
const remove = (col, own) => async (op, ctx) => {
|
|
@@ -152,7 +138,7 @@ export function makeDispatcher(
|
|
|
152
138
|
|
|
153
139
|
add('add-writer', async (op, ctx) => {
|
|
154
140
|
if (!isKey(op.master) || !isKey(op.writer) || !isSig(op.sig)) return
|
|
155
|
-
if (!Identity.verify(op.master,
|
|
141
|
+
if (!Identity.verify(op.master, admission(ctx.dbKey, op.writer, ctx.key), op.sig)) return
|
|
156
142
|
const genesis = await isGenesis(ctx.view)
|
|
157
143
|
const inviter = await getRole(ctx.view, op.master)
|
|
158
144
|
if (!genesis && !can(inviter, INVITE)) throw CeroError.REFUSED('invite')
|
|
@@ -165,10 +151,10 @@ export function makeDispatcher(
|
|
|
165
151
|
await ctx.host.addWriter(op.writer, { isIndexer: true })
|
|
166
152
|
const ts = op.ts || 0
|
|
167
153
|
await insert(ctx.view, 'devices', `@${ns}/devices`, {
|
|
168
|
-
id:
|
|
154
|
+
id: hid.encode(op.writer),
|
|
169
155
|
// bind to the joiner when the op says so: if their member row never
|
|
170
156
|
// lands the device resolves to nobody, instead of to the inviter
|
|
171
|
-
memberId: op.memberId ||
|
|
157
|
+
memberId: op.memberId || hid.encode(op.master),
|
|
172
158
|
createdAt: ts,
|
|
173
159
|
updatedAt: ts
|
|
174
160
|
})
|
|
@@ -176,28 +162,28 @@ export function makeDispatcher(
|
|
|
176
162
|
|
|
177
163
|
add('del-writer', async (op, ctx) => {
|
|
178
164
|
if (!isKey(op.writer)) return
|
|
179
|
-
const target = await getDevice(ctx.view,
|
|
165
|
+
const target = await getDevice(ctx.view, hid.encode(op.writer))
|
|
180
166
|
if (target?.memberId !== (await getSignerMember(ctx.view, ctx.key))) {
|
|
181
167
|
if (!(await canRemoveMember(ctx.view, ctx.key, target?.memberId))) {
|
|
182
168
|
throw CeroError.REFUSED('remove')
|
|
183
169
|
}
|
|
184
170
|
}
|
|
185
171
|
await ctx.host.removeWriter(op.writer)
|
|
186
|
-
await ctx.view.delete(`@${ns}/devices`, { id:
|
|
172
|
+
await ctx.view.delete(`@${ns}/devices`, { id: hid.encode(op.writer) })
|
|
187
173
|
})
|
|
188
174
|
|
|
189
175
|
add('claim-writer', async (op, ctx) => {
|
|
190
176
|
if (!isKey(op.identity) || !isKey(op.writer) || !isSig(op.sig)) return
|
|
191
|
-
if (!Identity.verify(op.identity,
|
|
177
|
+
if (!Identity.verify(op.identity, ownership(ctx.dbKey, op.writer), op.sig)) return
|
|
192
178
|
// a claim is only meaningful from the claiming core — from any other it is
|
|
193
179
|
// inapplicable, not refused, so it must not abort that writer's batch
|
|
194
180
|
if (!b4a.equals(ctx.key, op.writer)) return
|
|
195
|
-
const memberId =
|
|
181
|
+
const memberId = hid.encode(op.identity)
|
|
196
182
|
if (!can(await getRole(ctx.view, op.identity), WRITE)) throw CeroError.REFUSED('write')
|
|
197
183
|
await ctx.host.addWriter(op.writer, { isIndexer: true })
|
|
198
184
|
const ts = op.ts || 0
|
|
199
185
|
await insert(ctx.view, 'devices', `@${ns}/devices`, {
|
|
200
|
-
id:
|
|
186
|
+
id: hid.encode(op.writer),
|
|
201
187
|
memberId,
|
|
202
188
|
createdAt: ts,
|
|
203
189
|
updatedAt: ts
|
|
@@ -211,7 +197,7 @@ export function makeDispatcher(
|
|
|
211
197
|
if (!can(r, INVITE) || !grants(r, op.role)) throw CeroError.REFUSED('invite')
|
|
212
198
|
}
|
|
213
199
|
await insert(ctx.view, 'members', `@${ns}/members`, op)
|
|
214
|
-
const deviceId =
|
|
200
|
+
const deviceId = hid.encode(op.key)
|
|
215
201
|
const existingDevice = await getDevice(ctx.view, deviceId)
|
|
216
202
|
const ts = op.updatedAt || 0
|
|
217
203
|
await insert(ctx.view, 'devices', `@${ns}/devices`, {
|
|
@@ -255,7 +241,7 @@ export function makeDispatcher(
|
|
|
255
241
|
if (next.key) await ctx.host.removeWriter(next.key)
|
|
256
242
|
for (const device of await ctx.view.find(`@${ns}/devices`, {}).toArray()) {
|
|
257
243
|
if (device.memberId !== next.id) continue
|
|
258
|
-
await ctx.host.removeWriter(
|
|
244
|
+
await ctx.host.removeWriter(hid.decode(device.id))
|
|
259
245
|
}
|
|
260
246
|
// the device rows stay: they are how a key resolves to a member, and
|
|
261
247
|
// deleting them would leave the demoted writer unresolvable — which the
|
|
@@ -275,7 +261,7 @@ export function makeDispatcher(
|
|
|
275
261
|
// leave writers admitted or stale device rows behind
|
|
276
262
|
for (const device of await ctx.view.find(`@${ns}/devices`, {}).toArray()) {
|
|
277
263
|
if (device.memberId !== op.id) continue
|
|
278
|
-
await ctx.host.removeWriter(
|
|
264
|
+
await ctx.host.removeWriter(hid.decode(device.id))
|
|
279
265
|
await ctx.view.delete(`@${ns}/devices`, { id: device.id })
|
|
280
266
|
}
|
|
281
267
|
await ctx.view.delete(`@${ns}/members`, op)
|
|
@@ -318,9 +304,9 @@ export function makeDispatcher(
|
|
|
318
304
|
await ctx.view.insert(`@${ns}/${EPOCHS}`, row)
|
|
319
305
|
// per-peer hydration (unseal own envelope → keyring) — never in a dry
|
|
320
306
|
// run: the announcement itself must be appended under the OLD epoch
|
|
321
|
-
if (
|
|
307
|
+
if (!ctx.dryRun) {
|
|
322
308
|
try {
|
|
323
|
-
await
|
|
309
|
+
await onepoch(row)
|
|
324
310
|
} catch (err) {
|
|
325
311
|
onerror(err)
|
|
326
312
|
}
|
|
@@ -338,7 +324,7 @@ export function makeDispatcher(
|
|
|
338
324
|
throw CeroError.REFUSED('remove')
|
|
339
325
|
}
|
|
340
326
|
}
|
|
341
|
-
await ctx.host.removeWriter(
|
|
327
|
+
await ctx.host.removeWriter(hid.decode(existing.id))
|
|
342
328
|
await ctx.view.delete(`@${ns}/devices`, op)
|
|
343
329
|
})
|
|
344
330
|
|
|
@@ -455,7 +441,7 @@ export function makeDispatcher(
|
|
|
455
441
|
return {
|
|
456
442
|
dispatcher,
|
|
457
443
|
async apply(nodes, view, host) {
|
|
458
|
-
const dbKey =
|
|
444
|
+
const dbKey = key()
|
|
459
445
|
// Nodes land in a transaction this loop owns, so a refusal can discard the
|
|
460
446
|
// whole batch — a batch is one writer's contiguous run (autobee keys it on
|
|
461
447
|
// batch[0].key), so an abort only ever drops the offending writer's ops.
|