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;
|
pub mod rewrite;
|
||||||
|
|
||||||
use std::{panic, str::FromStr};
|
use std::{panic, str::FromStr};
|
||||||
|
|
||||||
|
use error::{Result, RewriterError};
|
||||||
use js_sys::{Function, Object, Reflect};
|
use js_sys::{Function, Object, Reflect};
|
||||||
use rewrite::{rewrite, Config, EncodeFn};
|
use rewrite::{rewrite, Config, EncodeFn};
|
||||||
use thiserror::Error;
|
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use wasm_bindgen::prelude::*;
|
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]
|
#[wasm_bindgen]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#[wasm_bindgen(js_namespace = console)]
|
#[wasm_bindgen(js_namespace = console)]
|
||||||
|
|
|
@ -6,8 +6,10 @@ use std::{
|
||||||
str::{from_utf8, FromStr},
|
str::{from_utf8, FromStr},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub mod error;
|
||||||
pub mod rewrite;
|
pub mod rewrite;
|
||||||
|
|
||||||
|
use error::Result;
|
||||||
use rewrite::rewrite;
|
use rewrite::rewrite;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
@ -55,8 +57,8 @@ fn append_string(data: &[u8], escaped: &mut String, may_skip: bool) -> bool {
|
||||||
fn encode_into<E>(
|
fn encode_into<E>(
|
||||||
mut data: &[u8],
|
mut data: &[u8],
|
||||||
may_skip_write: bool,
|
may_skip_write: bool,
|
||||||
mut push_str: impl FnMut(&str) -> Result<(), E>,
|
mut push_str: impl FnMut(&str) -> std::result::Result<(), E>,
|
||||||
) -> Result<bool, E> {
|
) -> std::result::Result<bool, E> {
|
||||||
let mut pushed = false;
|
let mut pushed = false;
|
||||||
loop {
|
loop {
|
||||||
// Fast path to skip over safe chars at the beginning of the remaining string
|
// 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()
|
encode(&s).to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dorewrite(source_text: &str) -> String {
|
fn dorewrite(source_text: &str) -> Result<String> {
|
||||||
from_utf8(
|
Ok(from_utf8(
|
||||||
rewrite(
|
rewrite(
|
||||||
source_text,
|
source_text,
|
||||||
Url::from_str("https://google.com/glorngle/si.js").unwrap(),
|
Url::from_str("https://google.com/glorngle/si.js").unwrap(),
|
||||||
|
@ -126,11 +128,10 @@ fn dorewrite(source_text: &str) -> String {
|
||||||
scramitize: false,
|
scramitize: false,
|
||||||
strict_rewrites: false,
|
strict_rewrites: false,
|
||||||
},
|
},
|
||||||
)
|
)?
|
||||||
.as_slice(),
|
.as_slice(),
|
||||||
)
|
)?
|
||||||
.unwrap()
|
.to_string())
|
||||||
.to_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
|
@ -138,14 +139,14 @@ fn main() -> std::io::Result<()> {
|
||||||
let path = Path::new(&name);
|
let path = Path::new(&name);
|
||||||
let source_text = std::fs::read_to_string(path)?;
|
let source_text = std::fs::read_to_string(path)?;
|
||||||
|
|
||||||
println!("{}", dorewrite(&source_text));
|
println!("{}", dorewrite(&source_text).unwrap());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::{fs, path::Path};
|
use std::fs;
|
||||||
|
|
||||||
use boa_engine::{
|
use boa_engine::{
|
||||||
js_str, js_string,
|
js_str, js_string,
|
||||||
|
@ -160,7 +161,7 @@ mod tests {
|
||||||
fn google() {
|
fn google() {
|
||||||
// sanity check- just making sure it won't crash
|
// sanity check- just making sure it won't crash
|
||||||
let source_text = include_str!("../sample/google.js");
|
let source_text = include_str!("../sample/google.js");
|
||||||
dorewrite(source_text);
|
dorewrite(source_text).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -239,7 +240,7 @@ function check(val) {
|
||||||
))
|
))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let rewritten = dorewrite(&content);
|
let rewritten = dorewrite(&content).unwrap();
|
||||||
println!("{}", rewritten);
|
println!("{}", rewritten);
|
||||||
|
|
||||||
context
|
context
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use core::str;
|
use core::str;
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
|
|
||||||
use crate::{error, Result, RewriterError};
|
|
||||||
use oxc_allocator::Allocator;
|
use oxc_allocator::Allocator;
|
||||||
use oxc_ast::{
|
use oxc_ast::{
|
||||||
ast::{
|
ast::{
|
||||||
|
@ -15,6 +14,8 @@ use oxc_span::{Atom, SourceType, Span};
|
||||||
use oxc_syntax::operator::{AssignmentOperator, UnaryOperator};
|
use oxc_syntax::operator::{AssignmentOperator, UnaryOperator};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
use crate::error::{Result, RewriterError};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum JsChange {
|
enum JsChange {
|
||||||
GenericChange {
|
GenericChange {
|
||||||
|
@ -466,6 +467,7 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Result<Vec<u8>> {
|
||||||
let cloned = js.to_string();
|
let cloned = js.to_string();
|
||||||
let err = err.with_source_code(cloned);
|
let err = err.with_source_code(cloned);
|
||||||
println!("oxc parse error {err:?}");
|
println!("oxc parse error {err:?}");
|
||||||
|
#[cfg(target_family = "wasm")]
|
||||||
error(&format!("oxc parse error {err:?}"))
|
error(&format!("oxc parse error {err:?}"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue