File size: 4,116 Bytes
bee6636 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
use std::{
env,
ffi::OsString,
fs,
path::PathBuf,
sync::Arc,
time::{Duration, Instant},
};
use anyhow::{Context, Result};
use clap::Parser;
use html::{Rewriter, attrmap, rule::RewriteRule};
use oxc::{
allocator::{Allocator, StringBuilder},
diagnostics::NamedSource,
};
use rewriter::NativeRewriter;
mod rewriter;
mod test_runner;
#[derive(Parser)]
pub struct RewriterOptions {
#[clap(long, default_value = "/scrammedjet/")]
prefix: String,
#[clap(long, default_value = "$wrap")]
wrapfn: String,
#[clap(long, default_value = "$gwrap")]
wrapthisfn: String,
#[clap(long, default_value = "$import")]
importfn: String,
#[clap(long, default_value = "$rewrite")]
rewritefn: String,
#[clap(long, default_value = "$meta")]
metafn: String,
#[clap(long, default_value = "$setrealm")]
setrealmfn: String,
#[clap(long, default_value = "$pushsourcemap")]
pushsourcemapfn: String,
#[clap(long, default_value = "https://google.com/glorngle/si.js")]
base: String,
#[clap(long, default_value = "glongle1")]
sourcetag: String,
#[clap(long, default_value_t = false)]
is_module: bool,
#[clap(long, default_value_t = false)]
capture_errors: bool,
#[clap(long, default_value_t = false)]
do_sourcemaps: bool,
#[clap(long, default_value_t = false)]
scramitize: bool,
#[clap(long, default_value_t = false)]
strict_rewrites: bool,
}
impl Default for RewriterOptions {
fn default() -> Self {
Self::parse_from(std::iter::empty::<OsString>())
}
}
#[derive(Parser)]
#[command(version = clap::crate_version!())]
pub enum Cli {
/// Rewrite a file
Rewrite {
file: PathBuf,
#[clap(flatten)]
config: RewriterOptions,
},
/// Rewrite a file multiple times for flamegraphing
Bench {
file: PathBuf,
iterations: u32,
#[clap(flatten)]
config: RewriterOptions,
},
Html {
file: PathBuf,
},
}
fn main() -> Result<()> {
let args = Cli::parse();
match args {
Cli::Rewrite { file, config } => {
let mut rewriter = NativeRewriter::new(&config);
let data = fs::read_to_string(file).context("failed to read file")?;
let res = rewriter.rewrite(&data, &config)?;
let source =
Arc::new(NamedSource::new(data.clone(), config.base).with_language("javascript"));
println!(
"rewritten:\n{}",
str::from_utf8(&res.js).context("failed to parse rewritten js")?
);
let unrewritten = NativeRewriter::unrewrite(&res);
eprintln!("errors:");
for err in res.errors {
eprintln!("{}", err.with_source_code(source.clone()));
}
rewriter.reset();
println!(
"unrewritten matches orig: {}",
data.as_bytes() == unrewritten.as_slice()
);
}
Cli::Bench {
file,
iterations,
config,
} => {
let mut rewriter = NativeRewriter::new(&config);
let data = fs::read_to_string(file).context("failed to read file")?;
let mut duration = Duration::from_secs(0);
let cnt = iterations * 100;
for x in 1..=cnt {
let before = Instant::now();
rewriter
.rewrite(&data, &config)
.context("failed to rewrite")?;
let after = Instant::now();
rewriter.reset();
duration += after - before;
if x % 100 == 0 {
println!("{x}...");
}
}
println!("iterations: {cnt}");
println!("total time: {duration:?}");
println!("avg time: {:?}", duration / cnt);
}
Cli::Html { file } => {
let data = fs::read_to_string(file).context("failed to read file")?;
let mut alloc = Allocator::new();
let rules = vec![RewriteRule {
attrs: attrmap!({
"href": ["a", "link"]
}),
func: Box::new(|alloc, x, ()| {
let mut build = StringBuilder::from_str_in(x, alloc);
build.push_str(" :3");
Ok(Some(build.into_str()))
}),
}];
let rewriter = Rewriter::new(
rules,
Box::new(|_, _, ()| Ok(Some("__EXTERNAL_TOOL_VAL__"))),
)?;
let ret = rewriter.rewrite(&alloc, &data, &(), true)?;
println!("{}", str::from_utf8(&ret)?);
alloc.reset();
}
}
Ok(())
}
|