From ce1c8172d9ffa69dfb36b41b7679e0e6c94423c6 Mon Sep 17 00:00:00 2001 From: promer94 Date: Mon, 8 Jul 2024 22:46:56 +0800 Subject: [PATCH] feat(vercel): add `getConnInfo` for vercel adapter (#3085) --- src/adapter/vercel/conninfo.test.ts | 18 ++++++++++++++++++ src/adapter/vercel/conninfo.ts | 9 +++++++++ src/adapter/vercel/index.ts | 1 + 3 files changed, 28 insertions(+) create mode 100644 src/adapter/vercel/conninfo.test.ts create mode 100644 src/adapter/vercel/conninfo.ts diff --git a/src/adapter/vercel/conninfo.test.ts b/src/adapter/vercel/conninfo.test.ts new file mode 100644 index 00000000..7d390af5 --- /dev/null +++ b/src/adapter/vercel/conninfo.test.ts @@ -0,0 +1,18 @@ +import { Context } from '../../context' +import { getConnInfo } from './conninfo' + +describe('getConnInfo', () => { + it('Should getConnInfo works', () => { + const address = Math.random().toString() + const req = new Request('http://localhost/', { + headers: { + 'x-real-ip': address, + }, + }) + const c = new Context(req) + + const info = getConnInfo(c) + + expect(info.remote.address).toBe(address) + }) +}) diff --git a/src/adapter/vercel/conninfo.ts b/src/adapter/vercel/conninfo.ts new file mode 100644 index 00000000..763af117 --- /dev/null +++ b/src/adapter/vercel/conninfo.ts @@ -0,0 +1,9 @@ +import type { GetConnInfo } from '../../helper/conninfo' + +export const getConnInfo: GetConnInfo = (c) => ({ + remote: { + // https://github.com/vercel/vercel/blob/b70bfb5fbf28a4650d4042ce68ca5c636d37cf44/packages/edge/src/edge-headers.ts#L10-L12C32 + address: c.req.header('x-real-ip'), + addressType: 'unknown', + }, +}) diff --git a/src/adapter/vercel/index.ts b/src/adapter/vercel/index.ts index 190bea70..796cfd1d 100644 --- a/src/adapter/vercel/index.ts +++ b/src/adapter/vercel/index.ts @@ -4,3 +4,4 @@ */ export { handle } from './handler' +export { getConnInfo } from './conninfo'