mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 06:20:02 -04:00
use rewriter wasm directly
This commit is contained in:
parent
1070e68dcf
commit
a69755936d
7 changed files with 45 additions and 24 deletions
|
@ -104,16 +104,4 @@ fi
|
||||||
mkdir -p dist/
|
mkdir -p dist/
|
||||||
|
|
||||||
cp rewriter/wasm/out/optimized.wasm dist/scramjet.wasm.wasm
|
cp rewriter/wasm/out/optimized.wasm dist/scramjet.wasm.wasm
|
||||||
{
|
|
||||||
|
|
||||||
cat <<EOF
|
|
||||||
if ("document" in self && document?.currentScript) {
|
|
||||||
document.currentScript.remove();
|
|
||||||
}
|
|
||||||
EOF
|
|
||||||
echo -n "self.WASM = '"
|
|
||||||
base64 -w0 < "rewriter/wasm/out/optimized.wasm"
|
|
||||||
echo -n "';"
|
|
||||||
|
|
||||||
} > dist/scramjet.wasm.js
|
|
||||||
echo "Rewriter Build Complete!"
|
echo "Rewriter Build Complete!"
|
||||||
|
|
|
@ -9,11 +9,16 @@ import {
|
||||||
} from "../../../rewriter/wasm/out/wasm.js";
|
} from "../../../rewriter/wasm/out/wasm.js";
|
||||||
import { $scramjet, flagEnabled } from "../../scramjet";
|
import { $scramjet, flagEnabled } from "../../scramjet";
|
||||||
|
|
||||||
initSync({
|
if (self.WASM)
|
||||||
module: new WebAssembly.Module(
|
self.REAL_WASM = Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0));
|
||||||
Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0))
|
|
||||||
),
|
// only use in sw
|
||||||
});
|
export async function asyncInitRewriter() {
|
||||||
|
const buf = await fetch($scramjet.config.files.wasm).then((r) =>
|
||||||
|
r.arrayBuffer()
|
||||||
|
);
|
||||||
|
self.REAL_WASM = new Uint8Array(buf);
|
||||||
|
}
|
||||||
|
|
||||||
Error.stackTraceLimit = 50;
|
Error.stackTraceLimit = 50;
|
||||||
|
|
||||||
|
@ -25,6 +30,10 @@ function rewriteJsWrapper(
|
||||||
meta: URLMeta,
|
meta: URLMeta,
|
||||||
module: boolean
|
module: boolean
|
||||||
): string | ArrayBuffer {
|
): string | ArrayBuffer {
|
||||||
|
initSync({
|
||||||
|
module: new WebAssembly.Module(self.REAL_WASM),
|
||||||
|
});
|
||||||
|
|
||||||
let out: RewriterOutput;
|
let out: RewriterOutput;
|
||||||
const before = performance.now();
|
const before = performance.now();
|
||||||
try {
|
try {
|
||||||
|
|
1
src/types.d.ts
vendored
1
src/types.d.ts
vendored
|
@ -103,6 +103,7 @@ declare global {
|
||||||
};
|
};
|
||||||
COOKIE: string;
|
COOKIE: string;
|
||||||
WASM: string;
|
WASM: string;
|
||||||
|
REAL_WASM: Uint8Array;
|
||||||
ScramjetController: typeof ScramjetController;
|
ScramjetController: typeof ScramjetController;
|
||||||
|
|
||||||
// the scramjet client belonging to a window
|
// the scramjet client belonging to a window
|
||||||
|
|
|
@ -49,6 +49,28 @@ export async function handleFetch(
|
||||||
requesturl.searchParams.delete("dest");
|
requesturl.searchParams.delete("dest");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (requesturl.pathname == this.config.files.wasm) {
|
||||||
|
return fetch(this.config.files.wasm).then(async (x) => {
|
||||||
|
const buf = await x.arrayBuffer();
|
||||||
|
const b64 = btoa(
|
||||||
|
new Uint8Array(buf)
|
||||||
|
.reduce(
|
||||||
|
(data, byte) => (data.push(String.fromCharCode(byte)), data),
|
||||||
|
[]
|
||||||
|
)
|
||||||
|
.join("")
|
||||||
|
);
|
||||||
|
|
||||||
|
let payload = "";
|
||||||
|
payload += `if ("document" in self && document.currentScript) { document.currentScript.remove(); }\n`;
|
||||||
|
payload += `self.WASM = '${b64}';`;
|
||||||
|
|
||||||
|
return new Response(payload, {
|
||||||
|
headers: { "content-type": "text/javascript" },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
requesturl.pathname.startsWith(this.config.prefix + "blob:") ||
|
requesturl.pathname.startsWith(this.config.prefix + "blob:") ||
|
||||||
requesturl.pathname.startsWith(this.config.prefix + "data:")
|
requesturl.pathname.startsWith(this.config.prefix + "data:")
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { handleFetch } from "./fetch";
|
||||||
import type BareClient from "@mercuryworkshop/bare-mux";
|
import type BareClient from "@mercuryworkshop/bare-mux";
|
||||||
import { ScramjetConfig } from "../types";
|
import { ScramjetConfig } from "../types";
|
||||||
import { $scramjet, loadCodecs } from "../scramjet";
|
import { $scramjet, loadCodecs } from "../scramjet";
|
||||||
|
import { asyncInitRewriter } from "../shared/rewriters/js";
|
||||||
|
|
||||||
export class ScramjetServiceWorker extends EventTarget {
|
export class ScramjetServiceWorker extends EventTarget {
|
||||||
client: BareClient;
|
client: BareClient;
|
||||||
|
@ -69,12 +70,14 @@ export class ScramjetServiceWorker extends EventTarget {
|
||||||
const store = tx.objectStore("config");
|
const store = tx.objectStore("config");
|
||||||
const config = store.get("config");
|
const config = store.get("config");
|
||||||
|
|
||||||
config.onsuccess = () => {
|
config.onsuccess = async () => {
|
||||||
this.config = config.result;
|
this.config = config.result;
|
||||||
$scramjet.config = config.result;
|
$scramjet.config = config.result;
|
||||||
|
|
||||||
loadCodecs();
|
loadCodecs();
|
||||||
|
|
||||||
|
await asyncInitRewriter();
|
||||||
|
|
||||||
resolve();
|
resolve();
|
||||||
};
|
};
|
||||||
config.onerror = () => reject(config.error);
|
config.onerror = () => reject(config.error);
|
||||||
|
@ -87,6 +90,8 @@ export class ScramjetServiceWorker extends EventTarget {
|
||||||
route({ request }: FetchEvent) {
|
route({ request }: FetchEvent) {
|
||||||
if (request.url.startsWith(location.origin + this.config.prefix))
|
if (request.url.startsWith(location.origin + this.config.prefix))
|
||||||
return true;
|
return true;
|
||||||
|
else if (request.url.startsWith(location.origin + this.config.files.wasm))
|
||||||
|
return true;
|
||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,11 +6,7 @@ if (navigator.userAgent.includes("Firefox")) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
importScripts(
|
importScripts("/scram/scramjet.shared.js", "/scram/scramjet.worker.js");
|
||||||
"/scram/scramjet.wasm.js",
|
|
||||||
"/scram/scramjet.shared.js",
|
|
||||||
"/scram/scramjet.worker.js"
|
|
||||||
);
|
|
||||||
|
|
||||||
const scramjet = new ScramjetServiceWorker();
|
const scramjet = new ScramjetServiceWorker();
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const scramjet = new ScramjetController({
|
const scramjet = new ScramjetController({
|
||||||
files: {
|
files: {
|
||||||
wasm: "/scram/scramjet.wasm.js",
|
wasm: "/scram/scramjet.wasm.wasm",
|
||||||
worker: "/scram/scramjet.worker.js",
|
worker: "/scram/scramjet.worker.js",
|
||||||
client: "/scram/scramjet.client.js",
|
client: "/scram/scramjet.client.js",
|
||||||
shared: "/scram/scramjet.shared.js",
|
shared: "/scram/scramjet.shared.js",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue