@cero-base/tools 2.12.0 → 3.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cero-base/tools",
3
- "version": "2.12.0",
3
+ "version": "3.0.1",
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,
@@ -76,7 +76,7 @@
76
76
  "test:node": "find test -name '*.test.js' | sort | xargs -P1 -n1 brittle-node"
77
77
  },
78
78
  "dependencies": {
79
- "@cero-base/cero": "^2.12.0",
79
+ "@cero-base/cero": "^3.0.1",
80
80
  "b4a": "^1.9.0",
81
81
  "bare-crypto": "^1.15.4",
82
82
  "bare-net": "^2.3.3",
package/src/connect.js CHANGED
@@ -37,6 +37,7 @@ export class Session {
37
37
  this.wire = new Framed(stream, (msg) => this._onmessage(msg))
38
38
  stream.on('error', () => {}) // a transport error shouldn't crash the consumer
39
39
  stream.on('close', () => this._onclose())
40
+ if (this.token) this.wire.send({ token: this.token })
40
41
  }
41
42
 
42
43
  /**
@@ -137,7 +138,7 @@ export class Session {
137
138
 
138
139
  /** @private */
139
140
  _send(frame) {
140
- this.wire.send(this.token ? { token: this.token, ...frame } : frame)
141
+ this.wire.send(frame)
141
142
  }
142
143
 
143
144
  /** @private */
package/src/protocol.js CHANGED
@@ -2,7 +2,7 @@ import b4a from 'b4a'
2
2
 
3
3
  const BYTES = '$b'
4
4
  // cap a frame and the backlog so a bad length prefix cannot grow memory without bound
5
- const MAX_FRAME = 16 * 1024 * 1024
5
+ export const MAX_FRAME = 16 * 1024 * 1024
6
6
 
7
7
  function toWire(v) {
8
8
  if (b4a.isBuffer(v)) return { [BYTES]: b4a.toHex(v) }
@@ -38,6 +38,7 @@ export class Framed {
38
38
  constructor(stream, onMessage) {
39
39
  this.stream = stream
40
40
  this.onMessage = onMessage
41
+ this.max = MAX_FRAME
41
42
  this.buf = b4a.alloc(0)
42
43
  stream.on('data', (chunk) => this._ondata(chunk))
43
44
  }
@@ -64,7 +65,7 @@ export class Framed {
64
65
  this.buf = b4a.concat([this.buf, chunk])
65
66
  while (this.buf.length >= 4) {
66
67
  const len = new DataView(this.buf.buffer, this.buf.byteOffset, 4).getUint32(0, true)
67
- if (len > MAX_FRAME) {
68
+ if (len > this.max) {
68
69
  this.stream.destroy() // oversized/garbage prefix — drop the connection
69
70
  return
70
71
  }
package/src/redact.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import b4a from 'b4a'
2
2
 
3
3
  const DEFAULT_DENY =
4
- /^(seed|secret|secretkey|password|passphrase|token|private|privatekey|encryptionkey|mnemonic)$/i
4
+ /^(seed|secret|secretkey|password|passphrase|token|private|privatekey|encryptionkey|mnemonic|epochs)$/i
5
5
 
6
6
  /**
7
7
  * Build a row masker for devtools. A field is masked when its dotted path is in `fields`,
package/src/server.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { get, watch } from '@cero-base/cero'
2
2
 
3
- import { Framed } from './protocol.js'
3
+ import { Framed, MAX_FRAME } from './protocol.js'
4
4
 
5
5
  /**
6
6
  * A decoded request frame. Carries the correlation `id` and `method`, plus the
@@ -40,6 +40,8 @@ export class TapServer {
40
40
  /** @private */
41
41
  this._authed = !opts.token
42
42
  this.wire = new Framed(stream, (req) => this.onRequest(req))
43
+ // until the token checks out, a frame only needs room for the token
44
+ if (opts.token) this.wire.max = 64 * 1024
43
45
  /** @private */
44
46
  this._cleanup = () => this._teardown()
45
47
  stream.on('close', this._cleanup)
@@ -59,6 +61,8 @@ export class TapServer {
59
61
  if (!this._authed) {
60
62
  if (req.token !== this.opts.token) return this.stream.destroy()
61
63
  this._authed = true
64
+ this.wire.max = MAX_FRAME
65
+ if (!req.method) return
62
66
  }
63
67
  const handler = this[req.method]
64
68
  if (!handler || !METHODS.has(req.method)) {
@@ -98,11 +102,7 @@ export class TapServer {
98
102
  */
99
103
  async get(req) {
100
104
  const ref = this._resolveRef(req)
101
- const r = await get(ref, req.query)
102
- const data = Array.isArray(r.data)
103
- ? r.data.map((row) => this.redact(req.ref, row))
104
- : r.data && this.redact(req.ref, r.data)
105
- this._send({ id: req.id, ok: true, data: { ...r, data } })
105
+ this._send({ id: req.id, ok: true, data: this._mask(req.ref, await get(ref, req.query)) })
106
106
  }
107
107
 
108
108
  /**
@@ -114,10 +114,7 @@ export class TapServer {
114
114
  const ref = this._resolveRef(req)
115
115
  const s = watch(ref, req.query)
116
116
  this._track(req.id, () => s.destroy())
117
- s.on('data', (d) => {
118
- const data = Array.isArray(d.data) ? d.data.map((row) => this.redact(req.ref, row)) : d.data
119
- this._send({ id: req.id, frame: { ...d, data } })
120
- })
117
+ s.on('data', (d) => this._send({ id: req.id, frame: this._mask(req.ref, d) }))
121
118
  s.on('end', () => this._send({ id: req.id, end: true }))
122
119
  // a watch-stream error must not crash the host — end the client stream cleanly
123
120
  s.on('error', () => {
@@ -174,6 +171,15 @@ export class TapServer {
174
171
  return handle[req.ref]
175
172
  }
176
173
 
174
+ /** @private */
175
+ _mask(ref, r) {
176
+ const row = (v) => v && this.redact(ref, v)
177
+ const data = Array.isArray(r.data) ? r.data.map(row) : row(r.data)
178
+ if (!r.changes) return { ...r, data }
179
+ const changes = r.changes.map((c) => ({ ...c, prev: row(c.prev), next: row(c.next) }))
180
+ return { ...r, data, changes }
181
+ }
182
+
177
183
  /** @private */
178
184
  _source(req, src) {
179
185
  if (!src) {
package/src/stats.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  * Read-only snapshot of a handle's existing p2p/storage counters.
3
+ * `cores` lists every open core in the handle's corestore, which a child shares with its root.
3
4
  *
4
5
  * @param {import('@cero-base/cero').Handle} handle A cero root or child handle.
5
6
  * @param {number} [at] Sample timestamp, stamped by the caller.
@@ -13,11 +14,12 @@ export function stats(handle, at = 0) {
13
14
  try {
14
15
  const map = handle.store?.store?.cores
15
16
  if (map) {
16
- for (const core of map.values()) {
17
+ // corestore tracks its open cores, not sessions: the counters sit on their state
18
+ for (const core of map) {
17
19
  cores.push({
18
- length: core.length ?? 0,
19
- byteLength: core.byteLength ?? 0,
20
- peers: core.peers?.length ?? 0
20
+ length: core.state?.length ?? 0,
21
+ byteLength: core.state?.byteLength ?? 0,
22
+ peers: core.replicator?.peers?.length ?? 0
21
23
  })
22
24
  }
23
25
  }
@@ -1,3 +1,4 @@
1
+ export declare const MAX_FRAME: number;
1
2
  /**
2
3
  * Length-prefixed JSON framing over a Duplex `stream`. Buffers incoming chunks
3
4
  * and delivers each decoded frame to `onMessage`; `send` writes a framed frame.
@@ -5,6 +6,7 @@
5
6
  export declare class Framed {
6
7
  stream: import("streamx").Duplex<import("streamx").DuplexEvents>;
7
8
  onMessage: (msg: unknown) => void;
9
+ max: number;
8
10
  buf: Uint8Array<ArrayBufferLike>;
9
11
  /**
10
12
  * @param {import('streamx').Duplex} stream
package/types/server.d.ts CHANGED
@@ -107,6 +107,8 @@ export declare class TapServer {
107
107
  /** @private */
108
108
  private _resolveRef;
109
109
  /** @private */
110
+ private _mask;
111
+ /** @private */
110
112
  private _source;
111
113
  /** @private */
112
114
  private _track;
package/types/stats.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  * Read-only snapshot of a handle's existing p2p/storage counters.
3
+ * `cores` lists every open core in the handle's corestore, which a child shares with its root.
3
4
  *
4
5
  * @param {import('@cero-base/cero').Handle} handle A cero root or child handle.
5
6
  * @param {number} [at] Sample timestamp, stamped by the caller.