@johnhenry/browsermesh-discovery 0.0.3 → 0.0.5

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/sw-routing.mjs +34 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnhenry/browsermesh-discovery",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "DHT, peer discovery, naming, swarm coordination, and stealth networking for BrowserMesh",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
@@ -1,10 +1,27 @@
1
1
  /**
2
- // STATUS: INTEGRATED — wired into ClawserPod lifecycle, proven via E2E testing
3
2
  * clawser-mesh-sw-routing.js -- ServiceWorker mesh:// fetch routing.
4
3
  *
5
4
  * Intercepts mesh:// protocol requests and *.mesh.local hostnames
6
5
  * in the Service Worker fetch handler, delegating to mesh RPC.
7
6
  *
7
+ * STATUS: `parseMeshRequest()` is wired to a real transport as of
8
+ * `browsermesh-apps`' `mesh-fetch.mjs` (Phase 2 of the
9
+ * `browsermesh-fetch-websocket.md` plan), which uses it directly to power
10
+ * `createBrowserMeshFetch()` -- a directly-callable `fetch(url, init)`-shaped
11
+ * function bound to a live `mesh-rpc` service (`mesh-rpc.mjs`, Phase 1).
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.
24
+ *
8
25
  * No browser-only imports at module level.
9
26
  *
10
27
  * Run tests:
@@ -113,8 +130,22 @@ export class MeshFetchRouter {
113
130
  try {
114
131
  const result = await this.#onRpc({ podId, method, path, headers, body })
115
132
  const status = result.status ?? 200
116
- const resHeaders = result.headers ?? { 'content-type': 'application/json' }
117
- 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
+ }
118
149
  return new Response(resBody, { status, headers: resHeaders })
119
150
  } catch (err) {
120
151
  return new Response(JSON.stringify({ error: err.message }), {