Skip to content

Commit 6899c0f

Browse files
committed
fix: An issue where the builtin MessagePort and MessageChannel implementations were interfering with the ability to transfer MessagePort objects
1 parent bc28d5a commit 6899c0f

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

deno/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import {
55
StructureWithRefs,
66
} from './StructureClone.ts';
77
import { MessageEvent, Transferrable } from './MessageTarget.ts';
8-
import { MessagePort, MessageChannel } from './MessageChannel.ts';
8+
import {
9+
MessagePort as MessagePortShim,
10+
MessageChannel as MessageChannelShim,
11+
} from './MessageChannel.ts';
912

1013
const address = Deno.args[0];
1114
const scriptType = Deno.args[1];
@@ -60,12 +63,16 @@ function patchGlobalThis(send: (json: string) => void) {
6063
(<any>globalThis).postMessage = (data: any, transfer?: Transferrable[]) =>
6164
postMessage(null, data, transfer);
6265

63-
if (typeof (<any>globalThis).MessageChannel === 'undefined') {
64-
(<any>globalThis).MessageChannel = MessageChannel;
66+
if (typeof (<any>globalThis).MessageChannel !== 'undefined') {
67+
(<any>globalThis).BuiltinMessageChannel = (<any>(
68+
globalThis
69+
)).MessageChannel;
6570
}
66-
if (typeof (<any>globalThis).MessagePort === 'undefined') {
67-
(<any>globalThis).MessagePort = MessagePort;
71+
if (typeof (<any>globalThis).MessagePort !== 'undefined') {
72+
(<any>globalThis).BuiltinMessagePort = (<any>globalThis).MessagePort;
6873
}
74+
(<any>globalThis).MessageChannel = MessageChannelShim;
75+
(<any>globalThis).MessagePort = MessagePortShim;
6976

7077
return function onmessage(message: string) {
7178
if (typeof message === 'string') {
@@ -119,7 +126,7 @@ function patchGlobalThis(send: (json: string) => void) {
119126
function handleTransfers(transfer?: Transferrable[]) {
120127
if (transfer) {
121128
for (let t of transfer) {
122-
if (t instanceof MessagePort) {
129+
if (t instanceof MessagePortShim) {
123130
const channel = t.channelID;
124131
ports.set(t.channelID, {
125132
port: t,

0 commit comments

Comments
 (0)