| 1 |
// META: global=window,worker,shadowrealm |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
const error1 = new Error('a unique string'); |
| 5 |
error1.name = 'error1'; |
| 6 |
|
| 7 |
test(() => { |
| 8 |
assert_throws_exactly(error1, () => { |
| 9 |
new WritableStream({}, { |
| 10 |
get size() { |
| 11 |
throw error1; |
| 12 |
}, |
| 13 |
highWaterMark: 5 |
| 14 |
}); |
| 15 |
}, 'construction should re-throw the error'); |
| 16 |
}, 'Writable stream: throwing strategy.size getter'); |
| 17 |
|
| 18 |
test(() => { |
| 19 |
assert_throws_js(TypeError, () => { |
| 20 |
new WritableStream({}, { size: 'a string' }); |
| 21 |
}); |
| 22 |
}, 'reject any non-function value for strategy.size'); |
| 23 |
|
| 24 |
test(() => { |
| 25 |
assert_throws_exactly(error1, () => { |
| 26 |
new WritableStream({}, { |
| 27 |
size() { |
| 28 |
return 1; |
| 29 |
}, |
| 30 |
get highWaterMark() { |
| 31 |
throw error1; |
| 32 |
} |
| 33 |
}); |
| 34 |
}, 'construction should re-throw the error'); |
| 35 |
}, 'Writable stream: throwing strategy.highWaterMark getter'); |
| 36 |
|
| 37 |
test(() => { |
| 38 |
|
| 39 |
for (const highWaterMark of [-1, -Infinity, NaN, 'foo', {}]) { |
| 40 |
assert_throws_js(RangeError, () => { |
| 41 |
new WritableStream({}, { |
| 42 |
size() { |
| 43 |
return 1; |
| 44 |
}, |
| 45 |
highWaterMark |
| 46 |
}); |
| 47 |
}, `construction should throw a RangeError for ${highWaterMark}`); |
| 48 |
} |
| 49 |
}, 'Writable stream: invalid strategy.highWaterMark'); |
| 50 |
|
| 51 |
promise_test(t => { |
| 52 |
const ws = new WritableStream({}, { |
| 53 |
size() { |
| 54 |
throw error1; |
| 55 |
}, |
| 56 |
highWaterMark: 5 |
| 57 |
}); |
| 58 |
|
| 59 |
const writer = ws.getWriter(); |
| 60 |
|
| 61 |
const p1 = promise_rejects_exactly(t, error1, writer.write('a'), 'write should reject with the thrown error'); |
| 62 |
|
| 63 |
const p2 = promise_rejects_exactly(t, error1, writer.closed, 'closed should reject with the thrown error'); |
| 64 |
|
| 65 |
return Promise.all([p1, p2]); |
| 66 |
}, 'Writable stream: throwing strategy.size method'); |
| 67 |
|
| 68 |
promise_test(() => { |
| 69 |
const sizes = [NaN, -Infinity, Infinity, -1]; |
| 70 |
return Promise.all(sizes.map(size => { |
| 71 |
const ws = new WritableStream({}, { |
| 72 |
size() { |
| 73 |
return size; |
| 74 |
}, |
| 75 |
highWaterMark: 5 |
| 76 |
}); |
| 77 |
|
| 78 |
const writer = ws.getWriter(); |
| 79 |
|
| 80 |
return writer.write('a').then(() => assert_unreached('write must reject'), writeE => { |
| 81 |
assert_equals(writeE.name, 'RangeError', `write must reject with a RangeError for ${size}`); |
| 82 |
|
| 83 |
return writer.closed.then(() => assert_unreached('write must reject'), closedE => { |
| 84 |
assert_equals(closedE, writeE, `closed should reject with the same error as write`); |
| 85 |
}); |
| 86 |
}); |
| 87 |
})); |
| 88 |
}, 'Writable stream: invalid strategy.size return value'); |
| 89 |
|
| 90 |
test(() => { |
| 91 |
assert_throws_js(TypeError, () => new WritableStream(undefined, { |
| 92 |
size: 'not a function', |
| 93 |
highWaterMark: NaN |
| 94 |
}), 'WritableStream constructor should throw a TypeError'); |
| 95 |
}, 'Writable stream: invalid size beats invalid highWaterMark'); |