diff --git a/src/server/lib/files.ts b/src/server/lib/files.ts index d267241..a9e085d 100644 --- a/src/server/lib/files.ts +++ b/src/server/lib/files.ts @@ -79,13 +79,13 @@ export interface StatusCodeError { message: string } -// this is probably really stupid. I'm sorry! - -type ResolveType> = T extends Promise ? U: never; +/** + * @description This function does not respect backpressure and should be worked out of the codebase. Superseded by startPushingWebStream() + */ async function pushWebStream(stream: Readable, webStream: ReadableStream) { const reader = await webStream.getReader() - let result: ResolveType> = { done: false, value: undefined } + let result: Awaited> = { done: false, value: undefined } let last = true while ( !result.done ) { @@ -95,6 +95,24 @@ async function pushWebStream(stream: Readable, webStream: ReadableStream) { 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 { export interface UploadStream {