@cero-base/cero 1.1.1 → 1.2.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 +53 -24
- package/package.json +18 -7
- package/src/bluetooth.js +172 -0
- package/src/build/index.js +17 -4
- package/src/build/schemas.js +3 -1
- package/src/extensions/handle-sync.js +14 -3
- package/src/extensions/profile-sync.js +10 -2
- package/src/handle/index.js +39 -11
- package/src/index.js +49 -11
- package/src/lib/internal.js +7 -1
- package/src/lib/operators.js +38 -18
- package/src/local/index.js +4 -2
- package/src/rpc/client.js +18 -5
- package/src/rpc/server.js +30 -7
- package/types/bluetooth.d.ts +65 -0
- package/types/build/index.d.ts +2 -1
- package/types/build/schemas.d.ts +2 -0
- package/types/extensions/handle-sync.d.ts +2 -0
- package/types/extensions/profile-sync.d.ts +2 -0
- package/types/handle/index.d.ts +14 -0
- package/types/index.d.ts +16 -1
- package/types/lib/internal.d.ts +22 -1
- package/types/lib/operators.d.ts +1 -7
- package/types/local/index.d.ts +6 -1
- package/src/CLAUDE.md +0 -3
- package/src/build/CLAUDE.md +0 -3
- package/src/extensions/CLAUDE.md +0 -3
- package/src/handle/CLAUDE.md +0 -3
- package/src/lib/CLAUDE.md +0 -3
- package/src/rpc/CLAUDE.md +0 -3
package/src/index.js
CHANGED
|
@@ -9,6 +9,7 @@ import { CeroError } from '@cero-base/core/errors'
|
|
|
9
9
|
|
|
10
10
|
import { Handle, Ref } from './handle/index.js'
|
|
11
11
|
import { Local } from './local/index.js'
|
|
12
|
+
import { Bluetooth } from './bluetooth.js'
|
|
12
13
|
import {
|
|
13
14
|
put,
|
|
14
15
|
set,
|
|
@@ -66,6 +67,9 @@ export { t, schema } from './lib/spec.js'
|
|
|
66
67
|
* @property {(err: any) => void} [onerror] Background-task error handler.
|
|
67
68
|
* @property {boolean} [recovery] Recovery flow — wipe local state and re-claim a writer slot.
|
|
68
69
|
* @property {number} [recoveryTimeout] Max wait for peer data + writer capability during recovery.
|
|
70
|
+
* @property {Uint8Array} [storageKey] 32-byte key encrypting local key material (master seed, device keypairs) at rest. Source it from the OS keychain — cero never stores it.
|
|
71
|
+
* @property {boolean} [extensions] `false` disables the bundled extensions (profileSync, handleSync) for this instance. Build with `{ extensions: false }` too so the spec matches.
|
|
72
|
+
* @property {boolean | any} [bluetooth] `true` enables nearby (Bluetooth) sync via `me.bluetooth`; pass a bare-bluetooth-shaped backend to inject one. Absent backend → `me.bluetooth.state === 'unsupported'`.
|
|
69
73
|
*/
|
|
70
74
|
|
|
71
75
|
/**
|
|
@@ -93,24 +97,27 @@ export async function cero(dir, spec, opts = {}) {
|
|
|
93
97
|
// lock leaks and a retry in the same process hits locked storage.
|
|
94
98
|
try {
|
|
95
99
|
await storage.ready()
|
|
100
|
+
await fs.promises.chmod(`${dir}/main`, 0o700)
|
|
96
101
|
store = new Corestore(storage, { manifestVersion: 2 })
|
|
97
102
|
await store.ready()
|
|
98
103
|
|
|
99
104
|
if (spec.local && spec.meta?.local) {
|
|
100
|
-
local = new Local(null, spec, { store })
|
|
105
|
+
local = new Local(null, spec, { store, storageKey: opts.storageKey })
|
|
101
106
|
await local.ready()
|
|
102
107
|
}
|
|
103
108
|
|
|
104
109
|
const identity = await resolveIdentity(opts, local)
|
|
105
110
|
const writer = local ? (await local.store.get('keypair')).data : null
|
|
106
111
|
|
|
107
|
-
// Channel stamp: a storage remembers its channel; reopening it under a different channel
|
|
108
|
-
//
|
|
109
|
-
//
|
|
110
|
-
if (local
|
|
111
|
-
const stored = (await local.store.get('environment')).data?.channel
|
|
112
|
-
|
|
113
|
-
|
|
112
|
+
// Channel stamp: a storage remembers its channel; reopening it under a different channel —
|
|
113
|
+
// including no channel at all, which would silently rejoin the global network — is a
|
|
114
|
+
// misconfiguration that could leak data across networks, so reject it.
|
|
115
|
+
if (local) {
|
|
116
|
+
const stored = (await local.store.get('environment')).data?.channel ?? null
|
|
117
|
+
const wanted = opts.channel ?? null
|
|
118
|
+
if (stored == null && wanted != null)
|
|
119
|
+
await local.store.set('environment', { channel: wanted })
|
|
120
|
+
else if (stored != null && stored !== wanted) throw CeroError.CHANNEL_MISMATCH()
|
|
114
121
|
}
|
|
115
122
|
|
|
116
123
|
network = new Network({ bootstrap: opts.bootstrap, channel: opts.channel })
|
|
@@ -164,10 +171,20 @@ export async function cero(dir, spec, opts = {}) {
|
|
|
164
171
|
if (opts.recovery) await me.recover({ timeout: opts.recoveryTimeout })
|
|
165
172
|
|
|
166
173
|
for (const ext of internal.extensions) {
|
|
174
|
+
if (ext.bundled && opts.extensions === false) continue
|
|
167
175
|
const off = await ext.setup?.(me)
|
|
168
176
|
if (typeof off === 'function') me.once('close', off)
|
|
169
177
|
}
|
|
170
178
|
|
|
179
|
+
if (opts.bluetooth) {
|
|
180
|
+
me.bluetooth = new Bluetooth(me, {
|
|
181
|
+
backend: opts.bluetooth === true ? null : opts.bluetooth,
|
|
182
|
+
autoStart: true
|
|
183
|
+
})
|
|
184
|
+
me.once('close', () => me.bluetooth.close().catch(safetyCatch))
|
|
185
|
+
await me.bluetooth.ready()
|
|
186
|
+
}
|
|
187
|
+
|
|
171
188
|
bind(me, null)
|
|
172
189
|
return me
|
|
173
190
|
} catch (err) {
|
|
@@ -205,7 +222,17 @@ export async function restore(me, phrase) {
|
|
|
205
222
|
const { _dir: dir, spec, _opts: opts } = me
|
|
206
223
|
// channel must carry over — without it the recovered instance rejoins the
|
|
207
224
|
// global identity topic and never meets its channeled peers (recovery timeout).
|
|
208
|
-
const {
|
|
225
|
+
const {
|
|
226
|
+
name,
|
|
227
|
+
bootstrap,
|
|
228
|
+
isMobile,
|
|
229
|
+
onerror,
|
|
230
|
+
routes,
|
|
231
|
+
recoveryTimeout,
|
|
232
|
+
channel,
|
|
233
|
+
storageKey,
|
|
234
|
+
extensions
|
|
235
|
+
} = opts
|
|
209
236
|
|
|
210
237
|
await me.close()
|
|
211
238
|
await fs.promises.rm(`${dir}/main`, { recursive: true, force: true })
|
|
@@ -218,6 +245,8 @@ export async function restore(me, phrase) {
|
|
|
218
245
|
routes,
|
|
219
246
|
recoveryTimeout,
|
|
220
247
|
channel,
|
|
248
|
+
storageKey,
|
|
249
|
+
extensions,
|
|
221
250
|
phrase,
|
|
222
251
|
recovery: true
|
|
223
252
|
})
|
|
@@ -242,11 +271,20 @@ cero.restore = restore
|
|
|
242
271
|
cero.schema = schema
|
|
243
272
|
cero.bind = bind
|
|
244
273
|
cero.define = define
|
|
274
|
+
// test-only escape hatch (cross-package tests reset/seed the registry)
|
|
245
275
|
cero._internal = internal
|
|
246
276
|
// A bare function is shorthand for a behavior-only extension: `{ setup: fn }`.
|
|
247
277
|
// Accepts both `use(a, b)` and `use([a, b])` (and a mix) for easier composition.
|
|
248
|
-
|
|
249
|
-
|
|
278
|
+
// A named extension replaces any registered one with the same name — so
|
|
279
|
+
// `use(profileSync({ fields }))` reconfigures the bundled default instead of
|
|
280
|
+
// doubling it.
|
|
281
|
+
cero.use = (...exts) => {
|
|
282
|
+
for (const e of exts.flat().map((e) => (typeof e === 'function' ? { setup: e } : e))) {
|
|
283
|
+
const i = e.name ? internal.extensions.findIndex((x) => x.name === e.name) : -1
|
|
284
|
+
if (i >= 0) internal.extensions[i] = e
|
|
285
|
+
else internal.extensions.push(e)
|
|
286
|
+
}
|
|
287
|
+
}
|
|
250
288
|
|
|
251
289
|
async function resolveIdentity(opts, local) {
|
|
252
290
|
if (opts.identity) return opts.identity
|
package/src/lib/internal.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
// Process-wide registry for extensions registered via `cero.use()`.
|
|
2
2
|
// `build()` folds in each extension's schema; `cero()` runs each setup.
|
|
3
|
-
|
|
3
|
+
// The bundled extensions are on by default — `cero.use(profileSync({...}))`
|
|
4
|
+
// replaces a default by name, `{ extensions: false }` (on build and cero)
|
|
5
|
+
// leaves them out entirely.
|
|
6
|
+
import { profileSync } from '../extensions/profile-sync.js'
|
|
7
|
+
import { handleSync } from '../extensions/handle-sync.js'
|
|
8
|
+
|
|
9
|
+
export const internal = { extensions: [profileSync(), handleSync()] }
|
package/src/lib/operators.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Readable } from 'streamx'
|
|
2
|
+
import b4a from 'b4a'
|
|
2
3
|
|
|
3
4
|
import { encodeId, decodeId } from '@cero-base/core/blobs/codec'
|
|
4
5
|
import { onAbort } from './utils.js'
|
|
@@ -30,11 +31,11 @@ export function resolveFile(handle, id, name) {
|
|
|
30
31
|
/**
|
|
31
32
|
* Insert (or overwrite by id) a row on `ref`. The `files` builtin is special:
|
|
32
33
|
* `put(handle.files, { data, type, name? })` uploads the bytes to this handle's
|
|
33
|
-
* blob store, records `{ id, name }`, and
|
|
34
|
+
* blob store, records `{ id, name }`, and resolves the file.
|
|
34
35
|
*
|
|
35
36
|
* @param {Ref} ref
|
|
36
37
|
* @param {Record<string, any>} row
|
|
37
|
-
* @returns {Promise<SingleResult
|
|
38
|
+
* @returns {Promise<SingleResult>}
|
|
38
39
|
*/
|
|
39
40
|
export const put = (ref, row) =>
|
|
40
41
|
ref.name === 'files' ? putFile(ref, row) : ref.handle.store.put(ref.name, row)
|
|
@@ -47,7 +48,7 @@ async function putFile(ref, row) {
|
|
|
47
48
|
const blobId = await handle.blobs.put(data)
|
|
48
49
|
const id = encodeId(handle.blobs.key, blobId, type)
|
|
49
50
|
await handle.store.call('add-file', { id, name })
|
|
50
|
-
return resolveFile(handle, id, name)
|
|
51
|
+
return { data: resolveFile(handle, id, name) }
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
/**
|
|
@@ -186,10 +187,13 @@ function registerBlobCore(handle, id) {
|
|
|
186
187
|
if (!id || !handle.root?._coreKeys) return
|
|
187
188
|
try {
|
|
188
189
|
const { coreKey } = decodeId(id)
|
|
189
|
-
const hex =
|
|
190
|
+
const hex = b4a.toString(coreKey, 'hex')
|
|
190
191
|
if (!handle.root._coreKeys.has(hex)) {
|
|
191
192
|
handle.root._coreKeys.set(hex, handle.root.store.encryptionKey)
|
|
192
193
|
}
|
|
194
|
+
// remember which handle read it, so close prunes the entry (re-registered
|
|
195
|
+
// on the next read if another handle still serves the same core)
|
|
196
|
+
if (handle !== handle.root) (handle._blobKeys ??= new Set()).add(hex)
|
|
193
197
|
} catch {
|
|
194
198
|
// ignore invalid ids
|
|
195
199
|
}
|
|
@@ -239,28 +243,44 @@ export const watch = (ref, q, opts) => {
|
|
|
239
243
|
const owner = ref.handle
|
|
240
244
|
if (ref.kind !== 'handle') {
|
|
241
245
|
const src = owner.store.watch(ref.name, q)
|
|
242
|
-
const out =
|
|
243
|
-
destroy(cb) {
|
|
244
|
-
src.destroy()
|
|
245
|
-
cb(null)
|
|
246
|
-
}
|
|
247
|
-
})
|
|
248
|
-
src.on('data', (res) => out.push(resolveResult(ref, res)))
|
|
249
|
-
src.on('end', () => out.push(null))
|
|
250
|
-
src.on('error', (err) => out.destroy(err))
|
|
246
|
+
const out = snapshotStream(src, (res) => resolveResult(ref, res))
|
|
251
247
|
return bindStream(owner, out, opts)
|
|
252
248
|
}
|
|
253
249
|
const source = parentStore(ref).watch('handles', q)
|
|
250
|
+
const out = snapshotStream(source, (snap) => normalize(snap?.data, ref.name))
|
|
251
|
+
return bindStream(owner, out, opts)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Snapshots are idempotent — under a slow consumer hold only the NEWEST one
|
|
255
|
+
// instead of queueing every intermediate (a busy room + un-drained reader
|
|
256
|
+
// used to buffer full result sets without bound).
|
|
257
|
+
function snapshotStream(src, map) {
|
|
258
|
+
let pending
|
|
259
|
+
let wanted = false
|
|
254
260
|
const out = new Readable({
|
|
261
|
+
read(cb) {
|
|
262
|
+
wanted = true
|
|
263
|
+
flush()
|
|
264
|
+
cb(null)
|
|
265
|
+
},
|
|
255
266
|
destroy(cb) {
|
|
256
|
-
|
|
267
|
+
src.destroy()
|
|
257
268
|
cb(null)
|
|
258
269
|
}
|
|
259
270
|
})
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
271
|
+
const flush = () => {
|
|
272
|
+
if (!wanted || pending === undefined || out.destroyed) return
|
|
273
|
+
const snap = pending
|
|
274
|
+
pending = undefined
|
|
275
|
+
wanted = out.push(snap) !== false
|
|
276
|
+
}
|
|
277
|
+
src.on('data', (res) => {
|
|
278
|
+
pending = map(res)
|
|
279
|
+
flush()
|
|
280
|
+
})
|
|
281
|
+
src.on('end', () => out.push(null))
|
|
282
|
+
src.on('error', (err) => out.destroy(err))
|
|
283
|
+
return out
|
|
264
284
|
}
|
|
265
285
|
|
|
266
286
|
// Universal signal instantiator. Dispatch on the second arg:
|
package/src/local/index.js
CHANGED
|
@@ -9,6 +9,7 @@ import { attachRefs } from '../lib/utils.js'
|
|
|
9
9
|
* @typedef {object} LocalOpts
|
|
10
10
|
* @property {any} [root] Pre-existing HypercoreStorage to reuse.
|
|
11
11
|
* @property {any} [store] Pre-existing Corestore to reuse.
|
|
12
|
+
* @property {Uint8Array} [storageKey] 32-byte key encrypting the local store at rest.
|
|
12
13
|
*/
|
|
13
14
|
|
|
14
15
|
/**
|
|
@@ -22,7 +23,7 @@ export class Local extends ReadyResource {
|
|
|
22
23
|
* @param {any} spec Built cero spec — must include `spec.local.database` and `spec.meta.local`.
|
|
23
24
|
* @param {LocalOpts} [opts]
|
|
24
25
|
*/
|
|
25
|
-
constructor(dir, spec, { root, store } = {}) {
|
|
26
|
+
constructor(dir, spec, { root, store, storageKey } = {}) {
|
|
26
27
|
super()
|
|
27
28
|
if (!root && !store && (typeof dir !== 'string' || !dir))
|
|
28
29
|
throw CeroError.REQUIRED('dir, root, or store')
|
|
@@ -34,7 +35,8 @@ export class Local extends ReadyResource {
|
|
|
34
35
|
this.store = Storage.bee(dir, {
|
|
35
36
|
spec: { database: spec.local.database, meta: spec.meta.local },
|
|
36
37
|
root,
|
|
37
|
-
store
|
|
38
|
+
store,
|
|
39
|
+
storageKey
|
|
38
40
|
})
|
|
39
41
|
}
|
|
40
42
|
|
package/src/rpc/client.js
CHANGED
|
@@ -152,7 +152,7 @@ const operators = {
|
|
|
152
152
|
async put(name, row) {
|
|
153
153
|
const codec = this._codec()
|
|
154
154
|
const schema = this.schemaOf(name)
|
|
155
|
-
if (
|
|
155
|
+
if (name === 'files') {
|
|
156
156
|
const res = await this.rpc.addFile({
|
|
157
157
|
handle: this.id,
|
|
158
158
|
data: row.data,
|
|
@@ -225,7 +225,11 @@ const operators = {
|
|
|
225
225
|
info?.kind === 'single'
|
|
226
226
|
? codec.decodeRow(schema, res.data)
|
|
227
227
|
: codec.decodeRows(schema, res.data)
|
|
228
|
-
return {
|
|
228
|
+
return {
|
|
229
|
+
data: this._resolveFiles(name, raw),
|
|
230
|
+
total: res.total === -1 ? null : res.total,
|
|
231
|
+
size: res.size
|
|
232
|
+
}
|
|
229
233
|
},
|
|
230
234
|
|
|
231
235
|
/**
|
|
@@ -285,7 +289,11 @@ const operators = {
|
|
|
285
289
|
refInfo.kind === 'single'
|
|
286
290
|
? (codec.decodeRow(schema, snap.data) ?? null)
|
|
287
291
|
: codec.decodeRows(schema, snap.data)
|
|
288
|
-
out.push({
|
|
292
|
+
out.push({
|
|
293
|
+
data: this._resolveFiles(name, raw),
|
|
294
|
+
total: snap.total === -1 ? null : snap.total,
|
|
295
|
+
size: snap.size
|
|
296
|
+
})
|
|
289
297
|
})
|
|
290
298
|
// end exactly once whether the wire ends or the server destroys it (handle close)
|
|
291
299
|
let ended = false
|
|
@@ -320,8 +328,13 @@ const operators = {
|
|
|
320
328
|
* @param {{ role?: string }} [opts]
|
|
321
329
|
* @returns {Promise<string>}
|
|
322
330
|
*/
|
|
323
|
-
async invite({ role } = {}) {
|
|
324
|
-
const { invite } = await this.rpc.invite({
|
|
331
|
+
async invite({ role, expiresIn, multiUse } = {}) {
|
|
332
|
+
const { invite } = await this.rpc.invite({
|
|
333
|
+
handle: this.id,
|
|
334
|
+
role: role || '',
|
|
335
|
+
expiresIn: expiresIn || 0,
|
|
336
|
+
multiUse: multiUse === true
|
|
337
|
+
})
|
|
325
338
|
return invite
|
|
326
339
|
},
|
|
327
340
|
|
package/src/rpc/server.js
CHANGED
|
@@ -88,7 +88,7 @@ export class Server extends RPCServer {
|
|
|
88
88
|
/** Wire the `init` handler that lazily constructs the root cero handle. */
|
|
89
89
|
_wireInit() {
|
|
90
90
|
this.rpc.onInit(async () => {
|
|
91
|
-
if (this.me) throw
|
|
91
|
+
if (this.me) throw CeroError.CONFLICT('already initialized')
|
|
92
92
|
this.me = await cero(this.storage, this.spec, this.opts)
|
|
93
93
|
this.handles.set(this.me.id, this.me)
|
|
94
94
|
await this.me.fileServer.listen()
|
|
@@ -104,7 +104,7 @@ export class Server extends RPCServer {
|
|
|
104
104
|
/** Wire the `restore` handler that rebuilds the local store from a phrase. */
|
|
105
105
|
_wireRestore() {
|
|
106
106
|
this.rpc.onRestore(async ({ phrase }) => {
|
|
107
|
-
if (!this.me) throw
|
|
107
|
+
if (!this.me) throw CeroError.NOT_READY('Server', 'server')
|
|
108
108
|
this.me = await restore(this.me, phrase)
|
|
109
109
|
this.handles = new Map([[this.me.id, this.me]])
|
|
110
110
|
return this._identity()
|
|
@@ -144,7 +144,8 @@ export class Server extends RPCServer {
|
|
|
144
144
|
r.kind === 'single'
|
|
145
145
|
? codec.encodeRow(r.schema, result.data)
|
|
146
146
|
: codec.encodeRows(r.schema, result.data)
|
|
147
|
-
|
|
147
|
+
// total is int on the wire: -1 encodes "skipped" (null), see T2.1 lazy total
|
|
148
|
+
return { data, total: result.total ?? -1, size: result.size ?? 0 }
|
|
148
149
|
})
|
|
149
150
|
|
|
150
151
|
this.rpc.onGetOne(async ({ handle, ref, id, local }) => {
|
|
@@ -179,12 +180,30 @@ export class Server extends RPCServer {
|
|
|
179
180
|
stream.destroy()
|
|
180
181
|
return
|
|
181
182
|
}
|
|
183
|
+
// keep-latest under IPC backpressure: snapshots are idempotent, so an
|
|
184
|
+
// un-drained wire holds one pending snapshot (newest wins) instead of
|
|
185
|
+
// queueing every intermediate result set — encode only what ships
|
|
186
|
+
let pending = null
|
|
187
|
+
let blocked = false
|
|
182
188
|
const onData = (snap) => {
|
|
189
|
+
if (blocked) {
|
|
190
|
+
pending = snap
|
|
191
|
+
return
|
|
192
|
+
}
|
|
183
193
|
const data =
|
|
184
194
|
r.kind === 'single'
|
|
185
195
|
? codec.encodeRow(r.schema, snap.data)
|
|
186
196
|
: codec.encodeRows(r.schema, snap.data)
|
|
187
|
-
stream.write({ data, total: snap.total ??
|
|
197
|
+
blocked = stream.write({ data, total: snap.total ?? -1, size: snap.size ?? 0 }) === false
|
|
198
|
+
if (blocked) {
|
|
199
|
+
stream.once('drain', () => {
|
|
200
|
+
blocked = false
|
|
201
|
+
if (pending === null) return
|
|
202
|
+
const next = pending
|
|
203
|
+
pending = null
|
|
204
|
+
onData(next)
|
|
205
|
+
})
|
|
206
|
+
}
|
|
188
207
|
}
|
|
189
208
|
let set = this._watchStreams.get(handle)
|
|
190
209
|
if (!set) this._watchStreams.set(handle, (set = new Set()))
|
|
@@ -208,9 +227,13 @@ export class Server extends RPCServer {
|
|
|
208
227
|
|
|
209
228
|
/** Register invite/revoke/join RPC handlers. */
|
|
210
229
|
_wirePairing() {
|
|
211
|
-
this.rpc.onInvite(async ({ handle, role }) => {
|
|
230
|
+
this.rpc.onInvite(async ({ handle, role, expiresIn, multiUse }) => {
|
|
212
231
|
const h = this._resolve(handle)
|
|
213
|
-
const invite = await h.invite({
|
|
232
|
+
const invite = await h.invite({
|
|
233
|
+
role: role || undefined,
|
|
234
|
+
expiresIn: expiresIn || undefined,
|
|
235
|
+
multiUse: multiUse === true
|
|
236
|
+
})
|
|
214
237
|
return { invite }
|
|
215
238
|
})
|
|
216
239
|
|
|
@@ -328,7 +351,7 @@ export class Server extends RPCServer {
|
|
|
328
351
|
/** Wire the on-demand `seed` handler — surfaces the recovery phrase only when asked. */
|
|
329
352
|
_wireSeed() {
|
|
330
353
|
this.rpc.onSeed(async () => {
|
|
331
|
-
if (!this.me) throw
|
|
354
|
+
if (!this.me) throw CeroError.NOT_READY('Server', 'server')
|
|
332
355
|
return { phrase: this.me.identity.toPhrase() }
|
|
333
356
|
})
|
|
334
357
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `me.bluetooth` — the whole app-facing surface for nearby (Bluetooth) sync.
|
|
3
|
+
* Bluetooth is a cero feature, not a parallel API: it only changes how peers
|
|
4
|
+
* meet and carry bytes; capability-gated replication still decides what syncs.
|
|
5
|
+
*
|
|
6
|
+
* ```js
|
|
7
|
+
* const me = await cero(dir, spec, { channel, bluetooth: true })
|
|
8
|
+
* me.bluetooth.state // 'unsupported' | 'unauthorized' | 'off' | 'waiting' | 'on'
|
|
9
|
+
* await me.bluetooth.start()
|
|
10
|
+
* me.bluetooth.peers // Map of live BLE links
|
|
11
|
+
* me.bluetooth.on('update', () => {})
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @extends ReadyResource
|
|
15
|
+
*/
|
|
16
|
+
export class Bluetooth extends ReadyResource {
|
|
17
|
+
/**
|
|
18
|
+
* @param {object} handle Root cero Handle (network + identity + channel).
|
|
19
|
+
* @param {object} [opts]
|
|
20
|
+
* @param {any} [opts.backend] Injected bare-bluetooth-shaped backend (tests); lazy-loaded when absent.
|
|
21
|
+
* @param {boolean} [opts.autoStart] Start on handle open (from `cero({ bluetooth: true })`).
|
|
22
|
+
*/
|
|
23
|
+
constructor(handle: object, { backend, autoStart }?: {
|
|
24
|
+
backend?: any;
|
|
25
|
+
autoStart?: boolean;
|
|
26
|
+
});
|
|
27
|
+
_handle: any;
|
|
28
|
+
_backend: any;
|
|
29
|
+
_autoStart: boolean;
|
|
30
|
+
_transport: BluetoothTransport;
|
|
31
|
+
_announces: Set<any>;
|
|
32
|
+
/** @type {'unsupported'|'unauthorized'|'off'|'waiting'|'starting'|'on'} */
|
|
33
|
+
state: "unsupported" | "unauthorized" | "off" | "waiting" | "starting" | "on";
|
|
34
|
+
/**
|
|
35
|
+
* Offline join rendezvous. Both sides derive the same BLE service UUID from
|
|
36
|
+
* the invite, so they find each other with zero DHT: the host calls this
|
|
37
|
+
* while the invite QR is on screen; the joiner's `open(me.room, invite)`
|
|
38
|
+
* calls it automatically for the duration of the join. Returns a stop
|
|
39
|
+
* function — closing the QR must stop the advertisement so a photographed
|
|
40
|
+
* invite doesn't stay an ambient admission ticket. Auto-stops at the
|
|
41
|
+
* invite's expiry, on `bluetooth.stop()`, and on close.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} invite Z32 invite string.
|
|
44
|
+
* @returns {() => void}
|
|
45
|
+
*/
|
|
46
|
+
announce(invite: string): () => void;
|
|
47
|
+
/** @returns {Map<string, any>} Live BLE links, keyed by peer node id. */
|
|
48
|
+
get peers(): Map<string, any>;
|
|
49
|
+
/**
|
|
50
|
+
* Begin advertising + scanning on the channel-derived UUID. Idempotent.
|
|
51
|
+
* No-op (stays `unsupported`) when no backend is present.
|
|
52
|
+
*
|
|
53
|
+
* @returns {Promise<void>}
|
|
54
|
+
*/
|
|
55
|
+
start(): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Stop advertising/scanning and drop links. Idempotent. Sync stops; local
|
|
58
|
+
* data and the rest of the network (DHT) are untouched.
|
|
59
|
+
*
|
|
60
|
+
* @returns {Promise<void>}
|
|
61
|
+
*/
|
|
62
|
+
stop(): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
import ReadyResource from 'ready-resource';
|
|
65
|
+
import { BluetoothTransport } from '@cero-base/core/network/bluetooth';
|
package/types/build/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export function build(specDir: any, schema: any, { ns }?: {
|
|
1
|
+
export function build(specDir: any, schema: any, { ns, extensions }?: {
|
|
2
2
|
ns?: string;
|
|
3
|
+
extensions?: boolean;
|
|
3
4
|
}): Promise<void>;
|
|
4
5
|
export { getHyperdbType } from "./builtins.js";
|
|
5
6
|
export type Schema = import("@cero-base/core/schema").Schema;
|
package/types/build/schemas.d.ts
CHANGED
|
@@ -118,6 +118,8 @@ export const rpc: {
|
|
|
118
118
|
'req-invite': {
|
|
119
119
|
handle: import("@cero-base/core").Prim;
|
|
120
120
|
role: import("@cero-base/core").Prim;
|
|
121
|
+
expiresIn: import("@cero-base/core").Prim;
|
|
122
|
+
multiUse: import("@cero-base/core").Prim;
|
|
121
123
|
};
|
|
122
124
|
'req-revoke': {
|
|
123
125
|
handle: import("@cero-base/core").Prim;
|
package/types/handle/index.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export { Ref } from "../lib/utils.js";
|
|
|
20
20
|
* @property {Uint8Array} [encryptionKey] Existing encryption key.
|
|
21
21
|
* @property {string} [namespace] Corestore namespace.
|
|
22
22
|
* @property {KeyPair} [keyPair] Writer keypair.
|
|
23
|
+
* @property {boolean} [passive] Join discovery server-only; flip later with `setActive`.
|
|
23
24
|
* @property {boolean} [pair] When `false`, skips creating a `Pairing` session.
|
|
24
25
|
*
|
|
25
26
|
* @typedef {object} CreateChildOpts
|
|
@@ -195,6 +196,15 @@ export class Handle extends ReadyResource {
|
|
|
195
196
|
* @returns {Promise<void>}
|
|
196
197
|
*/
|
|
197
198
|
recover({ timeout }?: RecoverOpts): Promise<void>;
|
|
199
|
+
/**
|
|
200
|
+
* Flip this handle's swarm announce mode — `setActive(false)` demotes an
|
|
201
|
+
* idle/background room to server-only (still reachable, stops searching);
|
|
202
|
+
* `setActive(true)` promotes it back on focus. Cheap, safe to call often.
|
|
203
|
+
*
|
|
204
|
+
* @param {boolean} active
|
|
205
|
+
* @returns {Promise<void>}
|
|
206
|
+
*/
|
|
207
|
+
setActive(active: boolean): Promise<void>;
|
|
198
208
|
/**
|
|
199
209
|
* Mint a pairing invite for this handle.
|
|
200
210
|
*
|
|
@@ -368,6 +378,10 @@ export type HandleOpts = {
|
|
|
368
378
|
* Writer keypair.
|
|
369
379
|
*/
|
|
370
380
|
keyPair?: KeyPair;
|
|
381
|
+
/**
|
|
382
|
+
* Join discovery server-only; flip later with `setActive`.
|
|
383
|
+
*/
|
|
384
|
+
passive?: boolean;
|
|
371
385
|
/**
|
|
372
386
|
* When `false`, skips creating a `Pairing` session.
|
|
373
387
|
*/
|
package/types/index.d.ts
CHANGED
|
@@ -17,6 +17,9 @@
|
|
|
17
17
|
* @property {(err: any) => void} [onerror] Background-task error handler.
|
|
18
18
|
* @property {boolean} [recovery] Recovery flow — wipe local state and re-claim a writer slot.
|
|
19
19
|
* @property {number} [recoveryTimeout] Max wait for peer data + writer capability during recovery.
|
|
20
|
+
* @property {Uint8Array} [storageKey] 32-byte key encrypting local key material (master seed, device keypairs) at rest. Source it from the OS keychain — cero never stores it.
|
|
21
|
+
* @property {boolean} [extensions] `false` disables the bundled extensions (profileSync, handleSync) for this instance. Build with `{ extensions: false }` too so the spec matches.
|
|
22
|
+
* @property {boolean | any} [bluetooth] `true` enables nearby (Bluetooth) sync via `me.bluetooth`; pass a bare-bluetooth-shaped backend to inject one. Absent backend → `me.bluetooth.state === 'unsupported'`.
|
|
20
23
|
*/
|
|
21
24
|
/**
|
|
22
25
|
* Open (or create) a cero handle at `dir`. Sets up storage, network and
|
|
@@ -47,7 +50,7 @@ export namespace cero {
|
|
|
47
50
|
export { bind };
|
|
48
51
|
export { define };
|
|
49
52
|
export { internal as _internal };
|
|
50
|
-
export function use(...exts: any[]):
|
|
53
|
+
export function use(...exts: any[]): void;
|
|
51
54
|
}
|
|
52
55
|
/**
|
|
53
56
|
* Restore a cero instance from a mnemonic phrase. Closes the running
|
|
@@ -121,6 +124,18 @@ export type CeroOpts = {
|
|
|
121
124
|
* Max wait for peer data + writer capability during recovery.
|
|
122
125
|
*/
|
|
123
126
|
recoveryTimeout?: number;
|
|
127
|
+
/**
|
|
128
|
+
* 32-byte key encrypting local key material (master seed, device keypairs) at rest. Source it from the OS keychain — cero never stores it.
|
|
129
|
+
*/
|
|
130
|
+
storageKey?: Uint8Array;
|
|
131
|
+
/**
|
|
132
|
+
* `false` disables the bundled extensions (profileSync, handleSync) for this instance. Build with `{ extensions: false }` too so the spec matches.
|
|
133
|
+
*/
|
|
134
|
+
extensions?: boolean;
|
|
135
|
+
/**
|
|
136
|
+
* `true` enables nearby (Bluetooth) sync via `me.bluetooth`; pass a bare-bluetooth-shaped backend to inject one. Absent backend → `me.bluetooth.state === 'unsupported'`.
|
|
137
|
+
*/
|
|
138
|
+
bluetooth?: boolean | any;
|
|
124
139
|
};
|
|
125
140
|
import { t } from './lib/spec.js';
|
|
126
141
|
import { put } from './lib/operators.js';
|
package/types/lib/internal.d.ts
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
1
|
export namespace internal {
|
|
2
|
-
let extensions:
|
|
2
|
+
let extensions: ({
|
|
3
|
+
name: string;
|
|
4
|
+
bundled: boolean;
|
|
5
|
+
schema: {
|
|
6
|
+
profile: import("@cero-base/core").TypeDef;
|
|
7
|
+
members: {
|
|
8
|
+
kind: "extend";
|
|
9
|
+
fields: Record<string, import("@cero-base/core").Prim>;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
setup(me: any): void;
|
|
13
|
+
} | {
|
|
14
|
+
name: string;
|
|
15
|
+
bundled: boolean;
|
|
16
|
+
schema: {
|
|
17
|
+
handles: {
|
|
18
|
+
kind: "extend";
|
|
19
|
+
fields: Record<string, import("@cero-base/core").Prim>;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
setup(me: any): void;
|
|
23
|
+
})[];
|
|
3
24
|
}
|
package/types/lib/operators.d.ts
CHANGED
|
@@ -45,13 +45,7 @@ export function bind(handle: any, arg: Record<string, any> | string | null): any
|
|
|
45
45
|
export function define(map: Record<string, any>): void;
|
|
46
46
|
/** Test seam: clear all registered operators. */
|
|
47
47
|
export function _clearDefined(): void;
|
|
48
|
-
export function put(ref: Ref, row: Record<string, any>): Promise<SingleResult
|
|
49
|
-
id: string;
|
|
50
|
-
name?: string;
|
|
51
|
-
type: string;
|
|
52
|
-
size: number;
|
|
53
|
-
url: string;
|
|
54
|
-
}>;
|
|
48
|
+
export function put(ref: Ref, row: Record<string, any>): Promise<SingleResult>;
|
|
55
49
|
export function set(ref: Ref, row: Record<string, any>, opts?: {
|
|
56
50
|
upsert?: boolean;
|
|
57
51
|
}): Promise<SingleResult>;
|
package/types/local/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* @typedef {object} LocalOpts
|
|
3
3
|
* @property {any} [root] Pre-existing HypercoreStorage to reuse.
|
|
4
4
|
* @property {any} [store] Pre-existing Corestore to reuse.
|
|
5
|
+
* @property {Uint8Array} [storageKey] 32-byte key encrypting the local store at rest.
|
|
5
6
|
*/
|
|
6
7
|
/**
|
|
7
8
|
* Per-device, single-writer storage for cero — holds the master seed,
|
|
@@ -14,7 +15,7 @@ export class Local extends ReadyResource {
|
|
|
14
15
|
* @param {any} spec Built cero spec — must include `spec.local.database` and `spec.meta.local`.
|
|
15
16
|
* @param {LocalOpts} [opts]
|
|
16
17
|
*/
|
|
17
|
-
constructor(dir: string | null, spec: any, { root, store }?: LocalOpts);
|
|
18
|
+
constructor(dir: string | null, spec: any, { root, store, storageKey }?: LocalOpts);
|
|
18
19
|
dir: string;
|
|
19
20
|
spec: any;
|
|
20
21
|
store: Storage;
|
|
@@ -28,6 +29,10 @@ export type LocalOpts = {
|
|
|
28
29
|
* Pre-existing Corestore to reuse.
|
|
29
30
|
*/
|
|
30
31
|
store?: any;
|
|
32
|
+
/**
|
|
33
|
+
* 32-byte key encrypting the local store at rest.
|
|
34
|
+
*/
|
|
35
|
+
storageKey?: Uint8Array;
|
|
31
36
|
};
|
|
32
37
|
import ReadyResource from 'ready-resource';
|
|
33
38
|
import { Storage } from '@cero-base/core/storage';
|
package/src/CLAUDE.md
DELETED
package/src/build/CLAUDE.md
DELETED
package/src/extensions/CLAUDE.md
DELETED