Skip to content

Commit 2359bd7

Browse files
committed
Reduce complexity and dependencies
1 parent bd53e7f commit 2359bd7

File tree

8 files changed

+76
-82
lines changed

8 files changed

+76
-82
lines changed

README.md

+25-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Please consider starring the repository to show your ❤️ and support.
1010
## Installation
1111

1212
Requires Bun v1.0.4 or above.
13-
Requires Elysia v1.0.9 or above.
13+
Requires Elysia v1.0.9 or above.
1414
For older elysia versions please install v0.0.7 of this package
1515

1616
```bash
@@ -29,14 +29,17 @@ This plugin adds a `ip` property to the context object. It contains the client i
2929
import { Elysia } from "elysia";
3030
import { ip } from "elysia-ip";
3131

32-
new Elysia().use(ip()).get("/", ({ ip }) => ip).listen(3000);
32+
new Elysia()
33+
.use(ip())
34+
.get("/", ({ ip }) => ip)
35+
.listen(3000);
3336
```
3437

3538
### How does it work?
3639

3740
For Bun runtime, We use `server.requestIP` introduced in Bun v1.0.4 to get the client ip address and early return it.
3841

39-
It relies on headers for runtimes other than Bun.
42+
It relies on headers for runtimes other than Bun.
4043
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.
4144

4245
We also add support for `X-Forwarded-For` header (de-facto standard header) and other various headers.
@@ -63,16 +66,22 @@ You can even specify your own headers if you want to as following
6366
import { Elysia } from "elysia";
6467
import { ip } from "elysia-ip";
6568

66-
new Elysia().use(ip({ checkHeaders: ["X-Forwarded-For", "X-Real-IP"] })).get("/", ({ ip }) => ip).listen(3000);
69+
new Elysia()
70+
.use(ip({ checkHeaders: ["X-Forwarded-For", "X-Real-IP"] }))
71+
.get("/", ({ ip }) => ip)
72+
.listen(3000);
6773
```
6874

69-
or
75+
or
7076

7177
```ts
7278
import { Elysia } from "elysia";
7379
import { ip } from "elysia-ip";
7480

75-
new Elysia().use(ip({ checkHeaders: "X-Forwarded-For" })).get("/", ({ ip }) => ip).listen(3000);
81+
new Elysia()
82+
.use(ip({ checkHeaders: "X-Forwarded-For" }))
83+
.get("/", ({ ip }) => ip)
84+
.listen(3000);
7685
```
7786

7887
You can also switch to Headers only mode by setting `headersOnly` to `true`. This will only check headers and not the `server.requestIP` property.
@@ -81,11 +90,20 @@ You can also switch to Headers only mode by setting `headersOnly` to `true`. Thi
8190
import { Elysia } from "elysia";
8291
import { ip } from "elysia-ip";
8392

84-
new Elysia().use(ip({ headersOnly: true })).get("/", ({ ip }) => ip).listen(3000);
93+
new Elysia()
94+
.use(ip({ headersOnly: true }))
95+
.get("/", ({ ip }) => ip)
96+
.listen(3000);
8597
```
8698

99+
## Debugging
100+
101+
Please use run the command setting environment variable `NODE_DEBUG` to either `*` or `elysia-ip`
102+
87103
## License
104+
88105
MIT
89106

90107
## Author
108+
91109
Copyright (c) 2023 Gaurish Sethia, All Rights Reserved.

package.json

+2-9
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@
2121
"types": "./dist/index.d.ts",
2222
"bugs": "https://github.com/gaurishhs/elysia-ip/issues",
2323
"homepage": "https://github.com/gaurishhs/elysia-ip",
24-
"keywords": [
25-
"elysia",
26-
"typescript",
27-
"ip"
28-
],
24+
"keywords": ["elysia", "typescript", "ip"],
2925
"scripts": {
30-
"dev": "DEBUG=* bun run --hot example/basic.ts",
26+
"dev": "NODE_DEBUG=* bun run --hot example/basic.ts",
3127
"build": "rm -rf dist && tsc --project tsconfig.esm.json",
3228
"lint": "eslint . --ext .ts"
3329
},
@@ -43,8 +39,5 @@
4339
},
4440
"peerDependencies": {
4541
"elysia": ">= 1.0.9"
46-
},
47-
"dependencies": {
48-
"debug": "^4.3.4"
4942
}
5043
}

src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export { getIP } from "./services/getip"
2-
export { plugin as ip } from "./services/plugin"
1+
export { getIP } from "./services/getip";
2+
export { plugin as ip } from "./services/plugin";
33

4-
export { defaultOptions, headersToCheck } from "./constants"
5-
export type { IPHeaders, Options, InjectServer} from "./types"
4+
export { defaultOptions, headersToCheck } from "./constants";
5+
export type { IPHeaders, Options, InjectServer } from "./types";

