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

Function readableStreamFromIterable

lib/internal/webstreams/readablestream.js:1410–1455  ·  view source on GitHub ↗
(iterable)

Source from the content-addressed store, hash-verified

1408}
1409
1410function readableStreamFromIterable(iterable) {
1411 let stream;
1412 const iteratorGetter = iterable[SymbolAsyncIterator] ?? iterable[SymbolIterator];
1413 if (iteratorGetter == null || typeof iteratorGetter !== 'function') {
1414 throw new ERR_ARG_NOT_ITERABLE(iterable);
1415 }
1416 const iterator = FunctionPrototypeCall(iteratorGetter, iterable);
1417 if (iterator === null || (typeof iterator !== 'object' && typeof iterator !== 'function')) {
1418 throw new ERR_INVALID_STATE.TypeError('The iterator method must return an object');
1419 }
1420 const startAlgorithm = nonOpStart;
1421
1422 async function pullAlgorithm() {
1423 const iterResult = await iterator.next();
1424 if (typeof iterResult !== 'object' || iterResult === null) {
1425 throw new ERR_INVALID_STATE.TypeError(
1426 'The promise returned by the iterator.next() method must fulfill with an object');
1427 }
1428 if (iterResult.done) {
1429 readableStreamDefaultControllerClose(stream[kState].controller);
1430 } else {
1431 readableStreamDefaultControllerEnqueue(stream[kState].controller, await iterResult.value);
1432 }
1433 }
1434
1435 async function cancelAlgorithm(reason) {
1436 const returnMethod = iterator.return;
1437 if (returnMethod === undefined) {
1438 return;
1439 }
1440 const iterResult = await FunctionPrototypeCall(returnMethod, iterator, reason);
1441 if (typeof iterResult !== 'object' || iterResult === null) {
1442 throw new ERR_INVALID_STATE.TypeError(
1443 'The promise returned by the iterator.return() method must fulfill with an object');
1444 }
1445 }
1446
1447 stream = createReadableStream(
1448 startAlgorithm,
1449 pullAlgorithm,
1450 cancelAlgorithm,
1451 0,
1452 );
1453
1454 return stream;
1455}
1456
1457function readableStreamPipeTo(
1458 source,

Callers 1

fromMethod · 0.85

Calls 1

createReadableStreamFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…