From 4cd8683e739764e98a8b67df03ea3712e2e3406b Mon Sep 17 00:00:00 2001 From: Chad Curtis Date: Sat, 20 Sep 2025 18:09:03 +0000 Subject: [PATCH] Re-add purged, but informative documentation/comment items. --- iframe-fetch-client/MESSAGING_PROTOCOL.md | 14 +++++++++++++- iframe-fetch-client/_iframe-client/main.js | 9 +++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/iframe-fetch-client/MESSAGING_PROTOCOL.md b/iframe-fetch-client/MESSAGING_PROTOCOL.md index ed3186e1..0f1328b5 100644 --- a/iframe-fetch-client/MESSAGING_PROTOCOL.md +++ b/iframe-fetch-client/MESSAGING_PROTOCOL.md @@ -7,7 +7,8 @@ between the parent page and the iframe fetch client. The iframe fetch client uses JSON-RPC 2.0 over `postMessage` for communication. The ServiceWorker makes JSON-RPC calls directly, with the main thread acting as -a relay between the ServiceWorker and parent page. +a relay between the ServiceWorker and parent page. The only required method is +`fetch` for handling HTTP requests. ## Architecture @@ -125,6 +126,17 @@ full request body support - `-32002`: Request processing error - `-32003`: Invalid URL +**Request Body Support:** The system fully supports request bodies, enabling: + +- POST/PUT requests with JSON data +- Form submissions +- API calls with payloads +- File uploads (as base64 strings) +- Any HTTP method with body content + +Request bodies must be base64-encoded strings when not null. This ensures +consistent handling of both text and binary data across the messaging protocol. + ### `console` **Purpose:** Forward console messages from iframe to parent for display diff --git a/iframe-fetch-client/_iframe-client/main.js b/iframe-fetch-client/_iframe-client/main.js index 1edc13b7..83314e24 100644 --- a/iframe-fetch-client/_iframe-client/main.js +++ b/iframe-fetch-client/_iframe-client/main.js @@ -14,11 +14,12 @@ class JSONRPCClient { setupMessageListener() { window.addEventListener("message", (event) => { const message = event.data; + // Check if this is a response to our own request or a ServiceWorker request if (message.jsonrpc === "2.0" && message.id !== undefined) { if (this.pendingRequests.has(message.id)) { this.handleResponse(message); } else { - // Response for ServiceWorker - relay it + // This is a response to a ServiceWorker request, relay it back this.relayToServiceWorker(message); } } @@ -26,6 +27,7 @@ class JSONRPCClient { } relayToServiceWorker(response) { + // Forward the JSON-RPC response to ServiceWorker if (navigator.serviceWorker.controller) { navigator.serviceWorker.controller.postMessage(response); } @@ -37,6 +39,7 @@ class JSONRPCClient { return new Promise((resolve, reject) => { this.pendingRequests.set(id, { resolve, reject }); + // Send JSON-RPC request to parent window.parent.postMessage({ jsonrpc: "2.0", method: "fetch", @@ -44,6 +47,7 @@ class JSONRPCClient { id, }, "*"); + // Timeout after 10 seconds setTimeout(() => { if (this.pendingRequests.has(id)) { this.pendingRequests.delete(id); @@ -185,7 +189,7 @@ class FetchClient { await navigator.serviceWorker.ready; this.setupServiceWorkerCommunication(); - this.blockFutureRegistrations(); + this.blockFutureRegistrations(); // Block future registrations to prevent site from registering its own SW } setupServiceWorkerCommunication() { @@ -237,6 +241,7 @@ class FetchClient { } replaceDocument(htmlContent) { + // Create a new document from the HTML content const parser = new DOMParser(); const newDoc = parser.parseFromString(htmlContent, "text/html");