@johnhenry/browsermesh-pod 0.1.0 → 0.1.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 +2 -2
- package/src/ws-transport.mjs +25 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnhenry/browsermesh-pod",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Pod base class for browser execution contexts with Ed25519 identity, BroadcastChannel discovery, and peer messaging",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.mjs",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"@johnhenry/browsermesh-primitives": ">=0.2.0 <1.0.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@johnhenry/browsermesh-primitives": "^0.
|
|
41
|
+
"@johnhenry/browsermesh-primitives": "^0.3.0"
|
|
42
42
|
},
|
|
43
43
|
"engines": {
|
|
44
44
|
"node": ">=26.0.0"
|
package/src/ws-transport.mjs
CHANGED
|
@@ -229,8 +229,7 @@ export class WebSocketTransport {
|
|
|
229
229
|
const onOpen = () => {
|
|
230
230
|
this.#send(ws, { type: 'register', podId: this.#podId })
|
|
231
231
|
}
|
|
232
|
-
const
|
|
233
|
-
const data = this.#parse(event)
|
|
232
|
+
const handle = (data) => {
|
|
234
233
|
if (!data) return
|
|
235
234
|
if (isPrimary) this.#handlePrimaryMessage(data)
|
|
236
235
|
else this.#handleSignalingMessage(data)
|
|
@@ -241,6 +240,29 @@ export class WebSocketTransport {
|
|
|
241
240
|
finishReject(new Error(data.message || `WebSocketTransport: ${kind} registration error`))
|
|
242
241
|
}
|
|
243
242
|
}
|
|
243
|
+
// A Blob frame can only be read asynchronously. Once one is in flight,
|
|
244
|
+
// every later frame on this socket queues behind it so frames are still
|
|
245
|
+
// handled in arrival order; with no Blob in flight nothing changes and
|
|
246
|
+
// frames are handled synchronously.
|
|
247
|
+
let blobQueue = null
|
|
248
|
+
const onMessage = (event) => {
|
|
249
|
+
if (typeof Blob !== 'undefined' && event.data instanceof Blob) {
|
|
250
|
+
const prior = blobQueue ?? Promise.resolve()
|
|
251
|
+
const mine = prior
|
|
252
|
+
.then(() => event.data.arrayBuffer())
|
|
253
|
+
.then((buf) => handle(this.#parse({ data: buf })), () => {})
|
|
254
|
+
blobQueue = mine
|
|
255
|
+
mine.then(() => { if (blobQueue === mine) blobQueue = null })
|
|
256
|
+
return
|
|
257
|
+
}
|
|
258
|
+
if (blobQueue) {
|
|
259
|
+
const mine = blobQueue.then(() => handle(this.#parse(event)))
|
|
260
|
+
blobQueue = mine
|
|
261
|
+
mine.then(() => { if (blobQueue === mine) blobQueue = null })
|
|
262
|
+
return
|
|
263
|
+
}
|
|
264
|
+
handle(this.#parse(event))
|
|
265
|
+
}
|
|
244
266
|
const onClose = () => {
|
|
245
267
|
if (isPrimary) { this.#ready = false; this.#ws = null }
|
|
246
268
|
else { this.#sigWs = null }
|
|
@@ -271,7 +293,7 @@ export class WebSocketTransport {
|
|
|
271
293
|
if (typeof d === 'string') raw = d
|
|
272
294
|
else if (d instanceof ArrayBuffer) raw = new TextDecoder().decode(d)
|
|
273
295
|
else if (ArrayBuffer.isView(d)) raw = new TextDecoder().decode(d)
|
|
274
|
-
else return null //
|
|
296
|
+
else return null // anything else (Blobs are read by the caller first): not a frame we can read
|
|
275
297
|
return JSON.parse(raw)
|
|
276
298
|
} catch {
|
|
277
299
|
return null
|