1// META: global=window,worker 2// META: script=resources/readable-stream-from-array.js 3// META: script=resources/readable-stream-to-array.js 4// META: script=/common/sab.js 5'use strict'; 6 7["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => { 8 const inputChunkData = [73, 32, 240, 159, 146, 153, 32, 115, 116, 114, 101, 97, 109, 115]; 9 10 const emptyChunk = new Uint8Array(createBuffer(arrayBufferOrSharedArrayBuffer, 0)); 11 const inputChunk = new Uint8Array(createBuffer(arrayBufferOrSharedArrayBuffer, inputChunkData.length)); 12 13 inputChunk.set(inputChunkData); 14 15 const expectedOutputString = 'I \u{1F499} streams'; 16 17 promise_test(async () => { 18 const input = readableStreamFromArray([inputChunk]); 19 const output = input.pipeThrough(new TextDecoderStream()); 20 const array = await readableStreamToArray(output); 21 assert_array_equals(array, [expectedOutputString], 22 'the output should be in one chunk'); 23 }, 'decoding one UTF-8 chunk should give one output string - ' + arrayBufferOrSharedArrayBuffer); 24 25 promise_test(async () => { 26 const input = readableStreamFromArray([emptyChunk]); 27 const output = input.pipeThrough(new TextDecoderStream()); 28 const array = await readableStreamToArray(output); 29 assert_array_equals(array, [], 'no chunks should be output'); 30 }, 'decoding an empty chunk should give no output chunks - ' + arrayBufferOrSharedArrayBuffer); 31 32 promise_test(async () => { 33 const input = readableStreamFromArray([emptyChunk, inputChunk]); 34 const output = input.pipeThrough(new TextDecoderStream()); 35 const array = await readableStreamToArray(output); 36 assert_array_equals(array, [expectedOutputString], 37 'the output should be in one chunk'); 38 }, 'an initial empty chunk should be ignored - ' + arrayBufferOrSharedArrayBuffer); 39 40 promise_test(async () => { 41 const input = readableStreamFromArray([inputChunk, emptyChunk]); 42 const output = input.pipeThrough(new TextDecoderStream()); 43 const array = await readableStreamToArray(output); 44 assert_array_equals(array, [expectedOutputString], 45 'the output should be in one chunk'); 46 }, 'a trailing empty chunk should be ignored - ' + arrayBufferOrSharedArrayBuffer); 47 48 promise_test(async () => { 49 const chunk = new Uint8Array(createBuffer(arrayBufferOrSharedArrayBuffer, 3)); 50 chunk.set([0xF0, 0x9F, 0x92]); 51 const input = readableStreamFromArray([chunk]); 52 const output = input.pipeThrough(new TextDecoderStream()); 53 const array = await readableStreamToArray(output); 54 assert_array_equals(array, ['\uFFFD']); 55 }, 'UTF-8 EOF handling - ' + arrayBufferOrSharedArrayBuffer); 56}); 57 58promise_test(async () => { 59 const buffer = new ArrayBuffer(3); 60 const view = new Uint8Array(buffer, 1, 1); 61 view[0] = 65; 62 new MessageChannel().port1.postMessage(buffer, [buffer]); 63 const input = readableStreamFromArray([view]); 64 const output = input.pipeThrough(new TextDecoderStream()); 65 const array = await readableStreamToArray(output); 66 assert_array_equals(array, [], 'no chunks should be output'); 67}, 'decoding a transferred Uint8Array chunk should give no output'); 68 69promise_test(async () => { 70 const buffer = new ArrayBuffer(1); 71 new MessageChannel().port1.postMessage(buffer, [buffer]); 72 const input = readableStreamFromArray([buffer]); 73 const output = input.pipeThrough(new TextDecoderStream()); 74 const array = await readableStreamToArray(output); 75 assert_array_equals(array, [], 'no chunks should be output'); 76}, 'decoding a transferred ArrayBuffer chunk should give no output'); 77