From c703ed39cadd4a1fb32164148bb67fd1a800676a Mon Sep 17 00:00:00 2001 From: velzie Date: Sun, 14 Jul 2024 16:30:56 -0400 Subject: [PATCH] fix meriyah crash --- src/client/url.ts | 17 ++++++++++------- src/shared/rewriters/js.ts | 21 ++++++++++++--------- src/shared/rewriters/url.ts | 3 ++- tests/location.html | 7 +++++++ 4 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 tests/location.html diff --git a/src/client/url.ts b/src/client/url.ts index 46c1083..ba53345 100644 --- a/src/client/url.ts +++ b/src/client/url.ts @@ -1,10 +1,13 @@ import { encodeUrl } from "../shared/rewriters/url"; +export const URL = globalThis.URL; -URL = new Proxy(URL, { - construct(target, argArray, newTarget) { - if (typeof argArray[0] === "string") argArray[0] = encodeUrl(argArray[0]); - if (typeof argArray[1] === "string") argArray[1] = encodeUrl(argArray[1]); +if (globalThis.window) { + window.URL = new Proxy(URL, { + construct(target, argArray, newTarget) { + if (typeof argArray[0] === "string") argArray[0] = encodeUrl(argArray[0]); + if (typeof argArray[1] === "string") argArray[1] = encodeUrl(argArray[1]); - return Reflect.construct(target, argArray, newTarget); - }, -}) \ No newline at end of file + return Reflect.construct(target, argArray, newTarget); + }, + }) +} diff --git a/src/shared/rewriters/js.ts b/src/shared/rewriters/js.ts index 7c5ab4d..ae3d9d8 100644 --- a/src/shared/rewriters/js.ts +++ b/src/shared/rewriters/js.ts @@ -18,12 +18,14 @@ import * as ESTree from "estree"; export function rewriteJs(js: string, origin?: URL) { + const htmlcomment = //g; + js = js.replace(htmlcomment, ""); try { const ast = parseModule(js, { module: true, webcompat: true }); - + const identifierList = [ "window", "self", @@ -33,12 +35,12 @@ export function rewriteJs(js: string, origin?: URL) { "top", "location" ] - + const customTraveler = makeTraveler({ ImportDeclaration: (node: ESTree.ImportDeclaration) => { node.source.value = encodeUrl(node.source.value as string, origin); }, - + ImportExpression: (node: ESTree.ImportExpression) => { if (node.source.type === "Literal") { node.source.value = encodeUrl(node.source.value as string, origin); @@ -49,11 +51,11 @@ export function rewriteJs(js: string, origin?: URL) { node.source.name = `__wrapImport(${node.source.name})`; } }, - + ExportAllDeclaration: (node: ESTree.ExportAllDeclaration) => { node.source.value = encodeUrl(node.source.value as string, origin); }, - + ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => { // strings are Literals in ESTree syntax but these will always be strings if (node.source) node.source.value = encodeUrl(node.source.value as string, origin); @@ -76,16 +78,17 @@ export function rewriteJs(js: string, origin?: URL) { }, VariableDeclarator: (node: ESTree.VariableDeclarator) => { - if (node.init.type === "Identifier" && identifierList.includes(node.init.name)) { + if (node.init && node.init.type === "Identifier" && identifierList.includes(node.init.name)) { node.init.name = `$s(${node.init.name})`; } } }); - + customTraveler.go(ast); - + return generate(ast); - } catch { + } catch (e) { + console.error(e); console.log(js); return js; diff --git a/src/shared/rewriters/url.ts b/src/shared/rewriters/url.ts index 020f97a..16f6f39 100644 --- a/src/shared/rewriters/url.ts +++ b/src/shared/rewriters/url.ts @@ -1,3 +1,4 @@ +import { URL } from "../../client/url"; import { rewriteJs } from "./js"; function canParseUrl(url: string, origin?: URL) { @@ -42,4 +43,4 @@ export function decodeUrl(url: string | URL) { } else { return url; } -} \ No newline at end of file +} diff --git a/tests/location.html b/tests/location.html new file mode 100644 index 0000000..601ba88 --- /dev/null +++ b/tests/location.html @@ -0,0 +1,7 @@ + + +