@cero-base/cero 2.3.2 → 2.3.3
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/package.json +2 -2
- package/src/build/index.js +5 -1
- package/src/build/schemas.js +13 -0
- package/src/handle/index.js +24 -7
- package/src/lib/constants.js +2 -0
- package/src/local/index.js +16 -0
- package/src/rpc/server.js +2 -0
- package/types/build/schemas.d.ts +11 -0
- package/types/handle/index.d.ts +2 -0
- package/types/lib/constants.d.ts +2 -0
- package/types/local/index.d.ts +10 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/cero",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.3",
|
|
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.3.
|
|
102
|
+
"@cero-base/core": "^2.3.3",
|
|
103
103
|
"b4a": "^1.9.0",
|
|
104
104
|
"bare-abort-controller": "^1.1.2",
|
|
105
105
|
"bare-crypto": "^1.15.3",
|
package/src/build/index.js
CHANGED
|
@@ -9,7 +9,7 @@ import HRPCBuilder from 'hrpc'
|
|
|
9
9
|
|
|
10
10
|
import { CeroError } from '@cero-base/core/errors'
|
|
11
11
|
|
|
12
|
-
import { NS } from '../lib/constants.js'
|
|
12
|
+
import { NS, REFUSALS, SPENT } from '../lib/constants.js'
|
|
13
13
|
import { extensionsOf } from '../extensions/index.js'
|
|
14
14
|
import * as internal from './internal.js'
|
|
15
15
|
|
|
@@ -263,6 +263,9 @@ function emitMain(dir, ns, { types, collections, dispatches, indexes = [] }, { r
|
|
|
263
263
|
for (const desc of internal.collections(ns, 'main')) dns.collections.register(desc)
|
|
264
264
|
for (const desc of collections) dns.collections.register(desc)
|
|
265
265
|
for (const desc of indexes) dns.indexes.register(desc)
|
|
266
|
+
// after the app's: hyperdb numbers collections positionally and persists them
|
|
267
|
+
dns.collections.register({ name: REFUSALS, schema: `@${ns}/refusal`, key: ['id'] })
|
|
268
|
+
dns.collections.register({ name: SPENT, schema: `@${ns}/spent`, key: ['id'] })
|
|
266
269
|
HyperdbBuilder.toDisk(db, dbDir, { esm: true })
|
|
267
270
|
|
|
268
271
|
const d = Hyperdispatch.from(schemaDir, dispatchDir)
|
|
@@ -271,6 +274,7 @@ function emitMain(dir, ns, { types, collections, dispatches, indexes = [] }, { r
|
|
|
271
274
|
for (const desc of dispatches) xns.register(desc)
|
|
272
275
|
// after the app's dispatches: hyperdispatch numbers routes positionally and persists them
|
|
273
276
|
xns.register({ name: 'rotate-key', requestType: `@${ns}/epoch` })
|
|
277
|
+
xns.register({ name: 'del-refusal', requestType: `@${ns}/del-by-id` })
|
|
274
278
|
Hyperdispatch.toDisk(d, dispatchDir, { esm: true })
|
|
275
279
|
|
|
276
280
|
if (rpc) {
|
package/src/build/schemas.js
CHANGED
|
@@ -134,6 +134,19 @@ export const main = {
|
|
|
134
134
|
name: string,
|
|
135
135
|
isMobile: bool,
|
|
136
136
|
device: string
|
|
137
|
+
},
|
|
138
|
+
// a join the room turned away, until the joiner read why. `id` is the joiner's writer
|
|
139
|
+
refusal: {
|
|
140
|
+
id: required(string),
|
|
141
|
+
reply: required(bytes),
|
|
142
|
+
reason: required(string),
|
|
143
|
+
expires: int
|
|
144
|
+
},
|
|
145
|
+
// an invite spent or revoked, kept until it expires so a join with it hears why
|
|
146
|
+
spent: {
|
|
147
|
+
id: required(string),
|
|
148
|
+
reason: required(string),
|
|
149
|
+
expires: int
|
|
137
150
|
}
|
|
138
151
|
}
|
|
139
152
|
|
package/src/handle/index.js
CHANGED
|
@@ -124,6 +124,9 @@ export class Handle extends ReadyResource {
|
|
|
124
124
|
this._onerror = this._opts.onerror || ((err) => console.error(err))
|
|
125
125
|
/** @type {Set<Handle> | null} */
|
|
126
126
|
this.children = parent ? null : new Set()
|
|
127
|
+
// children still opening, not adopted yet: a close must close them too
|
|
128
|
+
/** @private */
|
|
129
|
+
this._pending = parent ? null : new Set()
|
|
127
130
|
/** @private */
|
|
128
131
|
this._typeHooks = parent ? null : new Set()
|
|
129
132
|
/** @private */
|
|
@@ -301,6 +304,7 @@ export class Handle extends ReadyResource {
|
|
|
301
304
|
() => this._blobs?.close(),
|
|
302
305
|
...[...(this._epochBlobs?.values() || [])].map((b) => () => b.close()),
|
|
303
306
|
...[...(this.children || [])].map((c) => () => c.close()),
|
|
307
|
+
...[...(this._pending || [])].map((c) => () => c.close()),
|
|
304
308
|
() => this._pair?.close()
|
|
305
309
|
]
|
|
306
310
|
this._epochBlobs = null
|
|
@@ -737,10 +741,12 @@ export class Handle extends ReadyResource {
|
|
|
737
741
|
this._loading?.delete(id)
|
|
738
742
|
return child
|
|
739
743
|
} catch (err) {
|
|
740
|
-
|
|
744
|
+
// a creation the parent's close broke fails with CLOSED, whichever resource went first
|
|
745
|
+
const reason = this.closing || this.closed ? CeroError.CLOSED('Handle') : err
|
|
746
|
+
if (abort) abort(reason)
|
|
741
747
|
if (inflightId) this._loading?.delete(inflightId)
|
|
742
748
|
await child.close().catch(safetyCatch)
|
|
743
|
-
throw
|
|
749
|
+
throw reason
|
|
744
750
|
}
|
|
745
751
|
}
|
|
746
752
|
|
|
@@ -941,11 +947,11 @@ export class Handle extends ReadyResource {
|
|
|
941
947
|
if (firstTime) {
|
|
942
948
|
await this._saveKeyPair(id, writer)
|
|
943
949
|
}
|
|
950
|
+
this._adopt(child, {})
|
|
944
951
|
} catch (err) {
|
|
945
952
|
await child.close().catch(safetyCatch)
|
|
946
953
|
throw err
|
|
947
954
|
}
|
|
948
|
-
this._adopt(child, {})
|
|
949
955
|
return child
|
|
950
956
|
}
|
|
951
957
|
|
|
@@ -954,6 +960,8 @@ export class Handle extends ReadyResource {
|
|
|
954
960
|
_room(opts) {
|
|
955
961
|
const child = new Handle({ parent: this, ...opts })
|
|
956
962
|
for (const hook of this._typeHooks) hook.apply(child)
|
|
963
|
+
this._pending.add(child)
|
|
964
|
+
child.once('close', () => this._pending.delete(child))
|
|
957
965
|
return child
|
|
958
966
|
}
|
|
959
967
|
|
|
@@ -975,6 +983,9 @@ export class Handle extends ReadyResource {
|
|
|
975
983
|
|
|
976
984
|
/** @private */
|
|
977
985
|
_adopt(child, info) {
|
|
986
|
+
// a closing parent has already closed its children, it would never close this one
|
|
987
|
+
if (this.closing || this.closed) throw CeroError.CLOSED('Handle')
|
|
988
|
+
this._pending.delete(child)
|
|
978
989
|
this.children.add(child)
|
|
979
990
|
this._serve(child)
|
|
980
991
|
this.emit('handle', child, info)
|
|
@@ -1113,14 +1124,20 @@ function delivered({ key, encryptionKey, epochs, writer }) {
|
|
|
1113
1124
|
|
|
1114
1125
|
function admitted(db, id) {
|
|
1115
1126
|
return new Promise((resolve, reject) => {
|
|
1127
|
+
const done = (err, member) => {
|
|
1128
|
+
db.off('update', onupdate)
|
|
1129
|
+
db.off('close', onclose)
|
|
1130
|
+
if (err) reject(err)
|
|
1131
|
+
else resolve(member)
|
|
1132
|
+
}
|
|
1116
1133
|
const check = async () => {
|
|
1117
1134
|
const { data } = await db.get('members', id)
|
|
1118
|
-
if (
|
|
1119
|
-
db.off('update', onupdate)
|
|
1120
|
-
resolve(data)
|
|
1135
|
+
if (data) done(null, data)
|
|
1121
1136
|
}
|
|
1122
|
-
const onupdate = () => check().catch(
|
|
1137
|
+
const onupdate = () => check().catch(done)
|
|
1138
|
+
const onclose = () => done(CeroError.CLOSED('Database'))
|
|
1123
1139
|
db.on('update', onupdate)
|
|
1140
|
+
db.once('close', onclose)
|
|
1124
1141
|
onupdate()
|
|
1125
1142
|
})
|
|
1126
1143
|
}
|
package/src/lib/constants.js
CHANGED
|
@@ -2,6 +2,8 @@ export const NS = 'cero'
|
|
|
2
2
|
export const COUNTERS = 'counters'
|
|
3
3
|
export const EPOCHS = 'epochs'
|
|
4
4
|
export const REMOVALS = 'removals'
|
|
5
|
+
export const REFUSALS = 'refusals'
|
|
6
|
+
export const SPENT = 'spent'
|
|
5
7
|
|
|
6
8
|
// Writer-admission / pairing timeout (ms), and the initial discovery-flush wait (ms).
|
|
7
9
|
export const TIMEOUT = 30000
|
package/src/local/index.js
CHANGED
|
@@ -39,6 +39,21 @@ export class Local extends ReadyResource {
|
|
|
39
39
|
store,
|
|
40
40
|
storageKey
|
|
41
41
|
})
|
|
42
|
+
/** @private */
|
|
43
|
+
this._owned = new Set()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Tie a watch stream to this store: it ends when the store closes.
|
|
48
|
+
*
|
|
49
|
+
* @template {import('streamx').Readable} T
|
|
50
|
+
* @param {T} stream
|
|
51
|
+
* @returns {T}
|
|
52
|
+
*/
|
|
53
|
+
own(stream) {
|
|
54
|
+
this._owned.add(stream)
|
|
55
|
+
stream.once('close', () => this._owned.delete(stream))
|
|
56
|
+
return stream
|
|
42
57
|
}
|
|
43
58
|
|
|
44
59
|
/** @private */
|
|
@@ -49,6 +64,7 @@ export class Local extends ReadyResource {
|
|
|
49
64
|
|
|
50
65
|
/** @private */
|
|
51
66
|
async _close() {
|
|
67
|
+
for (const stream of [...this._owned]) stream.destroy()
|
|
52
68
|
await this.store.close()
|
|
53
69
|
}
|
|
54
70
|
}
|
package/src/rpc/server.js
CHANGED
|
@@ -258,6 +258,8 @@ export class Server extends RPCServer {
|
|
|
258
258
|
if (!set) this._watchStreams.set(handle, (set = new Set()))
|
|
259
259
|
set.add(stream)
|
|
260
260
|
live.on('data', onData)
|
|
261
|
+
// a watch ending with its handle ends the client's
|
|
262
|
+
live.once('close', () => stream.destroy())
|
|
261
263
|
// channel teardown destroys the stream with CHANNEL_CLOSED; 'close' cleans up
|
|
262
264
|
stream.on('error', safetyCatch)
|
|
263
265
|
stream.on('close', () => {
|
package/types/build/schemas.d.ts
CHANGED
|
@@ -116,6 +116,17 @@ export declare const main: {
|
|
|
116
116
|
isMobile: import("@cero-base/core").Prim;
|
|
117
117
|
device: import("@cero-base/core").Prim;
|
|
118
118
|
};
|
|
119
|
+
refusal: {
|
|
120
|
+
id: import("@cero-base/core").Prim;
|
|
121
|
+
reply: import("@cero-base/core").Prim;
|
|
122
|
+
reason: import("@cero-base/core").Prim;
|
|
123
|
+
expires: import("@cero-base/core").Prim;
|
|
124
|
+
};
|
|
125
|
+
spent: {
|
|
126
|
+
id: import("@cero-base/core").Prim;
|
|
127
|
+
reason: import("@cero-base/core").Prim;
|
|
128
|
+
expires: import("@cero-base/core").Prim;
|
|
129
|
+
};
|
|
119
130
|
};
|
|
120
131
|
export declare const local: {
|
|
121
132
|
master: {
|
package/types/handle/index.d.ts
CHANGED
package/types/lib/constants.d.ts
CHANGED
|
@@ -2,5 +2,7 @@ export declare const NS = "cero";
|
|
|
2
2
|
export declare const COUNTERS = "counters";
|
|
3
3
|
export declare const EPOCHS = "epochs";
|
|
4
4
|
export declare const REMOVALS = "removals";
|
|
5
|
+
export declare const REFUSALS = "refusals";
|
|
6
|
+
export declare const SPENT = "spent";
|
|
5
7
|
export declare const TIMEOUT = 30000;
|
|
6
8
|
export declare const FLUSH = 500;
|
package/types/local/index.d.ts
CHANGED
|
@@ -28,12 +28,22 @@ export declare class Local extends ReadyResource {
|
|
|
28
28
|
dir: string;
|
|
29
29
|
spec: import("../lib/spec.js").Spec;
|
|
30
30
|
store: Storage;
|
|
31
|
+
/** @private */
|
|
32
|
+
_owned;
|
|
31
33
|
/**
|
|
32
34
|
* @param {string | null} dir Directory for the local store, or `null` when reusing an external `store`.
|
|
33
35
|
* @param {import('../lib/spec.js').Spec} spec Built cero spec, with `spec.local.database` and `spec.meta.local`.
|
|
34
36
|
* @param {LocalOpts} [opts]
|
|
35
37
|
*/
|
|
36
38
|
constructor(dir: string | null, spec: import('../lib/spec.js').Spec, { root, store, storageKey }?: LocalOpts);
|
|
39
|
+
/**
|
|
40
|
+
* Tie a watch stream to this store: it ends when the store closes.
|
|
41
|
+
*
|
|
42
|
+
* @template {import('streamx').Readable} T
|
|
43
|
+
* @param {T} stream
|
|
44
|
+
* @returns {T}
|
|
45
|
+
*/
|
|
46
|
+
own<T extends import('streamx').Readable>(stream: T): T;
|
|
37
47
|
/** @private */
|
|
38
48
|
private _open;
|
|
39
49
|
/** @private */
|