From 16161afb2b3a04ca7605fcd16de06efe3fabdef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Seery?= Date: Tue, 8 Aug 2023 20:19:32 -0300 Subject: [PATCH] fix: `EndpointOutput` type with encoding `'binary'` (#8003) * fix: `EndpointOutput` type with encoding `'binary'` * Changeset --- .changeset/funny-glasses-bathe.md | 5 +++++ packages/astro/src/@types/astro.ts | 13 +++++++++---- packages/astro/src/core/app/index.ts | 4 ++-- packages/astro/src/core/endpoint/index.ts | 9 +++------ .../astro/src/vite-plugin-astro-server/route.ts | 15 +++++++++------ 5 files changed, 28 insertions(+), 18 deletions(-) create mode 100644 .changeset/funny-glasses-bathe.md diff --git a/.changeset/funny-glasses-bathe.md b/.changeset/funny-glasses-bathe.md new file mode 100644 index 000000000..28db2f746 --- /dev/null +++ b/.changeset/funny-glasses-bathe.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixed `EndpointOutput` types with `{ encoding: 'binary' }` diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 6bcb011bb..1e594332d 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -1823,10 +1823,15 @@ export interface APIContext = Record; + } + | { + body: Uint8Array; + encoding: 'binary'; + }; export type APIRoute = Record> = ( context: APIContext diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index 34e55f93d..bec5368b6 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -195,7 +195,6 @@ export class App { } return response.response; } else { - const body = response.body; const headers = new Headers(); const mimeType = mime.getType(url.pathname); if (mimeType) { @@ -203,7 +202,8 @@ export class App { } else { headers.set('Content-Type', 'text/plain;charset=utf-8'); } - const bytes = this.#encoder.encode(body); + const bytes = + response.encoding !== 'binary' ? this.#encoder.encode(response.body) : response.body; headers.set('Content-Length', bytes.byteLength.toString()); const newResponse = new Response(bytes, { diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index 392ffa291..485190e47 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -18,12 +18,10 @@ const clientAddressSymbol = Symbol.for('astro.clientAddress'); const clientLocalsSymbol = Symbol.for('astro.locals'); export type EndpointCallResult = - | { + | (EndpointOutput & { type: 'simple'; - body: string; - encoding?: BufferEncoding; cookies: AstroCookies; - } + }) | { type: 'response'; response: Response; @@ -153,9 +151,8 @@ export async function callEndpoint } return { + ...response, type: 'simple', - body: response.body, - encoding: response.encoding, cookies: context.cookies, }; } diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts index ff926bca2..f58d248a3 100644 --- a/packages/astro/src/vite-plugin-astro-server/route.ts +++ b/packages/astro/src/vite-plugin-astro-server/route.ts @@ -246,12 +246,15 @@ export async function handleRoute({ if (computedMimeType) { contentType = computedMimeType; } - const response = new Response(Buffer.from(result.body, result.encoding), { - status: 200, - headers: { - 'Content-Type': `${contentType};charset=utf-8`, - }, - }); + const response = new Response( + result.encoding !== 'binary' ? Buffer.from(result.body, result.encoding) : result.body, + { + status: 200, + headers: { + 'Content-Type': `${contentType};charset=utf-8`, + }, + } + ); attachToResponse(response, result.cookies); await writeWebResponse(incomingResponse, response); }