diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index 9720fea66..b62ba8bed 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -91,32 +91,28 @@ export function createAPIContext({ type ResponseParameters = ConstructorParameters; - export class ResponseWithEncoding extends Response { - constructor( - body: ResponseParameters[0], - init: ResponseParameters[1], - encoding?: BufferEncoding - ) { - // If a body string is given, try to encode it to preserve the behaviour as simple objects. - // We don't do the full handling as simple objects so users can control how headers are set instead. - if (typeof body === 'string') { - // In NodeJS, we can use Buffer.from which supports all BufferEncoding - if (typeof Buffer !== 'undefined' && Buffer.from) { - body = Buffer.from(body, encoding); - } - // In non-NodeJS, use the web-standard TextEncoder for utf-8 strings - else if (encoding == null || encoding === 'utf8' || encoding === 'utf-8') { - body = encoder.encode(body); - } +export class ResponseWithEncoding extends Response { + constructor(body: ResponseParameters[0], init: ResponseParameters[1], encoding?: BufferEncoding) { + // If a body string is given, try to encode it to preserve the behaviour as simple objects. + // We don't do the full handling as simple objects so users can control how headers are set instead. + if (typeof body === 'string') { + // In NodeJS, we can use Buffer.from which supports all BufferEncoding + if (typeof Buffer !== 'undefined' && Buffer.from) { + body = Buffer.from(body, encoding); } - - super(body, init); - - if (encoding) { - this.headers.set('X-Astro-Encoding', encoding); + // In non-NodeJS, use the web-standard TextEncoder for utf-8 strings + else if (encoding == null || encoding === 'utf8' || encoding === 'utf-8') { + body = encoder.encode(body); } } + + super(body, init); + + if (encoding) { + this.headers.set('X-Astro-Encoding', encoding); + } } +} export async function callEndpoint( mod: EndpointHandler, diff --git a/packages/astro/src/core/polyfill.ts b/packages/astro/src/core/polyfill.ts index 3b271d464..c183d23f6 100644 --- a/packages/astro/src/core/polyfill.ts +++ b/packages/astro/src/core/polyfill.ts @@ -1,5 +1,5 @@ -import crypto from 'node:crypto'; import buffer from 'node:buffer'; +import crypto from 'node:crypto'; export function apply() { // Remove when Node 18 is dropped for Node 20 diff --git a/packages/integrations/node/src/createOutgoingHttpHeaders.ts b/packages/integrations/node/src/createOutgoingHttpHeaders.ts index a2f9b74e8..781a74de6 100644 --- a/packages/integrations/node/src/createOutgoingHttpHeaders.ts +++ b/packages/integrations/node/src/createOutgoingHttpHeaders.ts @@ -13,7 +13,7 @@ export const createOutgoingHttpHeaders = ( if (!headers) { return undefined; } - + // at this point, a multi-value'd set-cookie header is invalid (it was concatenated as a single CSV, which is not valid for set-cookie) const nodeHeaders: OutgoingHttpHeaders = Object.fromEntries(headers.entries());