Skip to content

Commit 03add87

Browse files
souldzinCompuIves
authored andcommitted
Fix access to constructor on xorigin object (codesandbox#3174)
**How?** It looks like in `protocol.ts` we need to send different params to `postMessage` if the `target` is a `Worker`... There's probably a more polymorphic way to handle all of this, but this commit implements a quick and dirty function which returns a default value if an error is thrown (i.e. a "SecurityError" in this case) **References:** - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Property_access_denied - https://gitlab.com/gitlab-org/gitlab/issues/27144#note_258460537
1 parent 0e722e0 commit 03add87

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

packages/codesandbox-api/src/protocol/protocol.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@ const generateId = () =>
22
// Such a random ID
33
Math.floor(Math.random() * 1000000 + Math.random() * 1000000);
44

5+
const getConstructorName = (x: any) => {
6+
try {
7+
return x.constructor.name;
8+
} catch(e) {
9+
return '';
10+
}
11+
};
12+
513
export default class Protocol {
614
private outgoingMessages: Set<number> = new Set();
715
private internalId: number;
16+
private isWorker: boolean;
817

918
constructor(
1019
private type: string,
@@ -13,6 +22,7 @@ export default class Protocol {
1322
) {
1423
this.createConnection();
1524
this.internalId = generateId();
25+
this.isWorker = getConstructorName(target) === 'Worker';
1626
}
1727

1828
getTypeId() {
@@ -91,11 +101,11 @@ export default class Protocol {
91101

92102
private _postMessage(m: any) {
93103
if (
94-
this.target.constructor.name === 'Worker' ||
104+
this.isWorker ||
95105
// @ts-ignore Unknown to TS
96106
(typeof DedicatedWorkerGlobalScope !== 'undefined' &&
97107
// @ts-ignore Unknown to TS
98-
target instanceof DedicatedWorkerGlobalScope)
108+
this.target instanceof DedicatedWorkerGlobalScope)
99109
) {
100110
// @ts-ignore
101111
this.target.postMessage(m);

0 commit comments

Comments
 (0)