From 51c7ddce809e88dd60c0e419e5caf1b9c908736a Mon Sep 17 00:00:00 2001 From: April Hall Date: Tue, 25 Feb 2025 11:03:55 -0500 Subject: [PATCH] fix: (UNSURE) ERR_INVALID_STATE Stream controller in the endpoint that streams files from the S3 bucket would ocassionally crash the server by throwing `TypeError [ERR_INVALID_STATE]: Invalid state: Controller is already closed`. This should fix that, but as the conditions that cause it to happen are unknown, this is not certain. --- src/routes/(server)/api/images/[filename]/+server.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/routes/(server)/api/images/[filename]/+server.ts b/src/routes/(server)/api/images/[filename]/+server.ts index b3da31d..1b3e967 100644 --- a/src/routes/(server)/api/images/[filename]/+server.ts +++ b/src/routes/(server)/api/images/[filename]/+server.ts @@ -12,7 +12,11 @@ export const GET = async ({ params }) => { const readableStream = new ReadableStream({ start(controller) { - stream.on('data', (chunk) => controller.enqueue(chunk)); + stream.on('data', (chunk) => { + if (controller.desiredSize !== null && controller.desiredSize > 0) { + controller.enqueue(chunk); + } + }); stream.on('end', () => controller.close()); stream.on('error', (err) => controller.error(err)); },