todo another rework of readfilestream yay

This commit is contained in:
May 2023-12-22 22:03:01 -08:00
parent c5040e1b79
commit 37216c8166

View file

@ -79,13 +79,13 @@ export interface StatusCodeError {
message: string message: string
} }
// this is probably really stupid. I'm sorry! /**
* @description This function does not respect backpressure and should be worked out of the codebase. Superseded by startPushingWebStream()
type ResolveType<T extends Promise<any>> = T extends Promise<infer U> ? U: never; */
async function pushWebStream(stream: Readable, webStream: ReadableStream) { async function pushWebStream(stream: Readable, webStream: ReadableStream) {
const reader = await webStream.getReader() const reader = await webStream.getReader()
let result: ResolveType<ReturnType<typeof reader.read>> = { done: false, value: undefined } let result: Awaited<ReturnType<typeof reader.read>> = { done: false, value: undefined }
let last = true let last = true
while ( !result.done ) { while ( !result.done ) {
@ -95,6 +95,24 @@ async function pushWebStream(stream: Readable, webStream: ReadableStream) {
return last return last
} }
async function startPushingWebStream(stream: Readable, webStream: ReadableStream) {
const reader = await webStream.getReader()
let pushing = false // acts as a debounce just in case
// (words of a girl paranoid from writing readfilestream)
return function() {
if (pushing) return
pushing = true
return reader.read().then(result => {
if (result.value)
stream.push(result.value)
pushing = false
return result.done
})
}
}
namespace StreamHelpers { namespace StreamHelpers {
export interface UploadStream { export interface UploadStream {