From 06304dd77f5576fee5ab983139ffe07b73e7b0fd Mon Sep 17 00:00:00 2001 From: monsieurswag Date: Thu, 3 Oct 2024 22:26:40 +0200 Subject: [PATCH] Modify the evidence file stream behavior to stop reading when being terminated by the client Fix useless code --- .../evidences/[id=uuid]/attachment/+server.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/frontend/src/routes/(app)/(third-party)/evidences/[id=uuid]/attachment/+server.ts b/frontend/src/routes/(app)/(third-party)/evidences/[id=uuid]/attachment/+server.ts index ed87e123c..9ac07bd10 100644 --- a/frontend/src/routes/(app)/(third-party)/evidences/[id=uuid]/attachment/+server.ts +++ b/frontend/src/routes/(app)/(third-party)/evidences/[id=uuid]/attachment/+server.ts @@ -29,9 +29,13 @@ export const GET: RequestHandler = async ({ fetch, setHeaders, params }) => { } const reader = attachmentResponse.body.getReader(); + let readerTerminated = false; const stream = new ReadableStream({ start(controller) { function push() { + if (readerTerminated) { + return; + } reader.read().then(({ done, value }) => { if (done) { controller.close(); @@ -42,6 +46,9 @@ export const GET: RequestHandler = async ({ fetch, setHeaders, params }) => { }); } push(); + }, + cancel() { + readerTerminated = true; } });