Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EndpointOutput type with encoding 'binary' #8003

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/funny-glasses-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixed `EndpointOutput` types with `{ encoding: 'binary' }`
13 changes: 9 additions & 4 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@ export class App {
}
return response.response;
} else {
const body = response.body;
const headers = new Headers();
const mimeType = mime.getType(url.pathname);
if (mimeType) {
headers.set('Content-Type', `${mimeType};charset=utf-8`);
} 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, {
Expand Down
9 changes: 3 additions & 6 deletions packages/astro/src/core/endpoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -153,9 +151,8 @@ export async function callEndpoint<MiddlewareResult = Response | EndpointOutput>
}

return {
...response,
type: 'simple',
body: response.body,
encoding: response.encoding,
cookies: context.cookies,
};
}
15 changes: 9 additions & 6 deletions packages/astro/src/vite-plugin-astro-server/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading