MCPcopy Create free account
hub / github.com/nodejs/node / toReadableSync

Function toReadableSync

lib/internal/streams/iter/classic.js:354–402  ·  view source on GitHub ↗

* Create a byte-mode Readable from an Iterable . * Fully synchronous -- _read() pulls from the iterator directly. * @param {Iterable } source * @param {object} [options] * @param {number} [options.highWaterMark] * @returns {stream.Readable}

(source, options = kNullPrototype)

Source from the content-addressed store, hash-verified

352 * @returns {stream.Readable}
353 */
354function toReadableSync(source, options = kNullPrototype) {
355 if (typeof source?.[SymbolIterator] !== 'function') {
356 throw new ERR_INVALID_ARG_TYPE('source', 'Iterable', source);
357 }
358
359 validateObject(options, 'options');
360 const {
361 highWaterMark = 64 * 1024,
362 } = options;
363 validateInteger(highWaterMark, 'options.highWaterMark', 0);
364
365 const ReadableCtor = lazyReadable();
366 const iterator = source[SymbolIterator]();
367 let hasBatch = false;
368 let batch;
369 let batchIndex = 0;
370
371 return new ReadableCtor({
372 __proto__: null,
373 highWaterMark,
374 read() {
375 for (;;) {
376 if (hasBatch) {
377 while (batchIndex < batch.length) {
378 if (!this.push(batch[batchIndex++])) return;
379 }
380 batch = undefined;
381 hasBatch = false;
382 batchIndex = 0;
383 }
384
385 const result = iterator.next();
386 const { done } = result;
387 if (done) {
388 this.push(null);
389 return;
390 }
391 batch = result.value;
392 hasBatch = true;
393 }
394 },
395 destroy(err, cb) {
396 batch = undefined;
397 hasBatch = false;
398 if (typeof iterator.return === 'function') iterator.return();
399 cb(err);
400 },
401 });
402}
403
404
405// ============================================================================

Callers 10

testBasicSyncFunction · 0.85
testSyncReadFunction · 0.85
testBackpressureSyncFunction · 0.85
testErrorSyncFunction · 0.85
testEmptySyncFunction · 0.85
testDestroySyncFunction · 0.85
testNotObjectModeSyncFunction · 0.85
testInvalidSourceSyncFunction · 0.85

Calls 1

lazyReadableFunction · 0.85

Tested by

no test coverage detected