Skip to content

Commit

Permalink
fix: handle calling writable.end() correctly (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezg27 authored Nov 15, 2023
1 parent 3575d60 commit efa3fe2
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ export function writeFromReadableStream(stream: ReadableStream<Uint8Array>, writ
return
}
const reader = stream.getReader()
writable.on('drain', onDrain)
writable.on('close', cancel)
writable.on('error', cancel)
reader.read().then(flow, cancel)
return reader.closed.finally(() => {
writable.off('close', cancel)
writable.off('error', cancel)
writable.off('drain', onDrain)
})
function cancel(error?: any) {
reader.cancel(error).catch(() => {})
Expand All @@ -28,7 +26,9 @@ export function writeFromReadableStream(stream: ReadableStream<Uint8Array>, writ
try {
if (done) {
writable.end()
} else if (writable.write(value)) {
} else if (!writable.write(value)) {
writable.once("drain", onDrain);
} else {
return reader.read().then(flow, cancel)
}
} catch (e) {
Expand Down

0 comments on commit efa3fe2

Please sign in to comment.