From dbd5f1ffc79508edebe1cca5a0e74a8796d03773 Mon Sep 17 00:00:00 2001 From: mohankumarelec Date: Mon, 22 Apr 2024 12:19:55 +0530 Subject: [PATCH 1/2] Removed compression for node runtime since its buffering the response while streaming --- src/handlers/handlerUtils.ts | 4 ++++ src/index.ts | 16 +++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/handlers/handlerUtils.ts b/src/handlers/handlerUtils.ts index 4d8a3559..e8c8d08b 100644 --- a/src/handlers/handlerUtils.ts +++ b/src/handlers/handlerUtils.ts @@ -33,6 +33,7 @@ import { import { env } from 'hono/adapter'; import { OpenAIChatCompleteJSONToStreamResponseTransform } from '../providers/openai/chatComplete'; import { OpenAICompleteJSONToStreamResponseTransform } from '../providers/openai/complete'; +import { getRuntimeKey } from 'hono/adapter'; /** * Constructs the request options for the API call. @@ -945,6 +946,9 @@ export function updateResponseHeaders( // Brotli compression causes errors at runtime, removing the header in that case response.headers.delete('content-encoding'); } + if (getRuntimeKey() == 'node') { + response.headers.delete('content-encoding'); + } // Delete content-length header to avoid conflicts with hono compress middleware // workerd environment handles this authomatically diff --git a/src/index.ts b/src/index.ts index 7573f8e9..48cc5d64 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,13 +31,15 @@ const app = new Hono(); * This check if its not any of the 2 and then applies the compress middleware to avoid double compression. */ -app.use('*', (c, next) => { - const runtime = getRuntimeKey(); - if (runtime !== 'lagon' && runtime !== 'workerd') { - return compress()(c, next); - } - return next(); -}); +if (getRuntimeKey() != 'node') { + app.use('*', (c, next) => { + const runtime = getRuntimeKey(); + if (runtime !== 'lagon' && runtime !== 'workerd') { + return compress()(c, next); + } + return next(); + }); +} /** * GET route for the root path. From e5eab5c4ad9f63e5f2e81a0a68cd1c01a9ac9c39 Mon Sep 17 00:00:00 2001 From: mohankumarelec Date: Mon, 29 Apr 2024 10:05:53 +0530 Subject: [PATCH 2/2] Update index.ts --- src/index.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 48cc5d64..f852f67e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,15 +31,13 @@ const app = new Hono(); * This check if its not any of the 2 and then applies the compress middleware to avoid double compression. */ -if (getRuntimeKey() != 'node') { - app.use('*', (c, next) => { - const runtime = getRuntimeKey(); - if (runtime !== 'lagon' && runtime !== 'workerd') { - return compress()(c, next); - } - return next(); - }); -} +app.use('*', (c, next) => { + const runtime = getRuntimeKey(); + if (runtime !== 'lagon' && runtime !== 'workerd' && runtime !== 'node') { + return compress()(c, next); + } + return next(); +}); /** * GET route for the root path.