mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 06:20: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,
|
||||
|
|
|
@ -49,6 +49,11 @@ fastify.register(fastifyStatic, {
|
|||
prefix: "/scram/",
|
||||
decorateReply: false,
|
||||
});
|
||||
fastify.register(fastifyStatic, {
|
||||
root: join(fileURLToPath(new URL(".", import.meta.url)), "./assets"),
|
||||
prefix: "/assets/",
|
||||
decorateReply: false,
|
||||
});
|
||||
fastify.register(fastifyStatic, {
|
||||
root: baremuxPath,
|
||||
prefix: "/baremux/",
|
||||
|
|
|
@ -4,7 +4,7 @@ if (!self.$scramjet) {
|
|||
}
|
||||
self.$scramjet.config = {
|
||||
prefix: "/scramjet/",
|
||||
codec: self.$scramjet.codecs.plain,
|
||||
codec: self.$scramjet.codecs.base64,
|
||||
config: "/scram/scramjet.config.js",
|
||||
shared: "/scram/scramjet.shared.js",
|
||||
worker: "/scram/scramjet.worker.js",
|
||||
|
|
|
@ -36,5 +36,6 @@ const tasks = {
|
|||
};
|
||||
|
||||
function taskRewriteJs(js: ArrayBuffer, origin: string): string {
|
||||
return rewriteJs(js, new URL(origin));
|
||||
// idk how to get the codec from here
|
||||
return rewriteJs(js, new URL(origin), () => {});
|
||||
}
|
||||
|
|
|
@ -80,7 +80,11 @@ async function handleResponse(
|
|||
}
|
||||
break;
|
||||
case "script":
|
||||
responseBody = rewriteJs(await response.arrayBuffer(), url);
|
||||
responseBody = rewriteJs(
|
||||
await response.arrayBuffer(),
|
||||
url,
|
||||
self.$scramjet.config.codec.encode
|
||||
);
|
||||
// Disable threading for now, it's causing issues.
|
||||
// responseBody = await this.threadpool.rewriteJs(await responseBody.arrayBuffer(), url.toString());
|
||||
break;
|
||||
|
|
|
@ -157,7 +157,7 @@ window.addEventListener("load", async () => {
|
|||
|
||||
return btoa(binary);
|
||||
}
|
||||
const arraybuffer = await (await fetch("/scramjet.png")).arrayBuffer();
|
||||
const arraybuffer = await (await fetch("/assets/scramjet.png")).arrayBuffer();
|
||||
console.log(
|
||||
"%cb",
|
||||
`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue