Fix returning images from endpoints in dev (#6163)
* fix(dev): Fix dev server responses not being encoded following the specified encoding * test: Add test * chore: changeset
This commit is contained in:
parent
9f85fb4ac2
commit
cee70f5c6a
5 changed files with 24 additions and 1 deletions
5
.changeset/honest-pugs-hang.md
Normal file
5
.changeset/honest-pugs-hang.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fix returning hex / base64 images from endpoints not working in dev
|
|
@ -200,7 +200,7 @@ export async function handleRoute(
|
|||
if (computedMimeType) {
|
||||
contentType = computedMimeType;
|
||||
}
|
||||
const response = new Response(result.body, {
|
||||
const response = new Response(Buffer.from(result.body, result.encoding), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': `${contentType};charset=utf-8`,
|
||||
|
|
|
@ -270,6 +270,15 @@ describe('Development Routing', () => {
|
|||
const response = await fixture.fetch('/images/1.svg');
|
||||
expect(response.headers.get('content-type')).to.match(/image\/svg\+xml/);
|
||||
});
|
||||
|
||||
it('correct encoding when loading /images/hex.ts', async () => {
|
||||
const response = await fixture.fetch('/images/hex');
|
||||
const body = await response.arrayBuffer();
|
||||
const hex = Buffer.from(body).toString('hex', 0, 4);
|
||||
|
||||
// Check if we have a PNG
|
||||
expect(hex).to.equal('89504e47');
|
||||
});
|
||||
});
|
||||
|
||||
describe('file format routing', () => {
|
||||
|
|
BIN
packages/astro/test/fixtures/with-endpoint-routes/src/astro.png
vendored
Normal file
BIN
packages/astro/test/fixtures/with-endpoint-routes/src/astro.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 10 KiB |
9
packages/astro/test/fixtures/with-endpoint-routes/src/pages/images/hex.ts
vendored
Normal file
9
packages/astro/test/fixtures/with-endpoint-routes/src/pages/images/hex.ts
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { readFileSync } from "node:fs";
|
||||
|
||||
export async function get({ params, request }) {
|
||||
const buffer = readFileSync(new URL('../../astro.png', import.meta.url));
|
||||
return {
|
||||
body: buffer.toString('hex'),
|
||||
encoding: 'hex',
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue