Skip to content

Commit a13ede5

Browse files
committed
better error handling and make tsc happy
1 parent 2d55610 commit a13ede5

File tree

5 files changed

+32
-229
lines changed

5 files changed

+32
-229
lines changed

README.md

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
![badge](https://github.com/gaurishhs/elysia-ip/actions/workflows/npm-publish.yml/badge.svg)
44

5-
Get the Client Ip Address in Elysia.
5+
Get the client ip address in Elysia.
6+
It works with Bun, Cloudflare, Fastly and other runtimes.
67

7-
It supports Cloudflare, Fastly and other runtimes as well.
8+
Please consider starring the repository to show your ❤️ and support.
89

910
## Installation
1011

@@ -18,9 +19,7 @@ bun a elysia-ip
1819

1920
### Introduction
2021

21-
This plugin let's you get client's ip address in Elysia.js
22-
23-
It supports Multiple runtimes (Cloudflare, Fastly Edge, etc.)
22+
This plugin adds a `ip` property to the context object. It contains the client ip address.
2423

2524
### Usage
2625

@@ -33,6 +32,8 @@ new Elysia().use(ip()).get("/", ({ ip }) => ip).listen(3000);
3332

3433
### How does it work?
3534

35+
For Bun runtime, We use `server.requestIP` introduced in Bun v1.0.4 to get the client ip address and early return it.
36+
3637
It relies on headers for runtimes other than Bun.
3738
Cloudflare and other providers send back specific headers, containing the IP address. For example `CF-Connecting-IP` for Cloudflare and `Fastly-Client-IP` for Fastly.
3839

@@ -54,7 +55,7 @@ Priority list:
5455
12. `true-client-ip` (Akamai and Cloudflare)
5556
13. `cf-pseudo-ipv4` (Cloudflare)
5657

57-
You can even specify your own header or list of headers if you want to as following
58+
You can even specify your own headers if you want to as following
5859

5960
```ts
6061
import { Elysia } from "elysia";
@@ -72,9 +73,6 @@ import { ip } from "elysia-ip";
7273
new Elysia().use(ip({ checkHeaders: "X-Forwarded-For" })).get("/", ({ ip }) => ip).listen(3000);
7374
```
7475

75-
For Bun runtime, We use `server.requestIP` introduced in Bun v1.0.4
76-
77-
7876
## License
7977
MIT
8078

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"ip"
2828
],
2929
"scripts": {
30-
"build": "tsc --project tsconfig.esm.json",
30+
"build": "tsc",
3131
"lint": "eslint . --ext .ts"
3232
},
3333
"license": "MIT",

src/index.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const headersToCheck: IPHeaders[] = [
1616
'cf-pseudo-ipv4', // Cloudflare
1717
]
1818

19-
const getIP = (headers: Headers, checkHeaders: IPHeaders[] = headersToCheck) => {
19+
export const getIP = (headers: Headers, checkHeaders: IPHeaders[] = headersToCheck) => {
2020
if (typeof checkHeaders === 'string' && headers.get(checkHeaders)) return headers.get(checkHeaders)
2121
// X-Forwarded-For is the de-facto standard header
2222
if (headers.get('x-forwarded-for')) return headers.get('x-forwarded-for')?.split(',')[0]
@@ -30,12 +30,19 @@ const getIP = (headers: Headers, checkHeaders: IPHeaders[] = headersToCheck) =>
3030
}
3131

3232
export const ip = (config: {
33+
/**
34+
* Customize headers to check for IP address
35+
* @default ['x-real-ip', 'x-client-ip', 'cf-connecting-ip', 'fastly-client-ip', 'x-cluster-client-ip', 'x-forwarded', 'forwarded-for', 'forwarded', 'x-forwarded', 'appengine-user-ip', 'true-client-ip', 'cf-pseudo-ipv4']
36+
*/
3337
checkHeaders?: IPHeaders[]
3438
} = {}) => (app: Elysia) => {
3539
return app.derive(({ request }) => {
36-
if (globalThis.Bun) return {
37-
ip: app.server!.requestIP(request)
38-
}
40+
if (globalThis.Bun) {
41+
if (!app.server) throw new Error(`Elysia server is not initialized. Make sure to call Elyisa.listen()`)
42+
return {
43+
ip: app.server.requestIP(request)
44+
}
45+
}
3946
const clientIP = getIP(request.headers, config.checkHeaders)
4047
return {
4148
ip: clientIP

tsconfig.esm.json

-110
This file was deleted.

0 commit comments

Comments
 (0)