MCPcopy Index your code
hub / github.com/nodejs/node / #pullFromSource

Method #pullFromSource

lib/internal/streams/iter/share.js:298–354  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

296 }
297
298 #pullFromSource() {
299 if (this.#sourceExhausted || this.#cancelled) {
300 return PromiseResolve();
301 }
302
303 if (this.#pulling) {
304 const { promise, resolve } = PromiseWithResolvers();
305 ArrayPrototypePush(this.#pullWaiters, resolve);
306 return promise;
307 }
308
309 this.#pulling = true;
310
311 return (async () => {
312 try {
313 if (!this.#sourceIterator) {
314 if (isAsyncIterable(this.#source)) {
315 this.#sourceIterator =
316 this.#source[SymbolAsyncIterator]();
317 } else if (isSyncIterable(this.#source)) {
318 const syncIterator =
319 this.#source[SymbolIterator]();
320 this.#sourceIterator = {
321 __proto__: null,
322 async next() {
323 return syncIterator.next();
324 },
325 async return() {
326 return syncIterator.return?.() ??
327 { __proto__: null, done: true, value: undefined };
328 },
329 };
330 } else {
331 throw new ERR_INVALID_ARG_TYPE(
332 'source', ['AsyncIterable', 'Iterable'], this.#source);
333 }
334 }
335
336 const result = await this.#sourceIterator.next();
337
338 if (result.done) {
339 this.#sourceExhausted = true;
340 } else {
341 this.#buffer.push(result.value);
342 }
343 } catch (error) {
344 this.#sourceError = wrapError(error);
345 this.#sourceExhausted = true;
346 } finally {
347 this.#pulling = false;
348 for (let i = 0; i < this.#pullWaiters.length; i++) {
349 this.#pullWaiters[i]();
350 }
351 this.#pullWaiters = [];
352 }
353 })();
354 }
355

Callers 2

getNextMethod · 0.45
nextMethod · 0.45

Calls 5

wrapErrorFunction · 0.85
isAsyncIterableFunction · 0.70
isSyncIterableFunction · 0.70
nextMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected