From 0995fea192fc1170c61ec7d55a74856e48b4d78c Mon Sep 17 00:00:00 2001 From: Toshit Chawda Date: Thu, 6 Mar 2025 20:47:11 -0800 Subject: [PATCH] add even more BLAZINGLY FAST logs --- src/client/shared/sourcemaps.ts | 84 ++++++++++++++++++++------------- src/shared/rewriters/js.ts | 40 ++++++++++++---- src/worker/fetch.ts | 11 ++++- 3 files changed, 93 insertions(+), 42 deletions(-) diff --git a/src/client/shared/sourcemaps.ts b/src/client/shared/sourcemaps.ts index c868f2c..77fd824 100644 --- a/src/client/shared/sourcemaps.ts +++ b/src/client/shared/sourcemaps.ts @@ -31,6 +31,43 @@ function getEnd(rewrite: Rewrite): number { const scramtag_ident = "/*scramtag "; +function registerRewrites(buf: Array, tag: string) { + const sourcemap = Uint8Array.from(buf); + const view = new DataView(sourcemap.buffer); + const decoder = new TextDecoder("utf-8"); + + const rewrites: Rewrite[] = []; + + const rewritelen = view.getUint32(0, true); + let cursor = 4; + for (let i = 0; i < rewritelen; i++) { + const type = view.getUint8(cursor) as RewriteType; + cursor += 1; + + if (type == RewriteType.Insert) { + const start = view.getUint32(cursor, true); + cursor += 4; + const size = view.getUint32(cursor, true); + cursor += 4; + + rewrites.push({ type, start, size }); + } else if (type == RewriteType.Replace) { + const start = view.getUint32(cursor, true); + cursor += 4; + const end = view.getUint32(cursor, true); + cursor += 4; + const len = view.getUint32(cursor, true); + cursor += 4; + + const str = decoder.decode(sourcemap.subarray(cursor, cursor + len)); + + rewrites.push({ type, start, end, str }); + } + } + + sourcemaps[tag] = rewrites; +} + function doUnrewrite(ctx: ProxyCtx) { let stringified: string = ctx.fn.call(ctx.this); @@ -112,42 +149,25 @@ export default function (client: ScramjetClient, self: Self) { globalThis.$scramjet.config.globals.pushsourcemapfn, { value: (buf: Array, tag: string) => { - const sourcemap = Uint8Array.from(buf); - const view = new DataView(sourcemap.buffer); - const decoder = new TextDecoder("utf-8"); + const before = performance.now(); + registerRewrites(buf, tag); + const after = performance.now(); - const rewrites: Rewrite[] = []; + const duration = after - before; - const rewritelen = view.getUint32(0, true); - let cursor = 4; - for (let i = 0; i < rewritelen; i++) { - const type = view.getUint8(cursor) as RewriteType; - cursor += 1; - - if (type == RewriteType.Insert) { - const start = view.getUint32(cursor, true); - cursor += 4; - const size = view.getUint32(cursor, true); - cursor += 4; - - rewrites.push({ type, start, size }); - } else if (type == RewriteType.Replace) { - const start = view.getUint32(cursor, true); - cursor += 4; - const end = view.getUint32(cursor, true); - cursor += 4; - const len = view.getUint32(cursor, true); - cursor += 4; - - const str = decoder.decode( - sourcemap.subarray(cursor, cursor + len) - ); - - rewrites.push({ type, start, end, str }); + if (flagEnabled("rewriterLogs", new URL(location.href))) { + let timespan: string; + if (duration < 1) { + timespan = "BLAZINGLY FAST"; + } else if (duration < 500) { + timespan = "decent speed"; + } else { + timespan = "really slow"; } + console.log( + `js rewrite parsing for scramtag ${tag} was ${timespan} (${duration.toFixed(2)}ms)` + ); } - - sourcemaps[tag] = rewrites; }, enumerable: false, writable: false, diff --git a/src/shared/rewriters/js.ts b/src/shared/rewriters/js.ts index 52adb4a..30a2b53 100644 --- a/src/shared/rewriters/js.ts +++ b/src/shared/rewriters/js.ts @@ -24,12 +24,12 @@ Error.stackTraceLimit = 50; const decoder = new TextDecoder(); -function rewriteJsWrapper( +function rewriteJsWasm( input: string | ArrayBuffer, source: string | null, meta: URLMeta, module: boolean -): string | ArrayBuffer { +): { js: string | ArrayBuffer; map: Uint8Array | null; tag: string } { initSync({ module: new WebAssembly.Module(self.REAL_WASM), }); @@ -59,16 +59,18 @@ function rewriteJsWrapper( console.warn("failed rewriting js for", source, err1, input); err1.message = `failed rewriting js for "${source}": ${err1.message}`; - return input; + return { js: input, tag: "", map: null }; } const after = performance.now(); - const { js, map, scramtag, errors, duration } = out; + let { js, map, scramtag, errors, duration } = out; if ((flagEnabled("sourcemaps", meta.base), !globalThis.clients)) { globalThis[globalThis.$scramjet.config.globals.pushsourcemapfn]( Array.from(map), scramtag ); + + map = null; } if (flagEnabled("rewriterLogs", meta.base)) { @@ -92,10 +94,14 @@ function rewriteJsWrapper( ); } - return typeof input === "string" ? decoder.decode(js) : js; + return { + js: typeof input === "string" ? decoder.decode(js) : js, + tag: scramtag, + map, + }; } -export function rewriteJs( +function rewriteJsInner( js: string | ArrayBuffer, url: string | null, meta: URLMeta, @@ -104,10 +110,28 @@ export function rewriteJs( if (flagEnabled("naiiveRewriter", meta.origin)) { const text = typeof js === "string" ? js : new TextDecoder().decode(js); - return rewriteJsNaiive(text); + return { js: rewriteJsNaiive(text), tag: "", map: null }; } - return rewriteJsWrapper(js, url, meta, module); + return rewriteJsWasm(js, url, meta, module); +} + +export function rewriteJs( + js: string | ArrayBuffer, + url: string | null, + meta: URLMeta, + module = false +) { + return rewriteJsInner(js, url, meta, module).js; +} + +export function rewriteJsWithMap( + js: string | ArrayBuffer, + url: string | null, + meta: URLMeta, + module = false +) { + return rewriteJsInner(js, url, meta, module); } // 1. does not work with modules diff --git a/src/worker/fetch.ts b/src/worker/fetch.ts index c94b1e9..3e32307 100644 --- a/src/worker/fetch.ts +++ b/src/worker/fetch.ts @@ -16,7 +16,8 @@ import { } from "../shared"; import type { URLMeta } from "../shared/rewriters/url"; -import { $scramjet } from "../scramjet"; +import { $scramjet, flagEnabled } from "../scramjet"; +import { rewriteJsWithMap } from "../shared/rewriters/js"; function newmeta(url: URL): URLMeta { return { @@ -360,12 +361,18 @@ async function rewriteBody( return response.body; } case "script": - return rewriteJs( + let { js, tag, map } = rewriteJsWithMap( await response.arrayBuffer(), response.finalURL, meta, workertype === "module" ); + if (flagEnabled("sourcemaps", meta.base) && map) { + js = + `${globalThis.$scramjet.config.globals.pushsourcemapfn}([${map.join(",")}], "${tag}");` + + js; + } + return js; case "style": return rewriteCss(await response.text(), meta); case "sharedworker":