(transformFn, flushFn)
| 57 | * @returns {{ readable: ReadableStream<any>, writable: WritableStream<any> }} |
| 58 | */ |
| 59 | export function transform (transformFn, flushFn) { |
| 60 | if (typeof transformFn !== 'function') { |
| 61 | /** @type {ClassicWebTransformFn} */ |
| 62 | const passthrough = (chunk, controller) => controller.enqueue(chunk) |
| 63 | transformFn = passthrough |
| 64 | } |
| 65 | |
| 66 | const tKind = fnKind(transformFn) |
| 67 | |
| 68 | if (tKind === 'asyncgen') { |
| 69 | return asyncGenTransform(/** @type {AsyncGenWebTransformFn} */ (transformFn), flushFn) |
| 70 | } |
| 71 | |
| 72 | /** @type {Transformer<any, any>} */ |
| 73 | const inner = {} |
| 74 | |
| 75 | if (tKind === 'async') { |
| 76 | const fn = /** @type {AsyncWebTransformFn} */ (transformFn) |
| 77 | inner.transform = async (chunk, controller) => { |
| 78 | const out = await fn(chunk) |
| 79 | if (out !== undefined) controller.enqueue(out) |
| 80 | } |
| 81 | } else { |
| 82 | const fn = /** @type {ClassicWebTransformFn} */ (transformFn) |
| 83 | inner.transform = (chunk, controller) => fn(chunk, controller) |
| 84 | } |
| 85 | |
| 86 | if (flushFn) { |
| 87 | if (fnKind(flushFn) === 'async') { |
| 88 | const fn = /** @type {AsyncWebFlushFn} */ (flushFn) |
| 89 | inner.flush = async (controller) => { |
| 90 | const out = await fn() |
| 91 | if (out !== undefined) controller.enqueue(out) |
| 92 | } |
| 93 | } else { |
| 94 | const fn = /** @type {ClassicWebFlushFn} */ (flushFn) |
| 95 | inner.flush = (controller) => fn(controller) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return new TransformStream(inner) |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Drive an async-generator transform as a `{ readable, writable }` pair. |
no test coverage detected