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

Function pullIntoWritable

packages/platform/node-shared/src/NodeSink.ts:105–206  ·  view source on GitHub ↗
(options: {
  readonly pull: Pull.Pull<NonEmptyReadonlyArray<A>, IE, unknown>
  readonly writable: Writable
  readonly onError: (error: unknown) => E
  readonly endOnDone?: boolean | undefined
  readonly encoding?: BufferEncoding | undefined
})

Source from the content-addressed store, hash-verified

103 * @since 4.0.0
104 */
105export const pullIntoWritable = <A, IE, E>(options: {
106 readonly pull: Pull.Pull<NonEmptyReadonlyArray<A>, IE, unknown>
107 readonly writable: Writable
108 readonly onError: (error: unknown) => E
109 readonly endOnDone?: boolean | undefined
110 readonly encoding?: BufferEncoding | undefined
111}): Pull.Pull<never, IE | E, unknown> =>
112 Effect.withFiber((fiber) => {
113 const dispatcher = fiber.currentDispatcher
114 const completion = Deferred.makeUnsafe<void>()
115 const failure = Deferred.makeUnsafe<never, E>()
116 const onError = (error: unknown) => Deferred.doneUnsafe(failure, Effect.failSync(() => options.onError(error)))
117 let errorEmitted = false
118 const onStreamError = (error: unknown) => {
119 errorEmitted = true
120 onError(error)
121 }
122 // finished() only reports once, so keep listening for errors from writes
123 // made after the writable has finished.
124 options.writable.on("error", onStreamError)
125 const cleanup = finished(options.writable, { readable: false }, (error) => {
126 if (error) {
127 onError(error)
128 } else {
129 Deferred.doneUnsafe(completion, Effect.void)
130 }
131 })
132 return options.pull.pipe(
133 Effect.flatMap((chunk) => {
134 let i = 0
135 return Effect.callback<void, E>((resume) => {
136 let cancelled = false
137 // Let Node finish auto-destruction before attempting another write.
138 const onFinish = () => dispatcher.scheduleTask(loop, 0)
139 const loop = () => {
140 options.writable.off("drain", loop)
141 options.writable.off("finish", onFinish)
142 for (; i < chunk.length;) {
143 if (cancelled) {
144 return
145 }
146 const success = options.writable.write(chunk[i++], options.encoding as any, (error) => {
147 if (!error) return
148 if (options.writable.closed) {
149 // Closed streams can reject writes through the callback only;
150 // there is no pending destruction to wait for.
151 onError(error)
152 } else {
153 // Let Node initiate auto-destruction before failure cleanup
154 // checks whether an error event is still pending.
155 dispatcher.scheduleTask(() => onError(error), 0)
156 }
157 })
158 if (!success) {
159 if (!cancelled) {
160 options.writable.once("drain", loop)
161 // A normally finished writable will never emit drain. Resume
162 // the loop so any remaining writes can report their errors.

Callers 2

fromDuplexFunction · 0.90
fromWritableChannelFunction · 0.85

Calls 12

finishedFunction · 0.85
removeErrorListenerFunction · 0.85
onceMethod · 0.80
onErrorFunction · 0.70
loopFunction · 0.70
cleanupFunction · 0.70
onMethod · 0.65
pipeMethod · 0.65
endMethod · 0.65
awaitMethod · 0.65
syncMethod · 0.45
doneMethod · 0.45

Tested by

no test coverage detected