From 98b5387a849d8e780d6c9f2941a1a0e5980a594a Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Mon, 21 Oct 2024 22:03:18 -0500 Subject: [PATCH 01/11] fix: eval not functioning properly --- package.json | 2 +- pnpm-lock.yaml | 44 +++++++++++++++++++-------------------- src/client/shared/wrap.ts | 2 +- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index ce74b09..677c574 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "@rspack/core": "^1.0.13", "@types/eslint": "^9.6.1", "@types/estree": "^1.0.6", - "@types/node": "^22.7.7", + "@types/node": "^22.7.8", "@types/serviceworker": "^0.0.101", "@typescript-eslint/eslint-plugin": "^8.11.0", "@typescript-eslint/parser": "^8.11.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e59c08f..c0cd50f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -67,8 +67,8 @@ importers: specifier: ^1.0.6 version: 1.0.6 '@types/node': - specifier: ^22.7.7 - version: 22.7.7 + specifier: ^22.7.8 + version: 22.7.8 '@types/serviceworker': specifier: ^0.0.101 version: 0.0.101 @@ -451,8 +451,8 @@ packages: '@types/node-forge@1.3.11': resolution: {integrity: sha512-FQx220y22OKNTqaByeBGqHWYz4cl94tpcxeFdvBo3wjG6XPBuZ0BNgNZRV5J5TFmmcsJ4IzsLkmGRiQbnYsBEQ==} - '@types/node@22.7.7': - resolution: {integrity: sha512-SRxCrrg9CL/y54aiMCG3edPKdprgMVGDXjA3gB8UmmBW5TcXzRUYAh8EWzTnSJFAd1rgImPELza+A3bJ+qxz8Q==} + '@types/node@22.7.8': + resolution: {integrity: sha512-a922jJy31vqR5sk+kAdIENJjHblqcZ4RmERviFsER4WJcEONqxKcjNOlk0q7OUfrF5sddT+vng070cdfMlrPLg==} '@types/qs@6.9.16': resolution: {integrity: sha512-7i+zxXdPD0T4cKDuxCUXJ4wHcsJLwENa6Z3dCu8cfCK743OGy5Nu1RmAGqDPsoTDINVEcdXKRvR/zre+P2Ku1A==} @@ -2816,26 +2816,26 @@ snapshots: '@types/body-parser@1.19.5': dependencies: '@types/connect': 3.4.38 - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@types/bonjour@3.5.13': dependencies: - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@types/connect-history-api-fallback@1.5.4': dependencies: '@types/express-serve-static-core': 5.0.0 - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@types/connect@3.4.38': dependencies: - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@types/cookie@0.4.1': {} '@types/cors@2.8.17': dependencies: - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@types/eslint@9.6.1': dependencies: @@ -2848,14 +2848,14 @@ snapshots: '@types/express-serve-static-core@4.19.6': dependencies: - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@types/qs': 6.9.16 '@types/range-parser': 1.2.7 '@types/send': 0.17.4 '@types/express-serve-static-core@5.0.0': dependencies: - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@types/qs': 6.9.16 '@types/range-parser': 1.2.7 '@types/send': 0.17.4 @@ -2870,27 +2870,27 @@ snapshots: '@types/fs-extra@11.0.4': dependencies: '@types/jsonfile': 6.1.4 - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@types/http-errors@2.0.4': {} '@types/http-proxy@1.17.15': dependencies: - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@types/json-schema@7.0.15': {} '@types/jsonfile@6.1.4': dependencies: - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@types/mime@1.3.5': {} '@types/node-forge@1.3.11': dependencies: - '@types/node': 22.7.7 + '@types/node': 22.7.8 - '@types/node@22.7.7': + '@types/node@22.7.8': dependencies: undici-types: 6.19.8 @@ -2905,7 +2905,7 @@ snapshots: '@types/send@0.17.4': dependencies: '@types/mime': 1.3.5 - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@types/serve-index@1.9.4': dependencies: @@ -2914,14 +2914,14 @@ snapshots: '@types/serve-static@1.15.7': dependencies: '@types/http-errors': 2.0.4 - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@types/send': 0.17.4 '@types/serviceworker@0.0.101': {} '@types/sockjs@0.3.36': dependencies: - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@types/tapable@2.2.7': dependencies: @@ -2929,7 +2929,7 @@ snapshots: '@types/ws@8.5.12': dependencies: - '@types/node': 22.7.7 + '@types/node': 22.7.8 '@typescript-eslint/eslint-plugin@8.11.0(@typescript-eslint/parser@8.11.0(eslint@9.13.0)(typescript@5.6.3))(eslint@9.13.0)(typescript@5.6.3)': dependencies: @@ -3480,7 +3480,7 @@ snapshots: dependencies: '@types/cookie': 0.4.1 '@types/cors': 2.8.17 - '@types/node': 22.7.7 + '@types/node': 22.7.8 accepts: 1.3.8 base64id: 2.0.0 cookie: 0.4.2 @@ -4022,7 +4022,7 @@ snapshots: jest-worker@27.5.1: dependencies: - '@types/node': 22.7.7 + '@types/node': 22.7.8 merge-stream: 2.0.0 supports-color: 8.1.1 diff --git a/src/client/shared/wrap.ts b/src/client/shared/wrap.ts index d8d57bd..705d9ec 100644 --- a/src/client/shared/wrap.ts +++ b/src/client/shared/wrap.ts @@ -9,7 +9,7 @@ export function createWrapFn(client: ScramjetClient, self: typeof globalThis) { return function (identifier: any) { if (identifier === self) return client.globalProxy; if (identifier === self.location) return client.locationProxy; - if (identifier === eval) return indirectEval; + if (identifier === eval) return indirectEval.bind(client); if (iswindow) { if (identifier === self.parent) { From e1a71bef1dce041a05ced7afe69e2f614cf95647 Mon Sep 17 00:00:00 2001 From: wearrrrr Date: Mon, 21 Oct 2024 22:55:44 -0500 Subject: [PATCH 02/11] log when oxc or scramjet internal error happens in test suite --- playwright.config.ts | 2 +- src/client/client.ts | 2 +- tests/util/inspectConsole.ts | 17 +++++++++++++++++ tests/util/setupPage.ts | 6 ++++++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/util/inspectConsole.ts diff --git a/playwright.config.ts b/playwright.config.ts index 001283a..75e9121 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -7,7 +7,7 @@ export default defineConfig({ testDir: "./tests", fullyParallel: true, forbidOnly: !!process.env.CI, - retries: 0, + retries: 2, reporter: "html", timeout: 20000, /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ diff --git a/src/client/client.ts b/src/client/client.ts index 3ab3964..ee1381b 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -328,7 +328,7 @@ export class ScramjetClient { if ((err.stack as any) instanceof Object) { //@ts-expect-error i'm not going to explain this err.stack = err.stack.stack; - console.error("ERROR FROM SCRMAJET INTERNALS", err); + console.error("ERROR FROM SCRAMJET INTERNALS", err); } else { throw err; } diff --git a/tests/util/inspectConsole.ts b/tests/util/inspectConsole.ts new file mode 100644 index 0000000..54780a9 --- /dev/null +++ b/tests/util/inspectConsole.ts @@ -0,0 +1,17 @@ +import { Page } from "@playwright/test"; + +export function registerInspect(page: Page) { + let hasOxcError = false; + let hasScramjetError = false; + page.on("console", async (msg) => { + if (msg.type() === "error") { + if (msg.text().includes("oxc parse error") && !hasOxcError) { + hasOxcError = true; + console.log("OXC parse error detected! Please review manually.") + } else if (msg.text().includes("ERROR FROM SCRAMJET INTERNALS") && !hasScramjetError) { + hasScramjetError = true; + console.log("Scramjet error detected! Please review manually."); + } + } + }); +} \ No newline at end of file diff --git a/tests/util/setupPage.ts b/tests/util/setupPage.ts index 77d5619..f91acdc 100644 --- a/tests/util/setupPage.ts +++ b/tests/util/setupPage.ts @@ -1,8 +1,11 @@ /* eslint-disable no-async-promise-executor */ import { expect, FrameLocator, Page } from "@playwright/test"; +import { registerInspect } from "./inspectConsole"; export function setupPage(page: Page, url: string): Promise { return new Promise(async (resolve) => { + // Hack to disable HTTP cache. + await page.route("**", route => route.continue()); // Goto base url defined in config. await page.goto("/"); await page.waitForSelector(".version > b"); @@ -18,6 +21,9 @@ export function setupPage(page: Page, url: string): Promise { await page.waitForTimeout(1000); await bar.press("Enter"); + + registerInspect(page); + resolve(frame); }); } \ No newline at end of file From 66929af43c65e68a55a40b85866ba896039c61a8 Mon Sep 17 00:00:00 2001 From: Toshit Chawda Date: Mon, 21 Oct 2024 22:27:55 -0700 Subject: [PATCH 03/11] return errors from rewriter, clean up code, update oxc --- rewriter/Cargo.lock | 207 ++++++++++++++++++++----------------- rewriter/Cargo.toml | 11 +- rewriter/src/error.rs | 2 + rewriter/src/lib.rs | 67 +++++++++++- rewriter/src/main.rs | 1 + rewriter/src/rewrite.rs | 95 ++--------------- src/shared/rewriters/js.ts | 27 ++++- 7 files changed, 218 insertions(+), 192 deletions(-) diff --git a/rewriter/Cargo.lock b/rewriter/Cargo.lock index 8f4c86f..a6bf6da 100644 --- a/rewriter/Cargo.lock +++ b/rewriter/Cargo.lock @@ -237,13 +237,14 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "compact_str" -version = "0.7.1" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f86b9c4c00838774a6d902ef931eff7470720c51d90c2e32cfe15dc304737b3f" +checksum = "6050c3a16ddab2e412160b31f2c871015704239bca62f72f6e5f0be631d3f644" dependencies = [ "castaway", "cfg-if", "itoa", + "rustversion", "ryu", "serde", "static_assertions", @@ -259,6 +260,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "cow-utils" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "417bef24afe1460300965a25ff4a24b8b45ad011948302ec221e8a0a81eb2c79" + [[package]] name = "crossbeam-utils" version = "0.8.20" @@ -352,19 +359,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "gloo-utils" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "037fcb07216cb3a30f7292bd0176b050b7b9a052ba830ef7d5d65f6dc64ba58e" -dependencies = [ - "js-sys", - "serde", - "serde_json", - "wasm-bindgen", - "web-sys", -] - [[package]] name = "hashbrown" version = "0.14.5" @@ -554,9 +548,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.159" +version = "0.2.161" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" [[package]] name = "litemap" @@ -606,7 +600,7 @@ dependencies = [ "owo-colors", "textwrap", "thiserror", - "unicode-width", + "unicode-width 0.1.14", ] [[package]] @@ -620,6 +614,12 @@ dependencies = [ "syn", ] +[[package]] +name = "nonmax" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "610a5acd306ec67f907abe5567859a3c693fb9886eb1f012ab8f2a47bef3db51" + [[package]] name = "num-bigint" version = "0.4.6" @@ -705,9 +705,9 @@ checksum = "fb37767f6569cd834a413442455e0f066d0d522de8630436e2a1761d9726ba56" [[package]] name = "oxc_allocator" -version = "0.20.0" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d413f72d3253e9adff573635cfb5aca6f092aaee8b4252ee2d02b36fccc8fd2c" +checksum = "40baa80a3e4ae7cea94c1e04dc8820162b934f5f5cbca8a96e80a0b85a2815f4" dependencies = [ "allocator-api2", "bumpalo", @@ -716,63 +716,92 @@ dependencies = [ [[package]] name = "oxc_ast" -version = "0.20.0" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c90e5316ea1cdc09844cacbd9d11dfdaf80894c84d8917a8021903238dfcb449" +checksum = "d7eb50a1c3485609b4b2051bd23352c1e6a8743b9e345dd6def70b3470948429" dependencies = [ "bitflags", "num-bigint", "oxc_allocator", "oxc_ast_macros", + "oxc_estree", + "oxc_regular_expression", "oxc_span", "oxc_syntax", "serde", "serde_json", - "tsify", "wasm-bindgen", ] [[package]] name = "oxc_ast_macros" -version = "0.20.0" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e75a62baf313dede70692fe7b0dbb84bfdb8cddb118020067b924ee759b08a4e" - -[[package]] -name = "oxc_diagnostics" -version = "0.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2963694eba6acab881fe014b843757b57f8bdb755f6014fb7f92790122bd57d" +checksum = "0a4b29106d9f2bbcca4f5ee7a9325c99844ff9721d10103625cd5278795507fe" dependencies = [ - "miette", - "owo-colors", - "textwrap", - "unicode-width", + "proc-macro2", + "quote", + "syn", ] [[package]] -name = "oxc_index" -version = "0.20.0" +name = "oxc_diagnostics" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbd279d0188e75bb7268c3d8f54a2b30837ea03135b739b90fb8360dd9f2add8" +checksum = "f2e7c7ff14a1a80cb229593d9fcfae6660c7b2cda8707caa4a89cb489656d156" +dependencies = [ + "miette", + "owo-colors", + "rustc-hash", + "textwrap", + "unicode-width 0.2.0", +] + +[[package]] +name = "oxc_ecmascript" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17d5cf1710748afcda1269998d2003af79df1fef7c5b4183054aeb1b2a803ff6" +dependencies = [ + "num-bigint", + "num-traits", + "oxc_ast", + "oxc_span", + "oxc_syntax", +] + +[[package]] +name = "oxc_estree" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "befa5f762524cdd768fee0321c4f19ee24e13e7340ca2a64629183a37875ba71" + +[[package]] +name = "oxc_index" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68a576b0f0a1ef9112504880a3ba82fa895c4cceb52cdc37825180c43b01336f" dependencies = [ "serde", ] [[package]] name = "oxc_parser" -version = "0.20.0" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7683a18fa64b8d7756b8a5630e84f075b4cd1dcf85adb04044055c33120e5f9" +checksum = "f43e722cd698a1ac383b645ddf6283757d4977b56f6dc278038b9ea74867a508" dependencies = [ "assert-unchecked", "bitflags", + "cow-utils", "memchr", "num-bigint", "num-traits", "oxc_allocator", "oxc_ast", "oxc_diagnostics", + "oxc_ecmascript", + "oxc_regular_expression", "oxc_span", "oxc_syntax", "rustc-hash", @@ -780,34 +809,57 @@ dependencies = [ ] [[package]] -name = "oxc_span" -version = "0.20.0" +name = "oxc_regular_expression" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efafab0c8cfb6ac6da1b687fef84424d242437f4cfbed51ddfba8ac046ca4a93" +checksum = "231ed7fd6173c41270e019c4d1b2cd4bfa463a8abba32e0107bed6a7e9ceca93" +dependencies = [ + "oxc_allocator", + "oxc_ast_macros", + "oxc_diagnostics", + "oxc_estree", + "oxc_span", + "phf", + "rustc-hash", + "serde", + "unicode-id-start", + "wasm-bindgen", +] + +[[package]] +name = "oxc_span" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "734602328c49aa8c31840e72662196ca1b0d7bf3585eca1f9ddbef127172112f" dependencies = [ "compact_str", "miette", "oxc_allocator", + "oxc_ast_macros", + "oxc_estree", "serde", - "tsify", "wasm-bindgen", ] [[package]] name = "oxc_syntax" -version = "0.20.0" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ff2ce5c9adcdaee8dc28834ff7a4d9e33b0ecd9c9ec56a5066703cdd3426bb3" +checksum = "6a56d62a59f4ace25365c1d056d7ce3129ca3fb20e9337b19a0efe62d6bc84cb" dependencies = [ + "assert-unchecked", "bitflags", "dashmap 6.1.0", + "nonmax", + "oxc_allocator", + "oxc_ast_macros", + "oxc_estree", "oxc_index", "oxc_span", "phf", "rustc-hash", "ryu-js", "serde", - "tsify", "unicode-id-start", "wasm-bindgen", ] @@ -917,9 +969,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.87" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3e4daa0dcf6feba26f985457cdf104d4b4256fc5a09547140f3631bb076b19a" +checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9" dependencies = [ "unicode-ident", ] @@ -993,6 +1045,7 @@ dependencies = [ "obfstr", "oxc_allocator", "oxc_ast", + "oxc_diagnostics", "oxc_parser", "oxc_span", "oxc_syntax", @@ -1072,22 +1125,11 @@ dependencies = [ "syn", ] -[[package]] -name = "serde_derive_internals" -version = "0.28.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e578a843d40b4189a4d66bba51d7684f57da5bd7c304c64e14bd63efbef49509" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "serde_json" -version = "1.0.128" +version = "1.0.132" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" +checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" dependencies = [ "itoa", "memchr", @@ -1133,9 +1175,9 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "syn" -version = "2.0.79" +version = "2.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" +checksum = "83540f837a8afc019423a8edb95b52a8effe46957ee402287f4292fae35be021" dependencies = [ "proc-macro2", "quote", @@ -1167,7 +1209,7 @@ checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9" dependencies = [ "smawk", "unicode-linebreak", - "unicode-width", + "unicode-width 0.1.14", ] [[package]] @@ -1272,31 +1314,6 @@ dependencies = [ "winnow", ] -[[package]] -name = "tsify" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6b26cf145f2f3b9ff84e182c448eaf05468e247f148cf3d2a7d67d78ff023a0" -dependencies = [ - "gloo-utils", - "serde", - "serde_json", - "tsify-macros", - "wasm-bindgen", -] - -[[package]] -name = "tsify-macros" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a94b0f0954b3e59bfc2c246b4c8574390d94a4ad4ad246aaf2fb07d7dfd3b47" -dependencies = [ - "proc-macro2", - "quote", - "serde_derive_internals", - "syn", -] - [[package]] name = "unicode-bidi" version = "0.3.17" @@ -1305,9 +1322,9 @@ checksum = "5ab17db44d7388991a428b2ee655ce0c212e862eff1768a455c58f9aad6e7893" [[package]] name = "unicode-id-start" -version = "1.3.0" +version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97e2a3c5fc9de285c0e805d98eba666adb4b2d9e1049ce44821ff7707cc34e91" +checksum = "2f322b60f6b9736017344fa0635d64be2f458fbc04eef65f6be22976dd1ffd5b" [[package]] name = "unicode-ident" @@ -1336,6 +1353,12 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" +[[package]] +name = "unicode-width" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd" + [[package]] name = "url" version = "2.5.2" diff --git a/rewriter/Cargo.toml b/rewriter/Cargo.toml index 9b0010b..6ad7cd8 100644 --- a/rewriter/Cargo.toml +++ b/rewriter/Cargo.toml @@ -27,11 +27,12 @@ console_error_panic_hook = "0.1.7" getrandom = { version = "0.2.15", features = ["js"] } js-sys = "0.3.69" obfstr = "0.4.3" -oxc_allocator = "0.20.0" -oxc_ast = { version = "0.20.0", features = ["serialize"]} -oxc_parser = { version = "0.20.0" } -oxc_span = "0.20.0" -oxc_syntax = "0.20.0" +oxc_allocator = "0.32.0" +oxc_ast = { version = "0.32.0", features = ["serialize"]} +oxc_diagnostics = "0.32.0" +oxc_parser = "0.32.0" +oxc_span = "0.32.0" +oxc_syntax = "0.32.0" rand = "0.8.5" serde = "1.0.204" serde-wasm-bindgen = "0.6.5" diff --git a/rewriter/src/error.rs b/rewriter/src/error.rs index 217d7c4..d920d1c 100644 --- a/rewriter/src/error.rs +++ b/rewriter/src/error.rs @@ -9,6 +9,8 @@ pub enum RewriterError { Url(#[from] url::ParseError), #[error("str fromutf8 error: {0}")] Str(#[from] std::str::Utf8Error), + #[error("reflect set failed: {0}")] + ReflectSetFail(String), #[error("{0} was not {1}")] Not(String, &'static str), diff --git a/rewriter/src/lib.rs b/rewriter/src/lib.rs index 8126378..87eaf4f 100644 --- a/rewriter/src/lib.rs +++ b/rewriter/src/lib.rs @@ -1,14 +1,26 @@ pub mod error; pub mod rewrite; -use std::{panic, str::FromStr}; +use std::{panic, str::FromStr, sync::Arc}; use error::{Result, RewriterError}; use js_sys::{Function, Object, Reflect}; +use oxc_diagnostics::{NamedSource, OxcDiagnostic}; use rewrite::{rewrite, Config, EncodeFn}; use url::Url; use wasm_bindgen::prelude::*; +#[wasm_bindgen(typescript_custom_section)] +const REWRITER_OUTPUT: &'static str = r#" +type RewriterOutput = { js: Uint8Array, errors: string[] }; +"#; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(typescript_type = "RewriterOutput")] + pub type RewriterOutput; +} + #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_namespace = console)] @@ -43,6 +55,14 @@ fn get_str(obj: &JsValue, k: &str) -> Result { .ok_or_else(|| RewriterError::not_str(k, obj)) } +fn set_obj(obj: &Object, k: &str, v: &JsValue) -> Result<()> { + if !Reflect::set(&obj.into(), &k.into(), v)? { + Err(RewriterError::ReflectSetFail(k.to_string())) + } else { + Ok(()) + } +} + fn get_flag(scramjet: &Object, url: &str, flag: &str) -> Result { let fenabled = get_obj(scramjet, "flagEnabled")? .dyn_into::() @@ -90,18 +110,51 @@ fn drmcheck() -> bool { return vec![obfstr!("http://localhost:1337")].contains(&true_origin.as_str()); } +fn create_rewriter_output( + out: (Vec, Vec), + url: String, + src: String, +) -> Result { + let src = Arc::new(NamedSource::new(url, src).with_language("javascript")); + let errs: Vec<_> = out + .1 + .into_iter() + .map(|x| format!("{:?}", x.with_source_code(src.clone()))) + .collect(); + + let obj = Object::new(); + set_obj(&obj, "js", &JsValue::from(out.0))?; + set_obj(&obj, "errors", &JsValue::from(errs))?; + + Ok(RewriterOutput::from(JsValue::from(obj))) +} + #[wasm_bindgen] -pub fn rewrite_js(js: &str, url: &str, scramjet: &Object) -> Result> { +pub fn rewrite_js( + js: &str, + url: &str, + script_url: String, + scramjet: &Object, +) -> Result { #[cfg(feature = "drm")] if !drmcheck() { return Vec::new(); } - rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?) + create_rewriter_output( + rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?, + script_url, + js.to_string(), + ) } #[wasm_bindgen] -pub fn rewrite_js_from_arraybuffer(js: &[u8], url: &str, scramjet: &Object) -> Result> { +pub fn rewrite_js_from_arraybuffer( + js: &[u8], + url: &str, + script_url: String, + scramjet: &Object, +) -> Result { #[cfg(feature = "drm")] if !drmcheck() { return Vec::new(); @@ -110,5 +163,9 @@ pub fn rewrite_js_from_arraybuffer(js: &[u8], url: &str, scramjet: &Object) -> R // we know that this is a valid utf-8 string let js = unsafe { std::str::from_utf8_unchecked(js) }; - rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?) + create_rewriter_output( + rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?, + script_url, + js.to_string(), + ) } diff --git a/rewriter/src/main.rs b/rewriter/src/main.rs index 504037f..5948d34 100644 --- a/rewriter/src/main.rs +++ b/rewriter/src/main.rs @@ -130,6 +130,7 @@ fn dorewrite(source_text: &str) -> Result { strict_rewrites: true, }, )? + .0 .as_slice(), )? .to_string()) diff --git a/rewriter/src/rewrite.rs b/rewriter/src/rewrite.rs index 7ae75d6..d32288d 100644 --- a/rewriter/src/rewrite.rs +++ b/rewriter/src/rewrite.rs @@ -9,8 +9,9 @@ use oxc_ast::{ visit::walk, Visit, }; +use oxc_diagnostics::OxcDiagnostic; use oxc_parser::Parser; -use oxc_span::{Atom, SourceType, Span}; +use oxc_span::{Atom, GetSpan, SourceType, Span}; use oxc_syntax::operator::{AssignmentOperator, UnaryOperator}; use url::Url; @@ -148,7 +149,7 @@ impl<'a> Visit<'a> for Rewriter { && !matches!(s.object, Expression::MetaProperty(_)) && !matches!(s.object, Expression::Super(_)) { - let span = expression_span(&s.object); + let span = s.object.span(); self.jschanges.push(JsChange::GenericChange { span: Span::new(span.start, span.start), text: " $scramitize(".to_string(), @@ -359,7 +360,7 @@ impl<'a> Visit<'a> for Rewriter { self.jschanges.push(JsChange::Assignment { name: s.name.to_string(), entirespan: it.span, - rhsspan: expression_span(&it.right), + rhsspan: it.right.span(), op: it.operator, }); @@ -383,55 +384,6 @@ impl<'a> Visit<'a> for Rewriter { } } -fn expression_span(e: &Expression) -> Span { - // enums.split("\n").filter(f=>f).map(p=>p.trimLeft()).filter(p=>!p.startsWith("#")).map(p=>p.replace(/\(.*/,"")).map(p=>`E::${p}(s) => s.span`).join(",\n") - use Expression as E; - match e { - E::BooleanLiteral(s) => s.span, - E::NullLiteral(s) => s.span, - E::NumericLiteral(s) => s.span, - E::BigIntLiteral(s) => s.span, - E::RegExpLiteral(s) => s.span, - E::StringLiteral(s) => s.span, - E::TemplateLiteral(s) => s.span, - E::Identifier(s) => s.span, - E::MetaProperty(s) => s.span, - E::Super(s) => s.span, - E::ArrayExpression(s) => s.span, - E::ArrowFunctionExpression(s) => s.span, - E::AssignmentExpression(s) => s.span, - E::AwaitExpression(s) => s.span, - E::BinaryExpression(s) => s.span, - E::CallExpression(s) => s.span, - E::ChainExpression(s) => s.span, - E::ClassExpression(s) => s.span, - E::ConditionalExpression(s) => s.span, - E::FunctionExpression(s) => s.span, - E::ImportExpression(s) => s.span, - E::LogicalExpression(s) => s.span, - E::NewExpression(s) => s.span, - E::ObjectExpression(s) => s.span, - E::ParenthesizedExpression(s) => s.span, - E::SequenceExpression(s) => s.span, - E::TaggedTemplateExpression(s) => s.span, - E::ThisExpression(s) => s.span, - E::UnaryExpression(s) => s.span, - E::UpdateExpression(s) => s.span, - E::YieldExpression(s) => s.span, - E::PrivateInExpression(s) => s.span, - E::JSXElement(s) => s.span, - E::JSXFragment(s) => s.span, - E::TSAsExpression(s) => s.span, - E::TSSatisfiesExpression(s) => s.span, - E::TSTypeAssertion(s) => s.span, - E::TSNonNullExpression(s) => s.span, - E::TSInstantiationExpression(s) => s.span, - E::ComputedMemberExpression(s) => s.span, - E::StaticMemberExpression(s) => s.span, - E::PrivateFieldExpression(s) => s.span, - } -} - // js MUST not be able to get a reference to any of these because sbx const UNSAFE_GLOBALS: &[&str] = &[ "window", @@ -459,19 +411,11 @@ fn random_string() -> String { .to_string() } -pub fn rewrite(js: &str, url: Url, config: Config) -> Result> { +pub fn rewrite(js: &str, url: Url, config: Config) -> Result<(Vec, Vec)> { let allocator = Allocator::default(); let source_type = SourceType::default(); let ret = Parser::new(&allocator, js, source_type).parse(); - for err in ret.errors { - let cloned = js.to_string(); - let err = err.with_source_code(cloned); - println!("oxc parse error {err:?}"); - #[cfg(target_family = "wasm")] - crate::error(&format!("oxc parse error {err:?}")) - } - let program = ret.program; let sourcetag = random_string(); @@ -575,9 +519,9 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Result> { format!( "((t)=>$scramjet$tryset({},\"{}\",t)||({}{}t))({})", name, - fmt_op(*op), + op.as_str(), name, - fmt_op(*op), + op.as_str(), &js[rhsspan.start as usize..rhsspan.end as usize] ) .as_bytes(), @@ -607,10 +551,10 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Result> { sourcemap.extend_from_slice(&buffer); - return Ok(sourcemap); + return Ok((sourcemap, ret.errors)); } - Ok(buffer) + Ok((buffer, ret.errors)) } fn json_escape_string(s: &str) -> String { @@ -629,24 +573,3 @@ fn json_escape_string(s: &str) -> String { } out } - -fn fmt_op(op: AssignmentOperator) -> &'static str { - match op { - AssignmentOperator::Assign => "=", - AssignmentOperator::Addition => "+=", - AssignmentOperator::Subtraction => "-=", - AssignmentOperator::Multiplication => "*=", - AssignmentOperator::Division => "/=", - AssignmentOperator::Remainder => "%=", - AssignmentOperator::Exponential => "**=", - AssignmentOperator::ShiftLeft => "<<=", - AssignmentOperator::ShiftRight => ">>=", - AssignmentOperator::ShiftRightZeroFill => ">>>=", - AssignmentOperator::BitwiseAnd => "&=", - AssignmentOperator::BitwiseXOR => "^=", - AssignmentOperator::BitwiseOR => "|=", - AssignmentOperator::LogicalAnd => "&&=", - AssignmentOperator::LogicalOr => "||=", - AssignmentOperator::LogicalNullish => "??=", - } -} diff --git a/src/shared/rewriters/js.ts b/src/shared/rewriters/js.ts index 5ccf61e..ad863fa 100644 --- a/src/shared/rewriters/js.ts +++ b/src/shared/rewriters/js.ts @@ -19,6 +19,15 @@ init(); Error.stackTraceLimit = 50; +function print_errors(errors: string[]) { + // TODO: maybe make this a scram flag? + if (true) { + for (const error of errors) { + console.error("oxc parse error", error); + } + } +} + export function rewriteJs(js: string | ArrayBuffer, meta: URLMeta) { if (flagEnabled("naiiveRewriter", meta.origin)) { const text = typeof js === "string" ? js : new TextDecoder().decode(js); @@ -30,13 +39,23 @@ export function rewriteJs(js: string | ArrayBuffer, meta: URLMeta) { // const before = performance.now(); if (typeof js === "string") { - js = new TextDecoder().decode(rewrite_js(js, meta.base.href, $scramjet)); - } else { - js = rewrite_js_from_arraybuffer( - new Uint8Array(js), + let { js: js_out, errors } = rewrite_js( + js, meta.base.href, + "PERCS_PLEASE_FILL_THIS_IN.js", $scramjet ); + js = new TextDecoder().decode(js_out); + print_errors(errors); + } else { + let { js: js_out, errors } = rewrite_js_from_arraybuffer( + new Uint8Array(js), + meta.base.href, + "PERCS_PLEASE_FILL_THIS_IN.js", + $scramjet + ); + js = js_out; + print_errors(errors); } // const after = performance.now(); From cd8495b7ae20deb779a576348ac64ccded116a52 Mon Sep 17 00:00:00 2001 From: Toshit Chawda Date: Mon, 21 Oct 2024 23:10:49 -0700 Subject: [PATCH 04/11] add measuring of the rewrite() function --- rewriter/Cargo.lock | 13 ++++++++ rewriter/Cargo.toml | 1 + rewriter/src/lib.rs | 35 ++++++++++++-------- src/shared/rewriters/js.ts | 65 ++++++++++++++++++++++++-------------- 4 files changed, 76 insertions(+), 38 deletions(-) diff --git a/rewriter/Cargo.lock b/rewriter/Cargo.lock index a6bf6da..2b78e65 100644 --- a/rewriter/Cargo.lock +++ b/rewriter/Cargo.lock @@ -513,6 +513,18 @@ dependencies = [ "hashbrown 0.15.0", ] +[[package]] +name = "instant" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "intrusive-collections" version = "0.9.7" @@ -1041,6 +1053,7 @@ dependencies = [ "boa_engine", "console_error_panic_hook", "getrandom", + "instant", "js-sys", "obfstr", "oxc_allocator", diff --git a/rewriter/Cargo.toml b/rewriter/Cargo.toml index 6ad7cd8..75ade9e 100644 --- a/rewriter/Cargo.toml +++ b/rewriter/Cargo.toml @@ -25,6 +25,7 @@ panic = "abort" [dependencies] console_error_panic_hook = "0.1.7" getrandom = { version = "0.2.15", features = ["js"] } +instant = { version = "0.1.13", features = ["wasm-bindgen"] } js-sys = "0.3.69" obfstr = "0.4.3" oxc_allocator = "0.32.0" diff --git a/rewriter/src/lib.rs b/rewriter/src/lib.rs index 87eaf4f..0621e85 100644 --- a/rewriter/src/lib.rs +++ b/rewriter/src/lib.rs @@ -1,9 +1,10 @@ pub mod error; pub mod rewrite; -use std::{panic, str::FromStr, sync::Arc}; +use std::{panic, str::FromStr, sync::Arc, time::Duration}; use error::{Result, RewriterError}; +use instant::Instant; use js_sys::{Function, Object, Reflect}; use oxc_diagnostics::{NamedSource, OxcDiagnostic}; use rewrite::{rewrite, Config, EncodeFn}; @@ -12,7 +13,7 @@ use wasm_bindgen::prelude::*; #[wasm_bindgen(typescript_custom_section)] const REWRITER_OUTPUT: &'static str = r#" -type RewriterOutput = { js: Uint8Array, errors: string[] }; +type RewriterOutput = { js: Uint8Array, errors: string[], duration: bigint }; "#; #[wasm_bindgen] @@ -110,10 +111,15 @@ fn drmcheck() -> bool { return vec![obfstr!("http://localhost:1337")].contains(&true_origin.as_str()); } +fn duration_to_millis_f64(duration: Duration) -> f64 { + (duration.as_secs() as f64) * 1_000f64 + (duration.subsec_nanos() as f64) / 1_000_000f64 +} + fn create_rewriter_output( out: (Vec, Vec), url: String, src: String, + duration: Duration, ) -> Result { let src = Arc::new(NamedSource::new(url, src).with_language("javascript")); let errs: Vec<_> = out @@ -123,8 +129,9 @@ fn create_rewriter_output( .collect(); let obj = Object::new(); - set_obj(&obj, "js", &JsValue::from(out.0))?; - set_obj(&obj, "errors", &JsValue::from(errs))?; + set_obj(&obj, "js", &out.0.into())?; + set_obj(&obj, "errors", &errs.into())?; + set_obj(&obj, "duration", &duration_to_millis_f64(duration).into())?; Ok(RewriterOutput::from(JsValue::from(obj))) } @@ -141,11 +148,11 @@ pub fn rewrite_js( return Vec::new(); } - create_rewriter_output( - rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?, - script_url, - js.to_string(), - ) + let before = Instant::now(); + let out = rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?; + let after = Instant::now(); + + create_rewriter_output(out, script_url, js.to_string(), after - before) } #[wasm_bindgen] @@ -163,9 +170,9 @@ pub fn rewrite_js_from_arraybuffer( // we know that this is a valid utf-8 string let js = unsafe { std::str::from_utf8_unchecked(js) }; - create_rewriter_output( - rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?, - script_url, - js.to_string(), - ) + let before = Instant::now(); + let out = rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?; + let after = Instant::now(); + + create_rewriter_output(out, script_url, js.to_string(), after - before) } diff --git a/src/shared/rewriters/js.ts b/src/shared/rewriters/js.ts index ad863fa..498c554 100644 --- a/src/shared/rewriters/js.ts +++ b/src/shared/rewriters/js.ts @@ -6,6 +6,7 @@ import { initSync, rewrite_js, rewrite_js_from_arraybuffer, + RewriterOutput, } from "../../../rewriter/out/rewriter.js"; import { $scramjet, flagEnabled } from "../../scramjet"; @@ -19,13 +20,51 @@ init(); Error.stackTraceLimit = 50; -function print_errors(errors: string[]) { +const decoder = new TextDecoder(); + +function rewriteJsWrapper( + input: string | ArrayBuffer, + meta: URLMeta +): string | ArrayBuffer { + let out: RewriterOutput; + if (typeof input === "string") { + out = rewrite_js( + input, + meta.base.href, + "PERCS_PLEASE_FILL_THIS_IN.js", + $scramjet + ); + } else { + out = rewrite_js_from_arraybuffer( + new Uint8Array(input), + meta.base.href, + "PERCS_PLEASE_FILL_THIS_IN.js", + $scramjet + ); + } + const { js, errors, duration } = out; + // TODO: maybe make this a scram flag? if (true) { for (const error of errors) { console.error("oxc parse error", error); } } + + // TODO: maybe make this a scram flag? + if (true) { + let timespan: string; + if (duration < 1n) { + timespan = "BLAZINGLY FAST"; + } else if (duration < 500n) { + timespan = "decent speed"; + } else { + timespan = "really slow"; + } + console.log(`oxc rewrite was ${timespan} (${duration}ms)`); + } + + return typeof input === "string" ? decoder.decode(js) : js; } export function rewriteJs(js: string | ArrayBuffer, meta: URLMeta) { @@ -37,29 +76,7 @@ export function rewriteJs(js: string | ArrayBuffer, meta: URLMeta) { return rewriteJsNaiive(text); } - // const before = performance.now(); - if (typeof js === "string") { - let { js: js_out, errors } = rewrite_js( - js, - meta.base.href, - "PERCS_PLEASE_FILL_THIS_IN.js", - $scramjet - ); - js = new TextDecoder().decode(js_out); - print_errors(errors); - } else { - let { js: js_out, errors } = rewrite_js_from_arraybuffer( - new Uint8Array(js), - meta.base.href, - "PERCS_PLEASE_FILL_THIS_IN.js", - $scramjet - ); - js = js_out; - print_errors(errors); - } - // const after = performance.now(); - - // dbg.debug("Rewrite took", Math.floor((after - before) * 10) / 10, "ms"); + js = rewriteJsWrapper(js, meta); return js; } From 9166fed573946514e437fa0763ae3b27693fa5d5 Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:58:04 +0000 Subject: [PATCH 05/11] dev: add codespace basic setup script --- codespace-basic-setup.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 codespace-basic-setup.sh diff --git a/codespace-basic-setup.sh b/codespace-basic-setup.sh new file mode 100644 index 0000000..0b3a5d0 --- /dev/null +++ b/codespace-basic-setup.sh @@ -0,0 +1,13 @@ +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh +source "$HOME/.cargo/env" # to import rustup in current shell +pnpm i +cargo install wasm-bindgen-cli +VER=$(curl --silent -qI https://github.com/WebAssembly/binaryen/releases/latest | awk -F '/' '/^location/ {print substr($NF, 1, length($NF)-1)}'); \ +curl -LO https://github.com/WebAssembly/binaryen/releases/download/$VER/binaryen-${VER}-x86_64-linux.tar.gz +tar xvf binaryen-${VER}-x86_64-linux.tar.gz +rm -rf binaryen-${VER}-x86_64-linux.tar.gz +mv binaryen-${VER}/bin/* ~/.local/bin +mv binaryen-${VER}/lib/* ~/.local/lib +rm -rf binaryen-${VER} +pnpm rewriter:build +pnpm build \ No newline at end of file From e7bef2b1c0e6e87471ef7d6258db458792be52ff Mon Sep 17 00:00:00 2001 From: Toshit Chawda Date: Tue, 22 Oct 2024 17:45:11 -0700 Subject: [PATCH 06/11] accept owned version of bytes and string --- README.md | 4 ++-- rewriter/src/lib.rs | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b920082..c8da3f3 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ npm version -Scramjet is an experimental inteception based web proxy that aims to be the successor to Ultraviolet. It is designed with security, developer friendlyness, and performance in mind. Scramjet strives to have a clean, organized codebase to improve maintainability. Scramjet is made to evade internet censorship and bypass arbitrary web browser restrictions. +Scramjet is an experimental interception based web proxy that aims to be the successor to Ultraviolet. It is designed with security, developer friendliness, and performance in mind. Scramjet strives to have a clean, organized codebase to improve maintainability. Scramjet is made to evade internet censorship and bypass arbitrary web browser restrictions. ## Supported Sites @@ -35,7 +35,7 @@ Some of the popular websites that Scramjet supports include: #### Building - Clone the repository with `git clone --recursive https://github.com/MercuryWorkshop/scramjet` -- Then, install your dependencies with `pnpm i` +- Then, install the dependencies with `pnpm i` - After, build the rewriter with `pnpm rewriter:build` - Finally, build Scramjet with `pnpm build` diff --git a/rewriter/src/lib.rs b/rewriter/src/lib.rs index 0621e85..da833ec 100644 --- a/rewriter/src/lib.rs +++ b/rewriter/src/lib.rs @@ -138,7 +138,7 @@ fn create_rewriter_output( #[wasm_bindgen] pub fn rewrite_js( - js: &str, + js: String, url: &str, script_url: String, scramjet: &Object, @@ -149,15 +149,15 @@ pub fn rewrite_js( } let before = Instant::now(); - let out = rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?; + let out = rewrite(&js, Url::from_str(url)?, get_config(scramjet, url)?)?; let after = Instant::now(); - create_rewriter_output(out, script_url, js.to_string(), after - before) + create_rewriter_output(out, script_url, js, after - before) } #[wasm_bindgen] pub fn rewrite_js_from_arraybuffer( - js: &[u8], + js: Vec, url: &str, script_url: String, scramjet: &Object, @@ -168,11 +168,11 @@ pub fn rewrite_js_from_arraybuffer( } // we know that this is a valid utf-8 string - let js = unsafe { std::str::from_utf8_unchecked(js) }; + let js = unsafe { String::from_utf8_unchecked(js) }; let before = Instant::now(); - let out = rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?; + let out = rewrite(&js, Url::from_str(url)?, get_config(scramjet, url)?)?; let after = Instant::now(); - create_rewriter_output(out, script_url, js.to_string(), after - before) + create_rewriter_output(out, script_url, js, after - before) } From b98d57a5bfaee8309c1c541c482b2e6d18d47e65 Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Wed, 23 Oct 2024 09:59:35 -0500 Subject: [PATCH 07/11] feat: add back self.top to wrapfn --- src/client/shared/wrap.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/client/shared/wrap.ts b/src/client/shared/wrap.ts index 705d9ec..70d184a 100644 --- a/src/client/shared/wrap.ts +++ b/src/client/shared/wrap.ts @@ -22,9 +22,23 @@ export function createWrapFn(client: ScramjetClient, self: typeof globalThis) { } } else if (identifier === self.document) { return client.documentProxy; - } + } else if (identifier === self.top) { + // instead of returning top, we need to return the uppermost parent that's inside a scramjet context + let current = self.self; - // TODO .top + for (;;) { + const test = current.parent.self; + if (test === current) break; // there is no parent, actual or emulated. + + // ... then `test` represents a window outside of the proxy context, and therefore `current` is the topmost window in the proxy context + if (!(SCRAMJETCLIENT in test)) break; + + // test is also insde a proxy, so we should continue up the chain + current = test; + } + + return current[SCRAMJETCLIENT].globalProxy.window; + } } return identifier; From 2261d12064d9be25ea90ba47081be51e052966bc Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Wed, 23 Oct 2024 10:45:23 -0500 Subject: [PATCH 08/11] chore: cleanup --- src/client/client.ts | 2 +- src/client/shared/wrap.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/client/client.ts b/src/client/client.ts index ee1381b..3846d12 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -13,7 +13,7 @@ import { unrewriteUrl, rewriteUrl, } from "../shared"; -import { BareClient as BareClientType } from "@mercuryworkshop/bare-mux"; +import type { BareClient as BareClientType } from "@mercuryworkshop/bare-mux"; import { createWrapFn } from "./shared/wrap"; import { NavigateEvent } from "./events"; import type { URLMeta } from "../shared/rewriters/url"; diff --git a/src/client/shared/wrap.ts b/src/client/shared/wrap.ts index 70d184a..b4dcab5 100644 --- a/src/client/shared/wrap.ts +++ b/src/client/shared/wrap.ts @@ -58,6 +58,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) { Object.defineProperty(self, config.globals.wrapthisfn, { value: function (i) { if (i === self) return client.globalProxy; + return i; }, writable: false, From 5f70c1c0ac6f352622346528bb753fc84b72eed2 Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Wed, 23 Oct 2024 10:54:46 -0500 Subject: [PATCH 09/11] fix: fix history proxy --- src/client/dom/history.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/dom/history.ts b/src/client/dom/history.ts index 37ece22..df48d3a 100644 --- a/src/client/dom/history.ts +++ b/src/client/dom/history.ts @@ -3,7 +3,7 @@ import { rewriteUrl } from "../../shared"; import { UrlChangeEvent } from "../events"; export default function (client: ScramjetClient, self: typeof globalThis) { - client.Proxy("history.pushState", { + client.Proxy("History.prototype.pushState", { apply(ctx) { if (ctx.args[2]) ctx.args[2] = rewriteUrl(ctx.args[2], client.meta); ctx.call(); @@ -13,7 +13,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) { }, }); - client.Proxy("history.replaceState", { + client.Proxy("History.prototype.replaceState", { apply(ctx) { if (ctx.args[2]) ctx.args[2] = rewriteUrl(ctx.args[2], client.meta); ctx.call(); From c34d0a07e112d658886a52b112997fc3a9948fdf Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Wed, 23 Oct 2024 21:14:32 -0500 Subject: [PATCH 10/11] fix: turn on strict rewrites by default --- src/client/shared/requests/beacon.ts | 2 +- src/client/shared/requests/fetch.ts | 12 ------------ src/controller/index.ts | 2 +- 3 files changed, 2 insertions(+), 14 deletions(-) diff --git a/src/client/shared/requests/beacon.ts b/src/client/shared/requests/beacon.ts index f758521..6158463 100644 --- a/src/client/shared/requests/beacon.ts +++ b/src/client/shared/requests/beacon.ts @@ -2,7 +2,7 @@ import { rewriteUrl } from "../../../shared"; import { ScramjetClient } from "../../client"; export default function (client: ScramjetClient, self) { - client.Proxy("navigator.sendBeacon", { + client.Proxy("Navigator.prototype.sendBeacon", { apply(ctx) { ctx.args[0] = rewriteUrl(ctx.args[0], client.meta); }, diff --git a/src/client/shared/requests/fetch.ts b/src/client/shared/requests/fetch.ts index db056d1..a395342 100644 --- a/src/client/shared/requests/fetch.ts +++ b/src/client/shared/requests/fetch.ts @@ -16,12 +16,6 @@ export default function (client: ScramjetClient, _self: typeof globalThis) { }, }); - // client.Proxy("Headers", { - // construct(ctx) { - // ctx.args[0] = rewriteHeaders(ctx.args[0]); - // }, - // }); - client.Proxy("Request", { construct(ctx) { if (typeof ctx.args[0] === "string" || ctx.args[0] instanceof URL) { @@ -43,10 +37,4 @@ export default function (client: ScramjetClient, _self: typeof globalThis) { return unrewriteUrl(ctx.get() as string); }, }); - - // client.Proxy("Response.redirect", { - // apply(ctx) { - // ctx.args[0] = encodeUrl(ctx.args[0]); - // }, - // }); } diff --git a/src/controller/index.ts b/src/controller/index.ts index c307354..0c2e9d3 100644 --- a/src/controller/index.ts +++ b/src/controller/index.ts @@ -30,7 +30,7 @@ export class ScramjetController { serviceworkers: false, naiiveRewriter: false, captureErrors: true, - strictRewrites: false, + strictRewrites: true, syncxhr: false, cleanerrors: false, scramitize: false, From 78454d516c5f88abead6e3e6f8a3babc0b117885 Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Wed, 23 Oct 2024 23:31:32 -0500 Subject: [PATCH 11/11] fix: hack around dumb ff bug in demo sw --- static/sw.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/static/sw.js b/static/sw.js index 613e50c..96f779a 100644 --- a/static/sw.js +++ b/static/sw.js @@ -1,3 +1,11 @@ +// dumb hack to allow firefox to work (please dont do this in prod) +if (navigator.userAgent.includes("Firefox")) { + Object.defineProperty(globalThis, "crossOriginIsolated", { + value: true, + writable: false, + }); +} + importScripts( "/scram/scramjet.wasm.js", "/scram/scramjet.shared.js",