mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 14:30:02 -04:00
esmodules
This commit is contained in:
parent
d433f67d67
commit
862f909624
9 changed files with 141 additions and 25 deletions
88
rewriter/Cargo.lock
generated
88
rewriter/Cargo.lock
generated
|
@ -87,6 +87,15 @@ dependencies = [
|
|||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "form_urlencoded"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456"
|
||||
dependencies = [
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gloo-utils"
|
||||
version = "0.1.7"
|
||||
|
@ -106,6 +115,16 @@ version = "0.14.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6"
|
||||
dependencies = [
|
||||
"unicode-bidi",
|
||||
"unicode-normalization",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.11"
|
||||
|
@ -336,6 +355,12 @@ dependencies = [
|
|||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
|
||||
|
||||
[[package]]
|
||||
name = "phf"
|
||||
version = "0.11.2"
|
||||
|
@ -424,11 +449,16 @@ dependencies = [
|
|||
name = "rewriter"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"oxc_allocator",
|
||||
"oxc_ast",
|
||||
"oxc_parser",
|
||||
"oxc_span",
|
||||
"oxc_syntax",
|
||||
"serde",
|
||||
"serde-wasm-bindgen",
|
||||
"url",
|
||||
"urlencoding",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
|
@ -477,6 +507,17 @@ dependencies = [
|
|||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde-wasm-bindgen"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8302e169f0eddcc139c70f139d19d6467353af16f9fce27e8c30158036a1e16b"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"serde",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.204"
|
||||
|
@ -576,6 +617,21 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938"
|
||||
dependencies = [
|
||||
"tinyvec_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec_macros"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
|
||||
|
||||
[[package]]
|
||||
name = "tsify"
|
||||
version = "0.4.5"
|
||||
|
@ -601,6 +657,12 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-bidi"
|
||||
version = "0.3.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-id-start"
|
||||
version = "1.2.0"
|
||||
|
@ -619,12 +681,38 @@ version = "0.1.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-normalization"
|
||||
version = "0.1.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5"
|
||||
dependencies = [
|
||||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d"
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c"
|
||||
dependencies = [
|
||||
"form_urlencoded",
|
||||
"idna",
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "urlencoding"
|
||||
version = "2.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.92"
|
||||
|
|
|
@ -7,9 +7,14 @@ edition = "2021"
|
|||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
js-sys = "0.3.69"
|
||||
oxc_allocator = "0.20.0"
|
||||
oxc_ast = { version = "0.20.0", features = ["serialize"]}
|
||||
oxc_parser = { version = "0.20.0" }
|
||||
oxc_span = "0.20.0"
|
||||
oxc_syntax = "0.20.0"
|
||||
serde = "1.0.204"
|
||||
serde-wasm-bindgen = "0.6.5"
|
||||
url = "2.5.2"
|
||||
urlencoding = "2.1.3"
|
||||
wasm-bindgen = "0.2.92"
|
||||
|
|
|
@ -7,7 +7,7 @@ cd ..
|
|||
|
||||
WASM=rewriter/out/rewriter_bg.wasm
|
||||
|
||||
time wasm-opt -Oz --vacuum --dce --enable-threads --enable-bulk-memory --enable-simd "$WASM" -o rewriter/out/optimized.wasm
|
||||
time wasm-opt -O4 --vacuum --dce --enable-threads --enable-bulk-memory --enable-simd "$WASM" -o rewriter/out/optimized.wasm
|
||||
|
||||
echo -n "self.WASM = '" > static/wasm.js
|
||||
base64 -w0 < "rewriter/out/optimized.wasm" >> static/wasm.js
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
pub mod rewrite;
|
||||
|
||||
use std::str::FromStr;
|
||||
|
||||
use rewrite::rewrite;
|
||||
use url::Url;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
|
@ -10,15 +13,15 @@ extern "C" {
|
|||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn rewrite_js(js: &str) -> String {
|
||||
rewrite(js)
|
||||
pub fn rewrite_js(js: &str, url: &str) -> String {
|
||||
rewrite(js, Url::from_str(url).unwrap())
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn rewrite_js_from_arraybuffer(js: &[u8]) -> String {
|
||||
pub fn rewrite_js_from_arraybuffer(js: &[u8], url: &str) -> String {
|
||||
// technically slower than the c++ string conversion but it will create *less copies*
|
||||
|
||||
let js = unsafe { std::str::from_utf8_unchecked(js) };
|
||||
|
||||
rewrite(js)
|
||||
rewrite(js, Url::from_str(url).unwrap())
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#![allow(clippy::print_stdout)]
|
||||
use std::{env, path::Path};
|
||||
use std::{env, path::Path, str::FromStr};
|
||||
|
||||
use oxc_allocator::Allocator;
|
||||
use oxc_ast::{
|
||||
|
@ -14,6 +14,7 @@ use oxc_syntax::scope::ScopeFlags;
|
|||
pub mod rewrite;
|
||||
|
||||
use rewrite::rewrite;
|
||||
use url::Url;
|
||||
|
||||
// Instruction:
|
||||
// create a `test.js`,
|
||||
|
@ -25,7 +26,13 @@ fn main() -> std::io::Result<()> {
|
|||
let path = Path::new(&name);
|
||||
let source_text = std::fs::read_to_string(path)?;
|
||||
|
||||
println!("{:#?}", rewrite(&source_text));
|
||||
println!(
|
||||
"{}",
|
||||
rewrite(
|
||||
&source_text,
|
||||
Url::from_str("https://google.com/glorngle/si.js").unwrap()
|
||||
)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@ use oxc_ast::{
|
|||
use oxc_parser::Parser;
|
||||
use oxc_span::{SourceType, Span};
|
||||
use oxc_syntax::scope::ScopeFlags;
|
||||
use url::Url;
|
||||
use urlencoding::encode;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum JsChange {
|
||||
|
@ -28,9 +30,10 @@ enum JsChange {
|
|||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Debug)]
|
||||
struct Rewriter {
|
||||
jschanges: Vec<JsChange>,
|
||||
base: Url,
|
||||
}
|
||||
|
||||
impl<'a> Visit<'a> for Rewriter {
|
||||
|
@ -76,17 +79,21 @@ impl<'a> Visit<'a> for Rewriter {
|
|||
}
|
||||
}
|
||||
fn visit_import_declaration(&mut self, it: &oxc_ast::ast::ImportDeclaration<'a>) {
|
||||
let url = it.source.value.to_string();
|
||||
// self.jschanges.push(JsChange::GenericChange {
|
||||
// span: it.source.span,
|
||||
// text: format!("\"/scramjet/{}\"", urlencoded),
|
||||
// });
|
||||
let name = it.source.value.to_string();
|
||||
let url = self.base.join(&name).unwrap();
|
||||
|
||||
let urlencoded = encode(url.as_str());
|
||||
|
||||
self.jschanges.push(JsChange::GenericChange {
|
||||
span: it.source.span,
|
||||
text: format!("\"/scramjet/{}\"", urlencoded),
|
||||
});
|
||||
walk::walk_import_declaration(self, it);
|
||||
}
|
||||
fn visit_import_expression(&mut self, it: &oxc_ast::ast::ImportExpression<'a>) {
|
||||
self.jschanges.push(JsChange::GenericChange {
|
||||
span: Span::new(it.span.start, it.span.start + 6),
|
||||
text: "(globalThis.$sImport)".to_string(),
|
||||
text: format!("(globalThis.$sImport(\"{}\"))", self.base),
|
||||
});
|
||||
walk::walk_import_expression(self, it);
|
||||
}
|
||||
|
@ -177,7 +184,7 @@ impl Rewriter {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn rewrite(js: &str) -> String {
|
||||
pub fn rewrite(js: &str, url: Url) -> String {
|
||||
let source_text = js.to_string();
|
||||
let allocator = Allocator::default();
|
||||
let source_type = SourceType::default();
|
||||
|
@ -194,6 +201,7 @@ pub fn rewrite(js: &str) -> String {
|
|||
|
||||
let mut ast_pass = Rewriter {
|
||||
jschanges: Vec::new(),
|
||||
base: url,
|
||||
};
|
||||
|
||||
ast_pass.visit_program(&program);
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
import { encodeUrl } from "../shared/rewriters/url"
|
||||
import { decodeUrl, encodeUrl } from "../shared/rewriters/url"
|
||||
|
||||
window.$sImport = function(url) {
|
||||
return (function() { }.constructor(`return import("${encodeUrl(url)}")`))();
|
||||
window.$sImport = function(base) {
|
||||
return function(url) {
|
||||
let resolved = new URL(url, base).href
|
||||
console.log(resolved)
|
||||
return (function() { }.constructor(`return import("${encodeUrl(resolved)}")`))();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -71,7 +71,7 @@ function traverseParsedHtml(node, origin?: URL) {
|
|||
let js = node.children[0].data;
|
||||
const htmlcomment = /<!--[\s\S]*?-->/g;
|
||||
js = js.replace(htmlcomment, "");
|
||||
node.children[0].data = rewriteJs(js);
|
||||
node.children[0].data = rewriteJs(js, origin);
|
||||
}
|
||||
if (node.name === "meta" && hasAttrib(node, "http-equiv")) {
|
||||
if (node.attribs["http-equiv"] === "content-security-policy") {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { parseModule } from "meriyah";
|
||||
import { generate } from "astring";
|
||||
import { makeTraveler } from "astravel";
|
||||
import { encodeUrl } from "./url";
|
||||
import { decodeUrl, encodeUrl } from "./url";
|
||||
import * as ESTree from "estree";
|
||||
|
||||
// i am a cat. i like to be petted. i like to be fed. i like to be
|
||||
|
@ -25,14 +25,15 @@ initSync(new WebAssembly.Module(
|
|||
|
||||
global.rws = rewriteJs;
|
||||
export function rewriteJs(js: string | ArrayBuffer, origin?: URL) {
|
||||
let rewrites;
|
||||
if ("window" in globalThis)
|
||||
origin ??= new URL(decodeUrl(location.href));
|
||||
|
||||
let before = performance.now();
|
||||
if (typeof js === "string") {
|
||||
rewrites = rewrite_js(js);
|
||||
js = rewrite_js(js, origin.toString());
|
||||
} else {
|
||||
js = new TextDecoder().decode(js);
|
||||
rewrites = rewrite_js(js);
|
||||
js = rewrite_js(js, origin.toString());
|
||||
}
|
||||
let after = performance.now();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue