MCPcopy
hub / github.com/remix-run/react-router / writeAsyncIterableToWritable

Function writeAsyncIterableToWritable

packages/react-router-node/stream.ts:132–174  ·  view source on GitHub ↗
(
  iterable: AsyncIterable<Uint8Array>,
  writable: Writable,
)

Source from the content-addressed store, hash-verified

130}
131
132export async function writeAsyncIterableToWritable(
133 iterable: AsyncIterable<Uint8Array>,
134 writable: Writable,
135) {
136 let writableError = monitorWritableError(writable);
137 let iterator = iterable[Symbol.asyncIterator]();
138 let completed = false;
139
140 try {
141 while (true) {
142 writableError.throwIfClosed();
143
144 let { done, value: chunk } = await writableError.race(iterator.next());
145
146 if (done) {
147 completed = true;
148 break;
149 }
150
151 writableError.throwIfClosed();
152
153 let canContinueWriting = writable.write(chunk);
154
155 if (!canContinueWriting) {
156 await waitForDrain(writable, writableError);
157 }
158 }
159
160 writable.end();
161 } catch (error: any) {
162 if (!completed) {
163 try {
164 Promise.resolve(iterator.return?.()).catch(() => {});
165 } catch {
166 // Ignore return errors so we preserve the original write failure.
167 }
168 }
169 writable.destroy(error);
170 throw error;
171 } finally {
172 writableError.cleanup();
173 }
174}
175
176export async function readableStreamToString(
177 stream: ReadableStream<Uint8Array>,

Callers 1

stream-test.tsFile · 0.90

Calls 6

monitorWritableErrorFunction · 0.85
waitForDrainFunction · 0.85
throwIfClosedMethod · 0.80
raceMethod · 0.80
nextMethod · 0.80
cleanupMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…