* 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)
| 118 | * @returns {Promise<Uint8Array[]>} |
| 119 | */ |
| 120 | async 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. |
no test coverage detected
searching dependent graphs…