From cee70f5c6ac9b0d2edc1f8a6f8f5043605576026 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Tue, 7 Feb 2023 16:52:15 +0100 Subject: [PATCH] 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 --- .changeset/honest-pugs-hang.md | 5 +++++ .../astro/src/vite-plugin-astro-server/route.ts | 2 +- packages/astro/test/dev-routing.test.js | 9 +++++++++ .../fixtures/with-endpoint-routes/src/astro.png | Bin 0 -> 10285 bytes .../src/pages/images/hex.ts | 9 +++++++++ 5 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 .changeset/honest-pugs-hang.md create mode 100644 packages/astro/test/fixtures/with-endpoint-routes/src/astro.png create mode 100644 packages/astro/test/fixtures/with-endpoint-routes/src/pages/images/hex.ts diff --git a/.changeset/honest-pugs-hang.md b/.changeset/honest-pugs-hang.md new file mode 100644 index 0000000000..a1c5cb1680 --- /dev/null +++ b/.changeset/honest-pugs-hang.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix returning hex / base64 images from endpoints not working in dev diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts index 5bd551ca9c..85643034f9 100644 --- a/packages/astro/src/vite-plugin-astro-server/route.ts +++ b/packages/astro/src/vite-plugin-astro-server/route.ts @@ -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`, diff --git a/packages/astro/test/dev-routing.test.js b/packages/astro/test/dev-routing.test.js index e7e5c82c54..c0f87375af 100644 --- a/packages/astro/test/dev-routing.test.js +++ b/packages/astro/test/dev-routing.test.js @@ -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', () => { diff --git a/packages/astro/test/fixtures/with-endpoint-routes/src/astro.png b/packages/astro/test/fixtures/with-endpoint-routes/src/astro.png new file mode 100644 index 0000000000000000000000000000000000000000..115453790d301464847a5dc610c6f34113392350 GIT binary patch literal 10285 zcmcI}WmsIx((VAk-Q8UW4{ielf@ZM6-Gam5?t=sk65K67a0$U(0t85cy9Rd)@R5D? zJ!e1Xe)s2H>*?uQRrS_e)m=Yk^@>tgeT9ieh6VrtFclSK!OwHp^Qb^Udj7Rv-&q3y zFeGe2Aaz9$h(_Jj$;#H=5&%$$O3^`vXbcnO=qDve+2kvswxiMs1ii=61MC@U%HYww zCqaglyLPXTROpMMp@)=UY?gWA&T>bHBa(^ys!NX+55; zj|6DMr$}VM){YlFVj;4Kv9e}2wC7Dn2+ijIFq2?6(WEjf+Kn=6ugE*EdR(dIxnyWaqTbv<3AUtbc$_K^r@@GP7b&egw? zMT3Ik;|XvSNRk0;64vjE(_BRepF7PzyEAbxh~o$pB0j(~9#STmS|et7h4D3f5E~S1lyix$^)jP1NO1KR)6gWL1Ww=s z$JfH-M$fy`f>FBQgM+XbYAk~kX^>Gw?#b>}Q@${hI0unfz)3i(bA#xUEW~BJViEdR z)nGSyCSC?Xm1)?~G*9m7Db$owC6&zzf^@`U!9vtwp<-T~iSO{RUManZr=`Dn1;w&A zNOz*LpLz_Oz^+{(WwM|nh_zs$GjteACXvNph`6!n%wS=`eR{tInZ&#dC2SnJsU>P5oPqWtI8&ElrMOTo0)$qpxv}uC zLlc*1M!}n$RcH^)hn-(nBir&%=tUFX)N#$_+Mk~*Xo+eyn-bbq1y;t3{fu4nsgLjt zh?}^}zZ3JqdYZ%6hf(&;*Cx*jAF%uY>_X6gz}!s+i@dzt#Z#-Q^K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