FileSystemWritableFileStream-piped.js (4521B)
1 'use strict'; 2 3 directory_test(async (t, root) => { 4 const handle = await createEmptyFile('foo_string.txt', root); 5 const wfs = await handle.createWritable(); 6 7 const rs = recordingReadableStream({ 8 start(controller) { 9 controller.enqueue('foo_string'); 10 controller.close(); 11 } 12 }); 13 14 await rs.pipeTo(wfs, { preventCancel: true }); 15 assert_equals(await getFileContents(handle), 'foo_string'); 16 assert_equals(await getFileSize(handle), 10); 17 }, 'can be piped to with a string'); 18 19 directory_test(async (t, root) => { 20 const handle = await createEmptyFile('foo_arraybuf.txt', root); 21 const wfs = await handle.createWritable(); 22 const buf = new ArrayBuffer(3); 23 const intView = new Uint8Array(buf); 24 intView[0] = 0x66; 25 intView[1] = 0x6f; 26 intView[2] = 0x6f; 27 28 const rs = recordingReadableStream({ 29 start(controller) { 30 controller.enqueue(buf); 31 controller.close(); 32 } 33 }); 34 35 await rs.pipeTo(wfs, { preventCancel: true }); 36 assert_equals(await getFileContents(handle), 'foo'); 37 assert_equals(await getFileSize(handle), 3); 38 }, 'can be piped to with an ArrayBuffer'); 39 40 directory_test(async (t, root) => { 41 const handle = await createEmptyFile('foo_blob.txt', root); 42 const wfs = await handle.createWritable(); 43 44 const rs = recordingReadableStream({ 45 start(controller) { 46 controller.enqueue(new Blob(['foo'])); 47 controller.close(); 48 } 49 }); 50 51 await rs.pipeTo(wfs, { preventCancel: true }); 52 assert_equals(await getFileContents(handle), 'foo'); 53 assert_equals(await getFileSize(handle), 3); 54 }, 'can be piped to with a Blob'); 55 56 directory_test(async (t, root) => { 57 const handle = await createEmptyFile('foo_write_param.txt', root); 58 const wfs = await handle.createWritable(); 59 60 const rs = recordingReadableStream({ 61 start(controller) { 62 controller.enqueue({type: 'write', data: 'foobar'}); 63 controller.close(); 64 } 65 }); 66 67 await rs.pipeTo(wfs, { preventCancel: true }); 68 assert_equals(await getFileContents(handle), 'foobar'); 69 assert_equals(await getFileSize(handle), 6); 70 }, 'can be piped to with a param object with write command'); 71 72 directory_test(async (t, root) => { 73 const handle = await createEmptyFile('foo_write_param.txt', root); 74 const wfs = await handle.createWritable(); 75 76 const rs = recordingReadableStream({ 77 start(controller) { 78 controller.enqueue({type: 'write', data: 'foobar'}); 79 controller.enqueue({type: 'truncate', size: 10}); 80 controller.enqueue({type: 'write', position: 0, data: 'baz'}); 81 controller.close(); 82 } 83 }); 84 85 await rs.pipeTo(wfs, { preventCancel: true }); 86 assert_equals(await getFileContents(handle), 'bazbar\0\0\0\0'); 87 assert_equals(await getFileSize(handle), 10); 88 }, 'can be piped to with a param object with multiple commands'); 89 90 directory_test(async (t, root) => { 91 const handle = await createEmptyFile('foo_write_queued.txt', root); 92 const wfs = await handle.createWritable(); 93 94 const rs = recordingReadableStream({ 95 start(controller) { 96 controller.enqueue('foo'); 97 controller.enqueue('bar'); 98 controller.enqueue('baz'); 99 controller.close(); 100 } 101 }); 102 103 await rs.pipeTo(wfs, { preventCancel: true }); 104 assert_equals(await getFileContents(handle), 'foobarbaz'); 105 assert_equals(await getFileSize(handle), 9); 106 }, 'multiple operations can be queued'); 107 108 directory_test(async (t, root) => { 109 const handle = await createEmptyFile('fetched.txt', root); 110 const wfs = await handle.createWritable(); 111 112 const response = await fetch('data:text/plain,fetched from far'); 113 const body = await response.body; 114 await body.pipeTo(wfs, { preventCancel: true }); 115 assert_equals(await getFileContents(handle), 'fetched from far'); 116 assert_equals(await getFileSize(handle), 16); 117 }, 'plays well with fetch'); 118 119 directory_test(async (t, root) => { 120 const handle = await createEmptyFile('aborted should_be_empty.txt', root); 121 const wfs = await handle.createWritable(); 122 123 const response = await fetch('data:text/plain,fetched from far'); 124 const body = await response.body; 125 126 const abortController = new AbortController(); 127 const signal = abortController.signal; 128 129 const promise = body.pipeTo(wfs, { signal }); 130 await abortController.abort(); 131 132 await promise_rejects_dom(t, 'AbortError', promise, 'stream is aborted'); 133 await promise_rejects_js(t, TypeError, wfs.close(), 'stream cannot be closed to flush writes'); 134 135 assert_equals(await getFileContents(handle), ''); 136 assert_equals(await getFileSize(handle), 0); 137 }, 'abort() aborts write');