* Read the entire stream and return as Uint8Array * * Note: Only use for smaller files, as this loads everything into memory
()
| 25 | * Note: Only use for smaller files, as this loads everything into memory |
| 26 | */ |
| 27 | async readAll(): Promise<{ data: Uint8Array; bytesProcessed: number }> { |
| 28 | const chunks: Uint8Array[] = []; |
| 29 | let totalSize = 0; |
| 30 | |
| 31 | try { |
| 32 | while (!this.aborted) { |
| 33 | const { done, value } = await this.reader.read(); |
| 34 | |
| 35 | if (done) break; |
| 36 | |
| 37 | chunks.push(value); |
| 38 | totalSize += value.length; |
| 39 | this.bytesProcessed += value.length; |
| 40 | |
| 41 | if (this.options.onProgress) { |
| 42 | this.options.onProgress(this.bytesProcessed); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Combine all chunks into a single Uint8Array |
| 47 | const result = new Uint8Array(totalSize); |
| 48 | let offset = 0; |
| 49 | |
| 50 | for (const chunk of chunks) { |
| 51 | result.set(chunk, offset); |
| 52 | offset += chunk.length; |
| 53 | } |
| 54 | |
| 55 | return { data: result, bytesProcessed: this.bytesProcessed }; |
| 56 | } catch (error) { |
| 57 | if (!this.aborted) { |
| 58 | console.error('Error reading stream:', error); |
| 59 | throw error; |
| 60 | } |
| 61 | |
| 62 | return { data: new Uint8Array(0), bytesProcessed: this.bytesProcessed }; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Process the stream in chunks |
no test coverage detected