Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back support for Astro.clientAddress to Vercel serverless #6484

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lemon-steaks-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---

Add back support for Astro.clientAddress
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ 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/8d1ba04825a540324bc003e85f36559a594aadc2/packages/kit/src/exports/node/index.js
Expand Down Expand Up @@ -99,13 +101,16 @@ export async function getRequest(
req: IncomingMessage,
bodySizeLimit?: number
): Promise<Request> {
return new Request(base + req.url, {
let headers = req.headers as Record<string, string>;
let request = new Request(base + req.url, {
// @ts-expect-error
duplex: 'half',
method: req.method,
headers: req.headers as Record<string, string>,
headers,
body: get_raw_body(req, bodySizeLimit),
});
Reflect.set(request, clientAddressSymbol, headers['x-forwarded-for']);
return request;
}

export async function setResponse(
Expand Down