diff --git a/rewriter/src/error.rs b/rewriter/src/error.rs new file mode 100644 index 0000000..217d7c4 --- /dev/null +++ b/rewriter/src/error.rs @@ -0,0 +1,45 @@ +use thiserror::Error; +use wasm_bindgen::{JsError, JsValue}; + +#[derive(Debug, Error)] +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("{0} was not {1}")] + Not(String, &'static str), + #[error("❗❗❗ ❗❗❗ REWRITER OFFSET OOB FAIL ❗❗❗ ❗❗❗")] + Oob, +} + +impl From for RewriterError { + fn from(value: JsValue) -> Self { + Self::Js(format!("{:?}", value)) + } +} + +impl From for JsValue { + fn from(value: RewriterError) -> Self { + JsError::from(value).into() + } +} + +impl RewriterError { + pub fn not_str(x: &str, obj: &JsValue) -> Self { + Self::Not(format!("{:?} in {:?}", x, obj), "string") + } + + pub fn not_fn(obj: JsValue) -> Self { + Self::Not(format!("{:?}", obj), "function") + } + + pub fn not_bool(obj: &JsValue) -> Self { + Self::Not(format!("{:?}", obj), "bool") + } +} + +pub type Result = std::result::Result; diff --git a/rewriter/src/lib.rs b/rewriter/src/lib.rs index 47a0eb9..66d54b0 100644 --- a/rewriter/src/lib.rs +++ b/rewriter/src/lib.rs @@ -1,54 +1,14 @@ +pub mod error; pub mod rewrite; use std::{panic, str::FromStr}; +use error::{Result, RewriterError}; use js_sys::{Function, Object, Reflect}; use rewrite::{rewrite, Config, EncodeFn}; -use thiserror::Error; use url::Url; use wasm_bindgen::prelude::*; -#[derive(Debug, Error)] -pub enum RewriterError { - #[error("JS: {0}")] - Js(String), - #[error("URL parse error: {0}")] - Url(#[from] url::ParseError), - - #[error("{0} was not {1}")] - Not(String, &'static str), - #[error("❗❗❗ ❗❗❗ REWRITER OFFSET OOB FAIL ❗❗❗ ❗❗❗")] - Oob, -} - -impl From for RewriterError { - fn from(value: JsValue) -> Self { - Self::Js(format!("{:?}", value)) - } -} - -impl From for JsValue { - fn from(value: RewriterError) -> Self { - JsError::from(value).into() - } -} - -impl RewriterError { - fn not_str(x: &str, obj: &JsValue) -> Self { - Self::Not(format!("{:?} in {:?}", x, obj), "string") - } - - fn not_fn(obj: JsValue) -> Self { - Self::Not(format!("{:?}", obj), "function") - } - - fn not_bool(obj: &JsValue) -> Self { - Self::Not(format!("{:?}", obj), "bool") - } -} - -pub type Result = std::result::Result; - #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_namespace = console)] diff --git a/rewriter/src/main.rs b/rewriter/src/main.rs index ddd60ae..67a56c7 100644 --- a/rewriter/src/main.rs +++ b/rewriter/src/main.rs @@ -6,8 +6,10 @@ use std::{ str::{from_utf8, FromStr}, }; +pub mod error; pub mod rewrite; +use error::Result; use rewrite::rewrite; use url::Url; @@ -55,8 +57,8 @@ fn append_string(data: &[u8], escaped: &mut String, may_skip: bool) -> bool { fn encode_into( mut data: &[u8], may_skip_write: bool, - mut push_str: impl FnMut(&str) -> Result<(), E>, -) -> Result { + mut push_str: impl FnMut(&str) -> std::result::Result<(), E>, +) -> std::result::Result { let mut pushed = false; loop { // Fast path to skip over safe chars at the beginning of the remaining string @@ -107,8 +109,8 @@ fn encode_string(s: String) -> String { encode(&s).to_string() } -fn dorewrite(source_text: &str) -> String { - from_utf8( +fn dorewrite(source_text: &str) -> Result { + Ok(from_utf8( rewrite( source_text, Url::from_str("https://google.com/glorngle/si.js").unwrap(), @@ -126,11 +128,10 @@ fn dorewrite(source_text: &str) -> String { scramitize: false, strict_rewrites: false, }, - ) + )? .as_slice(), - ) - .unwrap() - .to_string() + )? + .to_string()) } fn main() -> std::io::Result<()> { @@ -138,14 +139,14 @@ fn main() -> std::io::Result<()> { let path = Path::new(&name); let source_text = std::fs::read_to_string(path)?; - println!("{}", dorewrite(&source_text)); + println!("{}", dorewrite(&source_text).unwrap()); Ok(()) } #[cfg(test)] mod tests { - use std::{fs, path::Path}; + use std::fs; use boa_engine::{ js_str, js_string, @@ -160,7 +161,7 @@ mod tests { fn google() { // sanity check- just making sure it won't crash let source_text = include_str!("../sample/google.js"); - dorewrite(source_text); + dorewrite(source_text).unwrap(); } #[test] @@ -239,7 +240,7 @@ function check(val) { )) .unwrap(); - let rewritten = dorewrite(&content); + let rewritten = dorewrite(&content).unwrap(); println!("{}", rewritten); context diff --git a/rewriter/src/rewrite.rs b/rewriter/src/rewrite.rs index 75aee4f..d93d965 100644 --- a/rewriter/src/rewrite.rs +++ b/rewriter/src/rewrite.rs @@ -1,7 +1,6 @@ use core::str; use std::str::from_utf8; -use crate::{error, Result, RewriterError}; use oxc_allocator::Allocator; use oxc_ast::{ ast::{ @@ -15,6 +14,8 @@ use oxc_span::{Atom, SourceType, Span}; use oxc_syntax::operator::{AssignmentOperator, UnaryOperator}; use url::Url; +use crate::error::{Result, RewriterError}; + #[derive(Debug)] enum JsChange { GenericChange { @@ -466,6 +467,7 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Result> { let cloned = js.to_string(); let err = err.with_source_code(cloned); println!("oxc parse error {err:?}"); + #[cfg(target_family = "wasm")] error(&format!("oxc parse error {err:?}")) }