add module rewriting more deeply and clean things up

This commit is contained in:
Percs 2025-02-03 13:42:26 -06:00
parent 2dc6a47c41
commit 53f0ecd9e5
10 changed files with 42 additions and 31 deletions

View file

@ -345,7 +345,7 @@ export class ScramjetClient {
if (handler.construct) { if (handler.construct) {
h.construct = function ( h.construct = function (
constructor: any, constructor: any,
argArray: any[], args: any[],
newTarget: AnyFunction newTarget: AnyFunction
) { ) {
let returnValue: any = undefined; let returnValue: any = undefined;
@ -354,7 +354,7 @@ export class ScramjetClient {
const ctx: ProxyCtx = { const ctx: ProxyCtx = {
fn: constructor, fn: constructor,
this: null, this: null,
args: argArray, args,
newTarget: newTarget, newTarget: newTarget,
return: (r: any) => { return: (r: any) => {
earlyreturn = true; earlyreturn = true;
@ -379,14 +379,14 @@ export class ScramjetClient {
} }
if (handler.apply) { if (handler.apply) {
h.apply = function (fn: any, thisArg: any, argArray: any[]) { h.apply = function (fn: any, that: any, args: any[]) {
let returnValue: any = undefined; let returnValue: any = undefined;
let earlyreturn = false; let earlyreturn = false;
const ctx: ProxyCtx = { const ctx: ProxyCtx = {
fn, fn,
this: thisArg, this: that,
args: argArray, args,
newTarget: null, newTarget: null,
return: (r: any) => { return: (r: any) => {
earlyreturn = true; earlyreturn = true;

View file

@ -22,12 +22,12 @@ export default function (client: ScramjetClient, _self: typeof window) {
if (prop in NamedNodeMap.prototype && typeof value === "function") { if (prop in NamedNodeMap.prototype && typeof value === "function") {
return new Proxy(value, { return new Proxy(value, {
apply(target, thisArg, argArray) { apply(target, that, args) {
if (thisArg === proxy) { if (that === proxy) {
return Reflect.apply(target, map, argArray); return Reflect.apply(target, map, args);
} }
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, that, args);
}, },
}); });
} }

View file

@ -39,8 +39,8 @@ export default function (client: ScramjetClient) {
if (typeof v === "function") { if (typeof v === "function") {
return new Proxy(v, { return new Proxy(v, {
apply(target, thisArg, argArray) { apply(target, that, args) {
return Reflect.apply(target, style, argArray); return Reflect.apply(target, style, args);
}, },
}); });
} }

View file

@ -45,7 +45,7 @@ export function createLocationProxy(
} }
if (native.set) { if (native.set) {
desc.set = new Proxy(native.set, { desc.set = new Proxy(native.set, {
apply(target, thisArg, args) { apply(target, that, args) {
if (prop === "href") { if (prop === "href") {
// special case // special case
client.url = args[0]; client.url = args[0];
@ -83,20 +83,20 @@ export function createLocationProxy(
}); });
if (self.location.assign) if (self.location.assign)
fakeLocation.assign = new Proxy(self.location.assign, { fakeLocation.assign = new Proxy(self.location.assign, {
apply(target, thisArg, args) { apply(target, that, args) {
args[0] = rewriteUrl(args[0], client.meta); args[0] = rewriteUrl(args[0], client.meta);
Reflect.apply(target, self.location, args); Reflect.apply(target, self.location, args);
}, },
}); });
if (self.location.reload) if (self.location.reload)
fakeLocation.reload = new Proxy(self.location.reload, { fakeLocation.reload = new Proxy(self.location.reload, {
apply(target, thisArg, args) { apply(target, that, args) {
Reflect.apply(target, self.location, args); Reflect.apply(target, self.location, args);
}, },
}); });
if (self.location.replace) if (self.location.replace)
fakeLocation.replace = new Proxy(self.location.replace, { fakeLocation.replace = new Proxy(self.location.replace, {
apply(target, thisArg, args) { apply(target, that, args) {
args[0] = rewriteUrl(args[0], client.meta); args[0] = rewriteUrl(args[0], client.meta);
Reflect.apply(target, self.location, args); Reflect.apply(target, self.location, args);
}, },

View file

@ -43,9 +43,9 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
apply(ctx) { apply(ctx) {
if (ctx.args[0]) if (ctx.args[0])
ctx.args[0] = new Proxy(ctx.args[0], { ctx.args[0] = new Proxy(ctx.args[0], {
apply(target, thisArg, argArray) { apply(target, that, args) {
// console.warn("CAUGHT PROMISE REJECTION", argArray); // console.warn("CAUGHT PROMISE REJECTION", args);
Reflect.apply(target, thisArg, argArray); Reflect.apply(target, that, args);
}, },
}); });
}, },

View file

@ -48,8 +48,8 @@ export default function (client: ScramjetClient, self: Self) {
function wraplistener(listener: (...args: any) => any) { function wraplistener(listener: (...args: any) => any) {
return new Proxy(listener, { return new Proxy(listener, {
apply(target, thisArg, argArray) { apply(target, that, args) {
const realEvent: Event = argArray[0]; const realEvent: Event = args[0];
// we only need to handle events dispatched from the browser // we only need to handle events dispatched from the browser
if (realEvent.isTrusted) { if (realEvent.isTrusted) {
@ -62,7 +62,7 @@ export default function (client: ScramjetClient, self: Self) {
if (handler._init.call(realEvent) === false) return; if (handler._init.call(realEvent) === false) return;
} }
argArray[0] = new Proxy(realEvent, { args[0] = new Proxy(realEvent, {
get(_target, prop, reciever) { get(_target, prop, reciever) {
if (prop in handler) { if (prop in handler) {
return handler[prop].call(_target); return handler[prop].call(_target);
@ -78,13 +78,13 @@ export default function (client: ScramjetClient, self: Self) {
if (!self.event) { if (!self.event) {
Object.defineProperty(self, "event", { Object.defineProperty(self, "event", {
get() { get() {
return argArray[0]; return args[0];
}, },
configurable: true, configurable: true,
}); });
} }
const rv = Reflect.apply(target, thisArg, argArray); const rv = Reflect.apply(target, that, args);
return rv; return rv;
}, },

View file

@ -32,7 +32,7 @@ export default function (client: ScramjetClient, _self: typeof globalThis) {
// sharedworkers can only be constructed from window // sharedworkers can only be constructed from window
client.Proxy("SharedWorker", { client.Proxy("SharedWorker", {
construct({ args, call }) { construct({ args, call }) {
args[0] = rewriteUrl(args[0], client.meta) + "?dest=worker"; args[0] = rewriteUrl(args[0], client.meta) + "?dest=sharedworker";
if (args[1] && typeof args[1] === "string") if (args[1] && typeof args[1] === "string")
args[1] = `${client.url.origin}@${args[1]}`; args[1] = `${client.url.origin}@${args[1]}`;
@ -67,7 +67,8 @@ export default function (client: ScramjetClient, _self: typeof globalThis) {
client.Proxy("Worklet.prototype.addModule", { client.Proxy("Worklet.prototype.addModule", {
apply(ctx) { apply(ctx) {
if (ctx.args[0]) ctx.args[0] = rewriteUrl(ctx.args[0], client.meta); if (ctx.args[0])
ctx.args[0] = rewriteUrl(ctx.args[0], client.meta) + "?dest=worklet";
}, },
}); });
} }

View file

@ -240,6 +240,13 @@ function traverseParsedHtml(
if (node.name === "style" && node.children[0] !== undefined) if (node.name === "style" && node.children[0] !== undefined)
node.children[0].data = rewriteCss(node.children[0].data, meta); node.children[0].data = rewriteCss(node.children[0].data, meta);
if (
node.name === "script" &&
node.attribs.type === "module" &&
node.attribs.src
)
node.attribs.src = node.attribs.src + "?type=module";
if ( if (
node.name === "script" && node.name === "script" &&
/(application|text)\/javascript|module|importmap|undefined/.test( /(application|text)\/javascript|module|importmap|undefined/.test(

View file

@ -9,9 +9,9 @@ export function rewriteWorkers(
meta: URLMeta meta: URLMeta
) { ) {
let str = ""; let str = "";
const module = type === "module";
const script = (script) => { const script = (script) => {
if (type === "module") { if (module) {
str += `import "${$scramjet.config.files[script]}"\n`; str += `import "${$scramjet.config.files[script]}"\n`;
} else { } else {
str += `importScripts("${$scramjet.config.files[script]}");\n`; str += `importScripts("${$scramjet.config.files[script]}");\n`;
@ -23,14 +23,12 @@ export function rewriteWorkers(
str += `self.$scramjet.config = ${JSON.stringify($scramjet.config)};`; str += `self.$scramjet.config = ${JSON.stringify($scramjet.config)};`;
script("client"); script("client");
let rewritten = rewriteJs(js, url, meta); let rewritten = rewriteJs(js, url, meta, module);
if (rewritten instanceof Uint8Array) { if (rewritten instanceof Uint8Array) {
rewritten = new TextDecoder().decode(rewritten); rewritten = new TextDecoder().decode(rewritten);
} }
str += rewritten; str += rewritten;
// dbg.log("Rewrite", type, str);
return str; return str;
} }

View file

@ -341,7 +341,12 @@ async function rewriteBody(
return response.body; return response.body;
} }
case "script": case "script":
return rewriteJs(await response.arrayBuffer(), response.finalURL, meta); return rewriteJs(
await response.arrayBuffer(),
response.finalURL,
meta,
workertype === "module"
);
case "style": case "style":
return rewriteCss(await response.text(), meta); return rewriteCss(await response.text(), meta);
case "sharedworker": case "sharedworker":