@johnhenry/browsermesh-discovery 0.0.4 → 0.0.6

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 CHANGED
@@ -1,7 +1,18 @@
1
1
  # browsermesh-discovery
2
2
 
3
+ [![npm version](https://img.shields.io/npm/v/%40johnhenry%2Fbrowsermesh-discovery.svg)](https://www.npmjs.com/package/@johnhenry/browsermesh-discovery)
4
+ [![license](https://img.shields.io/npm/l/%40johnhenry%2Fbrowsermesh-discovery.svg)](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,7 +38,7 @@ 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
 
33
44
  ## License
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnhenry/browsermesh-discovery",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
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.1 <1.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": ">=24.0.0"
29
+ "node": ">=26.0.0"
30
30
  }
31
31
  }
@@ -9,12 +9,18 @@
9
9
  * `browsermesh-fetch-websocket.md` plan), which uses it directly to power
10
10
  * `createBrowserMeshFetch()` -- a directly-callable `fetch(url, init)`-shaped
11
11
  * function bound to a live `mesh-rpc` service (`mesh-rpc.mjs`, Phase 1).
12
- * `MeshFetchRouter` in THIS file remains the separate Service-Worker
13
- * `fetch`-event-interceptor shape (`Request` in, `Response|null` out) and is
14
- * not itself used by `mesh-fetch.mjs` (its `Request`-in shape doesn't fit a
15
- * direct `browserMeshFetch(url, init)` call; see that file's module doc
16
- * comment) -- it has no wired caller of its own yet, only this module's own
17
- * `onRpc` gap being unblocked at the transport layer.
12
+ * `MeshFetchRouter` in THIS file remained the separate Service-Worker
13
+ * `fetch`-event-interceptor shape (`Request` in, `Response|null` out), not
14
+ * itself used by `mesh-fetch.mjs` (its `Request`-in shape doesn't fit a
15
+ * direct `browserMeshFetch(url, init)` call) -- it had no wired caller of
16
+ * its own until `browsermesh-apps`' `serverless-fetch.mjs` (Phase 2 of
17
+ * `/Users/johnhenry/.claude/plans/at-some-point-within-joyful-dove.md`),
18
+ * which resolves the `podId` token to a site name (falling back to a
19
+ * literal podId) before delegating to a `mesh-rpc` request. That phase also
20
+ * fixed `route()`'s Response-building below to pass a binary
21
+ * (`Uint8Array`/`ArrayBuffer`) `result.body` straight to `new Response()`
22
+ * instead of `JSON.stringify`-ing it -- a bug this file's lack of any real
23
+ * caller had left latent.
18
24
  *
19
25
  * No browser-only imports at module level.
20
26
  *
@@ -124,8 +130,22 @@ export class MeshFetchRouter {
124
130
  try {
125
131
  const result = await this.#onRpc({ podId, method, path, headers, body })
126
132
  const status = result.status ?? 200
127
- const resHeaders = result.headers ?? { 'content-type': 'application/json' }
128
- const resBody = typeof result.body === 'string' ? result.body : JSON.stringify(result.body)
133
+ let resHeaders = result.headers
134
+ let resBody
135
+ if (result.body instanceof Uint8Array || result.body instanceof ArrayBuffer) {
136
+ // Binary body (e.g. a static asset's raw bytes, from the
137
+ // BrowserMesh Serverless plan's serverless-fetch.mjs) -- Response
138
+ // accepts BufferSource directly; JSON.stringify-ing it here would
139
+ // silently mangle it into `{"0":.., "1":..}` instead of real bytes.
140
+ resBody = result.body
141
+ resHeaders = resHeaders ?? {}
142
+ } else if (typeof result.body === 'string') {
143
+ resBody = result.body
144
+ resHeaders = resHeaders ?? { 'content-type': 'application/json' }
145
+ } else {
146
+ resBody = JSON.stringify(result.body)
147
+ resHeaders = resHeaders ?? { 'content-type': 'application/json' }
148
+ }
129
149
  return new Response(resBody, { status, headers: resHeaders })
130
150
  } catch (err) {
131
151
  return new Response(JSON.stringify({ error: err.message }), {