From 5debbc74a1fcda33a01fe5bdd599c1ab7ce0475f Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 7 Jul 2023 21:39:44 +0800 Subject: [PATCH] Fix test --- packages/astro/src/core/endpoint/index.ts | 31 ++++++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index 1b81ac2e8cd4..26910c7ffe20 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -144,7 +144,7 @@ export async function callEndpoint // If return simple endpoint object, convert to response if (!(response instanceof Response)) { - // In SSR, we convert to a full response with the correct headers to be sent out + // Validate properties not available in SSR if (env.ssr && !ctx.route?.prerender) { if (response.hasOwnProperty('headers')) { warn( @@ -172,23 +172,40 @@ export async function callEndpoint }; } + let body: BodyInit; const headers = new Headers(); - const pathname = new URL(ctx.request.url).pathname; + const pathname = ctx.route + ? // Try the static route `pathname` + ctx.route.pathname ?? + // Dynamic routes don't include `pathname`, so synthesize a path for these (e.g. 'src/pages/[slug].svg') + ctx.route.segments.map((s) => s.map((p) => p.content).join('')).join('/') + : // Fallback to pathname of the request + ctx.pathname; const mimeType = mime.getType(pathname) || 'text/plain'; headers.set('Content-Type', `${mimeType};charset=utf-8`); - const bytes = encoder.encode(response.body); - headers.set('Content-Length', bytes.byteLength.toString()); + if (typeof Buffer !== 'undefined' && Buffer.from) { + body = Buffer.from(response.body, response.encoding); + } else if ( + response.encoding == null || + response.encoding === 'utf8' || + response.encoding === 'utf-8' + ) { + body = encoder.encode(response.body); + headers.set('Content-Length', body.byteLength.toString()); + } else { + body = response.body; + } - response = new Response(bytes, { + response = new Response(body, { status: 200, headers, }); - - attachToResponse(response, context.cookies); } + attachToResponse(response, context.cookies); + return response; }