* Normalize an async streamable yield value to Uint8Array chunks. * Recursively flattens arrays, iterables, async iterables, promises, * and protocol conversions. * @yields {Uint8Array}
(value)
| 264 | * @yields {Uint8Array} |
| 265 | */ |
| 266 | async function* normalizeAsyncValue(value) { |
| 267 | // Handle promises first |
| 268 | if (isPromise(value)) { |
| 269 | const resolved = await value; |
| 270 | yield* normalizeAsyncValue(resolved); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | // Handle primitives |
| 275 | if (isPrimitiveChunk(value)) { |
| 276 | yield primitiveToUint8Array(value); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | // Handle ToAsyncStreamable protocol (check before ToStreamable) |
| 281 | if (hasProtocol(value, toAsyncStreamable)) { |
| 282 | const result = FunctionPrototypeCall(value[toAsyncStreamable], value); |
| 283 | if (isPromise(result)) { |
| 284 | yield* normalizeAsyncValue(await result); |
| 285 | } else { |
| 286 | yield* normalizeAsyncValue(result); |
| 287 | } |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | // Handle ToStreamable protocol |
| 292 | if (hasProtocol(value, toStreamable)) { |
| 293 | const result = FunctionPrototypeCall(value[toStreamable], value); |
| 294 | yield* normalizeAsyncValue(result); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | // Handle arrays (which are also iterable, but check first for efficiency) |
| 299 | if (ArrayIsArray(value)) { |
| 300 | for (let i = 0; i < value.length; i++) { |
| 301 | yield* normalizeAsyncValue(value[i]); |
| 302 | } |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | // Handle async iterables (check before sync iterables since some objects |
| 307 | // have both) |
| 308 | if (isAsyncIterable(value)) { |
| 309 | for await (const item of value) { |
| 310 | yield* normalizeAsyncValue(item); |
| 311 | } |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | // Handle sync iterables |
| 316 | if (isSyncIterable(value)) { |
| 317 | for (const item of value) { |
| 318 | yield* normalizeAsyncValue(item); |
| 319 | } |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | // Reject: no valid conversion |
no test coverage detected
searching dependent graphs…