@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
package/README.md CHANGED
@@ -86,7 +86,7 @@ db.before('put', (ctx) => {
86
86
  })
87
87
  ```
88
88
 
89
- `db.bootstrap(...)` swaps from the identity keypair to a fresh per-device writer. For a **second device** sharing the same identity, use `db.bootstrap({ recovering: true })` — it waits for the first peer append, then swaps off the identity-keyed bootstrap-writer slot so autobee correctly applies remote appends.
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 { toId, toKey, isKeyId, genId, subscribe } from '@cero-base/core/utils'
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 `brittle-node test/*.test.js` (brittle v4). `npm run build:test` regenerates the test fixture spec.
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,11 +1,11 @@
1
1
  {
2
2
  "name": "@cero-base/core",
3
- "version": "1.18.2",
3
+ "version": "2.0.0",
4
4
  "description": "cero p2p primitives — identity, storage, network, database, blobs, rpc, pairing.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "engines": {
8
- "node": ">=20"
8
+ "node": ">=22"
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
@@ -18,8 +18,7 @@
18
18
  "src",
19
19
  "types",
20
20
  "README.md",
21
- "LICENSE",
22
- "!**/CLAUDE.md"
21
+ "LICENSE"
23
22
  ],
24
23
  "publishConfig": {
25
24
  "access": "public"
@@ -121,18 +120,22 @@
121
120
  "events": {
122
121
  "bare": "bare-events",
123
122
  "default": "events"
123
+ },
124
+ "process": {
125
+ "bare": "bare-process",
126
+ "default": "process"
124
127
  }
125
128
  },
126
129
  "scripts": {
127
130
  "build": "node schema/build.js",
128
- "build:test": "rm -rf test/fixture/spec && node test/fixture/build.js",
131
+ "build:test": "rm -rf test/fixtures/spec && node test/fixtures/build.js",
129
132
  "build:types": "rm -rf types && tsc -p .",
130
133
  "pretest": "npm run build:test",
131
134
  "prepublishOnly": "npm run build:types",
132
135
  "test": "npm run test:bare",
133
- "test:bare": "brittle-bare test/bare.js test/*.test.js",
136
+ "test:bare": "brittle-bare $(find test -name '*.test.js' | sort)",
134
137
  "pretest:bare": "npm run build:test",
135
- "test:node": "brittle-node test/*.test.js"
138
+ "test:node": "brittle-node $(find test -name '*.test.js' | sort)"
136
139
  },
137
140
  "dependencies": {
138
141
  "@hyperswarm/secret-stream": "^6.9.1",
@@ -145,12 +148,12 @@
145
148
  "bip39-mnemonic": "^2.5.0",
146
149
  "blind-pairing": "^2.3.1",
147
150
  "blind-peering": "^2.7.0",
148
- "compact-encoding": "^3.3.2",
149
- "corestore": "^7.12.2",
151
+ "compact-encoding": "^3.4.0",
152
+ "corestore": "^7.12.3",
150
153
  "framed-stream": "^1.0.1",
151
154
  "hrpc": "^4.3.1",
152
155
  "hyperblobs": "^2.12.1",
153
- "hypercore": "^11.35.2",
156
+ "hypercore": "^11.35.4",
154
157
  "hypercore-blob-server": "^1.15.0",
155
158
  "hypercore-crypto": "^3.7.0",
156
159
  "hypercore-id-encoding": "^1.3.0",
@@ -177,7 +180,7 @@
177
180
  "bare-url": "^2.5.4",
178
181
  "blind-peer": "^3.14.1",
179
182
  "brittle": "^4.1.1",
180
- "typescript": "^5.9.3",
183
+ "typescript": "^7.0.2",
181
184
  "which-runtime": "^1.4.0"
182
185
  },
183
186
  "license": "Apache-2.0"
@@ -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 ? toId(this.key) : null
49
+ return this.key ? hid.encode(this.key) : null
50
50
  }
51
51
 
