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

Function collectAsync

lib/internal/streams/iter/consumers.js:120–155  ·  view source on GitHub ↗

* Collect chunks from an async or sync source into an array. * @param {AsyncIterable |Iterable } source * @param {AbortSignal} [signal] * @param {number} [limit] * @returns {Promise }

(source, signal, limit)

Source from the content-addressed store, hash-verified

118 * @returns {Promise<Uint8Array[]>}
119 */
120async function collectAsync(source, signal, limit) {
121 signal?.throwIfAborted();
122
123 // Normalize source via from() - accepts strings, ArrayBuffers, protocols, etc.
124 const normalized = from(source);
125 const chunks = [];
126
127 // Fast path: no signal and no limit
128 if (!signal && limit === undefined) {
129 for await (const batch of normalized) {
130 for (let i = 0; i < batch.length; i++) {
131 ArrayPrototypePush(chunks, batch[i]);
132 }
133 }
134 return chunks;
135 }
136
137 // Slow path: with signal or limit checks
138 let totalBytes = 0;
139
140 for await (const batch of normalized) {
141 signal?.throwIfAborted();
142 for (let i = 0; i < batch.length; i++) {
143 const chunk = batch[i];
144 if (limit !== undefined) {
145 totalBytes += TypedArrayPrototypeGetByteLength(chunk);
146 if (totalBytes > limit) {
147 throw new ERR_OUT_OF_RANGE('totalBytes', `<= ${limit}`, totalBytes);
148 }
149 }
150 ArrayPrototypePush(chunks, chunk);
151 }
152 }
153
154 return chunks;
155}
156
157/**
158 * Convert a Uint8Array to its backing ArrayBuffer, slicing if necessary.

Callers 4

bytesFunction · 0.85
textFunction · 0.85
arrayBufferFunction · 0.85
arrayFunction · 0.85

Calls 2

throwIfAbortedMethod · 0.80
fromFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…