diff --git a/rspack.config.js b/rspack.config.js index 8eca8af..8f85593 100644 --- a/rspack.config.js +++ b/rspack.config.js @@ -10,12 +10,10 @@ const __dirname = fileURLToPath(new URL(".", import.meta.url)); const packagemeta = JSON.parse(await readFile("package.json")); export default defineConfig({ - // change to production when needed mode: "development", entry: { shared: join(__dirname, "src/shared/index.ts"), worker: join(__dirname, "src/worker/index.ts"), - thread: join(__dirname, "src/thread/thread.ts"), client: join(__dirname, "src/client/index.ts"), codecs: join(__dirname, "src/codecs/index.ts"), controller: join(__dirname, "src/controller/index.ts"), diff --git a/src/controller/index.ts b/src/controller/index.ts index ca8827c..8469b74 100644 --- a/src/controller/index.ts +++ b/src/controller/index.ts @@ -21,7 +21,6 @@ export class ScramjetController { wasm: "/scramjet.wasm.js", shared: "/scramjet.shared.js", worker: "/scramjet.worker.js", - thread: "/scramjet.thread.js", client: "/scramjet.client.js", codecs: "/scramjet.codecs.js", sync: "/scramjet.sync.js", diff --git a/src/thread/thread.ts b/src/thread/thread.ts deleted file mode 100644 index dc208bc..0000000 --- a/src/thread/thread.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { rewriteJs } from "../shared/rewriters/js"; - -// @ts-ignore -onconnect = (e) => { - const port = e.ports[0]; - - console.log("thread: connected to port", port); - port.postMessage("ready"); - - let syncToken = 0; - port.onmessage = ({ data }) => { - console.log("thread: received message", data); - const [task, ...args] = data; - const token = syncToken++; - - try { - const res = tasks[task](...args); - console.log("thread: task", task, "completed with token", token); - port.postMessage({ - token, - result: res, - }); - } catch (e) { - port.postMessage({ - token, - error: e.message, - }); - } - - port.postMessage("idle"); - }; -}; - -const tasks = { - rewriteJs: taskRewriteJs, -}; - -function taskRewriteJs(js: ArrayBuffer, origin: string): string { - // idk how to get the codec from here - return rewriteJs(js, new URL(origin), () => {}); -} diff --git a/src/types.d.ts b/src/types.d.ts index c6d48fe..669568d 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -47,7 +47,6 @@ interface ScramjetConfig { wasm: string; shared: string; worker: string; - thread: string; client: string; codecs: string; sync: string; diff --git a/src/worker/fetch.ts b/src/worker/fetch.ts index 02fdd3c..838bfa9 100644 --- a/src/worker/fetch.ts +++ b/src/worker/fetch.ts @@ -287,8 +287,6 @@ async function rewriteBody( } case "script": return rewriteJs(await response.arrayBuffer(), meta); - // Disable threading for now, it's causing issues. - // responseBody = await this.threadpool.rewriteJs(await responseBody.arrayBuffer(), url.toString()); case "style": return rewriteCss(await response.text(), meta); case "sharedworker": diff --git a/src/worker/index.ts b/src/worker/index.ts index 79f8edd..84dc94c 100644 --- a/src/worker/index.ts +++ b/src/worker/index.ts @@ -1,12 +1,10 @@ import { FakeServiceWorker } from "./fakesw"; import { swfetch } from "./fetch"; -import { ScramjetThreadpool } from "./threadpool"; import type BareClient from "@mercuryworkshop/bare-mux"; export class ScramjetServiceWorker extends EventTarget { client: BareClient; config: typeof self.$scramjet.config; - threadpool: ScramjetThreadpool; syncPool: Record void> = {}; synctoken = 0; @@ -19,8 +17,6 @@ export class ScramjetServiceWorker extends EventTarget { super(); this.client = new self.$scramjet.shared.util.BareClient(); - this.threadpool = new ScramjetThreadpool(); - addEventListener("message", ({ data }: { data: MessageC2W }) => { if (!("scramjet$type" in data)) return; diff --git a/src/worker/threadpool.ts b/src/worker/threadpool.ts deleted file mode 100644 index 2381cef..0000000 --- a/src/worker/threadpool.ts +++ /dev/null @@ -1,88 +0,0 @@ -type Thread = { - handle: MessagePort; - ready: boolean; - busy: boolean; - syncToken: number; - promises: Map; -}; - -export class ScramjetThreadpool { - threads: Thread[] = []; - constructor() { - self.addEventListener("message", ({ data }) => { - if (data.scramjet$type == "add") { - this.spawn(data.handle); - } - }); - } - - spawn(handle) { - const thread = { - handle, - ready: false, - busy: false, - syncToken: 0, - promises: new Map(), - }; - - this.threads.push(thread); - - thread.handle.onmessage = (e) => { - if (e.data === "ready") { - thread.ready = true; - - return; - } - if (e.data === "idle") { - thread.busy = false; - - return; - } - - const { token, result, error } = e.data; - const { resolve, reject } = thread.promises.get(token); - thread.promises.delete(token); - - if (error) { - reject(error); - } else { - resolve(result); - } - }; - - thread.handle.start(); - } - - pick(): Thread | undefined { - const alive = this.threads.filter((t) => t.ready); - const idle = alive.filter((t) => !t.busy); - - // no threads - if (!alive.length) return; - - // there is a thread, but it's busy - if (!idle.length) return alive[Math.floor(Math.random() * alive.length)]; - - // there's an open thread - return idle[Math.floor(Math.random() * idle.length)]; - } - - run(task: string, args: any[], transferrable: any[]): Promise { - const thread = this.pick(); - if (!thread) throw new Error("No threads available"); - thread.busy = true; - - const token = thread.syncToken++; - - // console.log("runthread: dispatching task", task, "to thread", thread, "of token", token) - return new Promise((resolve, reject) => { - thread.promises.set(token, { resolve, reject }); - - thread.handle.postMessage([task, ...args], transferrable); - }); - } - - async rewriteJs(js: ArrayBuffer, origin: string): Promise { - return await this.run("rewriteJs", [js, origin], [js]); - } -} diff --git a/static/ui.js b/static/ui.js index 8514e12..2dc20c5 100644 --- a/static/ui.js +++ b/static/ui.js @@ -2,7 +2,6 @@ const scramjet = new ScramjetController({ wasm: "/scram/scramjet.wasm.js", codecs: "/scram/scramjet.codecs.js", worker: "/scram/scramjet.worker.js", - thread: "/scram/scramjet.thread.js", client: "/scram/scramjet.client.js", shared: "/scram/scramjet.shared.js", sync: "/scram/scramjet.sync.js",