@johnhenry/browsermesh-discovery 0.0.4 → 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 +28 -8
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.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",
@@ -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 }), {