52
52
  /**
@@ -109,21 +109,6 @@ export class Blobs extends ReadyResource {
109
109
  throw CeroError.INVALID('input must be a buffer or a Readable stream')
110
110
  }
111
111
 
112
- /**
113
- * @param {import('streamx').Readable} input
114
- * @returns {Promise<RawBlobId>}
115
- */
116
- async _putStream(input) {
117
- const ws = this.hyperblobs.createWriteStream()
118
- await new Promise((resolve, reject) => {
119
- input.on('error', reject)
120
- ws.on('error', reject)
121
- ws.on('close', resolve)
122
- input.pipe(ws)
123
- })
124
- return ws.id
125
- }
126
-
127
112
  /**
128
113
  * Resolve the blob bytes for a raw blobId. Fetches from peers if not local.
129
114
  *
@@ -159,6 +144,21 @@ export class Blobs extends ReadyResource {
159
144
  await this.hyperblobs.clear(blobId)
160
145
  }
161
146
 
147
+ /**
148
+ * @param {import('streamx').Readable} input
149
+ * @returns {Promise<RawBlobId>}
150
+ */
151
+ async _putStream(input) {
152
+ const ws = this.hyperblobs.createWriteStream()
153
+ await new Promise((resolve, reject) => {
154
+ input.on('error', reject)
155
+ ws.on('error', reject)
156
+ ws.on('close', resolve)
157
+ input.pipe(ws)
158
+ })
159
+ return ws.id
160
+ }
161
+
162
162
  _guard() {
163
163
  if (this.closed) throw CeroError.CLOSED('Blobs')
164
164
  if (!this.opened) throw CeroError.NOT_READY('Blobs', 'blobs')
@@ -7,8 +7,6 @@ import { decodeId } from './index.js'
7
7
 
8
8
  /**
9
9
  * HTTP server that turns a string file id into a renderable localhost URL.
10
- * Wraps hypercore-blob-server; the underlying store session is owned by
11
- * BlobServer and closed when `close()` is called.
12
10
  */
13
11
  export class FileServer {
14
12
  /**
@@ -2,13 +2,8 @@ import { Readable } from 'streamx'
2
2
  import b4a from 'b4a'
3
3
 
4
4
  /**
5
- * Pull-driven delta stream over a collection: batches of `{ prev, next }`
6
- * pairs diffed between per-stream head cursors (`db.diff(col, { from })`).
7
- * Update ticks only mark the stream dirty — the diff runs when the reader
8
- * demands, so backpressure folds bursts into one bigger batch and nothing is
9
- * ever dropped. The first batch, and any batch after the view is swapped or
10
- * fast-forwarded to a new core, carries the full matching state as inserts
11
- * with `reset: true`.
5
+ * Pull-driven delta stream over a collection: batches of `{ prev, next }` pairs diffed
6
+ * from the head this stream last reported.
12
7
  *
13
8
  * @param {import('./index.js').Database} db
14
9
  * @param {string} name Ref name (scopes the update ticks).
@@ -17,74 +12,53 @@ import b4a from 'b4a'
17
12
  * @returns {import('streamx').Readable}
18
13
  */
19
14
  export function makeChanges(db, name, col, matches) {
20
- let cursor = null
21
- let cursorView = null
15
+ let at = null
22
16
  let dirty = true
23
- let busy = false
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
- })
17
+ let wake = null
40
18
 
41
19
  const mark = () => {
42
20
  dirty = true
43
- pump()
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
- })
21
+ if (wake !== null) wake()
54
22
  }
23
+ const off = db.onUpdate(mark, name)
55
24
 
56
- const run = async () => {
57
- while (dirty && !stream.destroyed) {
58
- dirty = false
59
- const batch = await diff()
60
- if (batch && stream.push(batch) === false) return
25
+ const stream = new Readable({
26
+ async read(cb) {
27
+ try {
28
+ let batch = null
29
+ while (batch === null && !stream.destroying) {
30
+ if (!dirty) await new Promise((resolve) => (wake = resolve))
31
+ dirty = false
32
+ batch = await diff()
33
+ }
34
+ if (batch !== null) stream.push(batch)
35
+ cb(null)
36
+ } catch (err) {
37
+ cb(err)
38
+ }
39
+ },
40
+ predestroy() {
41
+ off()
42
+ mark()
61
43
  }
62
- }
44
+ })
63
45
 
64
46
  const diff = async () => {
65
47
  const view = db.view
66
- // snapshot() and engine.head() are synchronous — captured back to back
67
- // they describe the same state, so the cursor is exactly-once
48
+ // snapshot() and head() are synchronous, so the cursor is exactly-once
68
49
  const snap = view.snapshot()
69
50
  const head = view.engine.head()
51
+ const reset =
52
+ at === null || at.view !== view || head === null || !b4a.equals(at.head.key, head.key)
70
53
  try {
71
- const fresh = !cursor || cursorView !== view || !head || !b4a.equals(cursor.key, head.key)
72
54
  const changes = []
73
- if (fresh) {
74
- for (const next of await snap.find(col, {}).toArray()) {
75
- if (matches(next)) changes.push({ prev: null, next })
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
- }
55
+ for await (const { left, right } of snap.diff(col, reset ? {} : { from: at.head })) {
56
+ const prev = left && matches(left) ? left : null
57
+ const next = right && matches(right) ? right : null
58
+ if (prev || next) changes.push({ prev, next })
83
59
  }
84
- cursor = head
85
- cursorView = view
86
- if (fresh) return { changes, reset: true }
87
- return changes.length ? { changes, reset: false } : null
60
+ at = head && { head, view }
61
+ return reset || changes.length ? { changes, reset } : null
88
62
  } finally {
89
63
  await snap.close().catch(() => {})
90
64
  }