@johnhenry/browsermesh-discovery 0.0.5 → 0.1.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/README.md +28 -1
- package/package.json +3 -3
- package/src/stealth.mjs +73 -21
package/README.md
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
# browsermesh-discovery
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@johnhenry/browsermesh-discovery)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
3
6
|
DHT, peer discovery, naming, swarm coordination, and stealth networking for BrowserMesh.
|
|
4
7
|
|
|
8
|
+
## Why this exists
|
|
9
|
+
|
|
10
|
+
Identity (`browsermesh-primitives`/`browsermesh-core`) answers "who is this peer," but a mesh also needs to answer "which peers exist, and how do I find one I haven't talked to yet." That's a genuinely different problem -- Kademlia-style routing tables, gossip, service directories, name resolution, swarm membership/leader-election -- and bundling it into `browsermesh-core` would force every identity-only consumer to pull in DHT/swarm machinery it doesn't need. `browsermesh-discovery` exists as that separate layer: `DhtNode`/`RoutingTable`/`GossipProtocol` for peer-to-peer lookup, `DiscoveryManager`/`DiscoveryStrategy` for pluggable discovery mechanisms (including DHT-backed bootstrap), `MeshNameResolver` for human-readable naming, and `SwarmCoordinator`/`SwimMembership` for group membership and coordination. It depends only on `browsermesh-primitives`, for the shared `MESH_TYPE` message-type constants its DHT and swarm messages are tagged with.
|
|
11
|
+
|
|
12
|
+
## Used by
|
|
13
|
+
|
|
14
|
+
`@johnhenry/browsermesh-apps` treats this package as an optional peer: `mesh-bootstrap.mjs` lazily `import()`s it to wire a real DHT-backed discovery strategy into `createMeshNode()` (`mesh-dht.mjs`), and `serverless-fetch.mjs`'s `MeshFetchRouter` reuses this package's `parseMeshRequest()` directly to route `mesh://` URLs. Neither is a hard requirement -- `createMeshNode()` works with no discovery strategy at all, or with a caller-supplied one.
|
|
15
|
+
|
|
5
16
|
## Provenance
|
|
6
17
|
|
|
7
18
|
Extracted from the private `clawser` monorepo (previously `packages/browsermesh-discovery`), where it was manually published to npm, unscoped, as `browsermesh-discovery@0.1.0` (2026-07-17) with no CI ever automating that publish. This is its first release as part of the `@johnhenry/browsermesh` monorepo; the version restarts at `0.0.0` per family convention.
|
|
@@ -27,9 +38,25 @@ npm install @johnhenry/browsermesh-discovery @johnhenry/browsermesh-primitives
|
|
|
27
38
|
## Usage
|
|
28
39
|
|
|
29
40
|
```js
|
|
30
|
-
import { DhtNode, DiscoveryManager, SwarmCoordinator } from 'browsermesh-discovery';
|
|
41
|
+
import { DhtNode, DiscoveryManager, SwarmCoordinator } from '@johnhenry/browsermesh-discovery';
|
|
31
42
|
```
|
|
32
43
|
|
|
44
|
+
## `StealthAgent` is sharding, not encryption
|
|
45
|
+
|
|
46
|
+
`StealthAgent.hide(stateString)` / `reconstitute()` split a state string into
|
|
47
|
+
`threshold` plaintext data shards plus XOR parity shards and store them in the
|
|
48
|
+
DHT under `stealth:<agentId>:shard:<i>`. It improves availability, not secrecy:
|
|
49
|
+
|
|
50
|
+
- the shards contain the state verbatim (data shards) or its XOR (parity);
|
|
51
|
+
anyone who can read the DHT entries can read the state;
|
|
52
|
+
- every parity shard is the same XOR of all data chunks, so the state survives
|
|
53
|
+
the loss of **one** data shard, not `total - threshold` of them;
|
|
54
|
+
- the checksum detects accidental corruption only, not tampering;
|
|
55
|
+
- the keys name the agent they hold state for.
|
|
56
|
+
|
|
57
|
+
If the state is sensitive, encrypt it before `hide()` and decrypt after
|
|
58
|
+
`reconstitute()`; this package does not manage keys.
|
|
59
|
+
|
|
33
60
|
## License
|
|
34
61
|
|
|
35
62
|
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnhenry/browsermesh-discovery",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "DHT, peer discovery, naming, swarm coordination, and stealth networking for BrowserMesh",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.mjs",
|
|
@@ -19,13 +19,13 @@
|
|
|
19
19
|
"directory": "packages/browsermesh-discovery"
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@johnhenry/browsermesh-primitives": ">=0.0
|
|
22
|
+
"@johnhenry/browsermesh-primitives": ">=0.2.0 <1.0.0"
|
|
23
23
|
},
|
|
24
24
|
"scripts": {
|
|
25
25
|
"test": "node --import ./test/_setup-globals.mjs --test test/*.test.mjs"
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://opensource.johnhenry.me/browsermesh/",
|
|
28
28
|
"engines": {
|
|
29
|
-
"node": ">=
|
|
29
|
+
"node": ">=26.0.0"
|
|
30
30
|
}
|
|
31
31
|
}
|
package/src/stealth.mjs
CHANGED
|
@@ -1,10 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
2
|
// STATUS: INTEGRATED — wired into ClawserPod lifecycle, proven via E2E testing
|
|
3
|
-
* clawser-mesh-stealth.js --
|
|
3
|
+
* clawser-mesh-stealth.js -- Agent state sharding across the DHT.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* NOT ENCRYPTED, NOT ANONYMOUS. This module splits a state string into
|
|
6
|
+
* `threshold` plaintext data shards plus `total - threshold` XOR parity shards
|
|
7
|
+
* and stores them in the DHT under keys derived from the agent id
|
|
8
|
+
* (`stealth:<agentId>:shard:<i>`). It gives availability, not secrecy:
|
|
9
|
+
*
|
|
10
|
+
* - shard data is the state itself (data shards) or the XOR of it (parity);
|
|
11
|
+
* anyone who can read the DHT entries can read the state;
|
|
12
|
+
* - the parity shards are all identical (the XOR of every data chunk), so
|
|
13
|
+
* the scheme survives the loss of exactly ONE data shard, not `total -
|
|
14
|
+
* threshold`;
|
|
15
|
+
* - the shard checksum is a sum of char codes: it catches corruption, not
|
|
16
|
+
* tampering;
|
|
17
|
+
* - keys name the agent they belong to.
|
|
18
|
+
*
|
|
19
|
+
* If the state must be secret, encrypt it yourself before calling `hide()`
|
|
20
|
+
* (and decrypt after `reconstitute()`); this module does not manage keys.
|
|
21
|
+
*
|
|
22
|
+
* StateShard represents one fragment.
|
|
23
|
+
* ShardDistributor scatters shards across DHT nodes.
|
|
24
|
+
* ShardCollector retrieves and reconstructs state from DHT.
|
|
25
|
+
* StealthAgent orchestrates the hide/reconstitute lifecycle.
|
|
8
26
|
*
|
|
9
27
|
* StateShard represents an erasure-coded fragment.
|
|
10
28
|
* ShardDistributor scatters shards across DHT nodes.
|
|
@@ -17,8 +35,6 @@
|
|
|
17
35
|
* node --import ./web/test/_setup-globals.mjs --test web/test/clawser-mesh-dht.test.mjs
|
|
18
36
|
*/
|
|
19
37
|
|
|
20
|
-
import { STEALTH_SHARD } from './dht.mjs'
|
|
21
|
-
|
|
22
38
|
// ---------------------------------------------------------------------------
|
|
23
39
|
// Checksum Helper
|
|
24
40
|
// ---------------------------------------------------------------------------
|
|
@@ -41,7 +57,8 @@ function simpleChecksum(data) {
|
|
|
41
57
|
// ---------------------------------------------------------------------------
|
|
42
58
|
|
|
43
59
|
/**
|
|
44
|
-
*
|
|
60
|
+
* One fragment of sharded agent state: a plaintext slice of the state (data
|
|
61
|
+
* shard) or the XOR of all slices (parity shard). Not encrypted.
|
|
45
62
|
*/
|
|
46
63
|
export class StateShard {
|
|
47
64
|
/** @type {string} */
|
|
@@ -305,7 +322,13 @@ export class ShardCollector {
|
|
|
305
322
|
}
|
|
306
323
|
|
|
307
324
|
/**
|
|
308
|
-
* Reconstruct original state from
|
|
325
|
+
* Reconstruct the original state from at least `threshold` valid shards.
|
|
326
|
+
*
|
|
327
|
+
* All `threshold` data shards are used when present. If exactly one data
|
|
328
|
+
* shard is missing and a parity shard is available, the missing chunk is
|
|
329
|
+
* recovered as parity XOR the other chunks. Two or more missing data
|
|
330
|
+
* shards cannot be recovered (every parity shard carries the same XOR).
|
|
331
|
+
*
|
|
309
332
|
* @param {StateShard[]} shards - At least `threshold` valid shards
|
|
310
333
|
* @returns {string} Reconstructed state blob
|
|
311
334
|
*/
|
|
@@ -321,20 +344,42 @@ export class ShardCollector {
|
|
|
321
344
|
return idxA - idxB
|
|
322
345
|
})
|
|
323
346
|
|
|
324
|
-
//
|
|
325
|
-
const
|
|
347
|
+
// Data shards by index; parity shards (index >= threshold) separately
|
|
348
|
+
const byIndex = new Map()
|
|
349
|
+
let parity = null
|
|
350
|
+
for (const s of sorted) {
|
|
326
351
|
const idx = parseInt(s.shardId.split(':').pop(), 10)
|
|
327
|
-
|
|
328
|
-
|
|
352
|
+
if (idx < this.#threshold) byIndex.set(idx, s.data)
|
|
353
|
+
else if (!parity) parity = s.data
|
|
354
|
+
}
|
|
329
355
|
|
|
330
|
-
|
|
331
|
-
|
|
356
|
+
const missing = []
|
|
357
|
+
for (let i = 0; i < this.#threshold; i++) {
|
|
358
|
+
if (!byIndex.has(i)) missing.push(i)
|
|
332
359
|
}
|
|
333
360
|
|
|
334
|
-
|
|
361
|
+
if (missing.length > 1 || (missing.length === 1 && parity === null)) {
|
|
362
|
+
throw new Error(
|
|
363
|
+
`Not enough data shards for reconstruction: need ${this.#threshold}, got ${this.#threshold - missing.length}` +
|
|
364
|
+
' (parity can recover at most one missing data shard)',
|
|
365
|
+
)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (missing.length === 1) {
|
|
369
|
+
// parity = XOR of every data chunk, so the missing chunk is parity XOR the rest
|
|
370
|
+
let recovered = ''
|
|
371
|
+
for (let c = 0; c < parity.length; c++) {
|
|
372
|
+
let xorVal = parity.charCodeAt(c)
|
|
373
|
+
for (const data of byIndex.values()) xorVal ^= data.charCodeAt(c)
|
|
374
|
+
recovered += String.fromCharCode(xorVal)
|
|
375
|
+
}
|
|
376
|
+
byIndex.set(missing[0], recovered)
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Concatenate data chunks in order
|
|
335
380
|
let result = ''
|
|
336
|
-
for (
|
|
337
|
-
result +=
|
|
381
|
+
for (let i = 0; i < this.#threshold; i++) {
|
|
382
|
+
result += byIndex.get(i)
|
|
338
383
|
}
|
|
339
384
|
|
|
340
385
|
// Remove null padding
|
|
@@ -358,9 +403,15 @@ export class ShardCollector {
|
|
|
358
403
|
// ---------------------------------------------------------------------------
|
|
359
404
|
|
|
360
405
|
/**
|
|
361
|
-
* Orchestrates the hide/reconstitute lifecycle for
|
|
362
|
-
*
|
|
363
|
-
*
|
|
406
|
+
* Orchestrates the hide/reconstitute lifecycle for an agent's state.
|
|
407
|
+
*
|
|
408
|
+
* State is sharded across the DHT as plaintext data shards plus XOR parity
|
|
409
|
+
* (see the module header): it can be reconstructed from `threshold` shards
|
|
410
|
+
* (so one lost data shard is tolerated) but it is NOT encrypted and the shard
|
|
411
|
+
* keys name the agent. Encrypt the state before `hide()` if it is sensitive.
|
|
412
|
+
*
|
|
413
|
+
* Methods are `hide(stateBlob)`, `reconstitute()`, `isViable()` and
|
|
414
|
+
* `getManifest()`; they are synchronous.
|
|
364
415
|
*/
|
|
365
416
|
export class StealthAgent {
|
|
366
417
|
/** @type {string} */
|
|
@@ -404,7 +455,8 @@ export class StealthAgent {
|
|
|
404
455
|
}
|
|
405
456
|
|
|
406
457
|
/**
|
|
407
|
-
*
|
|
458
|
+
* Distribute the agent's state as shards across the DHT. This is sharding,
|
|
459
|
+
* not concealment: the shards are plaintext slices of `stateBlob`.
|
|
408
460
|
* @param {string} stateBlob - State data to hide
|
|
409
461
|
* @returns {object} Manifest with shard keys and metadata
|
|
410
462
|
*/
|