mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-12 22:10:01 -04:00
return errors from rewriter, clean up code, update oxc
This commit is contained in:
parent
e1a71bef1d
commit
66929af43c
7 changed files with 218 additions and 192 deletions
207
rewriter/Cargo.lock
generated
207
rewriter/Cargo.lock
generated
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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<String> {
|
|||
.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<bool> {
|
||||
let fenabled = get_obj(scramjet, "flagEnabled")?
|
||||
.dyn_into::<Function>()
|
||||
|
@ -90,18 +110,51 @@ fn drmcheck() -> bool {
|
|||
return vec![obfstr!("http://localhost:1337")].contains(&true_origin.as_str());
|
||||
}
|
||||
|
||||
fn create_rewriter_output(
|
||||
out: (Vec<u8>, Vec<OxcDiagnostic>),
|
||||
url: String,
|
||||
src: String,
|
||||
) -> Result<RewriterOutput> {
|
||||
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<Vec<u8>> {
|
||||
pub fn rewrite_js(
|
||||
js: &str,
|
||||
url: &str,
|
||||
script_url: String,
|
||||
scramjet: &Object,
|
||||
) -> Result<RewriterOutput> {
|
||||
#[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<Vec<u8>> {
|
||||
pub fn rewrite_js_from_arraybuffer(
|
||||
js: &[u8],
|
||||
url: &str,
|
||||
script_url: String,
|
||||
scramjet: &Object,
|
||||
) -> Result<RewriterOutput> {
|
||||
#[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(),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -130,6 +130,7 @@ fn dorewrite(source_text: &str) -> Result<String> {
|
|||
strict_rewrites: true,
|
||||
},
|
||||
)?
|
||||
.0
|
||||
.as_slice(),
|
||||
)?
|
||||
.to_string())
|
||||
|
|
|
@ -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<Vec<u8>> {
|
||||
pub fn rewrite(js: &str, url: Url, config: Config) -> Result<(Vec<u8>, Vec<OxcDiagnostic>)> {
|
||||
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<Vec<u8>> {
|
|||
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<Vec<u8>> {
|
|||
|
||||
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 => "??=",
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue