Skip to content

Commit bb95a6e

Browse files
committed
Revert "Remove the use of wildcard postMessage (codesandbox#1088)"
This reverts commit d2ef349.
1 parent 8779d00 commit bb95a6e

File tree

2 files changed

+12
-36
lines changed
  • packages

2 files changed

+12
-36
lines changed

packages/app/src/app/components/Preview/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ class BasePreview extends React.Component<Props, State> {
358358
handleMessage = (data: Object, source: any) => {
359359
if (data && data.codesandbox) {
360360
if (data.type === 'initialized' && source) {
361-
registerFrame(source, frameUrl(this.props.sandbox.id));
361+
registerFrame(source);
362362

363363
if (!this.state.frameInitialized && this.props.onInitialized) {
364364
this.disposeInitializer = this.props.onInitialized(this);

packages/codesandbox-api/src/dispatcher/index.ts

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,36 @@
11
// import * as debug from 'debug';
22
import host from './host';
33

4-
const bundlers: Map<Window, string> = new Map();
4+
const bundlers: Window[] = [];
55

66
// Whether the tab has a connection with the editor
77
export const isStandalone =
88
typeof window === 'undefined' || (!window.opener && window.parent === window);
99

10-
let parentOrigin: string | null = null;
11-
12-
const parentOriginListener = (e: MessageEvent) => {
13-
if (e.data.type === 'register-frame') {
14-
parentOrigin = e.data.origin;
15-
16-
self.removeEventListener('message', parentOriginListener);
17-
}
18-
};
19-
20-
self.addEventListener('message', parentOriginListener);
21-
2210
/**
2311
* Send a message to the editor, this is most probably an action you generated
2412
*
2513
* @export
2614
* @param {*} message
2715
* @returns
2816
*/
29-
export function dispatch(message: any) {
17+
export function dispatch(message: Object) {
3018
if (!message) return;
3119

3220
const newMessage = { ...message, codesandbox: true };
3321
notifyListeners(newMessage);
3422
notifyFrames(newMessage);
3523

3624
if (isStandalone) return;
37-
if (parentOrigin === null && message.type !== 'initialized') return;
3825

3926
if (window.opener) {
40-
window.opener.postMessage(newMessage, parentOrigin === null ? '*' : parentOrigin);
27+
window.opener.postMessage(newMessage, '*');
4128
} else {
42-
window.parent.postMessage(newMessage, parentOrigin === null ? '*' : parentOrigin);
29+
window.parent.postMessage(newMessage, '*');
4330
}
4431
}
4532

46-
export type Callback = (
47-
message: Object,
48-
source?: MessageEvent['source'] | null | undefined
49-
) => void;
33+
export type Callback = (message: Object, source?: Window | null | undefined) => void;
5034

5135
const listeners: { [id: string]: Callback } = {};
5236
let listenerId = 0;
@@ -74,17 +58,17 @@ export function notifyListeners(data: Object, source?: MessageEvent['source']) {
7458

7559
function notifyFrames(message: Object) {
7660
const rawMessage = JSON.parse(JSON.stringify(message));
77-
bundlers.forEach((origin, frame) => {
61+
bundlers.forEach(frame => {
7862
if (frame && frame.postMessage) {
79-
frame.postMessage({ ...rawMessage, codesandbox: true }, origin);
63+
frame.postMessage({ ...rawMessage, codesandbox: true }, '*');
8064
}
8165
});
8266
}
8367

8468
function eventListener(e: MessageEvent) {
8569
const { data } = e;
8670

87-
if (data && data.codesandbox && (parentOrigin === null || e.origin === parentOrigin)) {
71+
if (data && data.codesandbox) {
8872
notifyListeners(data, e.source);
8973
}
9074
}
@@ -94,17 +78,9 @@ function eventListener(e: MessageEvent) {
9478
*
9579
* @param frame
9680
*/
97-
export function registerFrame(frame: Window, origin: string) {
98-
if (!bundlers.has(frame)) {
99-
bundlers.set(frame, origin);
100-
101-
frame.postMessage(
102-
{
103-
type: 'register-frame',
104-
origin: document.location.origin,
105-
},
106-
origin
107-
);
81+
export function registerFrame(frame: Window) {
82+
if (bundlers.indexOf(frame) === -1) {
83+
bundlers.push(frame);
10884
}
10985
}
11086

0 commit comments

Comments
 (0)