Re-add purged, but informative documentation/comment items.

This commit is contained in:
Chad Curtis committed 2025-09-20 18:09:03 +00:00
1 parent 53ac813eb5
commit 4cd8683e73
2 files changed
+20 -3

No files matched your search

+13 -1
View File
@@ -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
+7 -2
View File
@@ -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");