1// META: global=window,worker 2// META: script=resources/readable-stream-from-array.js 3// META: script=resources/readable-stream-to-array.js 4 5const cases = [ 6 {encoding: 'utf-8', bytes: [0xEF, 0xBB, 0xBF, 0x61, 0x62, 0x63]}, 7 {encoding: 'utf-16le', bytes: [0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00]}, 8 {encoding: 'utf-16be', bytes: [0xFE, 0xFF, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63]} 9]; 10const BOM = '\uFEFF'; 11 12// |inputChunks| is an array of chunks, each represented by an array of 13// integers. |ignoreBOM| is true or false. The result value is the output of the 14// pipe, concatenated into a single string. 15async function pipeAndAssemble(inputChunks, encoding, ignoreBOM) { 16 const chunksAsUint8 = inputChunks.map(values => new Uint8Array(values)); 17 const readable = readableStreamFromArray(chunksAsUint8); 18 const outputArray = await readableStreamToArray(readable.pipeThrough( 19 new TextDecoderStream(encoding, {ignoreBOM}))); 20 return outputArray.join(''); 21} 22 23for (const testCase of cases) { 24 for (let splitPoint = 0; splitPoint < 4; ++splitPoint) { 25 promise_test(async () => { 26 const inputChunks = [testCase.bytes.slice(0, splitPoint), 27 testCase.bytes.slice(splitPoint)]; 28 const withIgnoreBOM = 29 await pipeAndAssemble(inputChunks, testCase.encoding, true); 30 assert_equals(withIgnoreBOM, BOM + 'abc', 'BOM should be preserved'); 31 32 const withoutIgnoreBOM = 33 await pipeAndAssemble(inputChunks, testCase.encoding, false); 34 assert_equals(withoutIgnoreBOM, 'abc', 'BOM should be stripped') 35 }, `ignoreBOM should work for encoding ${testCase.encoding}, split at ` + 36 `character ${splitPoint}`); 37 } 38} 39