mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 14: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 std::{panic, str::FromStr};
|
||||||
|
|
||||||
use js_sys::encode_uri_component;
|
use js_sys::Function;
|
||||||
use rewrite::rewrite;
|
use rewrite::{rewrite, EncodeFn};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
@ -13,26 +13,40 @@ extern "C" {
|
||||||
fn log(s: &str);
|
fn log(s: &str);
|
||||||
}
|
}
|
||||||
|
|
||||||
// import the SCRAM!!! jet encoder here later
|
|
||||||
fn encode(s: String) -> String {
|
|
||||||
encode_uri_component(&s).into()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn init() {
|
pub fn init() {
|
||||||
panic::set_hook(Box::new(console_error_panic_hook::hook));
|
panic::set_hook(Box::new(console_error_panic_hook::hook));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
fn create_encode_function(encode: Function) -> EncodeFn {
|
||||||
pub fn rewrite_js(js: &str, url: &str) -> Vec<u8> {
|
Box::new(move |str| {
|
||||||
rewrite(js, Url::from_str(url).unwrap(), Box::new(encode))
|
encode
|
||||||
|
.call1(&JsValue::NULL, &str.into())
|
||||||
|
.unwrap()
|
||||||
|
.as_string()
|
||||||
|
.unwrap()
|
||||||
|
.to_string()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[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*
|
// technically slower than the c++ string conversion but it will create *less copies*
|
||||||
|
|
||||||
let js = unsafe { std::str::from_utf8_unchecked(js) };
|
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 {
|
struct Rewriter {
|
||||||
jschanges: Vec<JsChange>,
|
jschanges: Vec<JsChange>,
|
||||||
base: Url,
|
base: Url,
|
||||||
|
|
|
@ -49,6 +49,11 @@ fastify.register(fastifyStatic, {
|
||||||
prefix: "/scram/",
|
prefix: "/scram/",
|
||||||
decorateReply: false,
|
decorateReply: false,
|
||||||
});
|
});
|
||||||
|
fastify.register(fastifyStatic, {
|
||||||
|
root: join(fileURLToPath(new URL(".", import.meta.url)), "./assets"),
|
||||||
|
prefix: "/assets/",
|
||||||
|
decorateReply: false,
|
||||||
|
});
|
||||||
fastify.register(fastifyStatic, {
|
fastify.register(fastifyStatic, {
|
||||||
root: baremuxPath,
|
root: baremuxPath,
|
||||||
prefix: "/baremux/",
|
prefix: "/baremux/",
|
||||||
|
|
|
@ -4,7 +4,7 @@ if (!self.$scramjet) {
|
||||||
}
|
}
|
||||||
self.$scramjet.config = {
|
self.$scramjet.config = {
|
||||||
prefix: "/scramjet/",
|
prefix: "/scramjet/",
|
||||||
codec: self.$scramjet.codecs.plain,
|
codec: self.$scramjet.codecs.base64,
|
||||||
config: "/scram/scramjet.config.js",
|
config: "/scram/scramjet.config.js",
|
||||||
shared: "/scram/scramjet.shared.js",
|
shared: "/scram/scramjet.shared.js",
|
||||||
worker: "/scram/scramjet.worker.js",
|
worker: "/scram/scramjet.worker.js",
|
||||||
|
|
|
@ -36,5 +36,6 @@ const tasks = {
|
||||||
};
|
};
|
||||||
|
|
||||||
function taskRewriteJs(js: ArrayBuffer, origin: string): string {
|
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;
|
break;
|
||||||
case "script":
|
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.
|
// Disable threading for now, it's causing issues.
|
||||||
// responseBody = await this.threadpool.rewriteJs(await responseBody.arrayBuffer(), url.toString());
|
// responseBody = await this.threadpool.rewriteJs(await responseBody.arrayBuffer(), url.toString());
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -157,7 +157,7 @@ window.addEventListener("load", async () => {
|
||||||
|
|
||||||
return btoa(binary);
|
return btoa(binary);
|
||||||
}
|
}
|
||||||
const arraybuffer = await (await fetch("/scramjet.png")).arrayBuffer();
|
const arraybuffer = await (await fetch("/assets/scramjet.png")).arrayBuffer();
|
||||||
console.log(
|
console.log(
|
||||||
"%cb",
|
"%cb",
|
||||||
`
|
`
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue