* Pull next chunk from source into buffer. * Returns a promise that resolves when the pull completes (or immediately if already pulling).
()
| 302 | * Returns a promise that resolves when the pull completes (or immediately if already pulling). |
| 303 | */ |
| 304 | private pullFromSource(): Promise<void> { |
| 305 | if (this.sourceExhausted || this.cancelled) { |
| 306 | return Promise.resolve(); |
| 307 | } |
| 308 | |
| 309 | // If already pulling, wait for that pull to complete |
| 310 | if (this.pulling) { |
| 311 | return new Promise<void>((resolve) => { |
| 312 | this.pullWaiters.push(resolve); |
| 313 | }); |
| 314 | } |
| 315 | |
| 316 | this.pulling = true; |
| 317 | |
| 318 | return (async () => { |
| 319 | try { |
| 320 | // Initialize iterator if needed |
| 321 | if (!this.sourceIterator) { |
| 322 | if (isAsyncIterable(this.source)) { |
| 323 | this.sourceIterator = this.source[Symbol.asyncIterator](); |
| 324 | } else if (isSyncIterable(this.source)) { |
| 325 | // Wrap sync iterator |
| 326 | const syncIterator = (this.source as Iterable<Uint8Array[]>)[Symbol.iterator](); |
| 327 | this.sourceIterator = { |
| 328 | async next() { |
| 329 | return syncIterator.next(); |
| 330 | }, |
| 331 | async return() { |
| 332 | return syncIterator.return?.() ?? { done: true, value: undefined }; |
| 333 | }, |
| 334 | }; |
| 335 | } else { |
| 336 | throw new TypeError('Source must be iterable'); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | const result = await this.sourceIterator.next(); |
| 341 | |
| 342 | if (result.done) { |
| 343 | this.sourceExhausted = true; |
| 344 | } else { |
| 345 | this.buffer.push(result.value); |
| 346 | } |
| 347 | } catch (error) { |
| 348 | this.sourceError = error instanceof Error ? error : new Error(String(error)); |
| 349 | this.sourceExhausted = true; |
| 350 | } finally { |
| 351 | this.pulling = false; |
| 352 | // Wake up waiters so they can check the buffer |
| 353 | for (const waiter of this.pullWaiters) { |
| 354 | waiter(); |
| 355 | } |
| 356 | this.pullWaiters = []; |
| 357 | } |
| 358 | })(); |
| 359 | } |
| 360 | |
| 361 | /** |
no test coverage detected