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

Function fromDuplex

packages/platform/node-shared/src/NodeStream.ts:78–115  ·  view source on GitHub ↗
(
  options: {
    readonly evaluate: LazyArg<Duplex>
    readonly onError?: (error: unknown) => E
    readonly chunkSize?: number | undefined
    readonly bufferSize?: number | undefined
    readonly endOnDone?: boolean | undefined
    readonly encoding?: BufferEncoding | undefined
  }
)

Source from the content-addressed store, hash-verified

76 * @since 4.0.0
77 */
78export const fromDuplex = <IE, I = Uint8Array, O = Uint8Array, E = Cause.UnknownError>(
79 options: {
80 readonly evaluate: LazyArg<Duplex>
81 readonly onError?: (error: unknown) => E
82 readonly chunkSize?: number | undefined
83 readonly bufferSize?: number | undefined
84 readonly endOnDone?: boolean | undefined
85 readonly encoding?: BufferEncoding | undefined
86 }
87): Channel.Channel<Arr.NonEmptyReadonlyArray<O>, IE | E, void, Arr.NonEmptyReadonlyArray<I>, IE> =>
88 Channel.fromTransform((upstream, scope) => {
89 const duplex = options.evaluate()
90 const exit = MutableRef.make<Exit.Exit<never, IE | E | Cause.Done> | undefined>(undefined)
91
92 return pullIntoWritable({
93 pull: upstream,
94 writable: duplex,
95 onError: options.onError ?? defaultOnError as any,
96 endOnDone: options.endOnDone,
97 encoding: options.encoding
98 }).pipe(
99 Effect.catchCause((cause) => {
100 if (Pull.isDoneCause(cause)) return Effect.void
101 exit.current = Exit.failCause(cause as Cause.Cause<IE | E | Cause.Done>)
102 return Effect.void
103 }),
104 Effect.forkIn(scope),
105 Effect.flatMap(() =>
106 readableToPullUnsafe({
107 scope,
108 exit,
109 readable: duplex,
110 onError: options.onError ?? defaultOnError as any,
111 chunkSize: options.chunkSize
112 })
113 )
114 )
115 })
116
117/**
118 * Pipes an Effect `Stream` through a Node `Duplex`, writing the stream's

Callers 1

NodeStream.tsFile · 0.70

Calls 5

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

Tested by

no test coverage detected