MCPcopy Create free account
hub / github.com/Effect-TS/effect / fromDuplex

Function fromDuplex

packages/platform/node-shared/src/NodeStream.ts:117–165  ·  view source on GitHub ↗
(
  options: {
    readonly evaluate: LazyArg<Duplex>
    readonly onError?: (error: unknown) => E
    /**
     * Maximum number of Node reads per pull (default 4096). Set to Infinity
     * for unbounded batching. Does not control the size of each Node read.
     */
    readonly chunkSize?: number | undefined
    /**
     * Size passed to Node's `read(size)`. Omit to use Node's unsized reads.
     */
    readonly readSize?: number | undefined
    readonly endOnDone?: boolean | undefined
    readonly encoding?: BufferEncoding | undefined
  }
)

Source from the content-addressed store, hash-verified

115 * @since 4.0.0
116 */
117export const fromDuplex = <IE, I = Uint8Array, O = Uint8Array, E = Cause.UnknownError>(
118 options: {
119 readonly evaluate: LazyArg<Duplex>
120 readonly onError?: (error: unknown) => E
121 /**
122 * Maximum number of Node reads per pull (default 4096). Set to Infinity
123 * for unbounded batching. Does not control the size of each Node read.
124 */
125 readonly chunkSize?: number | undefined
126 /**
127 * Size passed to Node's `read(size)`. Omit to use Node's unsized reads.
128 */
129 readonly readSize?: number | undefined
130 readonly endOnDone?: boolean | undefined
131 readonly encoding?: BufferEncoding | undefined
132 }
133): Channel.Channel<Arr.NonEmptyReadonlyArray<O>, IE | E, void, Arr.NonEmptyReadonlyArray<I>, IE> =>
134 Channel.fromTransform((upstream, scope) => {
135 const duplex = options.evaluate()
136 const exit = MutableRef.make<Exit.Exit<never, IE | E | Cause.Done> | undefined>(undefined)
137 const latch = Latch.makeUnsafe(false)
138
139 return pullIntoWritable({
140 pull: upstream,
141 writable: duplex,
142 onError: options.onError ?? defaultOnError as any,
143 endOnDone: options.endOnDone,
144 encoding: options.encoding
145 }).pipe(
146 Effect.catchCause((cause) => {
147 if (Pull.isDoneCause(cause)) return Effect.void
148 exit.current = Exit.failCause(cause as Cause.Cause<IE | E | Cause.Done>)
149 latch.openUnsafe()
150 return Effect.void
151 }),
152 Effect.forkIn(scope),
153 Effect.flatMap(() =>
154 readableToPullUnsafe({
155 scope,
156 exit,
157 latch,
158 readable: duplex,
159 onError: options.onError ?? defaultOnError as any,
160 chunkSize: options.chunkSize,
161 readSize: options.readSize
162 })
163 )
164 )
165 })
166
167/**
168 * Pipes an Effect `Stream` through a Node `Duplex`, writing the stream's

Callers 1

NodeStream.tsFile · 0.70

Calls 6

pullIntoWritableFunction · 0.90
readableToPullUnsafeFunction · 0.85
evaluateMethod · 0.65
makeMethod · 0.65
pipeMethod · 0.65
openUnsafeMethod · 0.65

Tested by

no test coverage detected