fix: EndpointOutput
type with encoding 'binary'
(#8003)
* fix: `EndpointOutput` type with encoding `'binary'` * Changeset
This commit is contained in:
parent
315d58f27b
commit
16161afb2b
5 changed files with 28 additions and 18 deletions
5
.changeset/funny-glasses-bathe.md
Normal file
5
.changeset/funny-glasses-bathe.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixed `EndpointOutput` types with `{ encoding: 'binary' }`
|
|
@ -1823,10 +1823,15 @@ export interface APIContext<Props extends Record<string, any> = Record<string, a
|
||||||
locals: App.Locals;
|
locals: App.Locals;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface EndpointOutput {
|
export type EndpointOutput =
|
||||||
|
| {
|
||||||
body: Body;
|
body: Body;
|
||||||
encoding?: BufferEncoding;
|
encoding?: Exclude<BufferEncoding, 'binary'>;
|
||||||
}
|
}
|
||||||
|
| {
|
||||||
|
body: Uint8Array;
|
||||||
|
encoding: 'binary';
|
||||||
|
};
|
||||||
|
|
||||||
export type APIRoute<Props extends Record<string, any> = Record<string, any>> = (
|
export type APIRoute<Props extends Record<string, any> = Record<string, any>> = (
|
||||||
context: APIContext<Props>
|
context: APIContext<Props>
|
||||||
|
|
|
@ -195,7 +195,6 @@ export class App {
|
||||||
}
|
}
|
||||||
return response.response;
|
return response.response;
|
||||||
} else {
|
} else {
|
||||||
const body = response.body;
|
|
||||||
const headers = new Headers();
|
const headers = new Headers();
|
||||||
const mimeType = mime.getType(url.pathname);
|
const mimeType = mime.getType(url.pathname);
|
||||||
if (mimeType) {
|
if (mimeType) {
|
||||||
|
@ -203,7 +202,8 @@ export class App {
|
||||||
} else {
|
} else {
|
||||||
headers.set('Content-Type', 'text/plain;charset=utf-8');
|
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());
|
headers.set('Content-Length', bytes.byteLength.toString());
|
||||||
|
|
||||||
const newResponse = new Response(bytes, {
|
const newResponse = new Response(bytes, {
|
||||||
|
|
|
@ -18,12 +18,10 @@ const clientAddressSymbol = Symbol.for('astro.clientAddress');
|
||||||
const clientLocalsSymbol = Symbol.for('astro.locals');
|
const clientLocalsSymbol = Symbol.for('astro.locals');
|
||||||
|
|
||||||
export type EndpointCallResult =
|
export type EndpointCallResult =
|
||||||
| {
|
| (EndpointOutput & {
|
||||||
type: 'simple';
|
type: 'simple';
|
||||||
body: string;
|
|
||||||
encoding?: BufferEncoding;
|
|
||||||
cookies: AstroCookies;
|
cookies: AstroCookies;
|
||||||
}
|
})
|
||||||
| {
|
| {
|
||||||
type: 'response';
|
type: 'response';
|
||||||
response: Response;
|
response: Response;
|
||||||
|
@ -153,9 +151,8 @@ export async function callEndpoint<MiddlewareResult = Response | EndpointOutput>
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
...response,
|
||||||
type: 'simple',
|
type: 'simple',
|
||||||
body: response.body,
|
|
||||||
encoding: response.encoding,
|
|
||||||
cookies: context.cookies,
|
cookies: context.cookies,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -246,12 +246,15 @@ export async function handleRoute({
|
||||||
if (computedMimeType) {
|
if (computedMimeType) {
|
||||||
contentType = computedMimeType;
|
contentType = computedMimeType;
|
||||||
}
|
}
|
||||||
const response = new Response(Buffer.from(result.body, result.encoding), {
|
const response = new Response(
|
||||||
|
result.encoding !== 'binary' ? Buffer.from(result.body, result.encoding) : result.body,
|
||||||
|
{
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': `${contentType};charset=utf-8`,
|
'Content-Type': `${contentType};charset=utf-8`,
|
||||||
},
|
},
|
||||||
});
|
}
|
||||||
|
);
|
||||||
attachToResponse(response, result.cookies);
|
attachToResponse(response, result.cookies);
|
||||||
await writeWebResponse(incomingResponse, response);
|
await writeWebResponse(incomingResponse, response);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue