castle-web-sdk 0.4.16 → 0.4.17
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/dist/runtime.js +56 -1
- package/package.json +1 -1
package/dist/runtime.js
CHANGED
|
@@ -441,6 +441,8 @@ function initHostCapture() {
|
|
|
441
441
|
// than starting a second capture alongside the one already running.
|
|
442
442
|
const inFlight = new Set();
|
|
443
443
|
window.addEventListener("message", (event) => {
|
|
444
|
+
if (!isTrustedShellMessage(event))
|
|
445
|
+
return;
|
|
444
446
|
const data = event.data;
|
|
445
447
|
if (!data || data.type !== HOST_CAPTURE_REQUEST)
|
|
446
448
|
return;
|
|
@@ -454,11 +456,17 @@ function initHostCapture() {
|
|
|
454
456
|
if (requestId)
|
|
455
457
|
inFlight.delete(requestId);
|
|
456
458
|
const target = event.source ?? window.parent;
|
|
457
|
-
target.postMessage({ type: HOST_CAPTURE_RESULT, requestId, dataUrl, ok: !!dataUrl },
|
|
459
|
+
target.postMessage({ type: HOST_CAPTURE_RESULT, requestId, dataUrl, ok: !!dataUrl }, event.origin);
|
|
458
460
|
};
|
|
459
461
|
void captureScreenshot().then(reply, () => reply(null));
|
|
460
462
|
});
|
|
461
463
|
}
|
|
464
|
+
function isTrustedShellMessage(event) {
|
|
465
|
+
const expectedOrigin = window
|
|
466
|
+
.__castleShellOrigin;
|
|
467
|
+
return (event.source === window.parent &&
|
|
468
|
+
(expectedOrigin ? event.origin === expectedOrigin : event.origin !== "null"));
|
|
469
|
+
}
|
|
462
470
|
// ─── local dev-server socket ───────────────────────────────────────────────────
|
|
463
471
|
// Reconnect policy mirrors the shell's terminal client: back off while the tab
|
|
464
472
|
// is visible, stay quiet while it is hidden or offline, and force a FRESH socket
|
|
@@ -490,6 +498,7 @@ let localServeSeen = false;
|
|
|
490
498
|
let needsWakeReconnect = false;
|
|
491
499
|
const intentionallyClosed = new WeakSet();
|
|
492
500
|
const connectionListeners = new Set();
|
|
501
|
+
let bridgeListening = false;
|
|
493
502
|
/**
|
|
494
503
|
* Notified `true` on every fresh dev-server socket and `false` when one is lost.
|
|
495
504
|
* What lets a queued write know the moment retrying is worth it again.
|
|
@@ -539,6 +548,10 @@ function connectLocal() {
|
|
|
539
548
|
clearReconnectTimer();
|
|
540
549
|
if (!canConnectLocal())
|
|
541
550
|
return;
|
|
551
|
+
if (typeof window !== "undefined" && window.parent !== window) {
|
|
552
|
+
connectBridge();
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
542
555
|
fetch("/__castle/ws-port")
|
|
543
556
|
.then((r) => r.json())
|
|
544
557
|
.then(({ port, path }) => {
|
|
@@ -555,6 +568,48 @@ function connectLocal() {
|
|
|
555
568
|
scheduleReconnect();
|
|
556
569
|
});
|
|
557
570
|
}
|
|
571
|
+
function connectBridge() {
|
|
572
|
+
if (bridgeListening || ws?.readyState === WebSocket.OPEN)
|
|
573
|
+
return;
|
|
574
|
+
bridgeListening = true;
|
|
575
|
+
const onInit = (event) => {
|
|
576
|
+
const data = event.data;
|
|
577
|
+
if (!data ||
|
|
578
|
+
data.type !== "castle-bridge-init" ||
|
|
579
|
+
!isTrustedShellMessage(event) ||
|
|
580
|
+
event.ports.length !== 1) {
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
const port = event.ports[0];
|
|
584
|
+
const socket = {
|
|
585
|
+
readyState: WebSocket.OPEN,
|
|
586
|
+
send: (value) => {
|
|
587
|
+
try {
|
|
588
|
+
port.postMessage(JSON.parse(value));
|
|
589
|
+
}
|
|
590
|
+
catch {
|
|
591
|
+
/* malformed local payload */
|
|
592
|
+
}
|
|
593
|
+
},
|
|
594
|
+
};
|
|
595
|
+
port.onmessage = (messageEvent) => {
|
|
596
|
+
const value = messageEvent.data;
|
|
597
|
+
if (!value || typeof value !== "object")
|
|
598
|
+
return;
|
|
599
|
+
handleLocalMessage(value);
|
|
600
|
+
};
|
|
601
|
+
port.start();
|
|
602
|
+
localServeSeen = true;
|
|
603
|
+
ws = socket;
|
|
604
|
+
for (const msg of logBuffer)
|
|
605
|
+
socket.send(JSON.stringify(msg));
|
|
606
|
+
logBuffer = [];
|
|
607
|
+
bridgeListening = false;
|
|
608
|
+
window.removeEventListener("message", onInit);
|
|
609
|
+
setConnected(true);
|
|
610
|
+
};
|
|
611
|
+
window.addEventListener("message", onInit);
|
|
612
|
+
}
|
|
558
613
|
function stopHeartbeat() {
|
|
559
614
|
if (pingTimer !== null) {
|
|
560
615
|
clearInterval(pingTimer);
|