@cero-base/core 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.
Files changed (39) hide show
  1. package/package.json +8 -8
  2. package/src/blobs/codec.js +25 -0
  3. package/src/blobs/index.js +5 -2
  4. package/src/blobs/server.js +2 -2
  5. package/src/database/dispatch.js +805 -208
  6. package/src/database/encryption.js +25 -21
  7. package/src/database/index.js +747 -124
  8. package/src/database/rotation.js +333 -38
  9. package/src/lib/constants.js +10 -0
  10. package/src/lib/errors.js +8 -0
  11. package/src/lib/schema.js +12 -2
  12. package/src/lib/spec/index.js +10 -5
  13. package/src/lib/spec/schema.json +13 -1
  14. package/src/lib/utils.js +24 -3
  15. package/src/mailbox/inbox.js +368 -41
  16. package/src/mailbox/index.js +75 -20
  17. package/src/mailbox/post.js +13 -8
  18. package/src/network/index.js +12 -5
  19. package/src/pairing/index.js +347 -57
  20. package/src/pairing/invite.js +36 -12
  21. package/src/pairing/response.js +78 -0
  22. package/src/push/index.js +20 -2
  23. package/types/blobs/codec.d.ts +13 -0
  24. package/types/blobs/index.d.ts +2 -0
  25. package/types/database/dispatch.d.ts +52 -7
  26. package/types/database/encryption.d.ts +13 -5
  27. package/types/database/index.d.ts +199 -19
  28. package/types/database/rotation.d.ts +85 -2
  29. package/types/lib/constants.d.ts +4 -0
  30. package/types/lib/errors.d.ts +6 -0
  31. package/types/lib/schema.d.ts +11 -0
  32. package/types/lib/utils.d.ts +2 -0
  33. package/types/mailbox/inbox.d.ts +78 -10
  34. package/types/mailbox/index.d.ts +42 -16
  35. package/types/mailbox/post.d.ts +7 -5
  36. package/types/pairing/index.d.ts +53 -8
  37. package/types/pairing/invite.d.ts +27 -8
  38. package/types/pairing/response.d.ts +50 -0
  39. package/types/push/index.d.ts +12 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cero-base/core",
3
- "version": "2.11.0",
3
+ "version": "3.0.0",
4
4
  "description": "cero p2p primitives — identity, storage, network, database, blobs, rpc, pairing.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -148,24 +148,24 @@
148
148
  "test": "npm run test:bare",
149
149
  "test:bare": "find test -name '*.test.js' ! -name network.test.js | sort | xargs -P4 -n1 brittle-bare && brittle-bare test/network/network.test.js",
150
150
  "pretest:bare": "npm run build:test",
151
- "test:node": "find test -name '*.test.js' | sort | xargs -P4 -n1 brittle-node"
151
+ "test:node": "find test -name '*.test.js' ! -name network.test.js | sort | xargs -P4 -n1 brittle-node && brittle-node test/network/network.test.js"
152
152
  },
153
153
  "dependencies": {
154
154
  "@hyperswarm/secret-stream": "^6.9.2",
155
- "autobee": "2.13.3",
155
+ "autobee": "2.13.4",
156
156
  "b4a": "^1.9.0",
157
157
  "bare-crypto": "^1.15.4",
158
158
  "bare-fs": "^4.8.4",
159
159
  "bare-path": "^3.1.2",
160
160
  "bip39-mnemonic": "^2.5.0",
161
- "blind-peering": "^2.13.1",
162
- "blind-push": "^1.2.0",
163
- "compact-encoding": "^3.5.2",
161
+ "blind-peering": "^2.14.0",
162
+ "blind-push": "^1.2.1",
163
+ "compact-encoding": "^3.6.0",
164
164
  "corestore": "^7.16.1",
165
165
  "framed-stream": "^1.0.1",
166
166
  "hrpc": "^4.3.1",
167
167
  "hyperblobs": "^2.13.0",
168
- "hypercore": "^11.40.0",
168
+ "hypercore": "^11.40.3",
169
169
  "hypercore-blob-server": "^1.15.1",
170
170
  "hypercore-crypto": "^3.7.0",
171
171
  "hypercore-id-encoding": "^1.3.0",
@@ -195,7 +195,7 @@
195
195
  "blind-peer": "^3.18.1",
196
196
  "blind-push-gateway": "^1.0.0",
197
197
  "brittle": "^4.5.0",
198
- "protomux": "^3.13.0",
198
+ "protomux": "^3.13.1",
199
199
  "protomux-rpc-router": "^1.3.0",
200
200
  "typescript": "^7.0.2",
201
201
  "which-runtime": "^1.4.0"
@@ -14,6 +14,30 @@ import { CeroError } from '../lib/errors.js'
14
14
 
15
15
  const BlobId = getEncoding('@cero/blob-id')
16
16
 
17
+ // a type is served as a header, where anything but printable ASCII breaks it
18
+ const PRINTABLE = /^[ -~]*$/
19
+
20
+ /**
21
+ * Refuse a type a new file id cannot carry: one that is not printable text.
22
+ *
23
+ * @param {unknown} type
24
+ */
25
+ export function checkType(type) {
26
+ if (typeof type !== 'string' || !PRINTABLE.test(type)) {
27
+ throw CeroError.INVALID('a file type is printable text')
28
+ }
29
+ }
30
+
31
+ /**
32
+ * The type to serve a file with: one an earlier build stored that a header cannot carry is left out.
33
+ *
34
+ * @param {string} type
35
+ * @returns {string}
36
+ */
37
+ export function servedType(type) {
38
+ return PRINTABLE.test(type) ? type : ''
39
+ }
40
+
17
41
  /**
18
42
  * Encode a coreKey + raw hyperblobs blobId + type into a durable string id.
19
43
  *
@@ -23,6 +47,7 @@ const BlobId = getEncoding('@cero/blob-id')
23
47
  * @returns {string}
24
48
  */
25
49
  export function encodeId(coreKey, blobId, type) {
50
+ checkType(type)
26
51
  return z32.encode(c.encode(BlobId, { coreKey, ...blobId, type }))
27
52
  }
28
53
 
@@ -39,6 +39,8 @@ export class Blobs extends ReadyResource {
39
39
 
40
40
  /** @private */
41
41
  this._coreKey = key || null
42
+ /** @private */
43
+ this._ns = null
42
44
  /** @type {import('hypercore') | null} */
43
45
  this.core = null
44
46
  /** @type {import('hyperblobs') | null} */
@@ -81,8 +83,8 @@ export class Blobs extends ReadyResource {
81
83
  if (this._coreKey) opts.key = this._coreKey
82
84
  else opts.name = this.name
83
85
 
84
- const ns = this.store.namespace(NS)
85
- this.core = ns.get(opts)
86
+ this._ns = this.store.namespace(NS)
87
+ this.core = this._ns.get(opts)
86
88
  await this.core.ready()
87
89
 
88
90
  this.hyperblobs = new Hyperblobs(this.core)
@@ -96,6 +98,7 @@ export class Blobs extends ReadyResource {
96
98
  async _close() {
97
99
  if (this.hyperblobs) await this.hyperblobs.close()
98
100
  if (this.core) await this.core.close()
101
+ if (this._ns) await this._ns.close()
99
102
  }
100
103
 
101
104
  /**
@@ -1,5 +1,5 @@
1
1
  import BlobServer from 'hypercore-blob-server'
2
- import { decodeId } from './index.js'
2
+ import { decodeId, servedType } from './codec.js'
3
3
 
4
4
  /**
5
5
  * @typedef {{ key: Buffer, encryptionKey: Buffer | null }} Resolved
@@ -39,7 +39,7 @@ export class FileServer {
39
39
  */
40
40
  getLink(id) {
41
41
  const { coreKey, blobId, type } = decodeId(id)
42
- return this.server.getLink(coreKey, { blob: blobId, type })
42
+ return this.server.getLink(coreKey, { blob: blobId, type: servedType(type) })
43
43
  }
44
44
 
45
45
  async close() {