MCPcopy Index your code
hub / github.com/nodejs/node / normalizeAsyncSource

Function normalizeAsyncSource

lib/internal/streams/iter/from.js:337–419  ·  view source on GitHub ↗

* Normalize an async streamable source, yielding batches of Uint8Array. * @param {AsyncIterable|Iterable} source * @yields {Uint8Array[]}

(source)

Source from the content-addressed store, hash-verified

335 * @yields {Uint8Array[]}
336 */
337async 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 = [];

Callers 1

fromFunction · 0.85

Calls 6

isUint8ArrayBatchFunction · 0.85
isUint8ArrayFunction · 0.85
normalizeAsyncValueFunction · 0.85
yieldBoundedBatchFunction · 0.85
isAsyncIterableFunction · 0.70
isSyncIterableFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…