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

Function from

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

* Create a ByteStreamReadable from a ByteInput or Streamable. * @param {string|ArrayBuffer|ArrayBufferView|Iterable|AsyncIterable} input * @returns {AsyncIterable }

(input)

Source from the content-addressed store, hash-verified

523 * @returns {AsyncIterable<Uint8Array[]>}
524 */
525function from(input) {
526 if (input == null) {
527 throw new ERR_INVALID_ARG_TYPE('input', 'a non-null value', input);
528 }
529
530 // Fast path: validated source already yields valid Uint8Array[] batches
531 if (input[kValidatedSource]) {
532 return input;
533 }
534
535 // Check for primitives first (ByteInput)
536 if (isPrimitiveChunk(input)) {
537 const chunk = primitiveToUint8Array(input);
538 return {
539 __proto__: null,
540 async *[SymbolAsyncIterator]() {
541 yield [chunk];
542 },
543 };
544 }
545
546 // Fast path: Uint8Array[] - yield in bounded sub-batches.
547 // Yielding the entire array as one batch forces downstream transforms
548 // to process all data at once, causing peak memory proportional to total
549 // data volume. Sub-batching keeps peak memory bounded while preserving
550 // the throughput benefit of batched processing.
551 if (ArrayIsArray(input)) {
552 if (input.length === 0) {
553 return {
554 __proto__: null,
555 async *[SymbolAsyncIterator]() {
556 // Empty - yield nothing
557 },
558 };
559 }
560 if (isUint8Array(input[0])) {
561 const allUint8 = ArrayPrototypeEvery(input, isUint8Array);
562 if (allUint8) {
563 const batch = input;
564 return {
565 __proto__: null,
566 async *[SymbolAsyncIterator]() {
567 if (batch.length <= FROM_BATCH_SIZE) {
568 yield batch;
569 } else {
570 for (let i = 0; i < batch.length; i += FROM_BATCH_SIZE) {
571 yield ArrayPrototypeSlice(batch, i, i + FROM_BATCH_SIZE);
572 }
573 }
574 },
575 };
576 }
577 }
578 }
579
580 // Check toAsyncStreamable protocol (takes precedence over toStreamable and
581 // iteration protocols)
582 if (typeof input[toAsyncStreamable] === 'function') {

Callers 5

collectAsyncFunction · 0.70
mergeFunction · 0.70
[SymbolAsyncIterator]Function · 0.70
[SymbolAsyncIterator]Function · 0.70
pipeToFunction · 0.70

Calls 6

isPrimitiveChunkFunction · 0.85
primitiveToUint8ArrayFunction · 0.85
isUint8ArrayFunction · 0.85
normalizeAsyncSourceFunction · 0.85
isSyncIterableFunction · 0.70
isAsyncIterableFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…