* Normalize an async streamable source, yielding batches of Uint8Array. * @param {AsyncIterable|Iterable} source * @yields {Uint8Array[]}
(source)
| 335 | * @yields {Uint8Array[]} |
| 336 | */ |
| 337 | async function* normalizeAsyncSource(source) { |
| 338 | // Prefer async iteration if available |
| 339 | if (isAsyncIterable(source)) { |
| 340 | for await (const value of source) { |
| 341 | // Fast path 1: value is already a Uint8Array[] batch |
| 342 | if (isUint8ArrayBatch(value)) { |
| 343 | if (value.length > 0) { |
| 344 | yield value; |
| 345 | } |
| 346 | continue; |
| 347 | } |
| 348 | // Fast path 2: value is a single Uint8Array (very common) |
| 349 | if (isUint8Array(value)) { |
| 350 | yield [value]; |
| 351 | continue; |
| 352 | } |
| 353 | // Slow path: normalize the value |
| 354 | const batch = []; |
| 355 | for await (const chunk of normalizeAsyncValue(value)) { |
| 356 | ArrayPrototypePush(batch, chunk); |
| 357 | } |
| 358 | if (batch.length > 0) { |
| 359 | yield batch; |
| 360 | } |
| 361 | } |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | // Fall back to sync iteration - batch sync values together with a bound. |
| 366 | if (isSyncIterable(source)) { |
| 367 | let batch = []; |
| 368 | |
| 369 | for (const value of source) { |
| 370 | // Fast path 1: value is already a Uint8Array[] batch |
| 371 | if (isUint8ArrayBatch(value)) { |
| 372 | // Flush any accumulated batch first |
| 373 | if (batch.length > 0) { |
| 374 | yield batch; |
| 375 | batch = []; |
| 376 | } |
| 377 | yield* yieldBoundedBatch(value); |
| 378 | continue; |
| 379 | } |
| 380 | // Fast path 2: value is a single Uint8Array (very common) |
| 381 | if (isUint8Array(value)) { |
| 382 | ArrayPrototypePush(batch, value); |
| 383 | if (batch.length === FROM_BATCH_SIZE) { |
| 384 | yield batch; |
| 385 | batch = []; |
| 386 | } |
| 387 | continue; |
| 388 | } |
| 389 | // Slow path: normalize the value - must flush and yield individually |
| 390 | if (batch.length > 0) { |
| 391 | yield batch; |
| 392 | batch = []; |
| 393 | } |
| 394 | let asyncBatch = []; |
no test coverage detected
searching dependent graphs…