mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-16 07:30:02 -04:00
use js function to encode
This commit is contained in:
parent
11a0c09179
commit
eebefbc070
7 changed files with 41 additions and 17 deletions
|
@ -2,8 +2,8 @@ pub mod rewrite;
|
|||
|
||||
use std::{panic, str::FromStr};
|
||||
|
||||
use js_sys::encode_uri_component;
|
||||
use rewrite::rewrite;
|
||||
use js_sys::Function;
|
||||
use rewrite::{rewrite, EncodeFn};
|
||||
use url::Url;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
|
@ -13,26 +13,40 @@ extern "C" {
|
|||
fn log(s: &str);
|
||||
}
|
||||
|
||||
// import the SCRAM!!! jet encoder here later
|
||||
fn encode(s: String) -> String {
|
||||
encode_uri_component(&s).into()
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn init() {
|
||||
panic::set_hook(Box::new(console_error_panic_hook::hook));
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn rewrite_js(js: &str, url: &str) -> Vec<u8> {
|
||||
rewrite(js, Url::from_str(url).unwrap(), Box::new(encode))
|
||||
fn create_encode_function(encode: Function) -> EncodeFn {
|
||||
Box::new(move |str| {
|
||||
encode
|
||||
.call1(&JsValue::NULL, &str.into())
|
||||
.unwrap()
|
||||
.as_string()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn rewrite_js_from_arraybuffer(js: &[u8], url: &str) -> Vec<u8> {
|
||||
pub fn rewrite_js(js: &str, url: &str, encode: Function) -> Vec<u8> {
|
||||
rewrite(
|
||||
js,
|
||||
Url::from_str(url).unwrap(),
|
||||
create_encode_function(encode),
|
||||
)
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn rewrite_js_from_arraybuffer(js: &[u8], url: &str, encode: Function) -> Vec<u8> {
|
||||
// technically slower than the c++ string conversion but it will create *less copies*
|
||||
|
||||
let js = unsafe { std::str::from_utf8_unchecked(js) };
|
||||
|
||||
rewrite(js, Url::from_str(url).unwrap(), Box::new(encode))
|
||||
rewrite(
|
||||
js,
|
||||
Url::from_str(url).unwrap(),
|
||||
create_encode_function(encode),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ enum JsChange {
|
|||
},
|
||||
}
|
||||
|
||||
type EncodeFn = Box<dyn Fn(String) -> String>;
|
||||
pub type EncodeFn = Box<dyn Fn(String) -> String>;
|
||||
struct Rewriter {
|
||||
jschanges: Vec<JsChange>,
|
||||
base: Url,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue