Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(api): respect backpressure when streaming asset downloads

Mohamed Bassemb5cab5be9043
diff --git a/packages/api/utils/upload.ts b/packages/api/utils/upload.ts
index 27059ae9..2e678ef4 100644
--- a/packages/api/utils/upload.ts
+++ b/packages/api/utils/upload.ts
@@ -38,15 +38,11 @@ export function webStreamToNode(
export function toWebReadableStream(
nodeStream: NodeJS.ReadableStream,
): ReadableStream<Uint8Array> {
- const reader = nodeStream as unknown as Readable;
-
- return new ReadableStream({
- start(controller) {
- reader.on("data", (chunk) => controller.enqueue(new Uint8Array(chunk)));
- reader.on("end", () => controller.close());
- reader.on("error", (err) => controller.error(err));
- },
- });
+ // Readable.toWeb propagates backpressure and destroys the source on cancel,
+ // so slow or disconnected clients don't cause the whole asset to be read.
+ return Readable.toWeb(
+ nodeStream as Readable,
+ ) as unknown as ReadableStream<Uint8Array>;
}
export async function uploadAsset(