mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-12 22:10:01 -04:00
add even more BLAZINGLY FAST logs
This commit is contained in:
parent
9b0af07516
commit
0995fea192
3 changed files with 93 additions and 42 deletions
|
@ -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);
|
||||
|
||||
|
@ -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 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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue