| 1 |
// META: global=window,worker,shadowrealm |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
const error1 = new Error('error1'); |
| 5 |
error1.name = 'error1'; |
| 6 |
|
| 7 |
const error2 = new Error('error2'); |
| 8 |
error2.name = 'error2'; |
| 9 |
|
| 10 |
promise_test(() => { |
| 11 |
let controller; |
| 12 |
const ws = new WritableStream({ |
| 13 |
start(c) { |
| 14 |
controller = c; |
| 15 |
} |
| 16 |
}); |
| 17 |
|
| 18 |
// Now error the stream after its construction. |
| 19 |
controller.error(error1); |
| 20 |
|
| 21 |
const writer = ws.getWriter(); |
| 22 |
|
| 23 |
assert_equals(writer.desiredSize, null, 'desiredSize should be null'); |
| 24 |
return writer.closed.catch(r => { |
| 25 |
assert_equals(r, error1, 'ws should be errored by the passed error'); |
| 26 |
}); |
| 27 |
}, 'controller argument should be passed to start method'); |
| 28 |
|
| 29 |
promise_test(t => { |
| 30 |
const ws = new WritableStream({ |
| 31 |
write(chunk, controller) { |
| 32 |
controller.error(error1); |
| 33 |
} |
| 34 |
}); |
| 35 |
|
| 36 |
const writer = ws.getWriter(); |
| 37 |
|
| 38 |
return Promise.all([ |
| 39 |
writer.write('a'), |
| 40 |
promise_rejects_exactly(t, error1, writer.closed, 'controller.error() in write() should error the stream') |
| 41 |
]); |
| 42 |
}, 'controller argument should be passed to write method'); |
| 43 |
|
| 44 |
// Older versions of the standard had the controller argument passed to close(). It wasn't useful, and so has been |
| 45 |
// removed. This test remains to identify implementations that haven't been updated. |
| 46 |
promise_test(t => { |
| 47 |
const ws = new WritableStream({ |
| 48 |
close(...args) { |
| 49 |
t.step(() => { |
| 50 |
assert_array_equals(args, [], 'no arguments should be passed to close'); |
| 51 |
}); |
| 52 |
} |
| 53 |
}); |
| 54 |
|
| 55 |
return ws.getWriter().close(); |
| 56 |
}, 'controller argument should not be passed to close method'); |
| 57 |
|
| 58 |
promise_test(() => { |
| 59 |
const ws = new WritableStream({}, { |
| 60 |
highWaterMark: 1000, |
| 61 |
size() { return 1; } |
| 62 |
}); |
| 63 |
|
| 64 |
const writer = ws.getWriter(); |
| 65 |
|
| 66 |
assert_equals(writer.desiredSize, 1000, 'desiredSize should be 1000'); |
| 67 |
return writer.ready.then(v => { |
| 68 |
assert_equals(v, undefined, 'ready promise should fulfill with undefined'); |
| 69 |
}); |
| 70 |
}, 'highWaterMark should be reflected to desiredSize'); |
| 71 |
|
| 72 |
promise_test(() => { |
| 73 |
const ws = new WritableStream({}, { |
| 74 |
highWaterMark: Infinity, |
| 75 |
size() { return 0; } |
| 76 |
}); |
| 77 |
|
| 78 |
const writer = ws.getWriter(); |
| 79 |
|
| 80 |
assert_equals(writer.desiredSize, Infinity, 'desiredSize should be Infinity'); |
| 81 |
|
| 82 |
return writer.ready; |
| 83 |
}, 'WritableStream should be writable and ready should fulfill immediately if the strategy does not apply ' + |
| 84 |
'backpressure'); |
| 85 |
|
| 86 |
test(() => { |
| 87 |
new WritableStream(); |
| 88 |
}, 'WritableStream should be constructible with no arguments'); |
| 89 |
|
| 90 |
test(() => { |
| 91 |
assert_throws_js(RangeError, () => new WritableStream({ type: 'bytes' }), 'constructor should throw'); |
| 92 |
}, `WritableStream can't be constructed with a defined type`); |
| 93 |
|
| 94 |
test(() => { |
| 95 |
const underlyingSink = { get start() { throw error1; } }; |
| 96 |
const queuingStrategy = { highWaterMark: 0, get size() { throw error2; } }; |
| 97 |
|
| 98 |
// underlyingSink is converted in prose in the method body, whereas queuingStrategy is done at the IDL layer. |
| 99 |
// So the queuingStrategy exception should be encountered first. |
| 100 |
assert_throws_exactly(error2, () => new WritableStream(underlyingSink, queuingStrategy)); |
| 101 |
}, 'underlyingSink argument should be converted after queuingStrategy argument'); |
| 102 |
|
| 103 |
test(() => { |
| 104 |
const ws = new WritableStream({}); |
| 105 |
|
| 106 |
const writer = ws.getWriter(); |
| 107 |
|
| 108 |
assert_equals(typeof writer.write, 'function', 'writer should have a write method'); |
| 109 |
assert_equals(typeof writer.abort, 'function', 'writer should have an abort method'); |
| 110 |
assert_equals(typeof writer.close, 'function', 'writer should have a close method'); |
| 111 |
|
| 112 |
assert_equals(writer.desiredSize, 1, 'desiredSize should start at 1'); |
| 113 |
|
| 114 |
assert_not_equals(typeof writer.ready, 'undefined', 'writer should have a ready property'); |
| 115 |
assert_equals(typeof writer.ready.then, 'function', 'ready property should be thenable'); |
| 116 |
assert_not_equals(typeof writer.closed, 'undefined', 'writer should have a closed property'); |
| 117 |
assert_equals(typeof writer.closed.then, 'function', 'closed property should be thenable'); |
| 118 |
}, 'WritableStream instances should have standard methods and properties'); |
| 119 |
|
| 120 |
test(() => { |
| 121 |
let WritableStreamDefaultController; |
| 122 |
new WritableStream({ |
| 123 |
start(c) { |
| 124 |
WritableStreamDefaultController = c.constructor; |
| 125 |
} |
| 126 |
}); |
| 127 |
|
| 128 |
assert_throws_js(TypeError, () => new WritableStreamDefaultController({}), |
| 129 |
'constructor should throw a TypeError exception'); |
| 130 |
}, 'WritableStreamDefaultController constructor should throw'); |
| 131 |
|
| 132 |
test(() => { |
| 133 |
let WritableStreamDefaultController; |
| 134 |
const stream = new WritableStream({ |
| 135 |
start(c) { |
| 136 |
WritableStreamDefaultController = c.constructor; |
| 137 |
} |
| 138 |
}); |
| 139 |
|
| 140 |
assert_throws_js(TypeError, () => new WritableStreamDefaultController(stream), |
| 141 |
'constructor should throw a TypeError exception'); |
| 142 |
}, 'WritableStreamDefaultController constructor should throw when passed an initialised WritableStream'); |
| 143 |
|
| 144 |
test(() => { |
| 145 |
const stream = new WritableStream(); |
| 146 |
const writer = stream.getWriter(); |
| 147 |
const WritableStreamDefaultWriter = writer.constructor; |
| 148 |
writer.releaseLock(); |
| 149 |
assert_throws_js(TypeError, () => new WritableStreamDefaultWriter({}), |
| 150 |
'constructor should throw a TypeError exception'); |
| 151 |
}, 'WritableStreamDefaultWriter should throw unless passed a WritableStream'); |
| 152 |
|
| 153 |
test(() => { |
| 154 |
const stream = new WritableStream(); |
| 155 |
const writer = stream.getWriter(); |
| 156 |
const WritableStreamDefaultWriter = writer.constructor; |
| 157 |
assert_throws_js(TypeError, () => new WritableStreamDefaultWriter(stream), |
| 158 |
'constructor should throw a TypeError exception'); |
| 159 |
}, 'WritableStreamDefaultWriter constructor should throw when stream argument is locked'); |