@cero-base/cero 2.6.0 → 2.7.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 +2 -2
- package/src/build/index.js +27 -6
- package/src/build/internal.js +2 -1
- package/src/build/schemas.js +12 -0
- package/src/handle/index.js +2 -0
- package/src/index.js +26 -7
- package/src/rpc/client.js +24 -2
- package/src/rpc/server.js +26 -1
- package/types/build/schemas.d.ts +11 -0
- package/types/index.d.ts +14 -1
- package/types/rpc/client.d.ts +14 -1
- package/types/rpc/server.d.ts +5 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/cero",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"description": "The ideal p2p API — everything is a handle, handles contain refs, refs contain rows.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -113,7 +113,7 @@
|
|
|
113
113
|
"test:node": "find test -name '*.test.js' | sort | xargs -P4 -n1 brittle-node"
|
|
114
114
|
},
|
|
115
115
|
"dependencies": {
|
|
116
|
-
"@cero-base/core": "^2.
|
|
116
|
+
"@cero-base/core": "^2.7.0",
|
|
117
117
|
"b4a": "^1.9.0",
|
|
118
118
|
"bare-abort-controller": "^1.1.2",
|
|
119
119
|
"bare-crypto": "^1.15.4",
|
package/src/build/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { join, resolve } from 'path'
|
|
2
|
-
import { promises as fs } from 'fs'
|
|
2
|
+
import { promises as fs, existsSync, readFileSync } from 'fs'
|
|
3
3
|
import { pathToFileURL } from 'url'
|
|
4
4
|
|
|
5
5
|
import b4a from 'b4a'
|
|
@@ -273,9 +273,10 @@ function emitMain(dir, ns, { types, collections, dispatches, indexes = [] }, { r
|
|
|
273
273
|
|
|
274
274
|
const s = Hyperschema.from(schemaDir)
|
|
275
275
|
const sns = s.namespace(ns)
|
|
276
|
-
|
|
277
|
-
for (const desc of types) sns.register(desc)
|
|
278
|
-
|
|
276
|
+
const keep = slots(schemaDir, ns)
|
|
277
|
+
for (const desc of internal.types('main', extend)) sns.register(keep(desc))
|
|
278
|
+
for (const desc of types) sns.register(keep(desc))
|
|
279
|
+
if (rpc) for (const desc of internal.types('rpc')) sns.register(keep(desc))
|
|
279
280
|
Hyperschema.toDisk(s, schemaDir, { esm: true })
|
|
280
281
|
|
|
281
282
|
const db = HyperdbBuilder.from(schemaDir, dbDir)
|
|
@@ -305,8 +306,9 @@ function emitLocal(dir, ns, { types, collections, indexes = [] }) {
|
|
|
305
306
|
|
|
306
307
|
const s = Hyperschema.from(schemaDir)
|
|
307
308
|
const sns = s.namespace(ns)
|
|
308
|
-
|
|
309
|
-
for (const desc of types) sns.register(desc)
|
|
309
|
+
const keep = slots(schemaDir, ns)
|
|
310
|
+
for (const desc of internal.types('local')) sns.register(keep(desc))
|
|
311
|
+
for (const desc of types) sns.register(keep(desc))
|
|
310
312
|
Hyperschema.toDisk(s, schemaDir, { esm: true })
|
|
311
313
|
|
|
312
314
|
const db = HyperdbBuilder.from(schemaDir, dbDir)
|
|
@@ -317,6 +319,25 @@ function emitLocal(dir, ns, { types, collections, indexes = [] }) {
|
|
|
317
319
|
HyperdbBuilder.toDisk(db, dbDir, { esm: true })
|
|
318
320
|
}
|
|
319
321
|
|
|
322
|
+
// a field keeps the slot the built spec gave it, whichever schema declares it: hyperschema stores
|
|
323
|
+
// a row by position, and a type's fields are cero's, then its extensions', then the app's, so one
|
|
324
|
+
// more of cero's would take an extension's slot. A field the spec lacks goes after the ones it has
|
|
325
|
+
function slots(schemaDir, ns) {
|
|
326
|
+
const file = join(schemaDir, 'schema.json')
|
|
327
|
+
const built = existsSync(file) ? JSON.parse(readFileSync(file, 'utf-8')).schema : []
|
|
328
|
+
const had = new Map(
|
|
329
|
+
built
|
|
330
|
+
.filter((x) => x.namespace === ns && x.fields)
|
|
331
|
+
.map((x) => [x.name, x.fields.map((f) => f.name)])
|
|
332
|
+
)
|
|
333
|
+
return (desc) => {
|
|
334
|
+
const names = had.get(desc.name)
|
|
335
|
+
if (!names || !desc.fields) return desc
|
|
336
|
+
const at = (f) => (names.includes(f.name) ? names.indexOf(f.name) : names.length)
|
|
337
|
+
return { ...desc, fields: desc.fields.toSorted((a, b) => at(a) - at(b)) }
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
320
341
|
const IMPORTS = {
|
|
321
342
|
database: (n) => `import ${n}Database from './handles/${n}/db/index.js'`,
|
|
322
343
|
dispatch: (n) => `import * as ${n}Dispatch from './handles/${n}/dispatch/index.js'`,
|
package/src/build/internal.js
CHANGED
|
@@ -194,7 +194,8 @@ const COMMANDS = [
|
|
|
194
194
|
['close-handle', 'req-handle', 'res-ok'],
|
|
195
195
|
// not a verb: it ends the handle on both sides
|
|
196
196
|
['leave', 'req-handle', 'res-ok'],
|
|
197
|
-
['verb', 'req-verb', 'res-data']
|
|
197
|
+
['verb', 'req-verb', 'res-data'],
|
|
198
|
+
['link', 'req-link', 'res-link', true]
|
|
198
199
|
]
|
|
199
200
|
|
|
200
201
|
/**
|
package/src/build/schemas.js
CHANGED
|
@@ -239,6 +239,9 @@ export const rpc = {
|
|
|
239
239
|
'req-restore': {
|
|
240
240
|
phrase: required(string)
|
|
241
241
|
},
|
|
242
|
+
'req-link': {
|
|
243
|
+
invite: required(string)
|
|
244
|
+
},
|
|
242
245
|
'req-row': {
|
|
243
246
|
handle: required(string),
|
|
244
247
|
ref: required(string),
|
|
@@ -313,6 +316,15 @@ export const rpc = {
|
|
|
313
316
|
fileToken: string,
|
|
314
317
|
deviceName: string
|
|
315
318
|
},
|
|
319
|
+
// a link's frames: the request's id as it is sent, then the identity once linked
|
|
320
|
+
'res-link': {
|
|
321
|
+
request: string,
|
|
322
|
+
id: string,
|
|
323
|
+
deviceId: string,
|
|
324
|
+
deviceName: string,
|
|
325
|
+
fileBase: string,
|
|
326
|
+
fileToken: string
|
|
327
|
+
},
|
|
316
328
|
'res-ok': {
|
|
317
329
|
ok: bool
|
|
318
330
|
},
|
package/src/handle/index.js
CHANGED
|
@@ -905,6 +905,8 @@ export class Handle extends ReadyResource {
|
|
|
905
905
|
/** @private */
|
|
906
906
|
async _carryOn() {
|
|
907
907
|
for (const { id, invite, type } of (await this.local.store.get('joins')).data) {
|
|
908
|
+
// the root's own device link row has no handle type: the launch resumes that one
|
|
909
|
+
if (!this.spec.handles?.[type]) continue
|
|
908
910
|
if (await this._joined(type, Invite.parse(invite).discoveryKey)) {
|
|
909
911
|
await this.local.store.del('joins', id)
|
|
910
912
|
continue
|
package/src/index.js
CHANGED
|
@@ -102,9 +102,6 @@ async function start(dir, spec, opts = {}) {
|
|
|
102
102
|
// a device invite, this launch's or the one a killed launch was waiting on
|
|
103
103
|
const pending = done || opts.phrase ? null : await pendingLink(local)
|
|
104
104
|
const linking = !done && !opts.phrase && !!(opts.invite || pending)
|
|
105
|
-
if (device?.setup === 'link' && !linking && !opts.phrase) {
|
|
106
|
-
throw CeroError.REQUIRED('invite: this dir waits for a device invite, or a phrase')
|
|
107
|
-
}
|
|
108
105
|
const resolved = linking ? null : await resolveIdentity(opts, local, done)
|
|
109
106
|
// cero creates only an identity it minted, in this launch or in the killed one it resumes
|
|
110
107
|
const creating =
|
|
@@ -144,7 +141,7 @@ async function start(dir, spec, opts = {}) {
|
|
|
144
141
|
await network.ready()
|
|
145
142
|
// the link first: who this device is comes with the reply
|
|
146
143
|
const linked = linking
|
|
147
|
-
? await
|
|
144
|
+
? await ask(local, network, spec, { ...opts, writer: keyPair, timeout }, pending)
|
|
148
145
|
: null
|
|
149
146
|
const identity = linked ? linked.identity : resolved.identity
|
|
150
147
|
const fresh = resolved?.fresh === true
|
|
@@ -251,9 +248,31 @@ export async function restore(me, phrase) {
|
|
|
251
248
|
return start(dir, spec, { ...opts, phrase })
|
|
252
249
|
}
|
|
253
250
|
|
|
251
|
+
/**
|
|
252
|
+
* Link this device to the identity behind a device invite: closes `me`, deletes `dir/main` and
|
|
253
|
+
* opens the directory with `invite`, as a new device of that identity, with no phrase on it.
|
|
254
|
+
*
|
|
255
|
+
* @param {Context} me The root to replace.
|
|
256
|
+
* @param {string} invite A device invite, as `cero.invite(me)` gives it on a device of that identity.
|
|
257
|
+
* @param {{ onrequest?: (id: string) => void }} [opts] `onrequest`: the request's id as it is sent, the one the accepting device sees in `me.requests`.
|
|
258
|
+
* @returns {Promise<Handle>} The linked root handle.
|
|
259
|
+
*/
|
|
260
|
+
export async function link(me, invite, { onrequest } = {}) {
|
|
261
|
+
if (!me?._dir) throw CeroError.INVALID('me must be a cero instance')
|
|
262
|
+
if (!invite) throw CeroError.REQUIRED('invite')
|
|
263
|
+
|
|
264
|
+
const { _dir: dir, spec, _opts: opts } = me
|
|
265
|
+
|
|
266
|
+
await me.close()
|
|
267
|
+
await fs.promises.rm(`${dir}/main`, { recursive: true, force: true })
|
|
268
|
+
|
|
269
|
+
// the other device is in someone's other hand: the wait is the invite's lifetime, deny ends it
|
|
270
|
+
return start(dir, spec, { ...opts, phrase: undefined, invite, onrequest, recoveryTimeout: 0 })
|
|
271
|
+
}
|
|
272
|
+
|
|
254
273
|
// the facade: cero.put and import { put } are the same function
|
|
255
|
-
/** @type {typeof start & typeof verbs & { t: typeof t, schema: typeof schema, peek: typeof peek, restore: typeof restore }} */
|
|
256
|
-
export const cero = Object.assign(start, verbs, { t, schema, peek, restore })
|
|
274
|
+
/** @type {typeof start & typeof verbs & { t: typeof t, schema: typeof schema, peek: typeof peek, restore: typeof restore, link: typeof link }} */
|
|
275
|
+
export const cero = Object.assign(start, verbs, { t, schema, peek, restore, link })
|
|
257
276
|
|
|
258
277
|
function pointerManifest(store, identity) {
|
|
259
278
|
return { version: store.manifestVersion, signers: [{ publicKey: identity.publicKey }] }
|
|
@@ -293,7 +312,7 @@ async function pendingLink(local) {
|
|
|
293
312
|
|
|
294
313
|
// the root's join: this device, bare, knocks with the invite and waits for a device of the identity
|
|
295
314
|
// to let it in. What it learns is kept in its joins row, so a killed launch resumes it
|
|
296
|
-
async function
|
|
315
|
+
async function ask(local, network, spec, { invite, writer, onrequest, timeout }, pending) {
|
|
297
316
|
const parsed = Invite.parse(invite ?? pending.invite)
|
|
298
317
|
if (parsed.data?.byteLength !== 32) throw CeroError.INVALID_INVITE('not a device invite')
|
|
299
318
|
const secret = pending?.device ?? Identity.randomKeyPair().secretKey
|
package/src/rpc/client.js
CHANGED
|
@@ -424,6 +424,28 @@ export async function restore(me, phrase) {
|
|
|
424
424
|
return me
|
|
425
425
|
}
|
|
426
426
|
|
|
427
|
+
/**
|
|
428
|
+
* Link the app to the identity behind a device invite, as `cero.link` does in the worker: the
|
|
429
|
+
* client is then that identity. `onrequest` gets the request's id as it is sent.
|
|
430
|
+
*
|
|
431
|
+
* @param {Client} me
|
|
432
|
+
* @param {string} invite
|
|
433
|
+
* @param {{ onrequest?: (id: string) => void }} [opts]
|
|
434
|
+
* @returns {Promise<Client>}
|
|
435
|
+
*/
|
|
436
|
+
export function link(me, invite, { onrequest } = {}) {
|
|
437
|
+
return new Promise((resolve, reject) => {
|
|
438
|
+
const wire = me.rpc.link({ invite })
|
|
439
|
+
wire.on('data', (frame) => {
|
|
440
|
+
if (frame.request) onrequest?.(frame.request)
|
|
441
|
+
else me._identify(frame)
|
|
442
|
+
})
|
|
443
|
+
wire.on('error', reject)
|
|
444
|
+
wire.on('end', () => resolve(me))
|
|
445
|
+
wire.on('close', () => reject(CeroError.INVALID('link: the worker closed the stream')))
|
|
446
|
+
})
|
|
447
|
+
}
|
|
448
|
+
|
|
427
449
|
/**
|
|
428
450
|
* Per-device `local`-namespace surface on a Client.
|
|
429
451
|
*/
|
|
@@ -636,8 +658,8 @@ function start(ipc, spec, opts) {
|
|
|
636
658
|
return /** @type {Promise<Context>} */ (connect(ipc, spec, opts))
|
|
637
659
|
}
|
|
638
660
|
|
|
639
|
-
/** @type {typeof start & typeof remote & { connect: typeof connect, restore: typeof restore, t: typeof t, schema: typeof schema }} */
|
|
640
|
-
export const cero = Object.assign(start, remote, { connect, restore, t, schema })
|
|
661
|
+
/** @type {typeof start & typeof remote & { connect: typeof connect, restore: typeof restore, link: typeof link, t: typeof t, schema: typeof schema }} */
|
|
662
|
+
export const cero = Object.assign(start, remote, { connect, restore, link, t, schema })
|
|
641
663
|
|
|
642
664
|
// the same shape a local root has
|
|
643
665
|
function device({ deviceId, deviceName }) {
|
package/src/rpc/server.js
CHANGED
|
@@ -4,7 +4,7 @@ import safetyCatch from 'safety-catch'
|
|
|
4
4
|
import { RPCServer, bindCodec } from '@cero-base/core/rpc'
|
|
5
5
|
import { CeroError } from '@cero-base/core/errors'
|
|
6
6
|
|
|
7
|
-
import { cero, restore } from '../index.js'
|
|
7
|
+
import { cero, restore, link } from '../index.js'
|
|
8
8
|
import {
|
|
9
9
|
put,
|
|
10
10
|
set,
|
|
@@ -139,6 +139,7 @@ export class Server extends RPCServer {
|
|
|
139
139
|
this._wireHandles()
|
|
140
140
|
this._wireVerbs()
|
|
141
141
|
this._wireRestore()
|
|
142
|
+
this._wireLink()
|
|
142
143
|
}
|
|
143
144
|
|
|
144
145
|
/** @private */
|
|
@@ -206,6 +207,30 @@ export class Server extends RPCServer {
|
|
|
206
207
|
})
|
|
207
208
|
}
|
|
208
209
|
|
|
210
|
+
/**
|
|
211
|
+
* Wire the `link` handler: a stream, the request's id as it is sent, the identity once linked.
|
|
212
|
+
* @private
|
|
213
|
+
*/
|
|
214
|
+
_wireLink() {
|
|
215
|
+
this.rpc.onLink((stream) => {
|
|
216
|
+
stream.on('error', safetyCatch)
|
|
217
|
+
const run = async () => {
|
|
218
|
+
if (!this.me) throw CeroError.NOT_READY('Server', 'server')
|
|
219
|
+
this.me = await link(this.me, stream.data.invite, {
|
|
220
|
+
onrequest: (id) => stream.write({ request: id })
|
|
221
|
+
})
|
|
222
|
+
this.handles = new Map([[this.me.id, this.me]])
|
|
223
|
+
stream.write({ request: '', ...this._identity() })
|
|
224
|
+
stream.end()
|
|
225
|
+
}
|
|
226
|
+
run().catch((err) => {
|
|
227
|
+
stream.once('error', () => {})
|
|
228
|
+
stream.writeStream.destroy(err)
|
|
229
|
+
stream.destroy()
|
|
230
|
+
})
|
|
231
|
+
})
|
|
232
|
+
}
|
|
233
|
+
|
|
209
234
|
/**
|
|
210
235
|
* Register the row-level RPC handlers (put/set/get/del/watch/call).
|
|
211
236
|
* @private
|
package/types/build/schemas.d.ts
CHANGED
|
@@ -199,6 +199,9 @@ export declare const rpc: {
|
|
|
199
199
|
'req-restore': {
|
|
200
200
|
phrase: import("@cero-base/core").Prim;
|
|
201
201
|
};
|
|
202
|
+
'req-link': {
|
|
203
|
+
invite: import("@cero-base/core").Prim;
|
|
204
|
+
};
|
|
202
205
|
'req-row': {
|
|
203
206
|
handle: import("@cero-base/core").Prim;
|
|
204
207
|
ref: import("@cero-base/core").Prim;
|
|
@@ -274,6 +277,14 @@ export declare const rpc: {
|
|
|
274
277
|
fileToken: import("@cero-base/core").Prim;
|
|
275
278
|
deviceName: import("@cero-base/core").Prim;
|
|
276
279
|
};
|
|
280
|
+
'res-link': {
|
|
281
|
+
request: import("@cero-base/core").Prim;
|
|
282
|
+
id: import("@cero-base/core").Prim;
|
|
283
|
+
deviceId: import("@cero-base/core").Prim;
|
|
284
|
+
deviceName: import("@cero-base/core").Prim;
|
|
285
|
+
fileBase: import("@cero-base/core").Prim;
|
|
286
|
+
fileToken: import("@cero-base/core").Prim;
|
|
287
|
+
};
|
|
277
288
|
'res-ok': {
|
|
278
289
|
ok: import("@cero-base/core").Prim;
|
|
279
290
|
};
|
package/types/index.d.ts
CHANGED
|
@@ -127,10 +127,23 @@ declare function start(dir: string, spec: import('./lib/spec.js').Spec, opts?: C
|
|
|
127
127
|
* @returns {Promise<Handle>} Freshly restored root handle.
|
|
128
128
|
*/
|
|
129
129
|
export declare function restore(me: Context, phrase: string): Promise<Handle>;
|
|
130
|
-
/**
|
|
130
|
+
/**
|
|
131
|
+
* Link this device to the identity behind a device invite: closes `me`, deletes `dir/main` and
|
|
132
|
+
* opens the directory with `invite`, as a new device of that identity, with no phrase on it.
|
|
133
|
+
*
|
|
134
|
+
* @param {Context} me The root to replace.
|
|
135
|
+
* @param {string} invite A device invite, as `cero.invite(me)` gives it on a device of that identity.
|
|
136
|
+
* @param {{ onrequest?: (id: string) => void }} [opts] `onrequest`: the request's id as it is sent, the one the accepting device sees in `me.requests`.
|
|
137
|
+
* @returns {Promise<Handle>} The linked root handle.
|
|
138
|
+
*/
|
|
139
|
+
export declare function link(me: Context, invite: string, { onrequest }?: {
|
|
140
|
+
onrequest?: (id: string) => void;
|
|
141
|
+
}): Promise<Handle>;
|
|
142
|
+
/** @type {typeof start & typeof verbs & { t: typeof t, schema: typeof schema, peek: typeof peek, restore: typeof restore, link: typeof link }} */
|
|
131
143
|
export declare const cero: typeof start & typeof verbs & {
|
|
132
144
|
t: typeof t;
|
|
133
145
|
schema: typeof schema;
|
|
134
146
|
peek: typeof peek;
|
|
135
147
|
restore: typeof restore;
|
|
148
|
+
link: typeof link;
|
|
136
149
|
};
|
package/types/rpc/client.d.ts
CHANGED
|
@@ -76,6 +76,18 @@ export type Context = (Client | Handle) & Record<string, Ref>;
|
|
|
76
76
|
* @returns {Promise<Client>}
|
|
77
77
|
*/
|
|
78
78
|
export declare function restore(me: Client, phrase: string): Promise<Client>;
|
|
79
|
+
/**
|
|
80
|
+
* Link the app to the identity behind a device invite, as `cero.link` does in the worker: the
|
|
81
|
+
* client is then that identity. `onrequest` gets the request's id as it is sent.
|
|
82
|
+
*
|
|
83
|
+
* @param {Client} me
|
|
84
|
+
* @param {string} invite
|
|
85
|
+
* @param {{ onrequest?: (id: string) => void }} [opts]
|
|
86
|
+
* @returns {Promise<Client>}
|
|
87
|
+
*/
|
|
88
|
+
export declare function link(me: Client, invite: string, { onrequest }?: {
|
|
89
|
+
onrequest?: (id: string) => void;
|
|
90
|
+
}): Promise<Client>;
|
|
79
91
|
/**
|
|
80
92
|
* Per-device `local`-namespace surface on a Client.
|
|
81
93
|
*/
|
|
@@ -213,10 +225,11 @@ declare function start(ipc: import('streamx').Duplex, spec: Spec, opts?: {
|
|
|
213
225
|
onerror?: (err: Error) => void;
|
|
214
226
|
push?: import('./push.js').Messaging;
|
|
215
227
|
}): Promise<Context>;
|
|
216
|
-
/** @type {typeof start & typeof remote & { connect: typeof connect, restore: typeof restore, t: typeof t, schema: typeof schema }} */
|
|
228
|
+
/** @type {typeof start & typeof remote & { connect: typeof connect, restore: typeof restore, link: typeof link, t: typeof t, schema: typeof schema }} */
|
|
217
229
|
export declare const cero: typeof start & typeof remote & {
|
|
218
230
|
connect: typeof connect;
|
|
219
231
|
restore: typeof restore;
|
|
232
|
+
link: typeof link;
|
|
220
233
|
t: typeof t;
|
|
221
234
|
schema: typeof schema;
|
|
222
235
|
};
|
package/types/rpc/server.d.ts
CHANGED
|
@@ -131,6 +131,11 @@ export declare class Server extends RPCServer {
|
|
|
131
131
|
* @private
|
|
132
132
|
*/
|
|
133
133
|
private _wireRestore;
|
|
134
|
+
/**
|
|
135
|
+
* Wire the `link` handler: a stream, the request's id as it is sent, the identity once linked.
|
|
136
|
+
* @private
|
|
137
|
+
*/
|
|
138
|
+
private _wireLink;
|
|
134
139
|
/**
|
|
135
140
|
* Register the row-level RPC handlers (put/set/get/del/watch/call).
|
|
136
141
|
* @private
|