decompression-with-detach.window.js (1435B)
1 // META: global=window,worker,shadowrealm 2 // META: script=resources/concatenate-stream.js 3 4 'use strict'; 5 6 const kInputLength = 1000000; 7 8 async function createLargeCompressedInput() { 9 const cs = new CompressionStream('deflate'); 10 // The input has to be large enough that it won't fit in a single chunk when 11 // decompressed. 12 const writer = cs.writable.getWriter(); 13 writer.write(new Uint8Array(kInputLength)); 14 writer.close(); 15 return concatenateStream(cs.readable); 16 } 17 18 promise_test(async () => { 19 const input = await createLargeCompressedInput(); 20 const ds = new DecompressionStream('deflate'); 21 const writer = ds.writable.getWriter(); 22 writer.write(input); 23 writer.close(); 24 // Object.prototype.then will be looked up synchronously when the promise 25 // returned by read() is resolved. 26 Object.defineProperty(Object.prototype, 'then', { 27 get() { 28 // Cause input to become detached and unreferenced. 29 try { 30 postMessage(undefined, 'nowhere', [input.buffer]); 31 } catch (e) { 32 // It's already detached. 33 } 34 } 35 }); 36 const output = await concatenateStream(ds.readable); 37 // If output successfully decompressed and gave the right length, we can be 38 // reasonably confident that no data corruption happened. 39 assert_equals( 40 output.byteLength, kInputLength, 'output should be the right length'); 41 }, 'data should be correctly decompressed even if input is detached partway');