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:
Erika 2023-02-07 16:52:15 +01:00 committed by GitHub
parent 9f85fb4ac2
commit cee70f5c6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix returning hex / base64 images from endpoints not working in dev

View file

@ -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`,

View file

@ -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', () => {

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View 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',
};
}