From b891e8140b3cdb07b0d86185c6e88ace24c039cc Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Tue, 11 Mar 2025 00:25:31 -0500 Subject: [PATCH] fix eslint stuff --- src/client/shared/sourcemaps.ts | 1 + src/shared/rewriters/js.ts | 40 ++++++++++++++++----------------- src/worker/fetch.ts | 10 +++++---- src/worker/index.ts | 6 +++-- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/client/shared/sourcemaps.ts b/src/client/shared/sourcemaps.ts index 8c0598d..b8d24a6 100644 --- a/src/client/shared/sourcemaps.ts +++ b/src/client/shared/sourcemaps.ts @@ -125,6 +125,7 @@ function doUnrewrite(ctx: ProxyCtx) { if (!rewrites) { console.warn("failed to get rewrites for tag", tag); + return ctx.return(stringified); } diff --git a/src/shared/rewriters/js.ts b/src/shared/rewriters/js.ts index c3dc188..570a734 100644 --- a/src/shared/rewriters/js.ts +++ b/src/shared/rewriters/js.ts @@ -100,6 +100,26 @@ function rewriteJsWasm( }; } +// 1. does not work with modules +// 2. cannot proxy import() +// 3. disables "use strict" optimizations +// 4. i think the global state can get clobbered somehow +// +// if you can ensure all the preconditions are met this is faster than full rewrites +function rewriteJsNaiive(js: string | ArrayBuffer) { + if (typeof js !== "string") { + js = new TextDecoder().decode(js); + } + + return ` + with (${$scramjet.config.globals.wrapfn}(globalThis)) { + + ${js} + + } + `; +} + function rewriteJsInner( js: string | Uint8Array, url: string | null, @@ -132,23 +152,3 @@ export function rewriteJsWithMap( ) { return rewriteJsInner(js, url, meta, module); } - -// 1. does not work with modules -// 2. cannot proxy import() -// 3. disables "use strict" optimizations -// 4. i think the global state can get clobbered somehow -// -// if you can ensure all the preconditions are met this is faster than full rewrites -export function rewriteJsNaiive(js: string | ArrayBuffer) { - if (typeof js !== "string") { - js = new TextDecoder().decode(js); - } - - return ` - with (${$scramjet.config.globals.wrapfn}(globalThis)) { - - ${js} - - } - `; -} diff --git a/src/worker/fetch.ts b/src/worker/fetch.ts index b341086..60e80cb 100644 --- a/src/worker/fetch.ts +++ b/src/worker/fetch.ts @@ -237,12 +237,12 @@ async function handleResponse( const maybeHeaders = responseHeaders["set-cookie"] || []; for (const cookie in maybeHeaders) { if (client) { - let promise = swtarget.dispatch(client, { + const promise = swtarget.dispatch(client, { scramjet$type: "cookie", cookie, url: url.href, }); - if (destination != "document" && destination != "iframe") { + if (destination !== "document" && destination !== "iframe") { await promise; } } @@ -358,7 +358,7 @@ async function rewriteBody( } else { return response.body; } - case "script": + case "script": { let { js, tag, map } = rewriteJsWithMap( new Uint8Array(await response.arrayBuffer()), response.finalURL, @@ -370,7 +370,9 @@ async function rewriteBody( `${globalThis.$scramjet.config.globals.pushsourcemapfn}([${map.join(",")}], "${tag}");` + (js instanceof Uint8Array ? new TextDecoder().decode(js) : js); } - return js; + + return js as unknown as ArrayBuffer; + } case "style": return rewriteCss(await response.text(), meta); case "sharedworker": diff --git a/src/worker/index.ts b/src/worker/index.ts index 5fe17ad..8b3ca24 100644 --- a/src/worker/index.ts +++ b/src/worker/index.ts @@ -43,6 +43,7 @@ export class ScramjetServiceWorker extends EventTarget { const cb = this.syncPool[data.scramjet$token]; delete this.syncPool[data.scramjet$token]; cb(data); + return; } @@ -67,13 +68,14 @@ export class ScramjetServiceWorker extends EventTarget { } async dispatch(client: Client, data: MessageW2C): Promise { - let token = this.synctoken++; + const token = this.synctoken++; let cb: (val: MessageC2W) => void; - let promise: Promise = new Promise((r) => (cb = r)); + const promise: Promise = new Promise((r) => (cb = r)); this.syncPool[token] = cb; data.scramjet$token = token; client.postMessage(data); + return await promise; }