src/services/debug.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { debuglog } from "node:util";
2+
3+
export const debug = (message: string) => {
4+
debuglog("elysia-ip")(message);
5+
};

src/services/getip.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
import { headersToCheck } from "../constants"
2-
import { IPHeaders } from "../types"
3-
import { logger } from "./logger"
1+
import { headersToCheck } from "../constants";
2+
import { IPHeaders } from "../types";
3+
import { debug } from "./debug";
44

55
export const getIP = (
66
headers: Headers,
77
checkHeaders: IPHeaders[] = headersToCheck,
88
) => {
99
if (typeof checkHeaders === "string" && headers.get(checkHeaders)) {
10-
logger("getIP", `Found ip from header ${checkHeaders}`)
11-
return headers.get(checkHeaders)
10+
debug(`getIP: Found ip from header ${checkHeaders}`);
11+
return headers.get(checkHeaders);
1212
}
1313

1414
// X-Forwarded-For is the de-facto standard header
1515
if (!checkHeaders && headers.get("x-forwarded-for")) {
16-
logger("getIP", "IP From Header x-forwarded-for")
17-
return headers.get("x-forwarded-for")?.split(",")[0]
16+
debug("getIP: IP From Header x-forwarded-for");
17+
return headers.get("x-forwarded-for")?.split(",")[0];
1818
}
1919

2020
if (!checkHeaders) {
21-
logger("getIP", "checkHeaders `false` return `null`")
22-
return null
21+
debug("getIP: No checkHeaders");
22+
return null;
2323
}
2424

25-
let clientIP: string | undefined | null = null
25+
let clientIP: string | undefined | null = null;
2626
for (const header of checkHeaders) {
27-
clientIP = headers.get(header)
27+
clientIP = headers.get(header);
2828
if (clientIP) {
29-
logger("getIP", `Found ip from header ${header}`)
30-
break
29+
debug(`getIP: Found ip from header ${header}`);
30+
break;
3131
}
3232
}
3333

3434
if (!clientIP) {
35-
logger("getIP", "Failed to get ip from header!")
36-
return
35+
debug("getIP: Failed to get ip from header!");
36+
return;
3737
}
38-
return clientIP
39-
}
38+
return clientIP;
39+
};

src/services/logger.ts

-4
This file was deleted.

src/services/plugin.ts

+23-25
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,48 @@
1-
import { Elysia } from "elysia"
2-
import { getIP } from "./getip"
3-
import { defaultOptions } from "../constants"
4-
import { logger } from "./logger"
5-
6-
import type { Options } from "../types"
1+
import { Elysia } from "elysia";
2+
import { getIP } from "./getip";
3+
import { defaultOptions } from "../constants";
4+
import { debug } from "./debug";
5+
import type { Options } from "../types";
76

87
export const plugin = (userOptions?: Partial<Options>) => (app: Elysia) => {
98
const options: Options = {
109
...defaultOptions,
1110
...userOptions,
12-
}
11+
};
1312

1413
return app.use(
1514
new Elysia({
1615
name: "elysia-ip",
1716
}).derive({ as: "global" }, ({ request }): { ip: string } => {
1817
serverIP: {
1918
if (!options.headersOnly && globalThis.Bun) {
20-
const server = options.injectServer(app)
19+
const server = options.injectServer(app);
2120
if (!server) {
22-
logger(
23-
"plugin",
24-
"Elysia server is not initialized. Make sure to call Elyisa.listen()",
25-
)
26-
logger("plugin", "use injectServer to inject Server instance")
27-
break serverIP
21+
debug(
22+
"plugin: Elysia server is not initialized. Make sure to call Elyisa.listen()",
23+
);
24+
debug("plugin: use injectServer to inject Server instance");
25+
break serverIP;
2826
}
2927

3028
if (!server.requestIP) {
31-
logger("plugin", "server.requestIP is null")
32-
logger("plugin", "Please check server instace")
33-
break serverIP
29+
debug("plugin: server.requestIP is null");
30+
debug("plugin: Please check server instace");
31+
break serverIP;
3432
}
3533

36-
const socketAddress = server.requestIP(request)
37-
logger("plugin", "socketAddress", socketAddress)
34+
const socketAddress = server.requestIP(request);
35+
debug(`plugin: socketAddress ${JSON.stringify(socketAddress)}`);
3836
if (!socketAddress) {
39-
logger("plugin", "ip from server.requestIP return `null`")
40-
break serverIP
37+
debug("plugin: ip from server.requestIP return `null`");
38+
break serverIP;
4139
}
42-
return { ip: socketAddress.address }
40+
return { ip: socketAddress.address };
4341
}
4442
}
4543
return {
4644
ip: getIP(request.headers, options.checkHeaders) || "",
47-
}
45+
};
4846
}),
49-
)
50-
}
47+
);
48+
};

test.ts

-16
This file was deleted.

0 commit comments

Comments
 (0)