1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
"use strict";
function receiveEventOnce(target, name) {
return new Promise(resolve => {
target.addEventListener(
name,
ev => {
resolve(ev);
},
{ once: true }
);
});
}
async function postAndTestMessageEvent(data, transfer, title) {
postMessage(data, "*", transfer);
const messagePortCount = transfer.filter(i => i instanceof MessagePort)
.length;
const ev = await receiveEventOnce(window, "message");
assert_equals(
ev.ports.length,
messagePortCount,
`Correct number of ports ${title}`
);
for (const [i, port] of ev.ports.entries()) {
assert_true(
port instanceof MessagePort,
`ports[${i}] include MessagePort ${title}`
);
}
for (const [key, value] of Object.entries(data)) {
assert_true(
ev.data[key] instanceof value.constructor,
`data.${key} has correct interface ${value.constructor.name} ${title}`
);
}
}
async function transferMessagePortWithOrder1(stream) {
const channel = new MessageChannel();
await postAndTestMessageEvent(
{ stream, port2: channel.port2 },
[stream, channel.port2],
`when transferring [${stream.constructor.name}, MessagePort]`
);
}
async function transferMessagePortWithOrder2(stream) {
const channel = new MessageChannel();
await postAndTestMessageEvent(
{ stream, port2: channel.port2 },
[channel.port2, stream],
`when transferring [MessagePort, ${stream.constructor.name}]`
);
}
async function transferMessagePortWithOrder3(stream) {
const channel = new MessageChannel();
await postAndTestMessageEvent(
{ port1: channel.port1, stream, port2: channel.port2 },
[channel.port1, stream, channel.port2],
`when transferring [MessagePort, ${stream.constructor.name}, MessagePort]`
);
}
async function transferMessagePortWithOrder4(stream) {
const channel = new MessageChannel();
await postAndTestMessageEvent(
{},
[channel.port1, stream, channel.port2],
`when transferring [MessagePort, ${stream.constructor.name}, MessagePort] but with empty data`
);
}
async function transferMessagePortWithOrder5(stream) {
const channel = new MessageChannel();
await postAndTestMessageEvent(
{ port2: channel.port2, port1: channel.port1, stream },
[channel.port1, stream, channel.port2],
`when transferring [MessagePort, ${stream.constructor.name}, MessagePort] but with data having different order`
);
}
async function transferMessagePortWithOrder6(stream) {
const channel = new MessageChannel();
await postAndTestMessageEvent(
{ port2: channel.port2, port1: channel.port1 },
[channel.port1, stream, channel.port2],
`when transferring [MessagePort, ${stream.constructor.name}, MessagePort] but with stream not being in the data`
);
}
async function transferMessagePortWithOrder7(stream) {
const channel = new MessageChannel();
await postAndTestMessageEvent(
{ stream },
[channel.port1, stream, channel.port2],
`when transferring [MessagePort, ${stream.constructor.name}, MessagePort] but with ports not being in the data`
);
}
async function transferMessagePortWith(constructor) {
await transferMessagePortWithOrder1(new constructor());
await transferMessagePortWithOrder2(new constructor());
await transferMessagePortWithOrder3(new constructor());
}
async function advancedTransferMesagePortWith(constructor) {
await transferMessagePortWithOrder4(new constructor());
await transferMessagePortWithOrder5(new constructor());
await transferMessagePortWithOrder6(new constructor());
await transferMessagePortWithOrder7(new constructor());
}
async function mixedTransferMessagePortWithOrder1() {
const channel = new MessageChannel();
const readable = new ReadableStream();
const writable = new WritableStream();
const transform = new TransformStream();
await postAndTestMessageEvent(
{
readable,
writable,
transform,
port1: channel.port1,
port2: channel.port2,
},
[readable, writable, transform, channel.port1, channel.port2],
`when transferring [ReadableStream, WritableStream, TransformStream, MessagePort, MessagePort]`
);
}
async function mixedTransferMessagePortWithOrder2() {
const channel = new MessageChannel();
const readable = new ReadableStream();
const writable = new WritableStream();
const transform = new TransformStream();
await postAndTestMessageEvent(
{ readable, writable, transform },
[transform, channel.port1, readable, channel.port2, writable],
`when transferring [TransformStream, MessagePort, ReadableStream, MessagePort, WritableStream]`
);
}
async function mixedTransferMessagePortWithOrder3() {
const channel = new MessageChannel();
const readable1 = new ReadableStream();
const readable2 = new ReadableStream();
const writable1 = new WritableStream();
const writable2 = new WritableStream();
const transform1 = new TransformStream();
const transform2 = new TransformStream();
await postAndTestMessageEvent(
{ readable1, writable1, transform1, readable2, writable2, transform2 },
[
transform2,
channel.port1,
readable1,
channel.port2,
writable2,
readable2,
writable1,
transform1,
],
`when transferring [TransformStream, MessagePort, ReadableStream, MessagePort, WritableStream, ReadableStream, WritableStream, TransformStream] but with the data having different order`
);
}
async function mixedTransferMesagePortWith() {
await mixedTransferMessagePortWithOrder1();
await mixedTransferMessagePortWithOrder2();
await mixedTransferMessagePortWithOrder3();
}
promise_test(async t => {
await transferMessagePortWith(ReadableStream);
}, "Transferring a MessagePort with a ReadableStream should set `.ports`");
promise_test(async t => {
await transferMessagePortWith(WritableStream);
}, "Transferring a MessagePort with a WritableStream should set `.ports`");
promise_test(async t => {
await transferMessagePortWith(TransformStream);
}, "Transferring a MessagePort with a TransformStream should set `.ports`");
promise_test(async t => {
await transferMessagePortWith(ReadableStream);
}, "Transferring a MessagePort with a ReadableStream should set `.ports`, advanced");
promise_test(async t => {
await transferMessagePortWith(WritableStream);
}, "Transferring a MessagePort with a WritableStream should set `.ports`, advanced");
promise_test(async t => {
await transferMessagePortWith(TransformStream);
}, "Transferring a MessagePort with a TransformStream should set `.ports`, advanced");
promise_test(async t => {
await mixedTransferMesagePortWith();
}, "Transferring a MessagePort with multiple streams should set `.ports`");
test(() => {
assert_throws_dom("DataCloneError", () =>
postMessage({ stream: new ReadableStream() }, "*")
);
}, "ReadableStream must not be serializable");
test(() => {
assert_throws_dom("DataCloneError", () =>
postMessage({ stream: new WritableStream() }, "*")
);
}, "WritableStream must not be serializable");
test(() => {
assert_throws_dom("DataCloneError", () =>
postMessage({ stream: new TransformStream() }, "*")
);
}, "TransformStream must not be serializable");
|