From 71aa86a06fa01b1857c16185eff3d9941f3e7e65 Mon Sep 17 00:00:00 2001 From: knextkoder Date: Fri, 27 Feb 2026 15:29:48 +0100 Subject: [PATCH 1/3] bug fix #1379: set content-type header based on response type in mapResponse and mapEarlyResponse --- src/adapter/bun/handler.ts | 24 ++++++++++++++++---- src/adapter/web-standard/handler.ts | 34 ++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 10 deletions(-) mode change 100644 => 100755 src/adapter/bun/handler.ts mode change 100644 => 100755 src/adapter/web-standard/handler.ts diff --git a/src/adapter/bun/handler.ts b/src/adapter/bun/handler.ts old mode 100644 new mode 100755 index b7c1a7e4..fa2e111e --- a/src/adapter/bun/handler.ts +++ b/src/adapter/bun/handler.ts @@ -25,6 +25,12 @@ export const mapResponse = ( request?: Request ): Response => { if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { + if ( + response?.constructor?.name === 'Object' || + response?.constructor?.name === 'Array' + ) + set.headers['content-type'] = 'application/json' + handleSet(set) switch (response?.constructor?.name) { @@ -33,7 +39,6 @@ export const mapResponse = ( case 'Array': case 'Object': - set.headers['content-type'] = 'application/json' return new Response(JSON.stringify(response), set as any) case 'ElysiaFile': @@ -141,7 +146,10 @@ export const mapResponse = ( const code = (response as any).charCodeAt(0) if (code === 123 || code === 91) { - if (!set.headers['Content-Type']) + if (set.headers instanceof Headers) { + if (!set.headers.has('Content-Type')) + set.headers.set('Content-Type', 'application/json') + } else if (!set.headers['Content-Type']) set.headers['Content-Type'] = 'application/json' return new Response( @@ -174,6 +182,12 @@ export const mapEarlyResponse = ( if (response === undefined || response === null) return if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { + if ( + response?.constructor?.name === 'Object' || + response?.constructor?.name === 'Array' + ) + set.headers['content-type'] = 'application/json' + handleSet(set) switch (response?.constructor?.name) { @@ -182,7 +196,6 @@ export const mapEarlyResponse = ( case 'Array': case 'Object': - set.headers['content-type'] = 'application/json' return new Response(JSON.stringify(response), set as any) case 'ElysiaFile': @@ -289,7 +302,10 @@ export const mapEarlyResponse = ( const code = (response as any).charCodeAt(0) if (code === 123 || code === 91) { - if (!set.headers['Content-Type']) + if (set.headers instanceof Headers) { + if (!set.headers.has('Content-Type')) + set.headers.set('Content-Type', 'application/json') + } else if (!set.headers['Content-Type']) set.headers['Content-Type'] = 'application/json' return new Response( diff --git a/src/adapter/web-standard/handler.ts b/src/adapter/web-standard/handler.ts old mode 100644 new mode 100755 index a679eb34..ce97ac68 --- a/src/adapter/web-standard/handler.ts +++ b/src/adapter/web-standard/handler.ts @@ -54,16 +54,24 @@ export const mapResponse = ( request?: Request ): Response => { if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { + switch (response?.constructor?.name) { + case 'String': + set.headers['content-type'] = 'text/plain' + break + case 'Array': + case 'Object': + set.headers['content-type'] = 'application/json' + break + } + handleSet(set) switch (response?.constructor?.name) { case 'String': - set.headers['content-type'] = 'text/plain' return new Response(response as string, set as any) case 'Array': case 'Object': - set.headers['content-type'] = 'application/json' return new Response(JSON.stringify(response), set as any) case 'ElysiaFile': @@ -171,7 +179,10 @@ export const mapResponse = ( const code = (response as any).charCodeAt(0) if (code === 123 || code === 91) { - if (!set.headers['Content-Type']) + if (set.headers instanceof Headers) { + if (!set.headers.has('Content-Type')) + set.headers.set('Content-Type', 'application/json') + } else if (!set.headers['Content-Type']) set.headers['Content-Type'] = 'application/json' return new Response( @@ -204,16 +215,24 @@ export const mapEarlyResponse = ( if (response === undefined || response === null) return if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { + switch (response?.constructor?.name) { + case 'String': + set.headers['content-type'] = 'text/plain' + break + case 'Array': + case 'Object': + set.headers['content-type'] = 'application/json' + break + } + handleSet(set) switch (response?.constructor?.name) { case 'String': - set.headers['content-type'] = 'text/plain' return new Response(response as string, set as any) case 'Array': case 'Object': - set.headers['content-type'] = 'application/json' return new Response(JSON.stringify(response), set as any) case 'ElysiaFile': @@ -320,7 +339,10 @@ export const mapEarlyResponse = ( const code = (response as any).charCodeAt(0) if (code === 123 || code === 91) { - if (!set.headers['Content-Type']) + if (set.headers instanceof Headers) { + if (!set.headers.has('Content-Type')) + set.headers.set('Content-Type', 'application/json') + } else if (!set.headers['Content-Type']) set.headers['Content-Type'] = 'application/json' return new Response( From c146d2a1249a884c1442a8e86a7b731684b1598d Mon Sep 17 00:00:00 2001 From: knextkoder Date: Fri, 27 Feb 2026 16:17:43 +0100 Subject: [PATCH 2/3] bug fix #1379: set content-type header based on response type in mapResponse and mapEarlyResponse --- src/adapter/bun/handler.ts | 10 +++++--- src/adapter/web-standard/handler.ts | 38 +++++++++++++++-------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/adapter/bun/handler.ts b/src/adapter/bun/handler.ts index fa2e111e..c83ad7fe 100755 --- a/src/adapter/bun/handler.ts +++ b/src/adapter/bun/handler.ts @@ -26,8 +26,9 @@ export const mapResponse = ( ): Response => { if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { if ( - response?.constructor?.name === 'Object' || - response?.constructor?.name === 'Array' + !set.headers['content-type'] && + (response?.constructor?.name === 'Object' || + response?.constructor?.name === 'Array') ) set.headers['content-type'] = 'application/json' @@ -183,8 +184,9 @@ export const mapEarlyResponse = ( if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { if ( - response?.constructor?.name === 'Object' || - response?.constructor?.name === 'Array' + !set.headers['content-type'] && + (response?.constructor?.name === 'Object' || + response?.constructor?.name === 'Array') ) set.headers['content-type'] = 'application/json' diff --git a/src/adapter/web-standard/handler.ts b/src/adapter/web-standard/handler.ts index ce97ac68..53e1a971 100755 --- a/src/adapter/web-standard/handler.ts +++ b/src/adapter/web-standard/handler.ts @@ -54,15 +54,16 @@ export const mapResponse = ( request?: Request ): Response => { if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { - switch (response?.constructor?.name) { - case 'String': - set.headers['content-type'] = 'text/plain' - break - case 'Array': - case 'Object': - set.headers['content-type'] = 'application/json' - break - } + if (!set.headers['content-type']) + switch (response?.constructor?.name) { + case 'String': + set.headers['content-type'] = 'text/plain' + break + case 'Array': + case 'Object': + set.headers['content-type'] = 'application/json' + break + } handleSet(set) @@ -215,15 +216,16 @@ export const mapEarlyResponse = ( if (response === undefined || response === null) return if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { - switch (response?.constructor?.name) { - case 'String': - set.headers['content-type'] = 'text/plain' - break - case 'Array': - case 'Object': - set.headers['content-type'] = 'application/json' - break - } + if (!set.headers['content-type']) + switch (response?.constructor?.name) { + case 'String': + set.headers['content-type'] = 'text/plain' + break + case 'Array': + case 'Object': + set.headers['content-type'] = 'application/json' + break + } handleSet(set) From 3549daf1e7dba49e74da5dcad5e22d807a27c014 Mon Sep 17 00:00:00 2001 From: knextkoder Date: Fri, 27 Feb 2026 16:34:24 +0100 Subject: [PATCH 3/3] bug fix #1379: set content-type header based on response type in mapResponse and mapEarlyResponse --- src/adapter/bun/handler.ts | 10 +++----- src/adapter/web-standard/handler.ts | 38 ++++++++++++++--------------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/adapter/bun/handler.ts b/src/adapter/bun/handler.ts index c83ad7fe..fa2e111e 100755 --- a/src/adapter/bun/handler.ts +++ b/src/adapter/bun/handler.ts @@ -26,9 +26,8 @@ export const mapResponse = ( ): Response => { if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { if ( - !set.headers['content-type'] && - (response?.constructor?.name === 'Object' || - response?.constructor?.name === 'Array') + response?.constructor?.name === 'Object' || + response?.constructor?.name === 'Array' ) set.headers['content-type'] = 'application/json' @@ -184,9 +183,8 @@ export const mapEarlyResponse = ( if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { if ( - !set.headers['content-type'] && - (response?.constructor?.name === 'Object' || - response?.constructor?.name === 'Array') + response?.constructor?.name === 'Object' || + response?.constructor?.name === 'Array' ) set.headers['content-type'] = 'application/json' diff --git a/src/adapter/web-standard/handler.ts b/src/adapter/web-standard/handler.ts index 53e1a971..ce97ac68 100755 --- a/src/adapter/web-standard/handler.ts +++ b/src/adapter/web-standard/handler.ts @@ -54,16 +54,15 @@ export const mapResponse = ( request?: Request ): Response => { if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { - if (!set.headers['content-type']) - switch (response?.constructor?.name) { - case 'String': - set.headers['content-type'] = 'text/plain' - break - case 'Array': - case 'Object': - set.headers['content-type'] = 'application/json' - break - } + switch (response?.constructor?.name) { + case 'String': + set.headers['content-type'] = 'text/plain' + break + case 'Array': + case 'Object': + set.headers['content-type'] = 'application/json' + break + } handleSet(set) @@ -216,16 +215,15 @@ export const mapEarlyResponse = ( if (response === undefined || response === null) return if (isNotEmpty(set.headers) || set.status !== 200 || set.cookie) { - if (!set.headers['content-type']) - switch (response?.constructor?.name) { - case 'String': - set.headers['content-type'] = 'text/plain' - break - case 'Array': - case 'Object': - set.headers['content-type'] = 'application/json' - break - } + switch (response?.constructor?.name) { + case 'String': + set.headers['content-type'] = 'text/plain' + break + case 'Array': + case 'Object': + set.headers['content-type'] = 'application/json' + break + } handleSet(set)