From 37216c8166ed47723897894d7a121ac29b12a559 Mon Sep 17 00:00:00 2001 From: stringsplit <77242831+nbitzz@users.noreply.github.com> Date: Fri, 22 Dec 2023 22:03:01 -0800 Subject: [PATCH] todo another rework of readfilestream yay --- src/server/lib/files.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) 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 {