File size: 3,347 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 |
pub mod error;
use error::{Result, RewriterError};
use htmlr::{HtmlRewriter, HtmlRewriterOutput, create_html, create_html_output, get_html_params};
use js_sys::{Function, Object, Reflect};
use jsr::{JsRewriter, JsRewriterOutput, create_js, create_js_output, get_js_flags};
use oxc::allocator::Allocator;
use wasm_bindgen::prelude::*;
use web_sys::Url;
mod htmlr;
mod jsr;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console, js_name = error)]
fn console_error(s: &str);
#[wasm_bindgen(js_namespace = console, js_name = error)]
fn console_error2(s: &JsValue);
}
fn get_obj(obj: &JsValue, k: &'static str) -> Result<JsValue> {
Ok(Reflect::get(obj, &k.into())?)
}
fn get_str(obj: &JsValue, k: &'static str) -> Result<String> {
Reflect::get(obj, &k.into())?
.as_string()
.ok_or_else(|| RewriterError::not_str(k))
}
fn set_obj(obj: &Object, k: &str, v: &JsValue) -> Result<()> {
if Reflect::set(&obj.into(), &k.into(), v)? {
Ok(())
} else {
Err(RewriterError::ReflectSetFail(k.to_string()))
}
}
fn get_flag(scramjet: &Object, url: &str, flag: &str) -> Result<bool> {
let fenabled = get_obj(scramjet, "flagEnabled")?
.dyn_into::<Function>()
.map_err(|_| RewriterError::not_fn("scramjet.flagEnabled"))?;
let ret = fenabled.call2(&JsValue::NULL, &flag.into(), &Url::new(url)?.into())?;
ret.as_bool()
.ok_or_else(|| RewriterError::not_bool("scramjet.flagEnabled return value"))
}
#[wasm_bindgen]
pub struct Rewriter {
alloc: Allocator,
scramjet: Object,
js: JsRewriter,
html: HtmlRewriter,
html_funcs: (Function, Function, Function, Function),
}
#[wasm_bindgen]
impl Rewriter {
#[wasm_bindgen(constructor)]
pub fn new(scramjet: Object) -> Result<Self> {
Ok(Self {
alloc: Allocator::default(),
js: create_js(&scramjet)?,
html: create_html(&scramjet)?,
html_funcs: get_html_params(&scramjet)?,
scramjet,
})
}
#[wasm_bindgen]
pub fn rewrite_js(
&mut self,
js: String,
base: String,
url: String,
module: bool,
) -> Result<JsRewriterOutput> {
let flags = get_js_flags(&self.scramjet, base, module)?;
let out = match self.js.rewrite(&self.alloc, &js, flags) {
Ok(x) => x,
Err(x) => {
self.alloc.reset();
Err(x)?
}
};
let ret = create_js_output(out, url, js);
self.alloc.reset();
ret
}
#[wasm_bindgen]
pub fn rewrite_js_bytes(
&mut self,
js: Vec<u8>,
base: String,
url: String,
module: bool,
) -> Result<JsRewriterOutput> {
// SAFETY: we know the js is a valid utf-8 string
let js = unsafe { std::string::String::from_utf8_unchecked(js) };
self.rewrite_js(js, base, url, module)
}
#[allow(clippy::needless_pass_by_value)]
#[wasm_bindgen]
pub fn rewrite_html(
&mut self,
html: String,
meta: Object,
cookie: Object,
from_top: bool,
) -> Result<HtmlRewriterOutput> {
let out = match self.html.rewrite(
&self.alloc,
&html,
&(
meta,
cookie,
self.html_funcs.0.clone(),
self.html_funcs.1.clone(),
self.html_funcs.2.clone(),
self.html_funcs.3.clone(),
),
from_top,
) {
Ok(x) => x,
Err(x) => {
self.alloc.reset();
Err(x)?
}
};
let ret = create_html_output(&out);
self.alloc.reset();
ret
}
}
|