Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deduplicate onClose implementations for web handlers, wait for stream…
… end in edge route modules
- Loading branch information
1 parent
fa82174
commit c83ae52
Showing
4 changed files
with
104 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** Monitor when the consumer finishes reading the response body. | ||
that's as close as we can get to `res.on('close')` using web APIs. | ||
*/ | ||
export function trackBodyConsumed( | ||
body: string | ReadableStream, | ||
onEnd: () => void | ||
): BodyInit { | ||
if (typeof body === 'string') { | ||
const generator = async function* generate() { | ||
const encoder = new TextEncoder() | ||
yield encoder.encode(body) | ||
onEnd() | ||
} | ||
// @ts-expect-error BodyInit typings doesn't seem to include AsyncIterables even though it's supported in practice | ||
return generator() | ||
} else { | ||
return trackStreamConsumed(body, onEnd) | ||
} | ||
} | ||
|
||
export function trackStreamConsumed<TChunk>( | ||
stream: ReadableStream<TChunk>, | ||
onEnd: () => void | ||
): ReadableStream<TChunk> { | ||
const closePassThrough = new TransformStream<TChunk, TChunk>({ | ||
flush: () => { | ||
return onEnd() | ||
}, | ||
}) | ||
return stream.pipeThrough(closePassThrough) | ||
} | ||
|
||
export function createCloseController() { | ||
return new CloseController() | ||
} | ||
|
||
export class CloseController { | ||
target = new EventTarget() | ||
listeners = 0 | ||
isClosed = false | ||
|
||
onClose(callback: () => void) { | ||
if (this.isClosed) { | ||
throw new Error('Cannot subscribe to a closed CloseController') | ||
} | ||
|
||
this.target.addEventListener('close', callback) | ||
this.listeners++ | ||
} | ||
|
||
dispatchClose() { | ||
if (this.isClosed) { | ||
throw new Error('Cannot close multiple times') | ||
} | ||
if (this.listeners > 0) { | ||
this.target.dispatchEvent(new Event('close')) | ||
} | ||
this.isClosed = true | ||
} | ||
} |