@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.
- package/package.json +1 -1
- package/src/sw-routing.mjs +28 -8
package/package.json
CHANGED
package/src/sw-routing.mjs
CHANGED
|
@@ -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
|
|
13
|
-
* `fetch`-event-interceptor shape (`Request` in, `Response|null` out)
|
|
14
|
-
*
|
|
15
|
-
* direct `browserMeshFetch(url, init)` call
|
|
16
|
-
*
|
|
17
|
-
*
|
|
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
|
-
|
|
128
|
-
|
|
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 }), {
|