remove servo code from wasm rewriter

This commit is contained in:
Toshit Chawda 2024-12-13 23:02:27 -08:00
parent 6c437d528f
commit 509b10337a
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
12 changed files with 39 additions and 51 deletions

2
rewriter/Cargo.lock generated
View file

@ -1320,7 +1320,6 @@ dependencies = [
"oxc", "oxc",
"smallvec", "smallvec",
"thiserror 2.0.7", "thiserror 2.0.7",
"url",
] ]
[[package]] [[package]]
@ -1682,7 +1681,6 @@ dependencies = [
"oxc", "oxc",
"rewriter", "rewriter",
"thiserror 2.0.7", "thiserror 2.0.7",
"url",
"wasm-bindgen", "wasm-bindgen",
"web-sys", "web-sys",
] ]

View file

@ -11,4 +11,3 @@ panic = "abort"
[workspace.dependencies] [workspace.dependencies]
oxc = "0.41.0" oxc = "0.41.0"
url = "2.5.4"

View file

@ -6,8 +6,8 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.94" anyhow = "1.0.94"
oxc = { workspace = true } oxc = { workspace = true }
url = { workspace = true } rewriter = { version = "0.1.0", path = "../rewriter" }
rewriter = { version = "0.1.0", path = "../rewriter", features = ["debug"] } url = "2.5.4"
urlencoding = "2.1.3" urlencoding = "2.1.3"
[dev-dependencies] [dev-dependencies]

View file

