fix rewriter bin

This commit is contained in:
Toshit Chawda 2024-10-21 16:23:39 -07:00
parent 5d809faa75
commit a3bed65986
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
4 changed files with 63 additions and 55 deletions

45
rewriter/src/error.rs Normal file
View file

@ -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<JsValue> for RewriterError {
fn from(value: JsValue) -> Self {
Self::Js(format!("{:?}", value))
}
}
impl From<RewriterError> 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<T> = std::result::Result<T, RewriterError>;

View file

@ -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<JsValue> for RewriterError {
fn from(value: JsValue) -> Self {
Self::Js(format!("{:?}", value))
}
}
impl From<RewriterError> 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<T> = std::result::Result<T, RewriterError>;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]

View file

@ -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<E>(
mut data: &[u8],
may_skip_write: bool,
mut push_str: impl FnMut(&str) -> Result<(), E>,
) -> Result<bool, E> {
mut push_str: impl FnMut(&str) -> std::result::Result<(), E>,
) -> std::result::Result<bool, E> {
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<String> {
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

View file

@ -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<Vec<u8>> {
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:?}"))
}