diff --git a/rewriter/src/lib.rs b/rewriter/src/lib.rs index 21e2f93..8259e3a 100644 --- a/rewriter/src/lib.rs +++ b/rewriter/src/lib.rs @@ -63,6 +63,7 @@ fn get_config(scramjet: &Object) -> Config { do_sourcemaps: get_bool(flags, "sourcemaps"), capture_errors: get_bool(flags, "captureErrors"), + scramitize: get_bool(flags, "scramitize"), } } diff --git a/rewriter/src/main.rs b/rewriter/src/main.rs index 170b547..b5f0f2e 100644 --- a/rewriter/src/main.rs +++ b/rewriter/src/main.rs @@ -123,6 +123,7 @@ fn dorewrite(source_text: &str) -> String { pushsourcemapfn: "$pushsourcemap".to_string(), capture_errors: true, do_sourcemaps: true, + scramitize: false, }, ) .as_slice(), diff --git a/rewriter/src/rewrite.rs b/rewriter/src/rewrite.rs index 949bcbc..9b64de0 100644 --- a/rewriter/src/rewrite.rs +++ b/rewriter/src/rewrite.rs @@ -51,6 +51,7 @@ pub struct Config { pub encode: EncodeFn, pub capture_errors: bool, + pub scramitize: bool, pub do_sourcemaps: bool, } @@ -127,6 +128,21 @@ impl<'a> Visit<'a> for Rewriter { return; // unwise to walk the rest of the tree } + + if self.config.scramitize + && !matches!(s.object, Expression::MetaProperty(_)) + && !matches!(s.object, Expression::Super(_)) + { + let span = expression_span(&s.object); + self.jschanges.push(JsChange::GenericChange { + span: Span::new(span.start, span.start), + text: format!(" $scramitize("), + }); + self.jschanges.push(JsChange::GenericChange { + span: Span::new(span.end, span.end), + text: format!(")"), + }); + } } _ => { // TODO @@ -135,6 +151,7 @@ impl<'a> Visit<'a> for Rewriter { // and it would slow down js execution a lot } } + walk::walk_member_expression(self, it); } fn visit_this_expression(&mut self, it: &oxc_ast::ast::ThisExpression) { @@ -173,6 +190,16 @@ impl<'a> Visit<'a> for Rewriter { return; } } + if self.config.scramitize { + self.jschanges.push(JsChange::GenericChange { + span: Span::new(it.span.start, it.span.start), + text: format!(" $scramitize("), + }); + self.jschanges.push(JsChange::GenericChange { + span: Span::new(it.span.end, it.span.end), + text: format!(")"), + }); + } walk::walk_call_expression(self, it); } @@ -462,7 +489,7 @@ pub fn rewrite(js: &str, url: Url, config: Config) -> Vec { rhsspan: _, op: _, } => entirespan.start, - JsChange::SourceTag { tagstart,tagend } => *tagstart, + JsChange::SourceTag { tagstart, tagend } => *tagstart, }; a.cmp(&b) }); diff --git a/src/client/shared/wrap.ts b/src/client/shared/wrap.ts index 4e54f4e..c28a5b6 100644 --- a/src/client/shared/wrap.ts +++ b/src/client/shared/wrap.ts @@ -51,6 +51,7 @@ export function createWrapFn(client: ScramjetClient, self: typeof globalThis) { }; } +export const order = 4; export default function (client: ScramjetClient, self: typeof globalThis) { // the main magic of the proxy. all attempts to access any "banned objects" will be redirected here, and instead served a proxy object // this contrasts from how other proxies will leave the root object alone and instead attempt to catch every member access @@ -61,6 +62,13 @@ export default function (client: ScramjetClient, self: typeof globalThis) { configurable: false, }); + self.$scramitize = function (v) { + if (typeof v === "string" && v.includes("scramjet")) { + debugger; + } + return v; + }; + // location = "..." can't be rewritten as wrapfn(location) = ..., so instead it will actually be rewritten as // ((t)=>$scramjet$tryset(location,"+=",t)||location+=t)(...); // it has to be a discrete function because there's always the possibility that "location" is a local variable diff --git a/src/controller/index.ts b/src/controller/index.ts index 8e2a94a..68d6acf 100644 --- a/src/controller/index.ts +++ b/src/controller/index.ts @@ -32,7 +32,8 @@ export class ScramjetController { naiiveRewriter: false, captureErrors: true, syncxhr: false, - cleanerrors: false, + cleanerrors: true, + scramitize: false, sourcemaps: true, }, }; diff --git a/src/types.d.ts b/src/types.d.ts index 699a120..1dc89fd 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -24,6 +24,7 @@ type ScramjetFlags = { naiiveRewriter: boolean; captureErrors: boolean; cleanerrors: boolean; + scramitize: boolean; sourcemaps: boolean; syncxhr: boolean; };