@cero-base/cero 2.2.1 → 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.
Files changed (42) hide show
  1. package/README.md +24 -16
  2. package/package.json +7 -6
  3. package/src/build/index.js +38 -34
  4. package/src/build/internal.js +96 -11
  5. package/src/build/schemas.js +119 -19
  6. package/src/extensions/handle-sync.js +8 -12
  7. package/src/extensions/index.js +17 -63
  8. package/src/extensions/mirror.js +44 -0
  9. package/src/extensions/profile-sync.js +10 -12
  10. package/src/handle/index.js +499 -374
  11. package/src/handle/join.js +144 -0
  12. package/src/index.js +89 -118
  13. package/src/lib/bluetooth.js +83 -19
  14. package/src/lib/constants.js +1 -0
  15. package/src/lib/operators.js +298 -128
  16. package/src/lib/peek.js +1 -1
  17. package/src/lib/refs.js +7 -7
  18. package/src/lib/spec.js +32 -0
  19. package/src/local/index.js +27 -3
  20. package/src/rpc/client.js +240 -202
  21. package/src/rpc/server.js +186 -133
  22. package/types/build/index.d.ts +3 -8
  23. package/types/build/internal.d.ts +86 -27
  24. package/types/build/schemas.d.ts +91 -12
  25. package/types/extensions/handle-sync.d.ts +4 -12
  26. package/types/extensions/index.d.ts +59 -59
  27. package/types/extensions/mirror.d.ts +13 -0
  28. package/types/extensions/profile-sync.d.ts +4 -13
  29. package/types/handle/index.d.ts +229 -198
  30. package/types/handle/join.d.ts +63 -0
  31. package/types/index.d.ts +27 -55
  32. package/types/lib/bluetooth.d.ts +57 -19
  33. package/types/lib/constants.d.ts +1 -0
  34. package/types/lib/operators.d.ts +166 -81
  35. package/types/lib/peek.d.ts +2 -2
  36. package/types/lib/refs.d.ts +14 -15
  37. package/types/lib/spec.d.ts +87 -0
  38. package/types/local/index.d.ts +19 -9
  39. package/types/rpc/client.d.ts +55 -112
  40. package/types/rpc/server.d.ts +91 -50
  41. package/src/rpc/index.js +0 -8
  42. package/types/rpc/index.d.ts +0 -7
@@ -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 {any} [root] Pre-existing HypercoreStorage to reuse.
11
- * @property {any} [store] Pre-existing Corestore to reuse.
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 {any} spec Built cero spec — must include `spec.local.database` and `spec.meta.local`.
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
+ }