@cero-base/cero 2.2.0 → 2.3.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 +24 -16
- package/package.json +7 -6
- package/src/build/index.js +38 -34
- package/src/build/internal.js +96 -11
- package/src/build/schemas.js +119 -19
- package/src/extensions/handle-sync.js +8 -12
- package/src/extensions/index.js +17 -63
- package/src/extensions/mirror.js +44 -0
- package/src/extensions/profile-sync.js +10 -12
- package/src/handle/index.js +499 -374
- package/src/handle/join.js +144 -0
- package/src/index.js +89 -118
- package/src/lib/bluetooth.js +83 -19
- package/src/lib/constants.js +1 -0
- package/src/lib/operators.js +298 -128
- package/src/lib/peek.js +1 -1
- package/src/lib/refs.js +7 -7
- package/src/lib/spec.js +32 -0
- package/src/local/index.js +27 -3
- package/src/rpc/client.js +240 -202
- package/src/rpc/server.js +186 -133
- package/types/build/index.d.ts +3 -8
- package/types/build/internal.d.ts +86 -27
- package/types/build/schemas.d.ts +91 -12
- package/types/extensions/handle-sync.d.ts +4 -12
- package/types/extensions/index.d.ts +59 -59
- package/types/extensions/mirror.d.ts +13 -0
- package/types/extensions/profile-sync.d.ts +4 -13
- package/types/handle/index.d.ts +229 -198
- package/types/handle/join.d.ts +63 -0
- package/types/index.d.ts +27 -55
- package/types/lib/bluetooth.d.ts +57 -19
- package/types/lib/constants.d.ts +1 -0
- package/types/lib/operators.d.ts +166 -81
- package/types/lib/peek.d.ts +2 -2
- package/types/lib/refs.d.ts +14 -15
- package/types/lib/spec.d.ts +87 -0
- package/types/local/index.d.ts +19 -9
- package/types/rpc/client.d.ts +55 -112
- package/types/rpc/server.d.ts +91 -50
- package/src/rpc/index.js +0 -8
- package/types/rpc/index.d.ts +0 -7
package/src/local/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import ReadyResource from 'ready-resource'
|
|
2
|
+
import b4a from 'b4a'
|
|
2
3
|
|
|
3
4
|
import { Storage } from '@cero-base/core/storage'
|
|
4
5
|
import { CeroError } from '@cero-base/core/errors'
|
|
@@ -7,8 +8,8 @@ import { Ref } from '../lib/refs.js'
|
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* @typedef {object} LocalOpts
|
|
10
|
-
* @property {
|
|
11
|
-
* @property {
|
|
11
|
+
* @property {import('hypercore-storage')} [root] Pre-existing HypercoreStorage to reuse.
|
|
12
|
+
* @property {import('corestore')} [store] Pre-existing Corestore to reuse.
|
|
12
13
|
* @property {Uint8Array} [storageKey] 32-byte key encrypting the local store at rest.
|
|
13
14
|
*/
|
|
14
15
|
|
|
@@ -19,7 +20,7 @@ import { Ref } from '../lib/refs.js'
|
|
|
19
20
|
export class Local extends ReadyResource {
|
|
20
21
|
/**
|
|
21
22
|
* @param {string | null} dir Directory for the local store, or `null` when reusing an external `store`.
|
|
22
|
-
* @param {
|
|
23
|
+
* @param {import('../lib/spec.js').Spec} spec Built cero spec, with `spec.local.database` and `spec.meta.local`.
|
|
23
24
|
* @param {LocalOpts} [opts]
|
|
24
25
|
*/
|
|
25
26
|
constructor(dir, spec, { root, store, storageKey } = {}) {
|
|
@@ -40,12 +41,35 @@ export class Local extends ReadyResource {
|
|
|
40
41
|
})
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
/** @private */
|
|
43
45
|
async _open() {
|
|
44
46
|
await this.store.ready()
|
|
45
47
|
Ref.attach(this, this.store.refs)
|
|
46
48
|
}
|
|
47
49
|
|
|
50
|
+
/** @private */
|
|
48
51
|
async _close() {
|
|
49
52
|
await this.store.close()
|
|
50
53
|
}
|
|
51
54
|
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* One of the mailbox's boxes, kept in the local store so mail survives a restart.
|
|
58
|
+
*
|
|
59
|
+
* @param {import('@cero-base/core/storage').Storage} store
|
|
60
|
+
* @param {'inbox' | 'outbox'} name
|
|
61
|
+
* @returns {import('@cero-base/core/mailbox').Box}
|
|
62
|
+
*/
|
|
63
|
+
export function box(store, name) {
|
|
64
|
+
return {
|
|
65
|
+
list: async () => (await store.get(name)).data.map(unpack),
|
|
66
|
+
put: (mail) => store.put(name, { ...mail, mirrors: b4a.concat(mail.mirrors || []) }),
|
|
67
|
+
del: (id) => store.del(name, id)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function unpack({ id, address, message, mirrors }) {
|
|
72
|
+
const keys = []
|
|
73
|
+
for (let i = 0; i < (mirrors?.byteLength || 0); i += 32) keys.push(mirrors.subarray(i, i + 32))
|
|
74
|
+
return { id, address, message, mirrors: keys }
|
|
75
|
+
}
|