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

Function fromReadable

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

* Convert a classic Readable (or duck-type) to a stream/iter async iterable. * * If the object implements the toAsyncStreamable protocol, delegates to it. * Otherwise, duck-type checks for read() + EventEmitter (on/off) and * wraps with a batched async iterator. * @param {object} readable - A c

(readable)

Source from the content-addressed store, hash-verified

208 * @returns {AsyncIterable<Uint8Array[]>} A stream/iter async iterable source.
209 */
210function fromReadable(readable) {
211 if (readable == null || typeof readable !== 'object') {
212 throw new ERR_INVALID_ARG_TYPE('readable', 'Readable', readable);
213 }
214
215 // Check cache first.
216 const cached = fromReadableCache.get(readable);
217 if (cached !== undefined) return cached;
218
219 // Protocol path: object implements toAsyncStreamable.
220 if (typeof readable[kToAsyncStreamable] === 'function') {
221 const result = readable[kToAsyncStreamable]();
222 fromReadableCache.set(readable, result);
223 return result;
224 }
225
226 // Duck-type path: object has read() and EventEmitter methods.
227 if (typeof readable.read !== 'function' ||
228 typeof readable.on !== 'function') {
229 throw new ERR_INVALID_ARG_TYPE('readable', 'Readable', readable);
230 }
231
232 // Determine normalization. If the stream has _readableState, use it
233 // to detect object-mode / encoding. Otherwise assume byte-mode.
234 const state = readable._readableState;
235 const normalize = (state && (state.objectMode || state.encoding)) ?
236 normalizeBatch : null;
237
238 const iter = createBatchedAsyncIterator(readable, normalize);
239 iter[kValidatedSource] = true;
240 iter.stream = readable;
241
242 fromReadableCache.set(readable, iter);
243 return iter;
244}
245
246
247// ============================================================================

Callers

nothing calls this directly

Calls 3

getMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…