Skip to content

Commit

Permalink
fix(vercel): update getRequest to use undici (#6380)
Browse files Browse the repository at this point in the history
* fix(vercel): update getRequest to use undici

* Changeset

* haha ts-expect-error is a double-edged sword

---------

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
  • Loading branch information
JuanM04 and natemoo-re authored Mar 1, 2023
1 parent 2678264 commit 0e378c3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-snakes-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---

Fixed undici-related bug
52 changes: 30 additions & 22 deletions packages/integrations/vercel/src/serverless/request-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import type { App } from 'astro/app';
import type { IncomingMessage, ServerResponse } from 'node:http';
import { splitCookiesString } from 'set-cookie-parser';

const clientAddressSymbol = Symbol.for('astro.clientAddress');

/*
Credits to the SvelteKit team
https://github.com/sveltejs/kit/blob/dd380b38c322272b414a7ec3ac2911f2db353f5c/packages/kit/src/exports/node/index.js
https://github.com/sveltejs/kit/blob/8d1ba04825a540324bc003e85f36559a594aadc2/packages/kit/src/exports/node/index.js
*/

function get_raw_body(req: IncomingMessage, body_size_limit?: number): ReadableStream | null {
Expand All @@ -32,7 +30,8 @@ function get_raw_body(req: IncomingMessage, body_size_limit?: number): ReadableS
if (!length) {
length = body_size_limit;
} else if (length > body_size_limit) {
throw new Error(
throw new HTTPError(
413,
`Received content-length of ${length}, but only accept up to ${body_size_limit} bytes.`
);
}
Expand Down Expand Up @@ -66,7 +65,8 @@ function get_raw_body(req: IncomingMessage, body_size_limit?: number): ReadableS
if (size > length) {
cancelled = true;
controller.error(
new Error(
new HTTPError(
413,
`request body size exceeded ${
content_length ? "'content-length'" : 'BODY_SIZE_LIMIT'
} of ${length}`
Expand Down Expand Up @@ -99,26 +99,20 @@ export async function getRequest(
req: IncomingMessage,
bodySizeLimit?: number
): Promise<Request> {
let headers = req.headers as Record<string, string>;
if (req.httpVersionMajor === 2) {
// we need to strip out the HTTP/2 pseudo-headers because node-fetch's
// Request implementation doesn't like them
headers = Object.assign({}, headers);
delete headers[':method'];
delete headers[':path'];
delete headers[':authority'];
delete headers[':scheme'];
}
const request = new Request(base + req.url, {
return new Request(base + req.url, {
// @ts-expect-error
duplex: 'half',
method: req.method,
headers,
headers: req.headers as Record<string, string>,
body: get_raw_body(req, bodySizeLimit),
});
Reflect.set(request, clientAddressSymbol, headers['x-forwarded-for']);
return request;
}

export async function setResponse(app: App, res: ServerResponse, response: Response) {
export async function setResponse(
app: App,
res: ServerResponse,
response: Response
): Promise<void> {
const headers = Object.fromEntries(response.headers);
let cookies: string[] = [];

Expand All @@ -129,8 +123,9 @@ export async function setResponse(app: App, res: ServerResponse, response: Respo
}

if (app.setCookieHeaders) {
const setCookieHeaders = Array.from(app.setCookieHeaders(response));
cookies.push(...setCookieHeaders);
for (const setCookieHeader of app.setCookieHeaders(response)) {
cookies.push(setCookieHeader);
}
}

res.writeHead(response.status, { ...headers, 'set-cookie': cookies });
Expand Down Expand Up @@ -188,3 +183,16 @@ export async function setResponse(app: App, res: ServerResponse, response: Respo
}
}
}

class HTTPError extends Error {
status: number;

constructor(status: number, reason: string) {
super(reason);
this.status = status;
}

get reason() {
return super.message;
}
}

0 comments on commit 0e378c3

Please sign in to comment.