(
stream: ReadableStream<Uint8Array>,
options: {
onChunkParsed?: (chunk: any[], isFirstChunk: boolean) => void;
onProgress?: (progress: number, stats: StreamingJSONParseStats) => void;
onComplete?: (result: DataParseResult) => void;
estimatedTotalBytes?: number;
sampleLimit?: number;
} = {}
)
| 172 | * to be parsed in most cases. For very large files, we'll implement progressive loading. |
| 173 | */ |
| 174 | const parseJSONStream = async ( |
| 175 | stream: ReadableStream<Uint8Array>, |
| 176 | options: { |
| 177 | onChunkParsed?: (chunk: any[], isFirstChunk: boolean) => void; |
| 178 | onProgress?: (progress: number, stats: StreamingJSONParseStats) => void; |
| 179 | onComplete?: (result: DataParseResult) => void; |
| 180 | estimatedTotalBytes?: number; |
| 181 | sampleLimit?: number; |
| 182 | } = {} |
| 183 | ): Promise<DataParseResult> => { |
| 184 | const { |
| 185 | onChunkParsed, |
| 186 | onProgress, |
| 187 | onComplete, |
| 188 | estimatedTotalBytes, |
| 189 | sampleLimit = 1000 |
| 190 | } = options; |
| 191 | |
| 192 | try { |
| 193 | setIsLoading(true); |
| 194 | setError(null); |
| 195 | setProgress(0); |
| 196 | |
| 197 | // Create StreamReader for efficient processing |
| 198 | const streamReader = new StreamReader(stream); |
| 199 | |
| 200 | // Initialize statistics |
| 201 | const stats: StreamingJSONParseStats = { |
| 202 | bytesProcessed: 0, |
| 203 | itemsProcessed: 0, |
| 204 | chunksProcessed: 0, |
| 205 | totalBytes: estimatedTotalBytes, |
| 206 | isNested: false |
| 207 | }; |
| 208 | |
| 209 | // JSON parsing requires the entire content in most cases |
| 210 | // For very large files, we'll implement a streaming JSON parser in future versions |
| 211 | // For now, we'll read the entire file but in chunks for progress reporting |
| 212 | |
| 213 | let jsonText = ''; |
| 214 | |
| 215 | // Read file in chunks for progress tracking |
| 216 | await streamReader.readByChunks(chunk => { |
| 217 | const text = new TextDecoder().decode(chunk, { stream: true }); |
| 218 | jsonText += text; |
| 219 | |
| 220 | // Update progress |
| 221 | stats.bytesProcessed = streamReader.getBytesProcessed(); |
| 222 | stats.chunksProcessed++; |
| 223 | |
| 224 | setParseStats({ ...stats }); |
| 225 | |
| 226 | // Calculate progress percentage |
| 227 | const progressValue = estimatedTotalBytes |
| 228 | ? stats.bytesProcessed / estimatedTotalBytes |
| 229 | : 0; |
| 230 | |
| 231 | setProgress(progressValue); |
no test coverage detected