mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 06:50:01 -04:00
fix rewriter bin
This commit is contained in:
parent
5d809faa75
commit
a3bed65986
4 changed files with 63 additions and 55 deletions
45
rewriter/src/error.rs
Normal file
45
rewriter/src/error.rs
Normal 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>;
|
|
@ -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)]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:?}"))
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue