@cero-base/cero 2.11.0 → 3.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.
- package/package.json +6 -5
- package/src/build/index.js +296 -57
- package/src/build/internal.js +80 -5
- package/src/build/schemas.js +118 -25
- package/src/extensions/mirror.js +2 -1
- package/src/extensions/profile-sync.js +17 -1
- package/src/handle/index.js +1222 -189
- package/src/index.js +68 -35
- package/src/lib/constants.js +4 -4
- package/src/lib/operators.js +66 -14
- package/src/lib/peek.js +14 -8
- package/src/lib/reserved.js +61 -0
- package/src/lib/spec.js +7 -1
- package/src/lib/swap.js +59 -6
- package/src/local/index.js +6 -2
- package/src/push/index.js +26 -13
- package/src/rpc/client.js +30 -10
- package/src/rpc/push.js +9 -3
- package/src/rpc/server.js +134 -51
- package/types/build/internal.d.ts +57 -0
- package/types/build/schemas.d.ts +58 -5
- package/types/extensions/index.d.ts +3 -0
- package/types/extensions/profile-sync.d.ts +1 -1
- package/types/handle/index.d.ts +123 -15
- package/types/index.d.ts +9 -6
- package/types/lib/constants.d.ts +2 -2
- package/types/lib/operators.d.ts +26 -6
- package/types/lib/peek.d.ts +7 -4
- package/types/lib/reserved.d.ts +1 -0
- package/types/lib/spec.d.ts +35 -2
- package/types/lib/swap.d.ts +28 -3
- package/types/push/index.d.ts +4 -12
- package/types/rpc/client.d.ts +35 -18
- package/types/rpc/server.d.ts +24 -3
- package/src/handle/join.js +0 -139
- package/types/handle/join.d.ts +0 -63
package/src/lib/swap.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import fs from 'fs'
|
|
2
|
+
import FDLock from 'fd-lock'
|
|
3
|
+
|
|
4
|
+
import { CeroError } from '@cero-base/core/errors'
|
|
2
5
|
|
|
3
6
|
// Another identity comes in beside `main`, in `main.next`, and takes its place only once it has opened: the new store
|
|
4
7
|
// is flushed to disk, then main moves to `main.old` and `main.next/main` to `main`, each rename atomic. A launch reads
|
|
5
|
-
// from what is on disk how far a killed swap got
|
|
8
|
+
// from what is on disk how far a killed swap got. The swap and every open of `dir` take turns on `main.lock`
|
|
6
9
|
const NEXT = 'main.next'
|
|
7
10
|
const OLD = 'main.old'
|
|
11
|
+
const LOCK = 'main.lock'
|
|
12
|
+
const RETRY = 20
|
|
8
13
|
|
|
9
14
|
// the dirs a swap is running in, in this process: their `main.next` is in use
|
|
10
15
|
const running = new Set()
|
|
@@ -32,8 +37,7 @@ export async function current(dir) {
|
|
|
32
37
|
|
|
33
38
|
/**
|
|
34
39
|
* A swap killed part way, finished or undone before anything opens `dir`: past its first rename the incoming
|
|
35
|
-
* identity had opened, so it takes main's place; without it the old one comes back.
|
|
36
|
-
* in `main.next` goes, unless a swap is running there.
|
|
40
|
+
* identity had opened, so it takes main's place; without it the old one comes back.
|
|
37
41
|
*
|
|
38
42
|
* @param {string} dir
|
|
39
43
|
*/
|
|
@@ -44,6 +48,41 @@ export async function settle(dir) {
|
|
|
44
48
|
if (!(await exists(main))) await fs.promises.rename(await current(dir), main)
|
|
45
49
|
await rm(old)
|
|
46
50
|
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The lock on `dir`, a file beside the stores, held from before a store closes or opens until the one in `main` is
|
|
55
|
+
* open: a store's own lock goes with its close and moves with its directory. Another holder is waited for, a try
|
|
56
|
+
* every few ms; a kill frees it.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} dir
|
|
59
|
+
* @param {number} [timeout] The longest wait in ms, 0 for no limit.
|
|
60
|
+
* @returns {Promise<{ close: () => Promise<void> }>} Held until closed.
|
|
61
|
+
*/
|
|
62
|
+
export async function lock(dir, timeout = 0) {
|
|
63
|
+
await fs.promises.mkdir(dir, { recursive: true })
|
|
64
|
+
const deadline = timeout ? Date.now() + timeout : Infinity
|
|
65
|
+
while (true) {
|
|
66
|
+
const held = new FDLock(await open(`${dir}/${LOCK}`))
|
|
67
|
+
try {
|
|
68
|
+
await held.ready()
|
|
69
|
+
return held
|
|
70
|
+
} catch (err) {
|
|
71
|
+
// a file that cannot be locked at all carries the system's code, one that is held does not
|
|
72
|
+
if (err.code) throw err
|
|
73
|
+
if (Date.now() >= deadline) throw CeroError.TIMEOUT(`The lock on ${dir}`)
|
|
74
|
+
await new Promise((resolve) => setTimeout(resolve, RETRY))
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* What a killed swap left in `main.next` goes, unless a swap is running there. Only once `main` is open: a swap in
|
|
81
|
+
* another process keeps main open while the incoming identity opens.
|
|
82
|
+
*
|
|
83
|
+
* @param {string} dir
|
|
84
|
+
*/
|
|
85
|
+
export async function sweep(dir) {
|
|
47
86
|
if (!running.has(dir)) await rm(nextOf(dir))
|
|
48
87
|
}
|
|
49
88
|
|
|
@@ -69,13 +108,22 @@ export async function staging(dir, fn) {
|
|
|
69
108
|
}
|
|
70
109
|
|
|
71
110
|
/**
|
|
72
|
-
* The identity opened and closed in `main.next
|
|
111
|
+
* The identity opened and closed in `main.next`, all of it on disk: a rename that follows never points at data
|
|
112
|
+
* still in a cache.
|
|
113
|
+
*
|
|
114
|
+
* @param {string} dir
|
|
115
|
+
*/
|
|
116
|
+
export async function flush(dir) {
|
|
117
|
+
await durable(`${nextOf(dir)}/main`)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The identity opened, closed and flushed in `main.next` takes main's place. Both must be closed.
|
|
73
122
|
*
|
|
74
123
|
* @param {string} dir
|
|
75
124
|
*/
|
|
76
125
|
export async function commit(dir) {
|
|
77
126
|
const incoming = `${nextOf(dir)}/main`
|
|
78
|
-
await durable(incoming)
|
|
79
127
|
await fs.promises.rename(`${dir}/main`, `${dir}/${OLD}`)
|
|
80
128
|
await fs.promises.rename(incoming, `${dir}/main`)
|
|
81
129
|
await sync(dir, true)
|
|
@@ -83,7 +131,6 @@ export async function commit(dir) {
|
|
|
83
131
|
await rm(nextOf(dir))
|
|
84
132
|
}
|
|
85
133
|
|
|
86
|
-
// every file and directory under `path` on disk, so a rename that follows never points at data still in a cache
|
|
87
134
|
async function durable(path) {
|
|
88
135
|
for (const entry of await fs.promises.readdir(path, { withFileTypes: true })) {
|
|
89
136
|
const at = `${path}/${entry.name}`
|
|
@@ -109,6 +156,12 @@ async function sync(path, dir) {
|
|
|
109
156
|
}
|
|
110
157
|
}
|
|
111
158
|
|
|
159
|
+
function open(path) {
|
|
160
|
+
return new Promise((resolve, reject) => {
|
|
161
|
+
fs.open(path, 'a', (err, fd) => (err ? reject(err) : resolve(fd)))
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
112
165
|
function exists(path) {
|
|
113
166
|
return fs.promises.stat(path).then(
|
|
114
167
|
() => true,
|
package/src/local/index.js
CHANGED
|
@@ -79,6 +79,10 @@ export class Local extends ReadyResource {
|
|
|
79
79
|
export function box(store, name) {
|
|
80
80
|
return {
|
|
81
81
|
list: async () => (await store.get(name)).data.map(unpack),
|
|
82
|
+
get: async (id) => {
|
|
83
|
+
const { data } = await store.get(name, id)
|
|
84
|
+
return data && unpack(data)
|
|
85
|
+
},
|
|
82
86
|
put: (mail) => store.put(name, pack(mail)),
|
|
83
87
|
del: (id) => store.del(name, id)
|
|
84
88
|
}
|
|
@@ -89,8 +93,8 @@ function pack({ mirrors = [], keep = true, ...mail }) {
|
|
|
89
93
|
return { ...mail, mirrors: b4a.concat(mirrors), erase: !keep }
|
|
90
94
|
}
|
|
91
95
|
|
|
92
|
-
function unpack({ id, address, message, mirrors, name, erase, fetched }) {
|
|
96
|
+
function unpack({ id, address, message, mirrors, name, erase, fetched, handled, sealed }) {
|
|
93
97
|
const keys = []
|
|
94
98
|
for (let i = 0; i < (mirrors?.byteLength || 0); i += 32) keys.push(mirrors.subarray(i, i + 32))
|
|
95
|
-
return { id, address, message, mirrors: keys, name, keep: !erase, fetched }
|
|
99
|
+
return { id, address, message, mirrors: keys, name, keep: !erase, fetched, handled, sealed }
|
|
96
100
|
}
|
package/src/push/index.js
CHANGED
|
@@ -4,32 +4,42 @@ import b4a from 'b4a'
|
|
|
4
4
|
import safetyCatch from 'safety-catch'
|
|
5
5
|
|
|
6
6
|
import c from 'compact-encoding'
|
|
7
|
-
import { verify, decrypt } from '@cero-base/core/push'
|
|
7
|
+
import { verify, decrypt, topic } from '@cero-base/core/push'
|
|
8
8
|
import { CeroError } from '@cero-base/core/errors'
|
|
9
9
|
import { epochEntries } from '@cero-base/core/database/encryption'
|
|
10
10
|
|
|
11
11
|
import { Local } from '../local/index.js'
|
|
12
|
-
import { current } from '../lib/swap.js'
|
|
12
|
+
import { current, lock } from '../lib/swap.js'
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* A push verified against the
|
|
16
|
-
* rode along and this device holds the handle's keys.
|
|
15
|
+
* A push verified against the handle its topic names, read from that handle's `topics` row alone,
|
|
16
|
+
* with the row it wrote when the block rode along and this device holds the handle's keys.
|
|
17
17
|
*
|
|
18
18
|
* @param {import('hypercore-storage')} storage
|
|
19
19
|
* @param {import('../lib/spec.js').Spec} spec
|
|
20
20
|
* @param {Uint8Array} payload
|
|
21
|
-
* @param {
|
|
21
|
+
* @param {import('@cero-base/core/storage').Storage} store The root's local store, which keeps the `topics` rows.
|
|
22
22
|
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null, row: object | null } | null>}
|
|
23
23
|
*/
|
|
24
|
-
export async function read(storage, spec, payload,
|
|
25
|
-
const
|
|
26
|
-
const
|
|
24
|
+
export async function read(storage, spec, payload, store) {
|
|
25
|
+
const id = topic(payload)
|
|
26
|
+
const keys = id && (await store.get('topics', id)).data
|
|
27
|
+
if (!keys) return null
|
|
28
|
+
const { handle, key, type, name, writers } = keys
|
|
29
|
+
const hit = await verify(storage, payload, [
|
|
30
|
+
{ id: handle, key, type, name, writers: keysOf(writers) }
|
|
31
|
+
])
|
|
27
32
|
if (!hit) return null
|
|
28
|
-
const keys = rows.find((r) => r.handle === hit.id)
|
|
29
33
|
const row = await rowOf(storage, spec, hit, keys)
|
|
30
34
|
return { id: hit.id, type: hit.type, name: hit.name, newer: hit.newer, ref: hit.ref, row }
|
|
31
35
|
}
|
|
32
36
|
|
|
37
|
+
function keysOf(bytes) {
|
|
38
|
+
const out = []
|
|
39
|
+
for (let i = 0; i < (bytes?.byteLength || 0); i += 32) out.push(bytes.subarray(i, i + 32))
|
|
40
|
+
return out
|
|
41
|
+
}
|
|
42
|
+
|
|
33
43
|
// the row a push's block holds: null when none rode along, or it does not decode with what we hold
|
|
34
44
|
async function rowOf(storage, spec, hit, { encryptionKey, epochs }) {
|
|
35
45
|
if (!hit.block || !encryptionKey) return null
|
|
@@ -57,23 +67,26 @@ async function rowOf(storage, spec, hit, { encryptionKey, epochs }) {
|
|
|
57
67
|
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null, row: object | null } | null>}
|
|
58
68
|
*/
|
|
59
69
|
export async function receive(dir, spec, payload, { storageKey } = {}) {
|
|
60
|
-
|
|
70
|
+
let held = null
|
|
71
|
+
let storage = null
|
|
61
72
|
let store = null
|
|
62
73
|
let local = null
|
|
63
74
|
try {
|
|
75
|
+
held = await lock(dir)
|
|
76
|
+
storage = new HypercoreStorage(await current(dir), { readOnly: true })
|
|
64
77
|
await storage.ready()
|
|
65
78
|
store = new Corestore(storage, { manifestVersion: 2 })
|
|
66
79
|
local = new Local(null, spec, { store, storageKey })
|
|
67
80
|
await local.ready()
|
|
68
|
-
|
|
69
|
-
return await read(storage, spec, payload, rows)
|
|
81
|
+
return await read(storage, spec, payload, local.store)
|
|
70
82
|
} catch (err) {
|
|
71
83
|
safetyCatch(err)
|
|
72
84
|
return null
|
|
73
85
|
} finally {
|
|
74
86
|
await local?.close().catch(safetyCatch)
|
|
75
87
|
await store?.close().catch(safetyCatch)
|
|
76
|
-
await storage
|
|
88
|
+
await storage?.close().catch(safetyCatch)
|
|
89
|
+
await held?.close().catch(safetyCatch)
|
|
77
90
|
}
|
|
78
91
|
}
|
|
79
92
|
|
package/src/rpc/client.js
CHANGED
|
@@ -5,7 +5,7 @@ import { CeroError } from '@cero-base/core/errors'
|
|
|
5
5
|
import { checkFields, checkRequired } from '@cero-base/core/utils'
|
|
6
6
|
import c from 'compact-encoding'
|
|
7
7
|
import z32 from 'z32'
|
|
8
|
-
import { decodeId } from '@cero-base/core/blobs/codec'
|
|
8
|
+
import { decodeId, servedType } from '@cero-base/core/blobs/codec'
|
|
9
9
|
|
|
10
10
|
import { Ref } from '../lib/refs.js'
|
|
11
11
|
import { follow } from './push.js'
|
|
@@ -22,6 +22,7 @@ export const {
|
|
|
22
22
|
del,
|
|
23
23
|
watch,
|
|
24
24
|
call,
|
|
25
|
+
supports,
|
|
25
26
|
open,
|
|
26
27
|
invite,
|
|
27
28
|
revoke,
|
|
@@ -29,6 +30,7 @@ export const {
|
|
|
29
30
|
accept,
|
|
30
31
|
deny,
|
|
31
32
|
leave,
|
|
33
|
+
removed,
|
|
32
34
|
close,
|
|
33
35
|
cancel,
|
|
34
36
|
suspend,
|
|
@@ -55,6 +57,13 @@ export { t, schema }
|
|
|
55
57
|
* @property {string|null} name
|
|
56
58
|
*
|
|
57
59
|
* @typedef {(Client | Handle) & Record<string, Ref>} Context A UI's root or handle: the verbs take it like an in-process one.
|
|
60
|
+
*
|
|
61
|
+
* @typedef {object} ClientOpts
|
|
62
|
+
* @property {(err: Error) => void} [onerror] Where the worker's background errors go, the console without one.
|
|
63
|
+
* @property {import('./push.js').Messaging} [push] The platform's messaging service, to follow the handles' push topics.
|
|
64
|
+
* @property {string} [phrase] On a worker whose store is new, it recovers the identity of this phrase, as `cero(dir, spec, { phrase })`.
|
|
65
|
+
* @property {string} [invite] On a worker whose store is new, it links to the identity of this device invite, as `cero(dir, spec, { invite })`.
|
|
66
|
+
* @property {(id: string) => void} [onrequest] With `invite`: the request's id as it is sent.
|
|
58
67
|
*/
|
|
59
68
|
|
|
60
69
|
const blobIdEnc = {
|
|
@@ -113,11 +122,11 @@ const operators = {
|
|
|
113
122
|
const base = root._fileBase || ''
|
|
114
123
|
const token = root._fileToken || ''
|
|
115
124
|
const { coreKey, blobId, type } = decodeId(id)
|
|
125
|
+
const served = servedType(type)
|
|
116
126
|
const key = z32.encode(coreKey)
|
|
117
127
|
const blob = z32.encode(c.encode(blobIdEnc, blobId))
|
|
118
|
-
const tp = type ? `&type=${encodeURIComponent(type)}` : ''
|
|
119
128
|
const tok = token ? `&token=${token}` : ''
|
|
120
|
-
return `${base}/?key=${key}&blob=${blob}
|
|
129
|
+
return `${base}/?key=${key}&blob=${blob}&type=${encodeURIComponent(served)}${tok}`
|
|
121
130
|
},
|
|
122
131
|
|
|
123
132
|
/**
|
|
@@ -365,6 +374,11 @@ const operators = {
|
|
|
365
374
|
await this.rpc.leave({ handle: this.id })
|
|
366
375
|
},
|
|
367
376
|
|
|
377
|
+
/** @private */
|
|
378
|
+
_removed(id) {
|
|
379
|
+
return this._verb('removed', id)
|
|
380
|
+
},
|
|
381
|
+
|
|
368
382
|
/** @private */
|
|
369
383
|
_cancel(invite) {
|
|
370
384
|
return this._verb('cancel', invite)
|
|
@@ -422,8 +436,8 @@ const operators = {
|
|
|
422
436
|
}
|
|
423
437
|
|
|
424
438
|
/**
|
|
425
|
-
*
|
|
426
|
-
*
|
|
439
|
+
* Switch the app to the identity of a recovery phrase, as `cero.restore` does in the worker: the
|
|
440
|
+
* client is then that identity.
|
|
427
441
|
*
|
|
428
442
|
* @param {Client} me
|
|
429
443
|
* @param {string} phrase
|
|
@@ -497,11 +511,13 @@ export class Client extends RPCClient {
|
|
|
497
511
|
/**
|
|
498
512
|
* @param {import('streamx').Duplex} ipc Framed IPC stream (must be writable).
|
|
499
513
|
* @param {Spec} spec Compiled cero spec (schema + rpc + handles).
|
|
500
|
-
* @param {
|
|
514
|
+
* @param {ClientOpts} [opts]
|
|
501
515
|
*/
|
|
502
|
-
constructor(ipc, spec, { onerror, push } = {}) {
|
|
516
|
+
constructor(ipc, spec, { onerror, push, phrase, invite, onrequest } = {}) {
|
|
503
517
|
super(ipc, spec)
|
|
504
518
|
/** @private */
|
|
519
|
+
this._how = { phrase, invite, onrequest }
|
|
520
|
+
/** @private */
|
|
505
521
|
this._push = push || null
|
|
506
522
|
if (spec.local?.schema && !spec.local.codec) bindCodec(spec.local)
|
|
507
523
|
Object.assign(this, operators)
|
|
@@ -539,7 +555,11 @@ export class Client extends RPCClient {
|
|
|
539
555
|
/** @private */
|
|
540
556
|
async _open() {
|
|
541
557
|
await super._open()
|
|
542
|
-
|
|
558
|
+
const { phrase, invite, onrequest } = this._how
|
|
559
|
+
// a worker on a new store opens as these say; on one with an identity they switch to another
|
|
560
|
+
if (phrase) await restore(this, phrase)
|
|
561
|
+
else if (invite) await link(this, invite, { onrequest })
|
|
562
|
+
else this._identify(await this.rpc.init({}))
|
|
543
563
|
Ref.attach(this, /** @type {Spec} */ (this.spec).meta.refs)
|
|
544
564
|
if (/** @type {Spec} */ (this.spec).meta.local?.refs) this.local = new LocalRefs(this)
|
|
545
565
|
this._pumpErrors()
|
|
@@ -646,7 +666,7 @@ class Handle {
|
|
|
646
666
|
*
|
|
647
667
|
* @param {import('streamx').Duplex} ipc
|
|
648
668
|
* @param {Spec} spec
|
|
649
|
-
* @param {
|
|
669
|
+
* @param {ClientOpts} [opts]
|
|
650
670
|
* @returns {Promise<Client>}
|
|
651
671
|
*/
|
|
652
672
|
export async function connect(ipc, spec, opts) {
|
|
@@ -661,7 +681,7 @@ export async function connect(ipc, spec, opts) {
|
|
|
661
681
|
*
|
|
662
682
|
* @param {import('streamx').Duplex} ipc Framed IPC duplex stream.
|
|
663
683
|
* @param {Spec} spec Built cero spec.
|
|
664
|
-
* @param {
|
|
684
|
+
* @param {ClientOpts} [opts]
|
|
665
685
|
* @returns {Promise<Context>}
|
|
666
686
|
*/
|
|
667
687
|
function start(ipc, spec, opts) {
|
package/src/rpc/push.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { watch, get, open, activate, receive } from '../lib/operators.js'
|
|
1
|
+
import { watch, get, open, close, activate, receive } from '../lib/operators.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @typedef {object} Messaging The platform's messaging service, as `connect` takes it for push.
|
|
@@ -48,10 +48,16 @@ export function follow(me, messaging) {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
// while the app sleeps the OS shows the push; the handle syncs first once it wakes on its own
|
|
51
|
+
// while the app sleeps the OS shows the push; the handle syncs first once it wakes on its own, for
|
|
52
|
+
// as long as it stays idle after this close
|
|
52
53
|
async function wake(me, payload) {
|
|
53
54
|
if ((await get(me.status)).data.suspended) return
|
|
54
55
|
const hit = await receive(me, payload)
|
|
55
56
|
if (!hit?.newer) return
|
|
56
|
-
|
|
57
|
+
const h = await open(me[hit.type], { id: hit.id })
|
|
58
|
+
try {
|
|
59
|
+
await activate(h)
|
|
60
|
+
} finally {
|
|
61
|
+
await close(h)
|
|
62
|
+
}
|
|
57
63
|
}
|