|
9 | 9 | BlobUnknownError, |
10 | 10 | requestApi, |
11 | 11 | } from './api'; |
12 | | -import { BlobError } from './helpers'; |
| 12 | +import { BlobError, createChunkTransformStream } from './helpers'; |
13 | 13 |
|
14 | 14 | describe('api', () => { |
15 | 15 | describe('request api', () => { |
@@ -133,3 +133,74 @@ describe('api', () => { |
133 | 133 | }); |
134 | 134 | }); |
135 | 135 | }); |
| 136 | + |
| 137 | +describe('createChunkTransformStream', () => { |
| 138 | + async function collectChunks( |
| 139 | + stream: ReadableStream<Uint8Array>, |
| 140 | + ): Promise<Uint8Array[]> { |
| 141 | + const chunks: Uint8Array[] = []; |
| 142 | + const reader = stream.getReader(); |
| 143 | + for (;;) { |
| 144 | + const { value, done } = await reader.read(); |
| 145 | + if (done) break; |
| 146 | + chunks.push(value); |
| 147 | + } |
| 148 | + return chunks; |
| 149 | + } |
| 150 | + |
| 151 | + it('accumulates small chunks and emits full-sized chunks', async () => { |
| 152 | + const chunkSize = 4; |
| 153 | + const stream = new ReadableStream<ArrayBuffer>({ |
| 154 | + start(controller) { |
| 155 | + controller.enqueue(new Uint8Array([1, 2]).buffer); |
| 156 | + controller.enqueue(new Uint8Array([3, 4, 5, 6]).buffer); |
| 157 | + controller.enqueue(new Uint8Array([7]).buffer); |
| 158 | + controller.close(); |
| 159 | + }, |
| 160 | + }); |
| 161 | + |
| 162 | + const chunks = await collectChunks( |
| 163 | + stream.pipeThrough(createChunkTransformStream(chunkSize)), |
| 164 | + ); |
| 165 | + |
| 166 | + expect(chunks).toHaveLength(2); |
| 167 | + expect(Array.from(chunks[0]!)).toEqual([1, 2, 3, 4]); |
| 168 | + expect(Array.from(chunks[1]!)).toEqual([5, 6, 7]); |
| 169 | + }); |
| 170 | + |
| 171 | + it('calls onProgress for each emitted chunk', async () => { |
| 172 | + const chunkSize = 3; |
| 173 | + const progress: number[] = []; |
| 174 | + const stream = new ReadableStream<ArrayBuffer>({ |
| 175 | + start(controller) { |
| 176 | + controller.enqueue(new Uint8Array([1, 2, 3, 4, 5]).buffer); |
| 177 | + controller.close(); |
| 178 | + }, |
| 179 | + }); |
| 180 | + |
| 181 | + await collectChunks( |
| 182 | + stream.pipeThrough( |
| 183 | + createChunkTransformStream(chunkSize, (bytes) => progress.push(bytes)), |
| 184 | + ), |
| 185 | + ); |
| 186 | + |
| 187 | + expect(progress).toEqual([3, 2]); |
| 188 | + }); |
| 189 | + |
| 190 | + it('flushes remaining bytes when stream ends', async () => { |
| 191 | + const chunkSize = 10; |
| 192 | + const stream = new ReadableStream<ArrayBuffer>({ |
| 193 | + start(controller) { |
| 194 | + controller.enqueue(new Uint8Array([1, 2, 3]).buffer); |
| 195 | + controller.close(); |
| 196 | + }, |
| 197 | + }); |
| 198 | + |
| 199 | + const chunks = await collectChunks( |
| 200 | + stream.pipeThrough(createChunkTransformStream(chunkSize)), |
| 201 | + ); |
| 202 | + |
| 203 | + expect(chunks).toHaveLength(1); |
| 204 | + expect(Array.from(chunks[0]!)).toEqual([1, 2, 3]); |
| 205 | + }); |
| 206 | +}); |
0 commit comments