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",
"smallvec",
"thiserror 2.0.7",
"url",
]
[[package]]
@ -1682,7 +1681,6 @@ dependencies = [
"oxc",
"rewriter",
"thiserror 2.0.7",
"url",
"wasm-bindgen",
"web-sys",
]

View file

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

View file

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

View file

@ -1,22 +1,21 @@
use std::str::FromStr;
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
use rewriter::{cfg::Config, rewrite};
use std::str::FromStr;
use url::Url;
use urlencoding::encode;
fn encode_string(str: String) -> String {
encode(&str).to_string()
}
pub fn bench(c: &mut Criterion) {
let discord = include_str!("../sample/discord.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 {
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(),
wrapfn: "$wrap".to_string(),

View file

@ -6,18 +6,17 @@ use rewriter::{cfg::Config, rewrite, RewriteResult};
use url::Url;
use urlencoding::encode;
fn encode_string(str: String) -> String {
encode(&str).to_string()
}
fn dorewrite(data: &str) -> Result<RewriteResult> {
let url = Url::from_str("https://google.com/glorngle/si.js").context("failed to make url")?;
rewrite(
data,
Config {
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(),
wrapfn: "$wrap".to_string(),

View file

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

View file

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

View file

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

View file

@ -49,9 +49,7 @@ where
E: Clone,
{
fn rewrite_url(&mut self, url: String) -> String {
let url = self.config.base.join(&url).unwrap();
let urlencoded = (self.config.encoder)(url.to_string());
let urlencoded = (self.config.urlrewriter)(url);
format!("\"{}{}\"", self.config.prefix, urlencoded)
}
@ -233,10 +231,9 @@ where
if self.config.capture_errors {
if let Some(h) = &it.handler {
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 {
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"] }
js-sys = "0.3.76"
oxc = { workspace = true }
url = { workspace = true }
rewriter = { version = "0.1.0", path = "../rewriter" }
rewriter = { version = "0.1.0", path = "../rewriter", default-features = false }
thiserror = "2.0.6"
wasm-bindgen = "0.2.99"
web-sys = { version = "0.3.76", features = ["Url"] }

View file

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

View file

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