MCPcopy
hub / github.com/rvagg/through2 / transform

Function transform

web.js:59–100  ·  view source on GitHub ↗
(transformFn, flushFn)

Source from the content-addressed store, hash-verified

57 * @returns {{ readable: ReadableStream<any>, writable: WritableStream<any> }}
58 */
59export 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.

Callers 15

classicControllerEnqueueFunction · 0.90
asyncgenFanOutFunction · 0.90
asyncgenFilterFunction · 0.90
flushClassicFunction · 0.90
flushAsyncFunction · 0.90
asyncRejectedPropagatesFunction · 0.90
asyncgenThrownPropagatesFunction · 0.90
noopDefaultFunction · 0.90

Calls 2

asyncGenTransformFunction · 0.85
fnKindFunction · 0.70

Tested by

no test coverage detected