@ -1,22 +1,21 @@
use std::str::FromStr;
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
use rewriter::{cfg::Config, rewrite}; use rewriter::{cfg::Config, rewrite};
use std::str::FromStr;
use url::Url; use url::Url;
use urlencoding::encode; use urlencoding::encode;
fn encode_string(str: String) -> String {
encode(&str).to_string()
}
pub fn bench(c: &mut Criterion) { pub fn bench(c: &mut Criterion) {
let discord = include_str!("../sample/discord.js"); let discord = include_str!("../sample/discord.js");
let google = include_str!("../sample/google.js"); let google = include_str!("../sample/google.js");
let url = Url::from_str("https://google.com/glorngle/si.js").expect("failed to make url");
let cfg = Config { let cfg = Config {
prefix: "/scrammedjet/".to_string(), prefix: "/scrammedjet/".to_string(),
encoder: Box::new(encode_string),
base: Url::from_str("https://google.com/glorngle/si.js").expect("invalid base"), base: url.to_string(),
urlrewriter: Box::new(move |x: String| encode(url.join(&x).unwrap().as_str()).to_string()),
sourcetag: "glongle1".to_string(), sourcetag: "glongle1".to_string(),
wrapfn: "$wrap".to_string(), wrapfn: "$wrap".to_string(),

View file

@ -6,18 +6,17 @@ use rewriter::{cfg::Config, rewrite, RewriteResult};
use url::Url; use url::Url;
use urlencoding::encode; use urlencoding::encode;
fn encode_string(str: String) -> String {
encode(&str).to_string()
}
fn dorewrite(data: &str) -> Result<RewriteResult> { fn dorewrite(data: &str) -> Result<RewriteResult> {
let url = Url::from_str("https://google.com/glorngle/si.js").context("failed to make url")?;
rewrite( rewrite(
data, data,
Config { Config {
prefix: "/scrammedjet/".to_string(), prefix: "/scrammedjet/".to_string(),
encoder: Box::new(encode_string), base: url.to_string(),
urlrewriter: Box::new(move |x: String| {
encode(url.join(&x).unwrap().as_str()).to_string()
}),
base: Url::from_str("https://google.com/glorngle/si.js").context("invalid base")?,
sourcetag: "glongle1".to_string(), sourcetag: "glongle1".to_string(),
wrapfn: "$wrap".to_string(), wrapfn: "$wrap".to_string(),

View file

@ -5,9 +5,9 @@ edition = "2021"
[dependencies] [dependencies]
oxc = { workspace = true } oxc = { workspace = true }
url = { workspace = true }
smallvec = "1.13.2" smallvec = "1.13.2"
thiserror = "2.0.6" thiserror = "2.0.6"
[features] [features]
default = ["debug"]
debug = [] debug = []

View file

@ -1,5 +1,3 @@
use url::Url;
#[derive(Clone)] #[derive(Clone)]
pub struct Config<E> pub struct Config<E>
where where
@ -8,7 +6,7 @@ where
{ {
pub prefix: String, pub prefix: String,
pub sourcetag: String, pub sourcetag: String,
pub base: Url, pub base: String,
pub wrapfn: String, pub wrapfn: String,
pub wrapthisfn: String, pub wrapthisfn: String,
@ -18,7 +16,8 @@ where
pub metafn: String, pub metafn: String,
pub pushsourcemapfn: String, pub pushsourcemapfn: String,
pub encoder: E, /// URL REWRITER IS RESPONSIBLE FOR ADDING BASE
pub urlrewriter: E,
pub capture_errors: bool, pub capture_errors: bool,
pub scramitize: bool, pub scramitize: bool,

View file

@ -35,7 +35,6 @@ pub(crate) enum Rewrite {
/// `$scramerr(name)` /// `$scramerr(name)`
ScramErr { ScramErr {
span: Span, span: Span,
name: CompactStr,
}, },
/// `$scramitize(span)` /// `$scramitize(span)`
Scramitize { Scramitize {

View file

@ -49,9 +49,7 @@ where
E: Clone, E: Clone,
{ {
fn rewrite_url(&mut self, url: String) -> String { fn rewrite_url(&mut self, url: String) -> String {
let url = self.config.base.join(&url).unwrap(); let urlencoded = (self.config.urlrewriter)(url);
let urlencoded = (self.config.encoder)(url.to_string());
format!("\"{}{}\"", self.config.prefix, urlencoded) format!("\"{}{}\"", self.config.prefix, urlencoded)
} }
@ -233,10 +231,9 @@ where
if self.config.capture_errors { if self.config.capture_errors {
if let Some(h) = &it.handler { if let Some(h) = &it.handler {
if let Some(name) = &h.param { if let Some(name) = &h.param {
if let Some(name) = name.pattern.get_identifier() { if name.pattern.get_identifier().is_some() {
self.jschanges.add(Rewrite::ScramErr { self.jschanges.add(Rewrite::ScramErr {
span: Span::new(h.body.span.start + 1, h.body.span.start + 1), span: Span::new(h.body.span.start + 1, h.body.span.start + 1),
name: name.to_compact_str(),
}); });
} }
} }

View file

@ -10,8 +10,7 @@ crate-type = ["cdylib"]
instant = { version = "0.1.13", features = ["wasm-bindgen"] } instant = { version = "0.1.13", features = ["wasm-bindgen"] }
js-sys = "0.3.76" js-sys = "0.3.76"
oxc = { workspace = true } oxc = { workspace = true }
url = { workspace = true } rewriter = { version = "0.1.0", path = "../rewriter", default-features = false }
rewriter = { version = "0.1.0", path = "../rewriter" }
thiserror = "2.0.6" thiserror = "2.0.6"
wasm-bindgen = "0.2.99" wasm-bindgen = "0.2.99"
web-sys = { version = "0.3.76", features = ["Url"] } web-sys = { version = "0.3.76", features = ["Url"] }

View file

@ -7,8 +7,6 @@ use wasm_bindgen::{JsError, JsValue};
pub enum RewriterError { pub enum RewriterError {
#[error("JS: {0}")] #[error("JS: {0}")]
Js(String), Js(String),
#[error("URL parse error: {0}")]
Url(#[from] url::ParseError),
#[error("str fromutf8 error: {0}")] #[error("str fromutf8 error: {0}")]
Str(#[from] std::str::Utf8Error), Str(#[from] std::str::Utf8Error),
#[error("Rewriter: {0}")] #[error("Rewriter: {0}")]

View file

@ -1,14 +1,14 @@
pub mod error; pub mod error;
use std::{str::FromStr, sync::Arc, time::Duration}; use std::{sync::Arc, time::Duration};
use error::{Result, RewriterError}; use error::{Result, RewriterError};
use instant::Instant; use instant::Instant;
use js_sys::{Function, Object, Reflect}; use js_sys::{Function, Object, Reflect};
use oxc::diagnostics::NamedSource; use oxc::diagnostics::NamedSource;
use rewriter::{cfg::Config, rewrite, RewriteResult}; use rewriter::{cfg::Config, rewrite, RewriteResult};
use url::Url;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use web_sys::Url;
#[wasm_bindgen(typescript_custom_section)] #[wasm_bindgen(typescript_custom_section)]
const REWRITER_OUTPUT: &'static str = r#" const REWRITER_OUTPUT: &'static str = r#"
@ -39,12 +39,16 @@ extern "C" {
fn error(s: &str); fn error(s: &str);
} }
fn create_encode_function(encode: JsValue) -> Result<impl Fn(String) -> String + Clone> { fn create_encode_function(
encode: JsValue,
base: String,
) -> Result<impl Fn(String) -> String + Clone> {
let encode = encode.dyn_into::<Function>()?; let encode = encode.dyn_into::<Function>()?;
Ok(move |str: String| { Ok(move |str: String| {
let url = Url::new_with_base(&str, &base).unwrap().to_string();
encode encode
.call1(&JsValue::NULL, &str.into()) .call1(&JsValue::NULL, &url.into())
.unwrap() .unwrap()
.as_string() .as_string()
.unwrap() .unwrap()
@ -74,25 +78,20 @@ fn get_flag(scramjet: &Object, url: &str, flag: &str) -> Result<bool> {
let fenabled = get_obj(scramjet, "flagEnabled")? let fenabled = get_obj(scramjet, "flagEnabled")?
.dyn_into::<Function>() .dyn_into::<Function>()
.map_err(|_| RewriterError::not_fn("scramjet.flagEnabled"))?; .map_err(|_| RewriterError::not_fn("scramjet.flagEnabled"))?;
let ret = fenabled.call2( let ret = fenabled.call2(&JsValue::NULL, &flag.into(), &Url::new(url)?.into())?;
&JsValue::NULL,
&flag.into(),
&web_sys::Url::new(url)?.into(),
)?;
ret.as_bool() ret.as_bool()
.ok_or_else(|| RewriterError::not_bool("scramjet.flagEnabled return value")) .ok_or_else(|| RewriterError::not_bool("scramjet.flagEnabled return value"))
} }
fn get_config(scramjet: &Object, url: &str) -> Result<Config<impl Fn(String) -> String + Clone>> { fn get_config(scramjet: &Object, url: String) -> Result<Config<impl Fn(String) -> String + Clone>> {
let codec = &get_obj(scramjet, "codec")?; let codec = &get_obj(scramjet, "codec")?;
let config = &get_obj(scramjet, "config")?; let config = &get_obj(scramjet, "config")?;
let globals = &get_obj(config, "globals")?; let globals = &get_obj(config, "globals")?;
Ok(Config { Ok(Config {
prefix: get_str(config, "prefix")?, prefix: get_str(config, "prefix")?,
encoder: create_encode_function(get_obj(codec, "encode")?)?, base: url.clone(),
base: Url::from_str(url)?,
sourcetag: scramtag(), sourcetag: scramtag(),
wrapfn: get_str(globals, "wrapfn")?, wrapfn: get_str(globals, "wrapfn")?,
@ -103,10 +102,12 @@ fn get_config(scramjet: &Object, url: &str) -> Result<Config<impl Fn(String) ->
setrealmfn: get_str(globals, "setrealmfn")?, setrealmfn: get_str(globals, "setrealmfn")?,
pushsourcemapfn: get_str(globals, "pushsourcemapfn")?, pushsourcemapfn: get_str(globals, "pushsourcemapfn")?,
do_sourcemaps: get_flag(scramjet, url, "sourcemaps")?, do_sourcemaps: get_flag(scramjet, &url, "sourcemaps")?,
capture_errors: get_flag(scramjet, url, "captureErrors")?, capture_errors: get_flag(scramjet, &url, "captureErrors")?,
scramitize: get_flag(scramjet, url, "scramitize")?, scramitize: get_flag(scramjet, &url, "scramitize")?,
strict_rewrites: get_flag(scramjet, url, "strictRewrites")?, strict_rewrites: get_flag(scramjet, &url, "strictRewrites")?,
urlrewriter: create_encode_function(get_obj(codec, "encode")?, url)?,
}) })
} }
@ -142,7 +143,7 @@ fn create_rewriter_output(
#[wasm_bindgen] #[wasm_bindgen]
pub fn rewrite_js( pub fn rewrite_js(
js: String, js: String,
url: &str, url: String,
script_url: String, script_url: String,
scramjet: &Object, scramjet: &Object,
) -> Result<JsRewriterOutput> { ) -> Result<JsRewriterOutput> {
@ -156,7 +157,7 @@ pub fn rewrite_js(
#[wasm_bindgen] #[wasm_bindgen]
pub fn rewrite_js_from_arraybuffer( pub fn rewrite_js_from_arraybuffer(
js: Vec<u8>, js: Vec<u8>,
url: &str, url: String,
script_url: String, script_url: String,
scramjet: &Object, scramjet: &Object,
) -> Result<JsRewriterOutput> { ) -> Result<JsRewriterOutput> {