summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.changeset/funny-glasses-bathe.md5
-rw-r--r--packages/astro/src/@types/astro.ts13
-rw-r--r--packages/astro/src/core/app/index.ts4
-rw-r--r--packages/astro/src/core/endpoint/index.ts9
-rw-r--r--packages/astro/src/vite-plugin-astro-server/route.ts15
5 files changed, 28 insertions, 18 deletions
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<Props extends Record<string, any> = Record<string, a
locals: App.Locals;
}
-export interface EndpointOutput {
- body: Body;
- encoding?: BufferEncoding;
-}
+export type EndpointOutput =
+ | {
+ body: Body;
+ encoding?: Exclude<BufferEncoding, 'binary'>;
+ }
+ | {
+ body: Uint8Array;
+ encoding: 'binary';
+ };
export type APIRoute<Props extends Record<string, any> = Record<string, any>> = (
context: APIContext<Props>
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<MiddlewareResult = Response | EndpointOutput>
}
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);
}