@cero-base/cero 2.2.1 → 2.3.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.
- 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 +91 -119
- package/src/lib/bluetooth.js +83 -19
- package/src/lib/constants.js +1 -0
- package/src/lib/operators.js +296 -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 +247 -205
- 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 +36 -53
- 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 +110 -93
- package/types/rpc/server.d.ts +91 -50
- package/src/rpc/index.js +0 -8
- package/types/rpc/index.d.ts +0 -7
package/README.md
CHANGED
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
> [!IMPORTANT]
|
|
4
4
|
> This project is experimental. The API is subject to change and may break at any time.
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
Describe your data. Cero stores it on the device, syncs it across your devices and shares it with the people you invite. No server.
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
Cero is the SDK apps use, one of the two Cero Base packages, on the [Pear](https://pears.com) stack by [Holepunch](https://holepunch.to). The other, Core ([`@cero-base/core`](https://www.npmjs.com/package/@cero-base/core)), holds the primitives Cero is built from.
|
|
9
9
|
|
|
10
10
|
```sh
|
|
11
11
|
npm install @cero-base/cero
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
A
|
|
14
|
+
A Cero app is three files.
|
|
15
15
|
|
|
16
16
|
```js
|
|
17
17
|
// schema.js
|
|
@@ -40,36 +40,44 @@ const me = await cero('./data', spec)
|
|
|
40
40
|
await cero.put(me.notes, { title: 'first', body: 'hello' })
|
|
41
41
|
|
|
42
42
|
const room = await cero.open(me.room, { name: 'general' })
|
|
43
|
-
console.log(await
|
|
43
|
+
console.log(await cero.invite(room)) // give this to a friend
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
Your friend, on their own machine:
|
|
46
|
+
Your friend, on their own machine, with the same three files:
|
|
47
47
|
|
|
48
48
|
```js
|
|
49
|
+
// index.js
|
|
50
|
+
import { cero } from '@cero-base/cero'
|
|
51
|
+
import { spec } from './spec/index.js'
|
|
52
|
+
|
|
53
|
+
const me = await cero('./data', spec)
|
|
54
|
+
const invite = '…' // the string you gave them
|
|
49
55
|
const room = await cero.open(me.room, { invite })
|
|
50
56
|
for await (const { data } of cero.watch(room.messages)) console.log(data)
|
|
51
57
|
```
|
|
52
58
|
|
|
53
|
-
That is a working, encrypted, offline-first, peer-to-peer app. Your phrase, `
|
|
59
|
+
That is a working, encrypted, offline-first, peer-to-peer app. Your phrase, `await cero.phrase(me)`, opens the same identity on any other device.
|
|
54
60
|
|
|
55
61
|
## Docs
|
|
56
62
|
|
|
57
63
|
- [Quickstart](https://github.com/lekinox/cero-base/blob/main/docs/quickstart.md)
|
|
58
|
-
- [Guides](https://github.com/lekinox/cero-base/blob/main/docs/README.md
|
|
64
|
+
- [Guides](https://github.com/lekinox/cero-base/blob/main/docs/README.md): schema, data, sharing, your devices, network, apps, extensions, how it works
|
|
59
65
|
- [API reference](https://github.com/lekinox/cero-base/blob/main/docs/api.md)
|
|
60
|
-
- [
|
|
66
|
+
- [Errors](https://github.com/lekinox/cero-base/blob/main/docs/errors.md)
|
|
61
67
|
|
|
62
68
|
## Exports
|
|
63
69
|
|
|
64
|
-
| Subpath | What it gives you
|
|
65
|
-
| ---------------------------- |
|
|
66
|
-
| `@cero-base/cero` | `cero`, `t`, `schema`, `restore`, `peek`, `Handle`, `Ref`, `Local`
|
|
67
|
-
| `@cero-base/cero/build` | `build(specDir, schema, opts)`
|
|
68
|
-
| `@cero-base/cero/server` | `serve(ipc, spec, opts)` and `Server`, the
|
|
69
|
-
| `@cero-base/cero/client` | `
|
|
70
|
-
| `@cero-base/cero/extensions` | `profileSync`, `handleSync`
|
|
70
|
+
| Subpath | What it gives you |
|
|
71
|
+
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
72
|
+
| `@cero-base/cero` | `cero`, `t`, `schema`, `restore`, `toSeed`, `peek`, the 24 operators, `Handle`, `Ref`, `Local` |
|
|
73
|
+
| `@cero-base/cero/build` | `build(specDir, schema, opts)` |
|
|
74
|
+
| `@cero-base/cero/server` | `serve(ipc, spec, opts)` and `Server`, the worker that owns the data |
|
|
75
|
+
| `@cero-base/cero/client` | `cero(ipc, spec)` (also `connect`), `Client`, `restore`, `t`, `schema` and every operator but `before`, `after` and `tx`, for a UI process |
|
|
76
|
+
| `@cero-base/cero/extensions` | `profileSync`, `handleSync`, `bundled`, `extensionsOf`, and a light `cero` with `t`, `schema` and the operators, for extension modules |
|
|
77
|
+
|
|
78
|
+
The 24 operators, each also on the facade as `cero.put` and so on: `put`, `set`, `get`, `del`, `watch`, `call`, `open`, `tx`, `invite`, `revoke`, `rotate`, `accept`, `deny`, `leave`, `close`, `cancel`, `suspend`, `resume`, `activate`, `deactivate`, `phrase`, `nearby`, `before`, `after`.
|
|
71
79
|
|
|
72
|
-
Built on the [Pear](https://pears.com) stack
|
|
80
|
+
Built on the [Pear](https://pears.com) stack: Hypercore, Autobee, HyperDB, Hyperblobs, Hyperswarm, blind-peering and Bare. Runs on Node and Bare, with TypeScript declarations.
|
|
73
81
|
|
|
74
82
|
## License
|
|
75
83
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/cero",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.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,17 +99,17 @@
|
|
|
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.
|
|
102
|
+
"@cero-base/core": "^2.3.1",
|
|
103
103
|
"b4a": "^1.9.0",
|
|
104
104
|
"bare-abort-controller": "^1.1.2",
|
|
105
105
|
"bare-crypto": "^1.15.3",
|
|
106
106
|
"bare-fs": "^4.8.1",
|
|
107
107
|
"bare-path": "^3.1.2",
|
|
108
|
-
"ble-swarm": "^2.3.
|
|
109
|
-
"compact-encoding": "^3.5.
|
|
110
|
-
"corestore": "^7.12.
|
|
108
|
+
"ble-swarm": "^2.3.2",
|
|
109
|
+
"compact-encoding": "^3.5.2",
|
|
110
|
+
"corestore": "^7.12.6",
|
|
111
111
|
"hrpc": "^4.3.1",
|
|
112
|
-
"hypercore": "^11.
|
|
112
|
+
"hypercore": "^11.37.0",
|
|
113
113
|
"hypercore-crypto": "^3.7.0",
|
|
114
114
|
"hypercore-id-encoding": "^1.3.0",
|
|
115
115
|
"hypercore-storage": "^3.3.1",
|
|
@@ -128,6 +128,7 @@
|
|
|
128
128
|
"bare-fetch": "^3.4.0",
|
|
129
129
|
"bare-process": "^4.5.1",
|
|
130
130
|
"bare-url": "^2.5.4",
|
|
131
|
+
"blind-peer": "^3.15.0",
|
|
131
132
|
"brittle": "^4.2.0",
|
|
132
133
|
"typescript": "^7.0.2"
|
|
133
134
|
},
|
package/src/build/index.js
CHANGED
|
@@ -20,8 +20,7 @@ import * as internal from './internal.js'
|
|
|
20
20
|
*
|
|
21
21
|
* @typedef {object} BuildOpts
|
|
22
22
|
* @property {string} [ns] Namespace prefix for emitted schema ids. Defaults to `'cero'`.
|
|
23
|
-
* @property {string | import('../extensions/index.js').Extension[]} [extensions] The extensions to fold in. A module specifier, relative to `specDir`, is imported here for its `extensions` export and written into the spec, so every process runs the same list. A list is folded in only. The bundled two by default, `[]` for none.
|
|
24
|
-
* @property {string} [operators] A module specifier, relative to `specDir`, written into the spec for its `operators` export, so every process binds the same map.
|
|
23
|
+
* @property {string | import('../extensions/index.js').Extension[]} [extensions] The extensions to fold in. A module specifier, relative to `specDir`, is imported here for its `extensions` export and written into the spec, so every process runs the same list. A list is folded in only, and `cero()` then needs it in its options. The bundled two by default, `[]` for none.
|
|
25
24
|
*/
|
|
26
25
|
|
|
27
26
|
/**
|
|
@@ -32,14 +31,11 @@ import * as internal from './internal.js'
|
|
|
32
31
|
* @param {BuildOpts} [opts]
|
|
33
32
|
* @returns {Promise<void>}
|
|
34
33
|
*/
|
|
35
|
-
export async function build(specDir, schema, { ns = NS, extensions
|
|
34
|
+
export async function build(specDir, schema, { ns = NS, extensions } = {}) {
|
|
36
35
|
const raw = /** @type {SchemaDefs & { local?: SchemaDefs }} */ (schema?.defs || schema)
|
|
37
36
|
if (!raw || typeof raw !== 'object') throw CeroError.REQUIRED('schema')
|
|
38
|
-
const from =
|
|
39
|
-
const exts = extensionsOf(
|
|
40
|
-
null,
|
|
41
|
-
from.extensions ? await load(from.extensions, specDir) : extensions
|
|
42
|
-
)
|
|
37
|
+
const from = str(extensions)
|
|
38
|
+
const exts = extensionsOf(null, from ? await load(from, specDir) : extensions)
|
|
43
39
|
|
|
44
40
|
// t.extend entries by internal type: the app schema first, then each extension at its scope
|
|
45
41
|
const extend = {}
|
|
@@ -61,12 +57,13 @@ export async function build(specDir, schema, { ns = NS, extensions, operators }
|
|
|
61
57
|
fold(raw, defs, false)
|
|
62
58
|
for (const ext of exts) if (ext.schema) fold(ext.schema, defs, true)
|
|
63
59
|
|
|
64
|
-
const main = compile(splitMain(defs), ns, 'main')
|
|
60
|
+
const main = compile(splitMain(defs), ns, 'main', true)
|
|
65
61
|
const local = compile(defs.local || {}, ns, 'local')
|
|
66
62
|
const handles = {}
|
|
67
63
|
for (const [name, child] of Object.entries(splitHandles(defs))) {
|
|
68
|
-
handles[name] = compile(child, ns, 'main')
|
|
64
|
+
handles[name] = compile(child, ns, 'main', false)
|
|
69
65
|
}
|
|
66
|
+
for (const { meta } of [main, ...Object.values(handles)]) extendBuiltins(meta.refs, extend)
|
|
70
67
|
|
|
71
68
|
emitMain(join(specDir, 'main'), ns, main, { rpc: true, extend })
|
|
72
69
|
emitLocal(join(specDir, 'local'), ns, local)
|
|
@@ -90,7 +87,7 @@ export async function build(specDir, schema, { ns = NS, extensions, operators }
|
|
|
90
87
|
|
|
91
88
|
await fs.writeFile(
|
|
92
89
|
join(specDir, 'index.js'),
|
|
93
|
-
wireModule(meta, Object.keys(handles), from),
|
|
90
|
+
wireModule(meta, Object.keys(handles), from, extensions),
|
|
94
91
|
'utf-8'
|
|
95
92
|
)
|
|
96
93
|
}
|
|
@@ -134,7 +131,7 @@ function isPlainHandle(v) {
|
|
|
134
131
|
return v && typeof v === 'object' && !v.kind && !v.prim
|
|
135
132
|
}
|
|
136
133
|
|
|
137
|
-
function compile(root, ns, scope = 'main') {
|
|
134
|
+
function compile(root, ns, scope = 'main', top = false) {
|
|
138
135
|
const ctx = {
|
|
139
136
|
types: [],
|
|
140
137
|
collections: [],
|
|
@@ -155,6 +152,13 @@ function compile(root, ns, scope = 'main') {
|
|
|
155
152
|
register(name, node, ctx)
|
|
156
153
|
}
|
|
157
154
|
|
|
155
|
+
if (scope === 'main') {
|
|
156
|
+
for (const [name, ref] of Object.entries(internal.live(ns, top))) {
|
|
157
|
+
if (name in root) throw CeroError.INVALID(`'${name}' is a builtin ref, rename it`)
|
|
158
|
+
ctx.meta.refs[name] = ref
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
158
162
|
return {
|
|
159
163
|
types: ctx.types,
|
|
160
164
|
collections: ctx.collections,
|
|
@@ -164,10 +168,11 @@ function compile(root, ns, scope = 'main') {
|
|
|
164
168
|
}
|
|
165
169
|
}
|
|
166
170
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
.
|
|
171
|
+
// a field a builtin gains through t.extend is declared like its own
|
|
172
|
+
function extendBuiltins(refs, extend) {
|
|
173
|
+
for (const [name, { type }] of Object.entries(internal.defs.main)) {
|
|
174
|
+
if (extend[type]) internal.declare(refs[name], extend[type])
|
|
175
|
+
}
|
|
171
176
|
}
|
|
172
177
|
|
|
173
178
|
function register(name, node, ctx) {
|
|
@@ -183,10 +188,10 @@ function register(name, node, ctx) {
|
|
|
183
188
|
ctx.collections.push({ name, schema: fqn, key: [] })
|
|
184
189
|
ctx.dispatches.push({ name: `set-${name}`, requestType: fqn })
|
|
185
190
|
ctx.dispatches.push({ name: `del-${name}`, requestType: `@${ctx.ns}/del-by-id` })
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
191
|
+
ctx.meta.refs[name] = internal.declare(
|
|
192
|
+
{ kind: 'single', path: [name], schema: fqn },
|
|
193
|
+
node.fields
|
|
194
|
+
)
|
|
190
195
|
return
|
|
191
196
|
}
|
|
192
197
|
if (node.kind === 'collection') {
|
|
@@ -206,15 +211,11 @@ function register(name, node, ctx) {
|
|
|
206
211
|
ctx.dispatches.push({ name: `add-${name}`, requestType: fqn })
|
|
207
212
|
ctx.dispatches.push({ name: `set-${name}`, requestType: fqn })
|
|
208
213
|
ctx.dispatches.push({ name: `del-${name}`, requestType: `@${ctx.ns}/del-by-id` })
|
|
209
|
-
const refEntry =
|
|
210
|
-
kind: 'collection',
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
fields: Object.keys(node.fields)
|
|
214
|
-
}
|
|
214
|
+
const refEntry = internal.declare(
|
|
215
|
+
{ kind: 'collection', path: [name], schema: fqn },
|
|
216
|
+
node.fields
|
|
217
|
+
)
|
|
215
218
|
if (node.own) refEntry.own = true
|
|
216
|
-
const fileFields = fileFieldNames(node.fields)
|
|
217
|
-
if (fileFields.length) refEntry.files = fileFields
|
|
218
219
|
ctx.meta.refs[name] = refEntry
|
|
219
220
|
if (node.indexes) {
|
|
220
221
|
for (const [idx, fields] of Object.entries(node.indexes)) {
|
|
@@ -319,10 +320,15 @@ function handleEntries(names, kinds) {
|
|
|
319
320
|
.join(',\n')
|
|
320
321
|
}
|
|
321
322
|
|
|
322
|
-
// the spec
|
|
323
|
-
|
|
323
|
+
// the spec carries the list cero() runs, so every process runs the same one; a list of objects
|
|
324
|
+
// given to build cannot be written, so cero() asks for it
|
|
325
|
+
function named(from, list) {
|
|
326
|
+
if (from) return `import { extensions } from '${from}'`
|
|
327
|
+
if (!list) return "import { bundled as extensions } from '@cero-base/cero/extensions'"
|
|
328
|
+
return `const extensions = ${list.length === 0 ? '[]' : 'null'}`
|
|
329
|
+
}
|
|
324
330
|
|
|
325
|
-
function wireModule(meta, names, from) {
|
|
331
|
+
function wireModule(meta, names, from, list) {
|
|
326
332
|
const kinds = ['database', 'dispatch', 'schema']
|
|
327
333
|
return `// autogenerated by cero/build
|
|
328
334
|
import database from './main/db/index.js'
|
|
@@ -332,8 +338,7 @@ import rpc from './main/rpc/index.js'
|
|
|
332
338
|
import localDatabase from './local/db/index.js'
|
|
333
339
|
import * as localSchema from './local/schema/index.js'
|
|
334
340
|
${handleImports(names, kinds)}
|
|
335
|
-
${named(
|
|
336
|
-
${named('operators', from.operators)}
|
|
341
|
+
${named(from, list)}
|
|
337
342
|
|
|
338
343
|
export const meta = ${JSON.stringify(meta, null, 2)}
|
|
339
344
|
|
|
@@ -345,7 +350,6 @@ export const spec = {
|
|
|
345
350
|
local: { database: localDatabase, schema: localSchema, meta: meta.local },
|
|
346
351
|
meta,
|
|
347
352
|
extensions,
|
|
348
|
-
operators,
|
|
349
353
|
handles: {
|
|
350
354
|
${handleEntries(names, kinds)}
|
|
351
355
|
}
|
package/src/build/internal.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CeroError } from '@cero-base/core/errors'
|
|
2
|
-
import { COUNTERS, EPOCHS } from '../lib/constants.js'
|
|
2
|
+
import { COUNTERS, EPOCHS, REMOVALS } from '../lib/constants.js'
|
|
3
3
|
import * as schemas from './schemas.js'
|
|
4
4
|
|
|
5
5
|
// internal refs by scope; kind defaults to collection
|
|
@@ -9,24 +9,66 @@ export const defs = {
|
|
|
9
9
|
devices: { type: 'device' },
|
|
10
10
|
invites: { type: 'invite' },
|
|
11
11
|
handles: { type: 'handle' },
|
|
12
|
-
files: { type: 'file' }
|
|
12
|
+
files: { type: 'file' },
|
|
13
|
+
requests: { type: 'request' }
|
|
13
14
|
},
|
|
14
15
|
local: {
|
|
15
16
|
master: { type: 'master', kind: 'single' },
|
|
16
17
|
keypair: { type: 'keypair', kind: 'single' },
|
|
17
18
|
'handle-keypairs': { type: 'handle-keypair' },
|
|
18
|
-
|
|
19
|
+
joins: { type: 'join' },
|
|
20
|
+
inbox: { type: 'mail' },
|
|
21
|
+
outbox: { type: 'mail' },
|
|
22
|
+
environment: { type: 'environment', kind: 'single' },
|
|
23
|
+
serving: { type: 'serving' }
|
|
19
24
|
}
|
|
20
25
|
}
|
|
21
26
|
|
|
27
|
+
// computed on the device, never stored: `get` and `watch` read them like any other ref
|
|
28
|
+
const LIVE = {
|
|
29
|
+
status: { type: 'status', kind: 'single' },
|
|
30
|
+
joins: { type: 'joining', root: true },
|
|
31
|
+
nearby: { type: 'peer', root: true }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @typedef {import('../lib/spec.js').RefInfo} RefInfo
|
|
36
|
+
* @typedef {{ prim: string, required?: boolean, array?: boolean }} FieldType A `t` field.
|
|
37
|
+
* @typedef {{ name: string, type: string, required: boolean, array?: boolean }} Column
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {string} ns
|
|
42
|
+
* @param {boolean} root
|
|
43
|
+
* @returns {Record<string, RefInfo>}
|
|
44
|
+
*/
|
|
45
|
+
export function live(ns, root) {
|
|
46
|
+
const out = {}
|
|
47
|
+
for (const [name, def] of Object.entries(LIVE)) {
|
|
48
|
+
if (def.root && !root) continue
|
|
49
|
+
out[name] = {
|
|
50
|
+
kind: def.kind || 'collection',
|
|
51
|
+
internal: true,
|
|
52
|
+
live: true,
|
|
53
|
+
schema: `@${ns}/${def.type}`
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return out
|
|
57
|
+
}
|
|
58
|
+
|
|
22
59
|
// a DSL primitive is its hyperdb column type, except bytes (a buffer) and file (an id string)
|
|
23
60
|
const COLUMN = { bytes: 'buffer', file: 'string' }
|
|
24
61
|
|
|
62
|
+
/**
|
|
63
|
+
* @param {Record<string, FieldType>} map
|
|
64
|
+
* @returns {Column[]}
|
|
65
|
+
*/
|
|
25
66
|
export function fields(map) {
|
|
26
67
|
return Object.entries(map).map(([name, m]) => ({
|
|
27
68
|
name,
|
|
28
69
|
type: COLUMN[m.prim] || m.prim,
|
|
29
|
-
required: m.required === true
|
|
70
|
+
required: m.required === true,
|
|
71
|
+
...(m.array && { array: true })
|
|
30
72
|
}))
|
|
31
73
|
}
|
|
32
74
|
|
|
@@ -38,6 +80,11 @@ function merge(type, base, extra) {
|
|
|
38
80
|
return { ...base, ...extra }
|
|
39
81
|
}
|
|
40
82
|
|
|
83
|
+
/**
|
|
84
|
+
* @param {'main' | 'local' | 'rpc'} scope
|
|
85
|
+
* @param {Record<string, Record<string, FieldType>>} [extend]
|
|
86
|
+
* @returns {Array<{ name: string, compact: boolean, fields: Column[] }>}
|
|
87
|
+
*/
|
|
41
88
|
export function types(scope, extend = {}) {
|
|
42
89
|
return Object.entries(schemas[scope]).map(([name, base]) => ({
|
|
43
90
|
name,
|
|
@@ -46,6 +93,11 @@ export function types(scope, extend = {}) {
|
|
|
46
93
|
}))
|
|
47
94
|
}
|
|
48
95
|
|
|
96
|
+
/**
|
|
97
|
+
* @param {string} ns
|
|
98
|
+
* @param {'main' | 'local'} scope
|
|
99
|
+
* @returns {Record<string, RefInfo & { path: string[] }>}
|
|
100
|
+
*/
|
|
49
101
|
export function refs(ns, scope) {
|
|
50
102
|
return Object.fromEntries(
|
|
51
103
|
Object.entries(defs[scope]).map(([name, def]) => [
|
|
@@ -54,13 +106,34 @@ export function refs(ns, scope) {
|
|
|
54
106
|
kind: def.kind || 'collection',
|
|
55
107
|
path: [name],
|
|
56
108
|
internal: true,
|
|
57
|
-
...(scope === 'main' && { verb: def.type }),
|
|
109
|
+
...(scope === 'main' && declare({ verb: def.type }, schemas.main[def.type])),
|
|
58
110
|
schema: `@${ns}/${def.type}`
|
|
59
111
|
}
|
|
60
112
|
])
|
|
61
113
|
)
|
|
62
114
|
}
|
|
63
115
|
|
|
116
|
+
/**
|
|
117
|
+
* Add `fields` to a ref: file ones resolve on read, required ones the RPC client fills on a set.
|
|
118
|
+
*
|
|
119
|
+
* @param {RefInfo} ref
|
|
120
|
+
* @param {Record<string, FieldType>} fields
|
|
121
|
+
* @returns {RefInfo}
|
|
122
|
+
*/
|
|
123
|
+
export function declare(ref, fields) {
|
|
124
|
+
ref.fields = [...(ref.fields ?? []), ...Object.keys(fields)]
|
|
125
|
+
for (const [name, m] of Object.entries(fields)) {
|
|
126
|
+
if (m.prim === 'file') ref.files = [...(ref.files ?? []), name]
|
|
127
|
+
if (m.required) ref.required = { ...ref.required, [name]: m.prim }
|
|
128
|
+
}
|
|
129
|
+
return ref
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @param {string} ns
|
|
134
|
+
* @param {'main' | 'local'} scope
|
|
135
|
+
* @returns {Array<{ name: string, schema: string, key: string[] }>}
|
|
136
|
+
*/
|
|
64
137
|
export function collections(ns, scope) {
|
|
65
138
|
const out = Object.entries(defs[scope]).map(([name, def]) => ({
|
|
66
139
|
name,
|
|
@@ -70,10 +143,15 @@ export function collections(ns, scope) {
|
|
|
70
143
|
if (scope === 'main') {
|
|
71
144
|
out.push({ name: COUNTERS, schema: `@${ns}/counter`, key: ['name'] })
|
|
72
145
|
out.push({ name: EPOCHS, schema: `@${ns}/epoch`, key: ['epoch'] })
|
|
146
|
+
out.push({ name: REMOVALS, schema: `@${ns}/removal`, key: ['id'] })
|
|
73
147
|
}
|
|
74
148
|
return out
|
|
75
149
|
}
|
|
76
150
|
|
|
151
|
+
/**
|
|
152
|
+
* @param {string} ns
|
|
153
|
+
* @returns {Array<{ name: string, requestType: string }>}
|
|
154
|
+
*/
|
|
77
155
|
export function dispatches(ns) {
|
|
78
156
|
return [
|
|
79
157
|
{ name: 'add-writer', requestType: `@${ns}/writer` },
|
|
@@ -83,7 +161,9 @@ export function dispatches(ns) {
|
|
|
83
161
|
{ name: `add-${type}`, requestType: `@${ns}/${type}` },
|
|
84
162
|
{ name: `set-${type}`, requestType: `@${ns}/${type}` },
|
|
85
163
|
{ name: `del-${type}`, requestType: `@${ns}/del-by-id` }
|
|
86
|
-
])
|
|
164
|
+
]),
|
|
165
|
+
{ name: 'join', requestType: `@${ns}/join` },
|
|
166
|
+
{ name: 'accept', requestType: `@${ns}/accept` }
|
|
87
167
|
]
|
|
88
168
|
}
|
|
89
169
|
|
|
@@ -97,24 +177,29 @@ const COMMANDS = [
|
|
|
97
177
|
['add-handle', 'req-row', 'res-handle'],
|
|
98
178
|
['set', 'req-row', 'res-data'],
|
|
99
179
|
['get', 'req-query', 'res-rows'],
|
|
100
|
-
['get-one', 'req-id', 'res-data'],
|
|
101
180
|
['del', 'req-id', 'res-ok'],
|
|
102
181
|
['watch', 'req-query', 'res-rows', true],
|
|
103
182
|
['call', 'req-call', 'res-data'],
|
|
104
183
|
['invite', 'req-invite', 'res-invite'],
|
|
105
184
|
['revoke', 'req-revoke', 'res-ok'],
|
|
106
185
|
['join', 'req-join', 'res-handle'],
|
|
186
|
+
['cancel', 'req-cancel', 'res-ok'],
|
|
107
187
|
['open-handle', 'req-open', 'res-handle'],
|
|
108
188
|
['close-handle', 'req-handle', 'res-ok'],
|
|
109
189
|
['leave', 'req-handle', 'res-ok'],
|
|
110
|
-
['changes', 'req-query', 'res-changes', true],
|
|
111
190
|
['rotate', 'req-handle', 'res-epoch'],
|
|
112
191
|
['set-active', 'req-set-active', 'res-ok'],
|
|
113
|
-
['suspend', 'req-
|
|
114
|
-
['resume', 'req-
|
|
115
|
-
['errors', 'req-empty', 'res-error', true]
|
|
192
|
+
['suspend', 'req-handle', 'res-ok'],
|
|
193
|
+
['resume', 'req-handle', 'res-ok'],
|
|
194
|
+
['errors', 'req-empty', 'res-error', true],
|
|
195
|
+
['answer', 'req-answer', 'res-ok'],
|
|
196
|
+
['nearby', 'req-nearby', 'res-ok']
|
|
116
197
|
]
|
|
117
198
|
|
|
199
|
+
/**
|
|
200
|
+
* @param {string} ns
|
|
201
|
+
* @returns {Array<{ name: string, request: { name: string }, response: { name: string, stream?: boolean } }>}
|
|
202
|
+
*/
|
|
118
203
|
export function commands(ns) {
|
|
119
204
|
return COMMANDS.map(([name, req, res, stream]) => ({
|
|
120
205
|
name,
|