Skip to content

Commit 7a20670

Browse files
committed
[core] restore old postmessage behavior
1 parent 91d07c8 commit 7a20670

File tree

5 files changed

+59
-19
lines changed

5 files changed

+59
-19
lines changed

packages/scramjet/packages/core/rewriter/js/src/changes.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub enum JsChangeType<'alloc: 'data, 'data> {
7878
op: AssignmentOperator,
7979
},
8080

81+
SetRealmFn,
8182
/// insert `)`
8283
ClosingParen {
8384
semi: bool,
@@ -251,6 +252,7 @@ impl<'alloc: 'data, 'data> Transform<'data> for JsChange<'alloc, 'data> {
251252
]),
252253
Ty::ImportFn => LL::replace(transforms![&cfg.importfn, "(\"", &flags.base, "\","]),
253254
Ty::MetaFn => LL::replace(transforms![&cfg.metafn, "(import.meta,\"", &flags.base, "\")"]),
255+
Ty::SetRealmFn => LL::replace(transforms!["$scramjet$setrealmfn", "({})."]),
254256
Ty::AssignmentLeft { name, op } => LL::replace(transforms![
255257
"((t)=>",
256258
&cfg.trysetfn,

packages/scramjet/packages/core/rewriter/js/src/rewrite.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub(crate) enum RewriteType<'alloc: 'data, 'data> {
2929
/// `cfg.metafn("cfg.base")`
3030
MetaFn,
3131

32+
SetRealmFn,
33+
3234
/// `location` -> `$sj_location`
3335
RewriteProperty {
3436
ident: Atom<'data>,
@@ -237,6 +239,7 @@ impl<'alloc: 'data, 'data> RewriteType<'alloc, 'data> {
237239
replace: true
238240
}
239241
)],
242+
Self::SetRealmFn => smallvec![change!(span, SetRealmFn)],
240243
Self::ImportFn => smallvec![change!(span, ImportFn)],
241244
Self::MetaFn => smallvec![change!(span, MetaFn)],
242245
Self::ScramErr { ident } => smallvec![change!(span!(end), ScramErrFn { ident })],

packages/scramjet/packages/core/rewriter/js/src/visitor.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,11 +472,17 @@ where
472472
if s.property.name == "postMessage" {
473473
// include the "postMessage" and the dot before it in the inner span
474474
// if `postMessage?.` remove the dot and the question mark
475-
let offset = if s.optional { 2 } else { 1 };
475+
// let offset = if s.optional { 2 } else { 1 };
476476

477-
self.jschanges.add(rewrite!(s.span, WrapPostMessage {
478-
inner: Span::new(s.property.span.start - offset, s.property.span.end),
479-
}));
477+
// self.jschanges.add(rewrite!(s.span, WrapPostMessage {
478+
// inner: Span::new(s.property.span.start - offset, s.property.span.end),
479+
// }));
480+
if s.property.name == "postMessage" {
481+
self.jschanges.add(rewrite!(s.property.span, SetRealmFn));
482+
483+
walk::walk_expression(self, &s.object);
484+
return; // unwise to walk the rest of the tree
485+
}
480486

481487
// walk::walk_expression(self, &s.object);
482488
return; // unwise to walk the rest of the tree

packages/scramjet/packages/core/src/client/shared/postmessage.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { iswindow } from "@client/entry";
22
import { SCRAMJETCLIENT } from "@/symbols";
33
import { ScramjetClient } from "@client/index";
4+
import { POLLUTANT } from "./realm";
45

56
export default function (client: ScramjetClient, self: Self) {
67
if (iswindow)
@@ -14,20 +15,20 @@ export default function (client: ScramjetClient, self: Self) {
1415

1516
let pollutant;
1617

17-
// if (typeof ctx.args[0] === "object" && ctx.args[0] !== null) {
18-
// pollutant = ctx.args[0]; // try to use the first object we can find because it's more reliable
19-
// } else if (typeof ctx.args[2] === "object" && ctx.args[2] !== null) {
20-
// pollutant = ctx.args[2]; // next try to use transfer
21-
// } else if (
22-
// ctx.this &&
23-
// POLLUTANT in ctx.this &&
24-
// typeof ctx.this[POLLUTANT] === "object" &&
25-
// ctx.this[POLLUTANT] !== null
26-
// ) {
27-
// pollutant = ctx.this[POLLUTANT]; // lastly try to use the object from $setrealm
28-
// } else {
29-
pollutant = {}; // give up
30-
// }
18+
if (typeof ctx.args[0] === "object" && ctx.args[0] !== null) {
19+
pollutant = ctx.args[0]; // try to use the first object we can find because it's more reliable
20+
} else if (typeof ctx.args[2] === "object" && ctx.args[2] !== null) {
21+
pollutant = ctx.args[2]; // next try to use transfer
22+
} else if (
23+
ctx.this &&
24+
POLLUTANT in ctx.this &&
25+
typeof ctx.this[POLLUTANT] === "object" &&
26+
ctx.this[POLLUTANT] !== null
27+
) {
28+
pollutant = ctx.this[POLLUTANT]; // lastly try to use the object from $setrealm
29+
} else {
30+
pollutant = {}; // give up
31+
}
3132

3233
// and now we can steal Function from the caller's realm
3334
const {
@@ -55,13 +56,15 @@ export default function (client: ScramjetClient, self: Self) {
5556
$scramjet$origin: callerClient.url.origin,
5657
$scramjet$data: ctx.args[0],
5758
};
58-
// console.error("?");
59+
// console.error("?", ctx.args);
5960
// eval("debugger");
6061

6162
// * origin because obviously
6263
if (typeof ctx.args[1] === "string") ctx.args[1] = "*";
6364
if (typeof ctx.args[1] === "object") ctx.args[1].targetOrigin = "*";
6465

66+
console.log("postmessage proxy", ctx.args);
67+
6568
ctx.return(wrappedPostMessage.call(ctx.fn, ...ctx.args));
6669
},
6770
});
@@ -75,6 +78,7 @@ export default function (client: ScramjetClient, self: Self) {
7578
apply(ctx) {
7679
// origin/source doesn't need to be preserved - it's null in the message event
7780

81+
console.error("postmessagfef", ctx.this, ctx.args);
7882
ctx.args[0] = {
7983
$scramjet$messagetype: "worker",
8084
$scramjet$data: ctx.args[0],
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ScramjetClient } from "@client/index";
2+
3+
export const POLLUTANT = Symbol.for("scramjet realm pollutant");
4+
5+
export default function (client: ScramjetClient, self: typeof globalThis) {
6+
// object.$setrealm({}).postMessage(...)
7+
// the empty object is the "pollutant" which can reconstruct the real realm
8+
// i explain more in postmessage.ts
9+
Object.defineProperty(self.Object.prototype, "$scramjet$setrealmfn", {
10+
value(pollution: object) {
11+
// this is bad!! sites could detect this
12+
Object.defineProperty(this, POLLUTANT, {
13+
value: pollution,
14+
writable: false,
15+
configurable: true,
16+
enumerable: false,
17+
});
18+
19+
return this;
20+
},
21+
writable: true,
22+
configurable: true,
23+
enumerable: false,
24+
});
25+
}

0 commit comments

Comments
 (0)