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

Function createBlobReaderIterable

lib/internal/blob.js:538–600  ·  view source on GitHub ↗
(reader, options = kEmptyObject)

Source from the content-addressed store, hash-verified

536const kMaxBatchChunks = 16;
537
538async function* createBlobReaderIterable(reader, options = kEmptyObject) {
539 const { getReadError } = options;
540 let wakeup = PromiseWithResolvers();
541 reader.setWakeup(wakeup.resolve);
542
543 try {
544 while (true) {
545 const batch = [];
546 let blocked = false;
547 let eos = false;
548 let error = null;
549
550 // Pull as many chunks as available synchronously.
551 // reader.pull(callback) calls the callback synchronously via
552 // MakeCallback, so we can collect multiple chunks per iteration
553 // step without any async overhead.
554 while (true) {
555 let pullResult;
556 reader.pull((status, buffer) => {
557 pullResult = { status, buffer };
558 });
559
560 if (pullResult.status === 0) {
561 eos = true;
562 break;
563 }
564 if (pullResult.status < 0) {
565 error = typeof getReadError === 'function' ?
566 getReadError(pullResult.status) :
567 new ERR_INVALID_STATE('The reader is not readable');
568 break;
569 }
570 if (pullResult.status === 2) {
571 blocked = true;
572 break;
573 }
574 ArrayPrototypePush(batch, new Uint8Array(pullResult.buffer));
575 if (batch.length >= kMaxBatchChunks) break;
576 }
577
578 if (batch.length > 0) {
579 yield batch;
580 }
581
582 if (eos) return;
583 if (error) throw error;
584
585 if (blocked) {
586 const fin = await wakeup.promise;
587 wakeup = PromiseWithResolvers();
588 reader.setWakeup(wakeup.resolve);
589 // If the wakeup was triggered by FIN (EndReadable), the DataQueue
590 // is capped. Continue the loop to pull again -- the next pull will
591 // return EOS. Without this, a race between the data notification
592 // and the FIN notification can leave the iterator waiting for a
593 // wakeup that will never come.
594 if (fin) continue;
595 }

Callers 1

[SymbolAsyncIterator]Method · 0.85

Calls 1

pullMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…