mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 22:40:01 -04:00
fix sync xhr hang
This commit is contained in:
parent
277d8c8ad9
commit
a8169c4cde
3 changed files with 20 additions and 9 deletions
|
@ -1,7 +1,7 @@
|
||||||
import { config, decodeUrl, encodeUrl, rewriteHeaders } from "../../../shared";
|
import { config, decodeUrl, encodeUrl, rewriteHeaders } from "../../../shared";
|
||||||
import { ScramjetClient } from "../../client";
|
import { ScramjetClient } from "../../client";
|
||||||
const nativeworker = Worker;
|
const nativeworker = Worker;
|
||||||
|
const postmessage = Worker.prototype.postMessage;
|
||||||
export default function (client: ScramjetClient, self: Self) {
|
export default function (client: ScramjetClient, self: Self) {
|
||||||
const worker = new nativeworker(config.sync);
|
const worker = new nativeworker(config.sync);
|
||||||
const ARGS = Symbol("xhr original args");
|
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 sab = new SharedArrayBuffer(1024, { maxByteLength: 2147483647 });
|
||||||
const view = new DataView(sab);
|
const view = new DataView(sab);
|
||||||
|
|
||||||
worker.postMessage({
|
postmessage.call(worker, {
|
||||||
sab,
|
sab,
|
||||||
args,
|
args,
|
||||||
headers: ctx.this[HEADERS],
|
headers: ctx.this[HEADERS],
|
||||||
body: ctx.args[0],
|
body: ctx.args[0],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let now = performance.now();
|
||||||
while (view.getUint8(0) === 0) {
|
while (view.getUint8(0) === 0) {
|
||||||
|
if (performance.now() - now > 1000) {
|
||||||
|
throw new Error("xhr timeout");
|
||||||
|
}
|
||||||
/* spin */
|
/* spin */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,12 +56,12 @@ export function rewriteHtml(
|
||||||
script(self.$scramjet.config["codecs"]),
|
script(self.$scramjet.config["codecs"]),
|
||||||
script("data:application/javascript;base64," + btoa(injected)),
|
script("data:application/javascript;base64," + btoa(injected)),
|
||||||
script(self.$scramjet.config["shared"]),
|
script(self.$scramjet.config["shared"]),
|
||||||
script(self.$scramjet.config["client"]),
|
script(self.$scramjet.config["client"])
|
||||||
script("/vc/browser.js"),
|
// script("/vc/browser.js"),
|
||||||
new Element("link", {
|
// new Element("link", {
|
||||||
rel: "stylesheet",
|
// rel: "stylesheet",
|
||||||
href: "/vc/browser.css",
|
// href: "/vc/browser.css",
|
||||||
})
|
// })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,8 +197,8 @@ function traverseParsedHtml(
|
||||||
if (v === null) delete node.attribs[attr];
|
if (v === null) delete node.attribs[attr];
|
||||||
else {
|
else {
|
||||||
node.attribs[attr] = v;
|
node.attribs[attr] = v;
|
||||||
node.attribs[`data-scramjet-${attr}`] = value;
|
|
||||||
}
|
}
|
||||||
|
node.attribs[`data-scramjet-${attr}`] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,5 +50,12 @@ addEventListener(
|
||||||
// release the lock, main thread will stop spinning now
|
// release the lock, main thread will stop spinning now
|
||||||
view.setUint8(0, 1);
|
view.setUint8(0, 1);
|
||||||
};
|
};
|
||||||
|
xhr.ontimeout =
|
||||||
|
xhr.onerror =
|
||||||
|
xhr.onabort =
|
||||||
|
() => {
|
||||||
|
console.error("xhr failed");
|
||||||
|
view.setUint8(0, 1);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue