MCPcopy Create free account
hub / github.com/WinterTC55/iter-streams / applyStatelessAsyncTransform

Function applyStatelessAsyncTransform

src/pull.ts:365–420  ·  view source on GitHub ↗

* Apply a single stateless async transform to a source.

(
  source: AsyncIterable<Uint8Array[] | null>,
  transform: TransformFn,
  options: TransformCallbackOptions
)

Source from the content-addressed store, hash-verified

363/**
364 * Apply a single stateless async transform to a source.
365 */
366async function* applyStatelessAsyncTransform(
367 source: AsyncIterable<Uint8Array[] | null>,
368 transform: TransformFn,
369 options: TransformCallbackOptions
370): AsyncGenerator<Uint8Array[]> {
371 for await (const chunks of source) {
372 const result = transform(chunks, options);
373 // Fast path: result is already Uint8Array[] (common case)
374 if (result === null) continue;
375 if (isUint8ArrayBatch(result)) {
376 if (result.length > 0) {
377 yield result;
378 }
379 continue;
380 }
381 // Handle Promise of Uint8Array[]
382 if (result instanceof Promise) {
383 const resolved = await result;
384 if (resolved === null) continue;
385 if (isUint8ArrayBatch(resolved)) {
386 if (resolved.length > 0) {
387 yield resolved;
388 }
389 continue;
390 }
391 // Fall through to slow path
392 yield* processTransformResultAsync(resolved);
393 continue;
394 }
395 // Fast path: sync generator/iterable - collect all yielded items into batches
396 // This avoids the overhead of processTransformResultAsync for simple generators
397 if (isSyncIterable(result) && !isAsyncIterable(result)) {
398 const batch: Uint8Array[] = [];
399 for (const item of result as Iterable<unknown>) {
400 // Fast path: item is Uint8Array[] batch (common for generators that yield batches)
401 if (isUint8ArrayBatch(item)) {
402 batch.push(...(item as Uint8Array[]));
403 } else if (item instanceof Uint8Array) {
404 // Single Uint8Array
405 batch.push(item);
406 } else if (item !== null && item !== undefined) {
407 // Other item types - flatten and add to batch
408 for await (const chunk of flattenTransformYieldAsync(item as TransformYield)) {
409 batch.push(chunk);
410 }
411 }
412 }
413 if (batch.length > 0) {
414 yield batch;
415 }
416 continue;
417 }
418 // Slow path for other types (async iterables, complex nested structures)
419 yield* processTransformResultAsync(result);
420 }
421}
422

Callers 1

createAsyncPipelineFunction · 0.70

Calls 7

isSyncIterableFunction · 0.90
isAsyncIterableFunction · 0.90
transformFunction · 0.70
isUint8ArrayBatchFunction · 0.70
pushMethod · 0.65

Tested by

no test coverage detected