From a8169c4cde6113f264f852fdfd7e268e47283ee5 Mon Sep 17 00:00:00 2001 From: velzie Date: Tue, 3 Sep 2024 11:23:13 -0400 Subject: [PATCH] fix sync xhr hang --- src/client/shared/requests/xmlhttprequest.ts | 8 ++++++-- src/shared/rewriters/html.ts | 14 +++++++------- src/sync.ts | 7 +++++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/client/shared/requests/xmlhttprequest.ts b/src/client/shared/requests/xmlhttprequest.ts index 77f617b..1390794 100644 --- a/src/client/shared/requests/xmlhttprequest.ts +++ b/src/client/shared/requests/xmlhttprequest.ts @@ -1,7 +1,7 @@ import { config, decodeUrl, encodeUrl, rewriteHeaders } from "../../../shared"; import { ScramjetClient } from "../../client"; const nativeworker = Worker; - +const postmessage = Worker.prototype.postMessage; export default function (client: ScramjetClient, self: Self) { const worker = new nativeworker(config.sync); const ARGS = Symbol("xhr original args"); @@ -34,14 +34,18 @@ export default function (client: ScramjetClient, self: Self) { const sab = new SharedArrayBuffer(1024, { maxByteLength: 2147483647 }); const view = new DataView(sab); - worker.postMessage({ + postmessage.call(worker, { sab, args, headers: ctx.this[HEADERS], body: ctx.args[0], }); + let now = performance.now(); while (view.getUint8(0) === 0) { + if (performance.now() - now > 1000) { + throw new Error("xhr timeout"); + } /* spin */ } diff --git a/src/shared/rewriters/html.ts b/src/shared/rewriters/html.ts index e242593..f6a9f67 100644 --- a/src/shared/rewriters/html.ts +++ b/src/shared/rewriters/html.ts @@ -56,12 +56,12 @@ export function rewriteHtml( script(self.$scramjet.config["codecs"]), script("data:application/javascript;base64," + btoa(injected)), script(self.$scramjet.config["shared"]), - script(self.$scramjet.config["client"]), - script("/vc/browser.js"), - new Element("link", { - rel: "stylesheet", - href: "/vc/browser.css", - }) + script(self.$scramjet.config["client"]) + // script("/vc/browser.js"), + // new Element("link", { + // rel: "stylesheet", + // href: "/vc/browser.css", + // }) ); } @@ -197,8 +197,8 @@ function traverseParsedHtml( if (v === null) delete node.attribs[attr]; else { node.attribs[attr] = v; - node.attribs[`data-scramjet-${attr}`] = value; } + node.attribs[`data-scramjet-${attr}`] = value; } } } diff --git a/src/sync.ts b/src/sync.ts index e803def..1372ca1 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -50,5 +50,12 @@ addEventListener( // release the lock, main thread will stop spinning now view.setUint8(0, 1); }; + xhr.ontimeout = + xhr.onerror = + xhr.onabort = + () => { + console.error("xhr failed"); + view.setUint8(0, 1); + }; } );