mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 06:50:01 -04:00
add BLAZINGLY FAST logs
This commit is contained in:
parent
f15cf568c1
commit
9b0af07516
1 changed files with 87 additions and 65 deletions
|
@ -1,5 +1,5 @@
|
|||
import { $scramjet, flagEnabled } from "../../scramjet";
|
||||
import { ScramjetClient } from "../client";
|
||||
import { flagEnabled } from "../../scramjet";
|
||||
import { ProxyCtx, ScramjetClient } from "../client";
|
||||
|
||||
enum RewriteType {
|
||||
Insert = 0,
|
||||
|
@ -29,67 +29,9 @@ function getEnd(rewrite: Rewrite): number {
|
|||
throw "unreachable";
|
||||
}
|
||||
|
||||
const sourcemaps: Record<string, Rewrite[]> = {};
|
||||
const scramtag_ident = "/*scramtag ";
|
||||
|
||||
export const enabled = (client: ScramjetClient) =>
|
||||
flagEnabled("sourcemaps", client.url);
|
||||
|
||||
export default function (client: ScramjetClient, self: Self) {
|
||||
// every script will push a sourcemap
|
||||
Object.defineProperty(
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
||||
sourcemaps[tag] = rewrites;
|
||||
},
|
||||
enumerable: false,
|
||||
writable: false,
|
||||
configurable: false,
|
||||
}
|
||||
);
|
||||
|
||||
const scramtag_ident = "/*scramtag ";
|
||||
|
||||
// when we rewrite javascript it will make function.toString leak internals
|
||||
// this can lead to double rewrites which is bad
|
||||
client.Proxy("Function.prototype.toString", {
|
||||
apply(ctx) {
|
||||
function doUnrewrite(ctx: ProxyCtx) {
|
||||
let stringified: string = ctx.fn.call(ctx.this);
|
||||
|
||||
// every function rewritten will have a scramtag comment
|
||||
|
@ -156,6 +98,86 @@ export default function (client: ScramjetClient, self: Self) {
|
|||
newString += stringified.slice(lastpos);
|
||||
|
||||
return ctx.return(newString);
|
||||
}
|
||||
|
||||
const sourcemaps: Record<string, Rewrite[]> = {};
|
||||
|
||||
export const enabled = (client: ScramjetClient) =>
|
||||
flagEnabled("sourcemaps", client.url);
|
||||
|
||||
export default function (client: ScramjetClient, self: Self) {
|
||||
// every script will push a sourcemap
|
||||
Object.defineProperty(
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
||||
sourcemaps[tag] = rewrites;
|
||||
},
|
||||
enumerable: false,
|
||||
writable: false,
|
||||
configurable: false,
|
||||
}
|
||||
);
|
||||
|
||||
// when we rewrite javascript it will make function.toString leak internals
|
||||
// this can lead to double rewrites which is bad
|
||||
client.Proxy("Function.prototype.toString", {
|
||||
apply(ctx) {
|
||||
const before = performance.now();
|
||||
doUnrewrite(ctx);
|
||||
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 unrewrite for function was ${timespan} (${duration.toFixed(2)}ms)`
|
||||
);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue