@cero-base/cero 2.4.0 → 2.4.1

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 (2) hide show
  1. package/package.json +2 -2
  2. package/src/build/index.js +31 -14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cero-base/cero",
3
- "version": "2.4.0",
3
+ "version": "2.4.1",
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,
@@ -99,7 +99,7 @@
99
99
  "test:node": "find test -name '*.test.js' | sort | xargs -P4 -n1 brittle-node"
100
100
  },
101
101
  "dependencies": {
102
- "@cero-base/core": "^2.4.0",
102
+ "@cero-base/core": "^2.4.1",
103
103
  "b4a": "^1.9.0",
104
104
  "bare-abort-controller": "^1.1.2",
105
105
  "bare-crypto": "^1.15.4",
@@ -2,6 +2,8 @@ import { join, resolve } from 'path'
2
2
  import { promises as fs } from 'fs'
3
3
  import { pathToFileURL } from 'url'
4
4
 
5
+ import b4a from 'b4a'
6
+ import crypto from 'hypercore-crypto'
5
7
  import Hyperschema from 'hyperschema'
6
8
  import HyperdbBuilder from 'hyperdb/builder'
7
9
  import Hyperdispatch from 'hyperdispatch'
@@ -71,12 +73,14 @@ export async function build(specDir, schema, { ns = NS, extensions } = {}) {
71
73
  emitMain(join(specDir, 'handles', name), ns, handle, { rpc: false, extend })
72
74
  }
73
75
 
76
+ const dirs = ['main', ...Object.keys(handles).map((n) => join('handles', n))]
77
+ const version = await contract(specDir, dirs)
74
78
  const meta = {
75
79
  ...main.meta,
76
- version: await contractVersion(join(specDir, 'main')),
80
+ version,
77
81
  local: local.meta,
78
82
  handles: Object.fromEntries(
79
- Object.entries(handles).map(([n, h]) => [n, { ...h.meta, type: n }])
83
+ Object.entries(handles).map(([n, h]) => [n, { ...h.meta, type: n, version }])
80
84
  )
81
85
  }
82
86
 
@@ -231,18 +235,31 @@ function register(name, node, ctx) {
231
235
  }
232
236
  }
233
237
 
234
- // the contract version is the highest of the schema, db and dispatch versions
235
- async function contractVersion(mainDir) {
236
- const read = async (rel) => {
237
- const j = JSON.parse(await fs.readFile(join(mainDir, rel), 'utf-8'))
238
- return j.version || 1
239
- }
240
- const versions = await Promise.all([
241
- read('schema/schema.json'),
242
- read('db/db.json'),
243
- read('dispatch/dispatch.json')
244
- ])
245
- return Math.max(...versions)
238
+ // one contract for the app, kept in contract.json: it rises once per build that changes what is
239
+ // stored, at the root or in any handle type, never for RPC alone. A spec without one starts from
240
+ // its builders' versions, so a shipped app's number never goes down
241
+ async function contract(specDir, dirs) {
242
+ const file = join(specDir, 'contract.json')
243
+ const shapes = await Promise.all(dirs.map((dir) => shapeOf(join(specDir, dir))))
244
+ const json = JSON.stringify(shapes.map((s) => s.stored))
245
+ const shape = b4a.toString(crypto.hash(b4a.from(json)), 'hex')
246
+ const kept = await fs.readFile(file, 'utf-8').then(JSON.parse, () => null)
247
+ const version = kept
248
+ ? kept.version + (kept.shape === shape ? 0 : 1)
249
+ : Math.max(...shapes.flatMap((s) => s.versions))
250
+ await fs.writeFile(file, JSON.stringify({ version, shape }, null, 2) + '\n')
251
+ return version
252
+ }
253
+
254
+ // what a spec directory stores: its types without the RPC ones, its collections and its routes
255
+ async function shapeOf(dir) {
256
+ const read = async (rel) => JSON.parse(await fs.readFile(join(dir, rel), 'utf-8'))
257
+ const rpc = new Set(internal.types('rpc').map((type) => type.name))
258
+ const { version: types, ...schema } = await read('schema/schema.json')
259
+ const { version: db, ...collections } = await read('db/db.json')
260
+ const { version: dispatch, ...routes } = await read('dispatch/dispatch.json')
261
+ schema.schema = schema.schema.filter((type) => !rpc.has(type.name))
262
+ return { stored: [schema, collections, routes], versions: [types || 1, db || 1, dispatch || 1] }
246
263
  }
247
264
 
248
265
  function emitMain(dir, ns, { types, collections, dispatches, indexes = [] }, { rpc, extend = {} }) {