Skip to content

Commit

Permalink
Fix returning images from endpoints in dev (#6163)
Browse files Browse the repository at this point in the history
* fix(dev): Fix dev server responses not being encoded following the specified encoding

* test: Add test

* chore: changeset
  • Loading branch information
Princesseuh authored Feb 7, 2023
1 parent 9f85fb4 commit cee70f5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/honest-pugs-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix returning hex / base64 images from endpoints not working in dev
2 changes: 1 addition & 1 deletion packages/astro/src/vite-plugin-astro-server/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
9 changes: 9 additions & 0 deletions packages/astro/test/dev-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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',
};
}

0 comments on commit cee70f5

Please sign in to comment.