@cero-base/tools 1.18.2 → 2.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.
- package/package.json +7 -8
- package/src/connect.js +61 -65
- package/src/protocol.js +18 -19
- package/src/redact.js +3 -6
- package/src/server.js +12 -16
- package/src/stats.js +1 -4
- package/src/tap.js +5 -11
- package/src/transport.js +3 -8
- package/types/connect.d.ts +20 -29
- package/types/format.d.ts +5 -5
- package/types/index.d.ts +6 -6
- package/types/protocol.d.ts +3 -3
- package/types/redact.d.ts +4 -6
- package/types/server.d.ts +18 -25
- package/types/stats.d.ts +2 -5
- package/types/tap.d.ts +29 -43
- package/types/transport.d.ts +4 -5
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/tools",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Passive devtools for cero — observe data, events, and p2p/replication stats over a local out-of-band pipe.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"engines": {
|
|
8
|
-
"node": ">=
|
|
8
|
+
"node": ">=22"
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
@@ -48,8 +48,7 @@
|
|
|
48
48
|
"types",
|
|
49
49
|
"bin",
|
|
50
50
|
"README.md",
|
|
51
|
-
"LICENSE"
|
|
52
|
-
"!**/CLAUDE.md"
|
|
51
|
+
"LICENSE"
|
|
53
52
|
],
|
|
54
53
|
"publishConfig": {
|
|
55
54
|
"access": "public"
|
|
@@ -72,11 +71,11 @@
|
|
|
72
71
|
"build:types": "rm -rf types && tsc -p .",
|
|
73
72
|
"prepublishOnly": "npm run build:types",
|
|
74
73
|
"test": "npm run test:bare",
|
|
75
|
-
"test:bare": "
|
|
76
|
-
"test:node": "
|
|
74
|
+
"test:bare": "find test -name '*.test.js' | sort | xargs -P1 -n1 brittle-bare",
|
|
75
|
+
"test:node": "find test -name '*.test.js' | sort | xargs -P1 -n1 brittle-node"
|
|
77
76
|
},
|
|
78
77
|
"dependencies": {
|
|
79
|
-
"@cero-base/cero": "^
|
|
78
|
+
"@cero-base/cero": "^2.0.0",
|
|
80
79
|
"b4a": "^1.8.1",
|
|
81
80
|
"bare-crypto": "^1.15.3",
|
|
82
81
|
"bare-net": "^2.3.3",
|
|
@@ -91,7 +90,7 @@
|
|
|
91
90
|
"bare-path": "^3.1.2",
|
|
92
91
|
"bare-url": "^2.5.4",
|
|
93
92
|
"brittle": "^4.1.1",
|
|
94
|
-
"typescript": "^
|
|
93
|
+
"typescript": "^7.0.2"
|
|
95
94
|
},
|
|
96
95
|
"license": "Apache-2.0",
|
|
97
96
|
"typesVersions": {
|
package/src/connect.js
CHANGED
|
@@ -11,10 +11,7 @@ import { Framed } from './protocol.js'
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
* Consumer SDK for a tap server reached over a local Duplex `stream`.
|
|
15
|
-
* a session exposing read methods (`handles`/`get`/`count`) and stream methods
|
|
16
|
-
* (`watch`/`events`/`stats`). Stream methods return a streamx Readable that
|
|
17
|
-
* cancels its server-side source when destroyed.
|
|
14
|
+
* Consumer SDK for a tap server reached over a local Duplex `stream`.
|
|
18
15
|
*
|
|
19
16
|
* @param {object} stream A streamx Duplex connected to a tap server.
|
|
20
17
|
* @returns {Promise<{ handles(): Promise<Handle[]>, get(ref: string, query?: Query, handleId?: string): Promise<unknown>, count(ref: string, query?: Query, handleId?: string): Promise<number>, watch(ref: string, query?: Query, handleId?: string): import('streamx').Readable, events(): import('streamx').Readable, stats(): import('streamx').Readable, close(): void }>}
|
|
@@ -35,67 +32,6 @@ export class Session {
|
|
|
35
32
|
stream.on('close', () => this._onclose())
|
|
36
33
|
}
|
|
37
34
|
|
|
38
|
-
// The transport dropped — fail in-flight requests and end live streams instead
|
|
39
|
-
// of leaving them hung and the maps leaking.
|
|
40
|
-
_onclose() {
|
|
41
|
-
for (const { reject } of this.pending.values()) {
|
|
42
|
-
reject(new TapError('connection closed', 'CLOSED'))
|
|
43
|
-
}
|
|
44
|
-
this.pending.clear()
|
|
45
|
-
for (const r of this.streams.values()) r.push(null)
|
|
46
|
-
this.streams.clear()
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
_onmessage(msg) {
|
|
50
|
-
if ('frame' in msg || msg.end) {
|
|
51
|
-
const r = this.streams.get(msg.id)
|
|
52
|
-
if (!r) return
|
|
53
|
-
if (msg.end) {
|
|
54
|
-
this.streams.delete(msg.id)
|
|
55
|
-
r.push(null)
|
|
56
|
-
} else {
|
|
57
|
-
r.push(msg.frame)
|
|
58
|
-
}
|
|
59
|
-
return
|
|
60
|
-
}
|
|
61
|
-
const p = this.pending.get(msg.id)
|
|
62
|
-
if (!p) return
|
|
63
|
-
this.pending.delete(msg.id)
|
|
64
|
-
if (msg.ok === false) p.reject(new TapError(msg.error, msg.code))
|
|
65
|
-
else p.resolve(msg.data)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
_newId() {
|
|
69
|
-
return ++this.seq
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
_send(frame) {
|
|
73
|
-
this.wire.send(this.token ? { token: this.token, ...frame } : frame)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
_request(method, fields) {
|
|
77
|
-
return new Promise((resolve, reject) => {
|
|
78
|
-
const id = this._newId()
|
|
79
|
-
this.pending.set(id, { resolve, reject })
|
|
80
|
-
this._send({ id, method, ...fields })
|
|
81
|
-
})
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
_stream(method, fields) {
|
|
85
|
-
const id = this._newId()
|
|
86
|
-
const r = new Readable({
|
|
87
|
-
destroy: (cb) => {
|
|
88
|
-
if (this.streams.delete(id)) {
|
|
89
|
-
this._send({ id: this._newId(), method: 'cancel', cancelId: id })
|
|
90
|
-
}
|
|
91
|
-
cb(null)
|
|
92
|
-
}
|
|
93
|
-
})
|
|
94
|
-
this.streams.set(id, r)
|
|
95
|
-
this._send({ id, method, ...fields })
|
|
96
|
-
return r
|
|
97
|
-
}
|
|
98
|
-
|
|
99
35
|
/**
|
|
100
36
|
* Lists the handles exposed by the server.
|
|
101
37
|
*
|
|
@@ -167,6 +103,66 @@ export class Session {
|
|
|
167
103
|
close() {
|
|
168
104
|
this.stream.end()
|
|
169
105
|
}
|
|
106
|
+
|
|
107
|
+
// fail in-flight requests and end live streams
|
|
108
|
+
_onclose() {
|
|
109
|
+
for (const { reject } of this.pending.values()) {
|
|
110
|
+
reject(new TapError('connection closed', 'CLOSED'))
|
|
111
|
+
}
|
|
112
|
+
this.pending.clear()
|
|
113
|
+
for (const r of this.streams.values()) r.push(null)
|
|
114
|
+
this.streams.clear()
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
_onmessage(msg) {
|
|
118
|
+
if ('frame' in msg || msg.end) {
|
|
119
|
+
const r = this.streams.get(msg.id)
|
|
120
|
+
if (!r) return
|
|
121
|
+
if (msg.end) {
|
|
122
|
+
this.streams.delete(msg.id)
|
|
123
|
+
r.push(null)
|
|
124
|
+
} else {
|
|
125
|
+
r.push(msg.frame)
|
|
126
|
+
}
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
const p = this.pending.get(msg.id)
|
|
130
|
+
if (!p) return
|
|
131
|
+
this.pending.delete(msg.id)
|
|
132
|
+
if (msg.ok === false) p.reject(new TapError(msg.error, msg.code))
|
|
133
|
+
else p.resolve(msg.data)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
_newId() {
|
|
137
|
+
return ++this.seq
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
_send(frame) {
|
|
141
|
+
this.wire.send(this.token ? { token: this.token, ...frame } : frame)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
_request(method, fields) {
|
|
145
|
+
return new Promise((resolve, reject) => {
|
|
146
|
+
const id = this._newId()
|
|
147
|
+
this.pending.set(id, { resolve, reject })
|
|
148
|
+
this._send({ id, method, ...fields })
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
_stream(method, fields) {
|
|
153
|
+
const id = this._newId()
|
|
154
|
+
const r = new Readable({
|
|
155
|
+
destroy: (cb) => {
|
|
156
|
+
if (this.streams.delete(id)) {
|
|
157
|
+
this._send({ id: this._newId(), method: 'cancel', cancelId: id })
|
|
158
|
+
}
|
|
159
|
+
cb(null)
|
|
160
|
+
}
|
|
161
|
+
})
|
|
162
|
+
this.streams.set(id, r)
|
|
163
|
+
this._send({ id, method, ...fields })
|
|
164
|
+
return r
|
|
165
|
+
}
|
|
170
166
|
}
|
|
171
167
|
|
|
172
168
|
class TapError extends Error {
|
package/src/protocol.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import b4a from 'b4a'
|
|
2
2
|
|
|
3
3
|
const BYTES = '$b'
|
|
4
|
-
//
|
|
5
|
-
// stuck consumer can't grow host memory without bound (devtools is never essential).
|
|
4
|
+
// cap a frame and the backlog so a bad length prefix cannot grow memory without bound
|
|
6
5
|
const MAX_FRAME = 16 * 1024 * 1024
|
|
7
6
|
|
|
8
7
|
function toWire(v) {
|
|
@@ -39,6 +38,23 @@ export class Framed {
|
|
|
39
38
|
stream.on('data', (chunk) => this._ondata(chunk))
|
|
40
39
|
}
|
|
41
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Encode `obj` to wire form and write it as one length-prefixed frame.
|
|
43
|
+
* @param {object} obj - message to send
|
|
44
|
+
* @returns {void}
|
|
45
|
+
*/
|
|
46
|
+
send(obj) {
|
|
47
|
+
// drop a consumer that has stopped reading rather than buffer unbounded
|
|
48
|
+
if ((this.stream.writableLength ?? 0) > MAX_FRAME) {
|
|
49
|
+
this.stream.destroy()
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
const body = b4a.from(JSON.stringify(toWire(obj)))
|
|
53
|
+
const head = b4a.alloc(4)
|
|
54
|
+
new DataView(head.buffer, head.byteOffset, 4).setUint32(0, body.length, true)
|
|
55
|
+
this.stream.write(b4a.concat([head, body]))
|
|
56
|
+
}
|
|
57
|
+
|
|
42
58
|
_ondata(chunk) {
|
|
43
59
|
this.buf = b4a.concat([this.buf, chunk])
|
|
44
60
|
while (this.buf.length >= 4) {
|
|
@@ -59,21 +75,4 @@ export class Framed {
|
|
|
59
75
|
this.onMessage(msg)
|
|
60
76
|
}
|
|
61
77
|
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Encode `obj` to wire form and write it as one length-prefixed frame.
|
|
65
|
-
* @param {object} obj - message to send
|
|
66
|
-
* @returns {void}
|
|
67
|
-
*/
|
|
68
|
-
send(obj) {
|
|
69
|
-
// drop a consumer that has stopped reading rather than buffer unbounded
|
|
70
|
-
if ((this.stream.writableLength ?? 0) > MAX_FRAME) {
|
|
71
|
-
this.stream.destroy()
|
|
72
|
-
return
|
|
73
|
-
}
|
|
74
|
-
const body = b4a.from(JSON.stringify(toWire(obj)))
|
|
75
|
-
const head = b4a.alloc(4)
|
|
76
|
-
new DataView(head.buffer, head.byteOffset, 4).setUint32(0, body.length, true)
|
|
77
|
-
this.stream.write(b4a.concat([head, body]))
|
|
78
|
-
}
|
|
79
78
|
}
|
package/src/redact.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
import b4a from 'b4a'
|
|
2
2
|
|
|
3
|
-
// Obvious secret-ish field names, masked by default once redaction is enabled.
|
|
4
3
|
const DEFAULT_DENY =
|
|
5
4
|
/^(seed|secret|secretkey|password|passphrase|token|private|privatekey|encryptionkey|mnemonic)$/i
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
|
-
* Build a row masker for devtools. A field is masked when its dotted path is in
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* `‹redacted:bytes(32)›` / `‹redacted:string›` — so the shape stays visible but
|
|
12
|
-
* the content is hidden. Everything else passes through untouched.
|
|
7
|
+
* Build a row masker for devtools. A field is masked when its dotted path is in `fields`,
|
|
8
|
+
* its name matches `deny` (default denylist above), or `match(key, value, path)` returns
|
|
9
|
+
* true.
|
|
13
10
|
*
|
|
14
11
|
* @param {{ fields?: string[], deny?: RegExp | false, match?: (key: string, value: any, path: string) => boolean }} [config]
|
|
15
12
|
* @returns {(ref: string, row: any) => any}
|
package/src/server.js
CHANGED
|
@@ -9,10 +9,7 @@ import { Framed } from './protocol.js'
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* Read-only inspection server over a local Duplex `stream`.
|
|
13
|
-
* handle tree rooted at `me`, exposing only MAIN refs declared in each
|
|
14
|
-
* handle's `spec.meta.refs` — local secret refs (master/keypair/
|
|
15
|
-
* handle-keypairs) are never reachable, since they live in `meta.local.refs`.
|
|
12
|
+
* Read-only inspection server over a local Duplex `stream`.
|
|
16
13
|
*
|
|
17
14
|
* @param {object} stream A streamx Duplex carrying length-prefixed JSON frames.
|
|
18
15
|
* @param {object} me The live root cero handle.
|
|
@@ -34,7 +31,7 @@ export class TapServer {
|
|
|
34
31
|
this.wire = new Framed(stream, (req) => this.onRequest(req))
|
|
35
32
|
this._cleanup = () => this._teardown()
|
|
36
33
|
stream.on('close', this._cleanup)
|
|
37
|
-
//
|
|
34
|
+
// a socket reset must not crash the host
|
|
38
35
|
stream.on('error', this._cleanup)
|
|
39
36
|
}
|
|
40
37
|
|
|
@@ -46,8 +43,7 @@ export class TapServer {
|
|
|
46
43
|
async onRequest(req) {
|
|
47
44
|
// a malformed frame (JSON null / scalar) has no method — ignore it
|
|
48
45
|
if (!req || typeof req !== 'object') return
|
|
49
|
-
//
|
|
50
|
-
// mismatch drops the connection (no probing across attempts)
|
|
46
|
+
// the first frame must carry the token, one shot
|
|
51
47
|
if (!this._authed) {
|
|
52
48
|
if (req.token !== this.opts.token) return this.stream.destroy()
|
|
53
49
|
this._authed = true
|
|
@@ -155,6 +151,15 @@ export class TapServer {
|
|
|
155
151
|
this._drop(req.cancelId)
|
|
156
152
|
}
|
|
157
153
|
|
|
154
|
+
/**
|
|
155
|
+
* Tear down all tracked streams and destroy the underlying stream.
|
|
156
|
+
* @returns {void}
|
|
157
|
+
*/
|
|
158
|
+
close() {
|
|
159
|
+
this._teardown()
|
|
160
|
+
this.stream.destroy()
|
|
161
|
+
}
|
|
162
|
+
|
|
158
163
|
_resolveRef(req) {
|
|
159
164
|
const me = this.me
|
|
160
165
|
const handle =
|
|
@@ -198,15 +203,6 @@ export class TapServer {
|
|
|
198
203
|
for (const off of this.tracked.values()) off()
|
|
199
204
|
this.tracked.clear()
|
|
200
205
|
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Tear down all tracked streams and destroy the underlying stream.
|
|
204
|
-
* @returns {void}
|
|
205
|
-
*/
|
|
206
|
-
close() {
|
|
207
|
-
this._teardown()
|
|
208
|
-
this.stream.destroy()
|
|
209
|
-
}
|
|
210
206
|
}
|
|
211
207
|
|
|
212
208
|
const METHODS = new Set(['handles', 'get', 'count', 'watch', 'events', 'stats', 'cancel'])
|
package/src/stats.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Read-only snapshot of a handle's existing p2p/storage counters.
|
|
3
|
-
* listeners, no mutation, never reads a secret. `dht.stats` and the corestore
|
|
4
|
-
* `cores` map are undocumented internals, so they are guarded — a fresh handle
|
|
5
|
-
* (pre-ready) yields zeros rather than throwing.
|
|
2
|
+
* Read-only snapshot of a handle's existing p2p/storage counters.
|
|
6
3
|
*
|
|
7
4
|
* @param {any} handle A cero root or child handle.
|
|
8
5
|
* @param {number} [at] Sample timestamp, stamped by the caller.
|
package/src/tap.js
CHANGED
|
@@ -6,10 +6,7 @@ import { redact } from './redact.js'
|
|
|
6
6
|
import { loopback } from './transport.js'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* An applied-op event fed into the ring buffer.
|
|
10
|
-
* prefix, `name` the ref name, `row` the decoded value, `writerKey` the source
|
|
11
|
-
* writer's key and `seq` a monotonic apply counter. After redaction `row` is
|
|
12
|
-
* replaced with its masked copy; the rest is spread through unchanged.
|
|
9
|
+
* An applied-op event fed into the ring buffer.
|
|
13
10
|
*
|
|
14
11
|
* @typedef {{ op: string, name: string, row: Record<string, unknown>, writerKey: Buffer, seq: number }} OpEvent
|
|
15
12
|
*/
|
|
@@ -21,11 +18,9 @@ import { loopback } from './transport.js'
|
|
|
21
18
|
*/
|
|
22
19
|
|
|
23
20
|
/**
|
|
24
|
-
* `cero.use(devtools())` tap extension. Bound to the root handle's lifecycle,
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* consumers. By default it serves a loopback TCP server on `port` (9111); pass
|
|
28
|
-
* a custom `transport` to serve it some other way. Nothing touches the app swarm.
|
|
21
|
+
* `cero.use(devtools())` tap extension. Bound to the root handle's lifecycle, it feeds
|
|
22
|
+
* every applied op into a bounded ring buffer, samples `stats(me)` on an interval, and
|
|
23
|
+
* serves a read-only inspection.
|
|
29
24
|
*
|
|
30
25
|
* @param {{ port?: number, host?: string, token?: string | false, bufferSize?: number, sampleInterval?: number, redact?: ((ref: string, row: Record<string, unknown>) => Record<string, unknown>) | { fields?: string[], match?: (key: string, value: unknown, path: string) => boolean, deny?: RegExp | false }, transport?: { accept(handler: (stream: object) => void): void } }} [opts]
|
|
31
26
|
* @returns {{ setup(me: object): () => void }}
|
|
@@ -36,8 +31,7 @@ export function devtools(opts = {}) {
|
|
|
36
31
|
const events = new Ring(opts.bufferSize ?? 1000)
|
|
37
32
|
const sampler = new Sampler(me, opts.sampleInterval ?? 1000)
|
|
38
33
|
const redactor = makeRedactor(opts.redact)
|
|
39
|
-
// any local process can dial the
|
|
40
|
-
// tap so co-tenant processes can't read app data. `token: false` opts out.
|
|
34
|
+
// any local process can dial the port, so a per-run token gates the tap
|
|
41
35
|
const token =
|
|
42
36
|
opts.token === false ? null : opts.token || crypto.randomBytes(16).toString('hex')
|
|
43
37
|
const transport =
|
package/src/transport.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import net from 'net'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Loopback TCP transport implementing the tap `{ accept(handler) }` contract
|
|
5
|
-
*
|
|
6
|
-
* Duplex, ready to pass straight to `serve()`.
|
|
4
|
+
* Loopback TCP transport implementing the tap `{ accept(handler) }` contract plus
|
|
5
|
+
* lifecycle.
|
|
7
6
|
*
|
|
8
7
|
* @param {{ port?: number, host?: string, token?: string | null }} [opts]
|
|
9
8
|
* @returns {{ accept(handler: (socket: any) => void): void, ready(): Promise<{ port: number }>, port: number | null, close(): void }}
|
|
@@ -21,12 +20,9 @@ export function loopback(opts = {}) {
|
|
|
21
20
|
})
|
|
22
21
|
let port = base
|
|
23
22
|
let tries = 0
|
|
24
|
-
// ready() resolves when listening, rejects on a terminal bind failure
|
|
25
|
-
// (so a caller awaiting it can't hang forever).
|
|
26
23
|
ready = new Promise((resolve, reject) => {
|
|
27
24
|
server.on('listening', () => resolve({ port: server.address().port }))
|
|
28
|
-
//
|
|
29
|
-
// than crashing the app — devtools is never essential.
|
|
25
|
+
// a taken port bumps to the next one
|
|
30
26
|
server.on('error', (err) => {
|
|
31
27
|
if (err.code === 'EADDRINUSE' && base && tries++ < 10) return server.listen(++port, host)
|
|
32
28
|
console.error(`[devtools] tap could not start: ${err.message}`)
|
|
@@ -61,7 +57,6 @@ export function dial(opts = {}) {
|
|
|
61
57
|
return net.connect(opts.port, opts.host ?? '127.0.0.1')
|
|
62
58
|
}
|
|
63
59
|
|
|
64
|
-
// VITE-style startup banner printed when the tap starts listening.
|
|
65
60
|
function banner(host, port, token) {
|
|
66
61
|
const e = '\x1b['
|
|
67
62
|
const r = `${e}0m`
|
package/types/connect.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { Readable } from 'streamx';
|
|
2
|
+
import { Framed } from './protocol.js';
|
|
3
|
+
export type Query = object;
|
|
4
|
+
export type Handle = Record<string, unknown>;
|
|
1
5
|
/**
|
|
2
6
|
* @typedef {object} Query Opaque query filter passed to the server.
|
|
3
7
|
*/
|
|
@@ -5,37 +9,28 @@
|
|
|
5
9
|
* @typedef {Record<string, unknown>} Handle A handle descriptor returned by the server.
|
|
6
10
|
*/
|
|
7
11
|
/**
|
|
8
|
-
* Consumer SDK for a tap server reached over a local Duplex `stream`.
|
|
9
|
-
* a session exposing read methods (`handles`/`get`/`count`) and stream methods
|
|
10
|
-
* (`watch`/`events`/`stats`). Stream methods return a streamx Readable that
|
|
11
|
-
* cancels its server-side source when destroyed.
|
|
12
|
+
* Consumer SDK for a tap server reached over a local Duplex `stream`.
|
|
12
13
|
*
|
|
13
14
|
* @param {object} stream A streamx Duplex connected to a tap server.
|
|
14
15
|
* @returns {Promise<{ handles(): Promise<Handle[]>, get(ref: string, query?: Query, handleId?: string): Promise<unknown>, count(ref: string, query?: Query, handleId?: string): Promise<number>, watch(ref: string, query?: Query, handleId?: string): import('streamx').Readable, events(): import('streamx').Readable, stats(): import('streamx').Readable, close(): void }>}
|
|
15
16
|
*/
|
|
16
|
-
export function connect(stream: object, { token }?: {}): Promise<{
|
|
17
|
+
export declare function connect(stream: object, { token }?: {}): Promise<{
|
|
17
18
|
handles(): Promise<Handle[]>;
|
|
18
19
|
get(ref: string, query?: Query, handleId?: string): Promise<unknown>;
|
|
19
20
|
count(ref: string, query?: Query, handleId?: string): Promise<number>;
|
|
20
|
-
watch(ref: string, query?: Query, handleId?: string): import(
|
|
21
|
-
events(): import(
|
|
22
|
-
stats(): import(
|
|
21
|
+
watch(ref: string, query?: Query, handleId?: string): import('streamx').Readable;
|
|
22
|
+
events(): import('streamx').Readable;
|
|
23
|
+
stats(): import('streamx').Readable;
|
|
23
24
|
close(): void;
|
|
24
25
|
}>;
|
|
25
|
-
export class Session {
|
|
26
|
-
constructor(stream: any, { token }?: {});
|
|
26
|
+
export declare class Session {
|
|
27
27
|
stream: any;
|
|
28
28
|
token: any;
|
|
29
29
|
pending: Map<any, any>;
|
|
30
30
|
streams: Map<any, any>;
|
|
31
31
|
seq: number;
|
|
32
32
|
wire: Framed;
|
|
33
|
-
|
|
34
|
-
_onmessage(msg: any): void;
|
|
35
|
-
_newId(): number;
|
|
36
|
-
_send(frame: any): void;
|
|
37
|
-
_request(method: any, fields: any): Promise<any>;
|
|
38
|
-
_stream(method: any, fields: any): Readable<import("streamx").ReadableEvents>;
|
|
33
|
+
constructor(stream: any, { token }?: {});
|
|
39
34
|
/**
|
|
40
35
|
* Lists the handles exposed by the server.
|
|
41
36
|
*
|
|
@@ -68,33 +63,29 @@ export class Session {
|
|
|
68
63
|
* @param {string} [handleId] Optional handle to scope the watch.
|
|
69
64
|
* @returns {import('streamx').Readable} Cancels its server-side source when destroyed.
|
|
70
65
|
*/
|
|
71
|
-
watch(ref: string, query?: Query, handleId?: string): import(
|
|
66
|
+
watch(ref: string, query?: Query, handleId?: string): import('streamx').Readable;
|
|
72
67
|
/**
|
|
73
68
|
* Streams server events.
|
|
74
69
|
*
|
|
75
70
|
* @returns {import('streamx').Readable} Cancels its server-side source when destroyed.
|
|
76
71
|
*/
|
|
77
|
-
events(): import(
|
|
72
|
+
events(): import('streamx').Readable;
|
|
78
73
|
/**
|
|
79
74
|
* Streams server stats.
|
|
80
75
|
*
|
|
81
76
|
* @returns {import('streamx').Readable} Cancels its server-side source when destroyed.
|
|
82
77
|
*/
|
|
83
|
-
stats(): import(
|
|
78
|
+
stats(): import('streamx').Readable;
|
|
84
79
|
/**
|
|
85
80
|
* Closes the session by ending the underlying stream.
|
|
86
81
|
*
|
|
87
82
|
* @returns {void}
|
|
88
83
|
*/
|
|
89
84
|
close(): void;
|
|
85
|
+
_onclose(): void;
|
|
86
|
+
_onmessage(msg: any): void;
|
|
87
|
+
_newId(): number;
|
|
88
|
+
_send(frame: any): void;
|
|
89
|
+
_request(method: any, fields: any): Promise<any>;
|
|
90
|
+
_stream(method: any, fields: any): Readable<import("streamx").ReadableEvents>;
|
|
90
91
|
}
|
|
91
|
-
/**
|
|
92
|
-
* Opaque query filter passed to the server.
|
|
93
|
-
*/
|
|
94
|
-
export type Query = object;
|
|
95
|
-
/**
|
|
96
|
-
* A handle descriptor returned by the server.
|
|
97
|
-
*/
|
|
98
|
-
export type Handle = Record<string, unknown>;
|
|
99
|
-
import { Framed } from './protocol.js';
|
|
100
|
-
import { Readable } from 'streamx';
|
package/types/format.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @param {Array<{ id: string, type: string | null }>} tree
|
|
5
5
|
* @returns {string}
|
|
6
6
|
*/
|
|
7
|
-
export function formatHandles(tree: Array<{
|
|
7
|
+
export declare function formatHandles(tree: Array<{
|
|
8
8
|
id: string;
|
|
9
9
|
type: string | null;
|
|
10
10
|
}>): string;
|
|
@@ -16,7 +16,7 @@ export function formatHandles(tree: Array<{
|
|
|
16
16
|
* @param {{ data: any, total?: number, size?: number }} result
|
|
17
17
|
* @returns {string}
|
|
18
18
|
*/
|
|
19
|
-
export function formatState(ref: string, result: {
|
|
19
|
+
export declare function formatState(ref: string, result: {
|
|
20
20
|
data: any;
|
|
21
21
|
total?: number;
|
|
22
22
|
size?: number;
|
|
@@ -28,7 +28,7 @@ export function formatState(ref: string, result: {
|
|
|
28
28
|
* @param {{ message?: string }} err
|
|
29
29
|
* @returns {string}
|
|
30
30
|
*/
|
|
31
|
-
export function formatError(ref: string, err: {
|
|
31
|
+
export declare function formatError(ref: string, err: {
|
|
32
32
|
message?: string;
|
|
33
33
|
}): string;
|
|
34
34
|
/**
|
|
@@ -37,7 +37,7 @@ export function formatError(ref: string, err: {
|
|
|
37
37
|
* @param {{ op: string, name: string, row: any, writerKey?: any, seq?: number }} e
|
|
38
38
|
* @returns {string}
|
|
39
39
|
*/
|
|
40
|
-
export function formatEvent(e: {
|
|
40
|
+
export declare function formatEvent(e: {
|
|
41
41
|
op: string;
|
|
42
42
|
name: string;
|
|
43
43
|
row: any;
|
|
@@ -50,7 +50,7 @@ export function formatEvent(e: {
|
|
|
50
50
|
* @param {{ network?: { connections: number, peers: number }, bee?: { local: number }, cores?: any[] }} s
|
|
51
51
|
* @returns {string}
|
|
52
52
|
*/
|
|
53
|
-
export function formatStats(s: {
|
|
53
|
+
export declare function formatStats(s: {
|
|
54
54
|
network?: {
|
|
55
55
|
connections: number;
|
|
56
56
|
peers: number;
|
package/types/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { stats } from
|
|
2
|
-
export { redact } from
|
|
3
|
-
export { devtools } from
|
|
4
|
-
export { serve } from
|
|
5
|
-
export { connect } from
|
|
6
|
-
export { loopback, dial } from
|
|
1
|
+
export { stats } from './stats.js';
|
|
2
|
+
export { redact } from './redact.js';
|
|
3
|
+
export { devtools } from './tap.js';
|
|
4
|
+
export { serve } from './server.js';
|
|
5
|
+
export { connect } from './connect.js';
|
|
6
|
+
export { loopback, dial } from './transport.js';
|
package/types/protocol.d.ts
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
* Length-prefixed JSON framing over a Duplex `stream`. Buffers incoming chunks
|
|
3
3
|
* and delivers each decoded frame to `onMessage`; `send` writes a framed frame.
|
|
4
4
|
*/
|
|
5
|
-
export class Framed {
|
|
6
|
-
constructor(stream: any, onMessage: any);
|
|
5
|
+
export declare class Framed {
|
|
7
6
|
stream: any;
|
|
8
7
|
onMessage: any;
|
|
9
8
|
buf: any;
|
|
10
|
-
|
|
9
|
+
constructor(stream: any, onMessage: any);
|
|
11
10
|
/**
|
|
12
11
|
* Encode `obj` to wire form and write it as one length-prefixed frame.
|
|
13
12
|
* @param {object} obj - message to send
|
|
14
13
|
* @returns {void}
|
|
15
14
|
*/
|
|
16
15
|
send(obj: object): void;
|
|
16
|
+
_ondata(chunk: any): void;
|
|
17
17
|
}
|
package/types/redact.d.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Build a row masker for devtools. A field is masked when its dotted path is in
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* `‹redacted:bytes(32)›` / `‹redacted:string›` — so the shape stays visible but
|
|
6
|
-
* the content is hidden. Everything else passes through untouched.
|
|
2
|
+
* Build a row masker for devtools. A field is masked when its dotted path is in `fields`,
|
|
3
|
+
* its name matches `deny` (default denylist above), or `match(key, value, path)` returns
|
|
4
|
+
* true.
|
|
7
5
|
*
|
|
8
6
|
* @param {{ fields?: string[], deny?: RegExp | false, match?: (key: string, value: any, path: string) => boolean }} [config]
|
|
9
7
|
* @returns {(ref: string, row: any) => any}
|
|
10
8
|
*/
|
|
11
|
-
export function redact(config?: {
|
|
9
|
+
export declare function redact(config?: {
|
|
12
10
|
fields?: string[];
|
|
13
11
|
deny?: RegExp | false;
|
|
14
12
|
match?: (key: string, value: any, path: string) => boolean;
|
package/types/server.d.ts
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
|
+
import { Framed } from './protocol.js';
|
|
2
|
+
export type ReqFrame = {
|
|
3
|
+
id?: string;
|
|
4
|
+
method?: string;
|
|
5
|
+
ref?: string;
|
|
6
|
+
handleId?: string;
|
|
7
|
+
query?: object;
|
|
8
|
+
cancelId?: string;
|
|
9
|
+
};
|
|
1
10
|
/**
|
|
2
11
|
* A decoded request frame. Carries the correlation `id` and `method`, plus the
|
|
3
12
|
* per-method fields a handler reads: `ref`, `handleId`, `query`, `cancelId`.
|
|
4
13
|
* @typedef {{ id?: string, method?: string, ref?: string, handleId?: string, query?: object, cancelId?: string }} ReqFrame
|
|
5
14
|
*/
|
|
6
15
|
/**
|
|
7
|
-
* Read-only inspection server over a local Duplex `stream`.
|
|
8
|
-
* handle tree rooted at `me`, exposing only MAIN refs declared in each
|
|
9
|
-
* handle's `spec.meta.refs` — local secret refs (master/keypair/
|
|
10
|
-
* handle-keypairs) are never reachable, since they live in `meta.local.refs`.
|
|
16
|
+
* Read-only inspection server over a local Duplex `stream`.
|
|
11
17
|
*
|
|
12
18
|
* @param {object} stream A streamx Duplex carrying length-prefixed JSON frames.
|
|
13
19
|
* @param {object} me The live root cero handle.
|
|
14
20
|
* @param {{ events?: { snapshot(): unknown, subscribe(fn: (e: object) => void): () => void }, stats?: { snapshot(): unknown, subscribe(fn: (s: object) => void): () => void }, redact?: (ref: string, row: Record<string, unknown>) => Record<string, unknown> }} [opts]
|
|
15
21
|
* @returns {{ close(): void }}
|
|
16
22
|
*/
|
|
17
|
-
export function serve(stream: object, me: object, opts?: {
|
|
23
|
+
export declare function serve(stream: object, me: object, opts?: {
|
|
18
24
|
events?: {
|
|
19
25
|
snapshot(): unknown;
|
|
20
26
|
subscribe(fn: (e: object) => void): () => void;
|
|
@@ -27,8 +33,7 @@ export function serve(stream: object, me: object, opts?: {
|
|
|
27
33
|
}): {
|
|
28
34
|
close(): void;
|
|
29
35
|
};
|
|
30
|
-
export class TapServer {
|
|
31
|
-
constructor(stream: any, me: any, opts?: {});
|
|
36
|
+
export declare class TapServer {
|
|
32
37
|
stream: any;
|
|
33
38
|
me: any;
|
|
34
39
|
redact: any;
|
|
@@ -37,6 +42,7 @@ export class TapServer {
|
|
|
37
42
|
_authed: boolean;
|
|
38
43
|
wire: Framed;
|
|
39
44
|
_cleanup: () => void;
|
|
45
|
+
constructor(stream: any, me: any, opts?: {});
|
|
40
46
|
/**
|
|
41
47
|
* Dispatch an inbound request frame to its named method handler.
|
|
42
48
|
* @param {ReqFrame} req A decoded request frame, expected to carry `id` and `method`.
|
|
@@ -85,28 +91,15 @@ export class TapServer {
|
|
|
85
91
|
* @returns {void}
|
|
86
92
|
*/
|
|
87
93
|
cancel(req: ReqFrame): void;
|
|
94
|
+
/**
|
|
95
|
+
* Tear down all tracked streams and destroy the underlying stream.
|
|
96
|
+
* @returns {void}
|
|
97
|
+
*/
|
|
98
|
+
close(): void;
|
|
88
99
|
_resolveRef(req: any): any;
|
|
89
100
|
_source(req: any, src: any): void;
|
|
90
101
|
_track(id: any, off: any): void;
|
|
91
102
|
_drop(id: any): void;
|
|
92
103
|
_send(obj: any): void;
|
|
93
104
|
_teardown(): void;
|
|
94
|
-
/**
|
|
95
|
-
* Tear down all tracked streams and destroy the underlying stream.
|
|
96
|
-
* @returns {void}
|
|
97
|
-
*/
|
|
98
|
-
close(): void;
|
|
99
105
|
}
|
|
100
|
-
/**
|
|
101
|
-
* A decoded request frame. Carries the correlation `id` and `method`, plus the
|
|
102
|
-
* per-method fields a handler reads: `ref`, `handleId`, `query`, `cancelId`.
|
|
103
|
-
*/
|
|
104
|
-
export type ReqFrame = {
|
|
105
|
-
id?: string;
|
|
106
|
-
method?: string;
|
|
107
|
-
ref?: string;
|
|
108
|
-
handleId?: string;
|
|
109
|
-
query?: object;
|
|
110
|
-
cancelId?: string;
|
|
111
|
-
};
|
|
112
|
-
import { Framed } from './protocol.js';
|
package/types/stats.d.ts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Read-only snapshot of a handle's existing p2p/storage counters.
|
|
3
|
-
* listeners, no mutation, never reads a secret. `dht.stats` and the corestore
|
|
4
|
-
* `cores` map are undocumented internals, so they are guarded — a fresh handle
|
|
5
|
-
* (pre-ready) yields zeros rather than throwing.
|
|
2
|
+
* Read-only snapshot of a handle's existing p2p/storage counters.
|
|
6
3
|
*
|
|
7
4
|
* @param {any} handle A cero root or child handle.
|
|
8
5
|
* @param {number} [at] Sample timestamp, stamped by the caller.
|
|
9
6
|
* @returns {{ handleId: string, network: { connections: number, peers: number, dht: any }, bee: { local: number, stats: any }, cores: Array<{ length: number, byteLength: number, peers: number }>, at: number }}
|
|
10
7
|
*/
|
|
11
|
-
export function stats(handle: any, at?: number): {
|
|
8
|
+
export declare function stats(handle: any, at?: number): {
|
|
12
9
|
handleId: string;
|
|
13
10
|
network: {
|
|
14
11
|
connections: number;
|
package/types/tap.d.ts
CHANGED
|
@@ -1,8 +1,29 @@
|
|
|
1
|
+
export type OpEvent = {
|
|
2
|
+
op: string;
|
|
3
|
+
name: string;
|
|
4
|
+
row: Record<string, unknown>;
|
|
5
|
+
writerKey: Buffer;
|
|
6
|
+
seq: number;
|
|
7
|
+
};
|
|
8
|
+
export type StatsSample = {
|
|
9
|
+
handleId: string;
|
|
10
|
+
network: {
|
|
11
|
+
connections: number;
|
|
12
|
+
peers: number;
|
|
13
|
+
dht: object | null;
|
|
14
|
+
};
|
|
15
|
+
bee: {
|
|
16
|
+
local: number;
|
|
17
|
+
};
|
|
18
|
+
cores: Array<{
|
|
19
|
+
length: number;
|
|
20
|
+
byteLength: number;
|
|
21
|
+
peers: number;
|
|
22
|
+
}>;
|
|
23
|
+
at: number;
|
|
24
|
+
};
|
|
1
25
|
/**
|
|
2
|
-
* An applied-op event fed into the ring buffer.
|
|
3
|
-
* prefix, `name` the ref name, `row` the decoded value, `writerKey` the source
|
|
4
|
-
* writer's key and `seq` a monotonic apply counter. After redaction `row` is
|
|
5
|
-
* replaced with its masked copy; the rest is spread through unchanged.
|
|
26
|
+
* An applied-op event fed into the ring buffer.
|
|
6
27
|
*
|
|
7
28
|
* @typedef {{ op: string, name: string, row: Record<string, unknown>, writerKey: Buffer, seq: number }} OpEvent
|
|
8
29
|
*/
|
|
@@ -12,16 +33,14 @@
|
|
|
12
33
|
* @typedef {{ handleId: string, network: { connections: number, peers: number, dht: object | null }, bee: { local: number }, cores: Array<{ length: number, byteLength: number, peers: number }>, at: number }} StatsSample
|
|
13
34
|
*/
|
|
14
35
|
/**
|
|
15
|
-
* `cero.use(devtools())` tap extension. Bound to the root handle's lifecycle,
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* consumers. By default it serves a loopback TCP server on `port` (9111); pass
|
|
19
|
-
* a custom `transport` to serve it some other way. Nothing touches the app swarm.
|
|
36
|
+
* `cero.use(devtools())` tap extension. Bound to the root handle's lifecycle, it feeds
|
|
37
|
+
* every applied op into a bounded ring buffer, samples `stats(me)` on an interval, and
|
|
38
|
+
* serves a read-only inspection.
|
|
20
39
|
*
|
|
21
40
|
* @param {{ port?: number, host?: string, token?: string | false, bufferSize?: number, sampleInterval?: number, redact?: ((ref: string, row: Record<string, unknown>) => Record<string, unknown>) | { fields?: string[], match?: (key: string, value: unknown, path: string) => boolean, deny?: RegExp | false }, transport?: { accept(handler: (stream: object) => void): void } }} [opts]
|
|
22
41
|
* @returns {{ setup(me: object): () => void }}
|
|
23
42
|
*/
|
|
24
|
-
export function devtools(opts?: {
|
|
43
|
+
export declare function devtools(opts?: {
|
|
25
44
|
port?: number;
|
|
26
45
|
host?: string;
|
|
27
46
|
token?: string | false;
|
|
@@ -38,36 +57,3 @@ export function devtools(opts?: {
|
|
|
38
57
|
}): {
|
|
39
58
|
setup(me: object): () => void;
|
|
40
59
|
};
|
|
41
|
-
/**
|
|
42
|
-
* An applied-op event fed into the ring buffer. `op` is the dispatch verb
|
|
43
|
-
* prefix, `name` the ref name, `row` the decoded value, `writerKey` the source
|
|
44
|
-
* writer's key and `seq` a monotonic apply counter. After redaction `row` is
|
|
45
|
-
* replaced with its masked copy; the rest is spread through unchanged.
|
|
46
|
-
*/
|
|
47
|
-
export type OpEvent = {
|
|
48
|
-
op: string;
|
|
49
|
-
name: string;
|
|
50
|
-
row: Record<string, unknown>;
|
|
51
|
-
writerKey: Buffer;
|
|
52
|
-
seq: number;
|
|
53
|
-
};
|
|
54
|
-
/**
|
|
55
|
-
* A periodic `stats(me)` sample. See `stats()` for the field meanings.
|
|
56
|
-
*/
|
|
57
|
-
export type StatsSample = {
|
|
58
|
-
handleId: string;
|
|
59
|
-
network: {
|
|
60
|
-
connections: number;
|
|
61
|
-
peers: number;
|
|
62
|
-
dht: object | null;
|
|
63
|
-
};
|
|
64
|
-
bee: {
|
|
65
|
-
local: number;
|
|
66
|
-
};
|
|
67
|
-
cores: Array<{
|
|
68
|
-
length: number;
|
|
69
|
-
byteLength: number;
|
|
70
|
-
peers: number;
|
|
71
|
-
}>;
|
|
72
|
-
at: number;
|
|
73
|
-
};
|
package/types/transport.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Loopback TCP transport implementing the tap `{ accept(handler) }` contract
|
|
3
|
-
*
|
|
4
|
-
* Duplex, ready to pass straight to `serve()`.
|
|
2
|
+
* Loopback TCP transport implementing the tap `{ accept(handler) }` contract plus
|
|
3
|
+
* lifecycle.
|
|
5
4
|
*
|
|
6
5
|
* @param {{ port?: number, host?: string, token?: string | null }} [opts]
|
|
7
6
|
* @returns {{ accept(handler: (socket: any) => void): void, ready(): Promise<{ port: number }>, port: number | null, close(): void }}
|
|
8
7
|
*/
|
|
9
|
-
export function loopback(opts?: {
|
|
8
|
+
export declare function loopback(opts?: {
|
|
10
9
|
port?: number;
|
|
11
10
|
host?: string;
|
|
12
11
|
token?: string | null;
|
|
@@ -25,7 +24,7 @@ export function loopback(opts?: {
|
|
|
25
24
|
* @param {{ port: number, host?: string }} [opts]
|
|
26
25
|
* @returns {any} A streamx-compatible Duplex socket.
|
|
27
26
|
*/
|
|
28
|
-
export function dial(opts?: {
|
|
27
|
+
export declare function dial(opts?: {
|
|
29
28
|
port: number;
|
|
30
29
|
host?: string;
|
|
31
30
|
}): any;
|