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

Function fromSync

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

* Create a SyncByteStreamReadable from a ByteInput or SyncStreamable. * @param {string|ArrayBuffer|ArrayBufferView|Iterable} input * @returns {Iterable }

(input)

Source from the content-addressed store, hash-verified

428 * @returns {Iterable<Uint8Array[]>}
429 */
430function fromSync(input) {
431 if (input == null) {
432 throw new ERR_INVALID_ARG_TYPE('input', 'a non-null value', input);
433 }
434
435 // Check for primitives first (ByteInput)
436 if (isPrimitiveChunk(input)) {
437 const chunk = primitiveToUint8Array(input);
438 return {
439 __proto__: null,
440 *[SymbolIterator]() {
441 yield [chunk];
442 },
443 };
444 }
445
446 // Fast path: Uint8Array[] - yield in bounded sub-batches.
447 // Yielding the entire array as one batch forces downstream transforms
448 // to process all data at once, causing peak memory proportional to total
449 // data volume. Sub-batching keeps peak memory bounded while preserving
450 // the throughput benefit of batched processing.
451 if (ArrayIsArray(input)) {
452 if (input.length === 0) {
453 return {
454 __proto__: null,
455 *[SymbolIterator]() {
456 // Empty - yield nothing
457 },
458 };
459 }
460 // Check if it's an array of Uint8Array (common case)
461 if (isUint8Array(input[0])) {
462 const allUint8 = ArrayPrototypeEvery(input, isUint8Array);
463 if (allUint8) {
464 const batch = input;
465 return {
466 __proto__: null,
467 *[SymbolIterator]() {
468 if (batch.length <= FROM_BATCH_SIZE) {
469 yield batch;
470 } else {
471 for (let i = 0; i < batch.length; i += FROM_BATCH_SIZE) {
472 yield ArrayPrototypeSlice(batch, i, i + FROM_BATCH_SIZE);
473 }
474 }
475 },
476 };
477 }
478 }
479 }
480
481 // Check toStreamable protocol (takes precedence over iteration protocols).
482 // toAsyncStreamable is ignored entirely in fromSync.
483 if (typeof input[toStreamable] === 'function') {
484 return fromSync(input[toStreamable]());
485 }
486
487 // Reject explicit async inputs

Callers 15

collectSyncFunction · 0.70
[SymbolIterator]Function · 0.70
pipeToSyncFunction · 0.70
testBytesSyncBasicFunction · 0.50
testBytesSyncLimitFunction · 0.50
testArrayBufferSyncBasicFunction · 0.50
testArraySyncBasicFunction · 0.50
testArraySyncLimitFunction · 0.50
testPullSyncIdentityFunction · 0.50

Calls 5

isPrimitiveChunkFunction · 0.85
primitiveToUint8ArrayFunction · 0.85
isUint8ArrayFunction · 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…