@johnhenry/browsermesh-apps 0.3.0 → 0.4.1
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 +7 -2
- package/src/cloud-storage.mjs +73 -13
- package/src/index.mjs +9 -0
- package/src/marketplace.mjs +290 -26
- package/src/peer-escrow.mjs +12 -8
- package/src/quotas.mjs +311 -12
- package/src/serverless-executor-andbox.mjs +141 -0
- package/src/serverless-fetch.mjs +70 -0
- package/src/serverless-functions.mjs +67 -0
- package/src/serverless-peer-select.mjs +69 -0
- package/src/serverless-proxy.mjs +127 -0
- package/src/serverless-router.mjs +115 -0
- package/src/serverless-sites.mjs +116 -0
- package/src/serverless-static.mjs +202 -0
- package/src/serverless-wire.mjs +87 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* serverless-wire.mjs -- shared binary-body encoding for the
|
|
3
|
+
* BrowserMesh Serverless plan's `mesh-rpc` hop
|
|
4
|
+
* (`/Users/johnhenry/.claude/plans/at-some-point-within-joyful-dove.md`,
|
|
5
|
+
* Phase 2).
|
|
6
|
+
*
|
|
7
|
+
* `mesh-rpc.mjs`'s wire protocol treats a response's `body` as a plain
|
|
8
|
+
* JSON-shaped envelope field (see that file's module doc comment: "typical
|
|
9
|
+
* callers stringify a JSON envelope first" is `peer-node.mjs`'s own
|
|
10
|
+
* guidance for `sendTo()`'s payload). A raw `Uint8Array`/`ArrayBuffer` body
|
|
11
|
+
* -- e.g. `serverless-static.mjs`'s file bytes -- is not safe to hand to
|
|
12
|
+
* that layer as-is, for the same reason `cloud-storage-backend.mjs` already
|
|
13
|
+
* base64-encodes chunk bytes before putting them in a command envelope:
|
|
14
|
+
* whatever the underlying transport does to serialize an envelope object,
|
|
15
|
+
* a raw byte array embedded in it cannot be assumed to survive intact.
|
|
16
|
+
*
|
|
17
|
+
* `encodeWireResponse()`/`decodeWireResponse()` are the matched pair used
|
|
18
|
+
* on either side of that hop -- `serverless-router.mjs` (server side, right
|
|
19
|
+
* before handing a response to `createMeshRpcService({onRequest})`) and
|
|
20
|
+
* `serverless-fetch.mjs` (client side, right after `meshRpcApi.request()`
|
|
21
|
+
* returns, before handing the result to `MeshFetchRouter`, which itself
|
|
22
|
+
* needs a real `Uint8Array`/`ArrayBuffer`/string body to build a correct
|
|
23
|
+
* `Response` -- see `sw-routing.mjs`'s `MeshFetchRouter.route()` fix in
|
|
24
|
+
* this same phase for the other half of this).
|
|
25
|
+
*
|
|
26
|
+
* @module serverless-wire
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** Internal header carrying the wire-encoding marker. Stripped before the final Response's headers reach a caller. */
|
|
30
|
+
const WIRE_ENCODING_HEADER = 'x-mesh-serverless-body-encoding'
|
|
31
|
+
|
|
32
|
+
/** @param {Uint8Array} bytes @returns {string} */
|
|
33
|
+
function toBase64(bytes) {
|
|
34
|
+
if (typeof Buffer !== 'undefined') return Buffer.from(bytes).toString('base64')
|
|
35
|
+
let bin = ''
|
|
36
|
+
for (const b of bytes) bin += String.fromCharCode(b)
|
|
37
|
+
return btoa(bin)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** @param {string} str @returns {Uint8Array} */
|
|
41
|
+
function fromBase64(str) {
|
|
42
|
+
if (typeof Buffer !== 'undefined') return new Uint8Array(Buffer.from(str, 'base64'))
|
|
43
|
+
const bin = atob(str)
|
|
44
|
+
const bytes = new Uint8Array(bin.length)
|
|
45
|
+
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i)
|
|
46
|
+
return bytes
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Server side: if `res.body` is binary, base64-encode it and mark the
|
|
51
|
+
* encoding via `WIRE_ENCODING_HEADER` so `decodeWireResponse()` on the
|
|
52
|
+
* other end knows to reverse it. String/plain-object bodies pass through
|
|
53
|
+
* unchanged -- `mesh-rpc.mjs` already handles those natively.
|
|
54
|
+
*
|
|
55
|
+
* @param {{status?: number, headers?: object, body?: *}} res
|
|
56
|
+
* @returns {{status: number, headers: object, body: *}}
|
|
57
|
+
*/
|
|
58
|
+
export function encodeWireResponse(res = {}) {
|
|
59
|
+
const status = res.status ?? 200
|
|
60
|
+
const headers = res.headers || {}
|
|
61
|
+
const body = res.body
|
|
62
|
+
if (body instanceof Uint8Array || body instanceof ArrayBuffer) {
|
|
63
|
+
const bytes = body instanceof ArrayBuffer ? new Uint8Array(body) : body
|
|
64
|
+
return { status, headers: { ...headers, [WIRE_ENCODING_HEADER]: 'base64' }, body: toBase64(bytes) }
|
|
65
|
+
}
|
|
66
|
+
return { status, headers, body }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Client side: reverses `encodeWireResponse()`. Always strips
|
|
71
|
+
* `WIRE_ENCODING_HEADER` from the returned headers, whether or not this
|
|
72
|
+
* particular response was binary-encoded, so it never leaks into a real
|
|
73
|
+
* `Response`'s headers.
|
|
74
|
+
*
|
|
75
|
+
* @param {{status?: number, headers?: object, body?: *}} res
|
|
76
|
+
* @returns {{status: number, headers: object, body: *}}
|
|
77
|
+
*/
|
|
78
|
+
export function decodeWireResponse(res = {}) {
|
|
79
|
+
const status = res.status ?? 200
|
|
80
|
+
const headers = { ...(res.headers || {}) }
|
|
81
|
+
const isBase64 = headers[WIRE_ENCODING_HEADER] === 'base64'
|
|
82
|
+
delete headers[WIRE_ENCODING_HEADER]
|
|
83
|
+
const body = isBase64 && typeof res.body === 'string' ? fromBase64(res.body) : res.body
|
|
84
|
+
return { status, headers, body }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { WIRE_ENCODING_HEADER }
|