Skip to content

Commit

Permalink
add even more BLAZINGLY FAST logs
Browse files Browse the repository at this point in the history
  • Loading branch information
r58Playz committed Mar 7, 2025
1 parent 9b0af07 commit 0995fea
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 44 deletions.
88 changes: 54 additions & 34 deletions src/client/shared/sourcemaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,43 @@ function getEnd(rewrite: Rewrite): number {

const scramtag_ident = "/*scramtag ";

function registerRewrites(buf: Array<number>, 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);

Expand Down Expand Up @@ -112,42 +149,25 @@ export default function (client: ScramjetClient, self: Self) {
globalThis.$scramjet.config.globals.pushsourcemapfn,
{
value: (buf: Array<number>, 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 });
const before = performance.now();
registerRewrites(buf, tag);
const after = performance.now();

const duration = after - before;

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,
Expand Down
40 changes: 32 additions & 8 deletions src/shared/rewriters/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
Expand Down Expand Up @@ -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)) {
Expand All @@ -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,
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/worker/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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":
Expand Down

0 comments on commit 0995fea

Please sign in to comment.