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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
const SAME_COMPARTMENT = "same-compartment";
const IFRAME_COMPARTMENT = "iframe-compartment";
const BIG_BUFFER_SIZE = 1000000;
const ITER_MAX = 10;
function makeBuffer(size) {
let buffer = new Uint8Array(size);
buffer.fill(42);
let value = 0;
for (let i = 0; i < 1000000; i += 1000) {
buffer.set([++value % 255], i);
}
return buffer;
}
function apply_compartment(compartment, data) {
if (compartment == SAME_COMPARTMENT) {
return self[data.func](data.args, self);
}
if (compartment == IFRAME_COMPARTMENT) {
const iframe = document.querySelector("#iframe").contentWindow;
return iframe[data.func](data.args, self);
}
ok(false, "Invalid compartment value");
return undefined;
}
async function test_nativeStream(compartment) {
info("test_nativeStream");
let r = await fetch("/");
return apply_compartment(compartment, {
func: "test_nativeStream_continue",
args: r,
});
}
async function test_nativeStream_continue(r, that) {
that.ok(r.body instanceof that.ReadableStream, "We have a ReadableStream");
let a = r.clone();
that.ok(a instanceof that.Response, "We have a cloned Response");
that.ok(a.body instanceof that.ReadableStream, "We have a ReadableStream");
let b = a.clone();
that.ok(b instanceof that.Response, "We have a cloned Response");
that.ok(b.body instanceof that.ReadableStream, "We have a ReadableStream");
let blob = await r.blob();
that.ok(blob instanceof that.Blob, "We have a blob");
let d = await a.body.getReader().read();
that.ok(!d.done, "We have read something!");
blob = await b.blob();
that.ok(blob instanceof that.Blob, "We have a blob");
}
async function test_timeout(compartment) {
info("test_timeout");
let blob = new Blob([""]);
let r = await fetch(URL.createObjectURL(blob));
return apply_compartment(compartment, {
func: "test_timeout_continue",
args: r,
});
}
async function test_timeout_continue(r, that) {
await r.body.getReader().read();
await new Promise(resolve => setTimeout(resolve, 0));
try {
await r.blob();
that.ok(false, "We cannot have a blob here!");
} catch (exc) {
that.ok(true, "We cannot have a blob here!");
}
}
async function test_nonNativeStream(compartment) {
info("test_nonNativeStream");
let buffer = makeBuffer(BIG_BUFFER_SIZE);
info("Buffer size: " + buffer.byteLength);
let r = new Response(
new ReadableStream({
start: controller => {
controller.enqueue(buffer);
controller.close();
},
})
);
return apply_compartment(compartment, {
func: "test_nonNativeStream_continue",
args: { r, buffer },
});
}
async function test_nonNativeStream_continue(data, that) {
that.ok(
data.r.body instanceof that.ReadableStream,
"We have a ReadableStream"
);
let a = data.r.clone();
that.ok(a instanceof that.Response, "We have a cloned Response");
that.ok(a.body instanceof that.ReadableStream, "We have a ReadableStream");
let b = a.clone();
that.ok(b instanceof that.Response, "We have a cloned Response");
that.ok(b.body instanceof that.ReadableStream, "We have a ReadableStream");
let blob = await data.r.blob();
that.ok(blob instanceof that.Blob, "We have a blob");
let d = await a.body.getReader().read();
that.ok(!d.done, "We have read something!");
blob = await b.blob();
that.ok(blob instanceof that.Blob, "We have a blob");
that.is(blob.size, data.buffer.byteLength, "Blob size matches");
}
async function test_noUint8Array(compartment) {
info("test_noUint8Array");
let r = new Response(
new ReadableStream({
start: controller => {
controller.enqueue("hello world!");
controller.close();
},
})
);
return apply_compartment(compartment, {
func: "test_noUint8Array_continue",
args: r,
});
}
async function test_noUint8Array_continue(r, that) {
that.ok(r.body instanceof that.ReadableStream, "We have a ReadableStream");
try {
await r.blob();
that.ok(false, "We cannot have a blob here!");
} catch (ex) {
that.ok(true, "We cannot have a blob here!");
}
}
async function test_pendingStream(compartment) {
let r = new Response(
new ReadableStream({
start: controller => {
controller.enqueue(makeBuffer(BIG_BUFFER_SIZE));
// Let's keep this controler open.
self.ccc = controller;
},
})
);
return apply_compartment(compartment, {
func: "test_pendingStream_continue",
args: r,
});
}
async function test_pendingStream_continue(r, that) {
let d = await r.body.getReader().read();
that.ok(!d.done, "We have read something!");
if ("close" in that) {
that.close();
}
}
async function test_nativeStream_cache(compartment) {
info("test_nativeStream_cache");
let origBody = "123456789abcdef";
let url = "/nativeStream";
let cache = await caches.open("nativeStream");
info("Storing a body as a string");
await cache.put(url, new Response(origBody));
return apply_compartment(compartment, {
func: "test_nativeStream_cache_continue",
args: { caches, cache, url, origBody },
});
}
async function test_nativeStream_cache_continue(data, that) {
that.info("Retrieving the stored value");
let cacheResponse = await data.cache.match(data.url);
that.info("Converting the response to text");
let cacheBody = await cacheResponse.text();
that.is(data.origBody, cacheBody, "Bodies match");
await data.caches.delete("nativeStream");
}
async function test_nonNativeStream_cache(compartment) {
info("test_nonNativeStream_cache");
let url = "/nonNativeStream";
let cache = await caches.open("nonNativeStream");
let buffer = makeBuffer(BIG_BUFFER_SIZE);
info("Buffer size: " + buffer.byteLength);
info("Storing a body as a string");
let r = new Response(
new ReadableStream({
start: controller => {
controller.enqueue(buffer);
controller.close();
},
})
);
return apply_compartment(compartment, {
func: "test_nonNativeStream_cache_continue",
args: { caches, cache, buffer, r },
});
}
async function test_nonNativeStream_cache_continue(data, that) {
await data.cache.put(data.url, data.r);
that.info("Retrieving the stored value");
let cacheResponse = await data.cache.match(data.url);
that.info("Converting the response to text");
let cacheBody = await cacheResponse.arrayBuffer();
that.ok(cacheBody instanceof that.ArrayBuffer, "Body is an array buffer");
that.is(cacheBody.byteLength, BIG_BUFFER_SIZE, "Body length is correct");
let value = 0;
for (let i = 0; i < 1000000; i += 1000) {
that.is(
new Uint8Array(cacheBody)[i],
++value % 255,
"byte in position " + i + " is correct"
);
}
await data.caches.delete("nonNativeStream");
}
async function test_codeExecution(compartment) {
info("test_codeExecution");
let r = new Response(
new ReadableStream({
start(c) {
controller = c;
},
pull() {
console.log("pull called");
},
})
);
return apply_compartment(compartment, {
func: "test_codeExecution_continue",
args: r,
});
}
// This is intended to just be a drop-in replacement for an old observer
// notification.
function addConsoleStorageListener(listener) {
const ConsoleAPIStorage = SpecialPowers.Cc[
"@mozilla.org/consoleAPI-storage;1"
].getService(SpecialPowers.Ci.nsIConsoleAPIStorage);
listener.__handler = (message, id) => {
listener.observe(message, id);
};
ConsoleAPIStorage.addLogEventListener(
listener.__handler,
SpecialPowers.wrap(document).nodePrincipal
);
}
function removeConsoleStorageListener(listener) {
const ConsoleAPIStorage = SpecialPowers.Cc[
"@mozilla.org/consoleAPI-storage;1"
].getService(SpecialPowers.Ci.nsIConsoleAPIStorage);
ConsoleAPIStorage.removeLogEventListener(listener.__handler);
}
async function test_codeExecution_continue(r, that) {
function consoleListener() {
addConsoleStorageListener(this);
}
var promise = new Promise(resolve => {
consoleListener.prototype = {
observe(aSubject) {
that.ok(true, "Something has been received");
var obj = aSubject.wrappedJSObject;
if (obj.arguments[0] && obj.arguments[0] === "pull called") {
that.ok(true, "Message received!");
removeConsoleStorageListener(this);
resolve();
}
},
};
});
var cl = new consoleListener();
r.body.getReader().read();
await promise;
}
async function test_global(compartment) {
info("test_global: " + compartment);
self.foo = 42;
self.iter = ITER_MAX;
let r = new Response(
new ReadableStream({
start(c) {
self.controller = c;
},
pull() {
if (!("iter" in self) || self.iter < 0 || self.iter > ITER_MAX) {
throw "Something bad is happening here!";
}
let buffer = new Uint8Array(1);
buffer.fill(self.foo);
self.controller.enqueue(buffer);
if (--self.iter == 0) {
controller.close();
}
},
})
);
return apply_compartment(compartment, {
func: "test_global_continue",
args: r,
});
}
async function test_global_continue(r, that) {
let a = await r.arrayBuffer();
that.is(
Object.getPrototypeOf(a),
that.ArrayBuffer.prototype,
"Body is an array buffer"
);
that.is(a.byteLength, ITER_MAX, "Body length is correct");
for (let i = 0; i < ITER_MAX; ++i) {
that.is(new Uint8Array(a)[i], 42, "Byte " + i + " is correct");
}
}
function workify(func) {
info("Workifying " + func);
return new Promise((resolve, reject) => {
let worker = new Worker("worker_readableStreams.js");
worker.postMessage(func);
worker.onmessage = function (e) {
if (e.data.type == "done") {
resolve();
return;
}
if (e.data.type == "error") {
reject(e.data.message);
return;
}
if (e.data.type == "test") {
ok(e.data.test, e.data.message);
return;
}
if (e.data.type == "info") {
info(e.data.message);
}
};
});
}
|