Skip to content

Commit 28b295b

Browse files
author
Gaurish Sethia
committed
initial commit
0 parents  commit 28b295b

10 files changed

+555
-0
lines changed

.eslintrc.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = {
2+
"env": {
3+
"es2021": true,
4+
"node": true
5+
},
6+
"extends": [
7+
"eslint:recommended",
8+
"plugin:@typescript-eslint/recommended"
9+
],
10+
"parser": "@typescript-eslint/parser",
11+
"parserOptions": {
12+
"ecmaVersion": "latest",
13+
"sourceType": "module"
14+
},
15+
"plugins": [
16+
"@typescript-eslint"
17+
],
18+
"rules": {
19+
"@typescript-eslint/ban-types": 'off',
20+
'@typescript-eslint/no-explicit-any': 'off'
21+
},
22+
}

.github/workflows/npm-publish.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2+
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3+
4+
name: Node.js Package
5+
6+
on:
7+
release:
8+
types: [created]
9+
10+
jobs:
11+
publish-npm:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- uses: actions/setup-node@v3
16+
with:
17+
node-version: 16
18+
registry-url: https://registry.npmjs.org/
19+
- run: npm i
20+
- run: npm run publish
21+
env:
22+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

.gitignore

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
2+
3+
# Logs
4+
5+
logs
6+
_.log
7+
npm-debug.log_
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Diagnostic reports (https://nodejs.org/api/report.html)
14+
15+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
16+
17+
# Runtime data
18+
19+
pids
20+
_.pid
21+
_.seed
22+
\*.pid.lock
23+
24+
# Directory for instrumented libs generated by jscoverage/JSCover
25+
26+
lib-cov
27+
28+
# Coverage directory used by tools like istanbul
29+
30+
coverage
31+
\*.lcov
32+
33+
# nyc test coverage
34+
35+
.nyc_output
36+
37+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
38+
39+
.grunt
40+
41+
# Bower dependency directory (https://bower.io/)
42+
43+
bower_components
44+
45+
# node-waf configuration
46+
47+
.lock-wscript
48+
49+
# Compiled binary addons (https://nodejs.org/api/addons.html)
50+
51+
build/Release
52+
53+
# Dependency directories
54+
55+
node_modules/
56+
jspm_packages/
57+
58+
# Snowpack dependency directory (https://snowpack.dev/)
59+
60+
web_modules/
61+
62+
# TypeScript cache
63+
64+
\*.tsbuildinfo
65+
66+
# Optional npm cache directory
67+
68+
.npm
69+
70+
# Optional eslint cache
71+
72+
.eslintcache
73+
74+
# Optional stylelint cache
75+
76+
.stylelintcache
77+
78+
# Microbundle cache
79+
80+
.rpt2_cache/
81+
.rts2_cache_cjs/
82+
.rts2_cache_es/
83+
.rts2_cache_umd/
84+
85+
# Optional REPL history
86+
87+
.node_repl_history
88+
89+
# Output of 'npm pack'
90+
91+
\*.tgz
92+
93+
# Yarn Integrity file
94+
95+
.yarn-integrity
96+
97+
# dotenv environment variable files
98+
99+
.env
100+
.env.development.local
101+
.env.test.local
102+
.env.production.local
103+
.env.local
104+
105+
# parcel-bundler cache (https://parceljs.org/)
106+
107+
.cache
108+
.parcel-cache
109+
110+
# Next.js build output
111+
112+
.next
113+
out
114+
115+
# Nuxt.js build / generate output
116+
117+
.nuxt
118+
dist
119+
120+
# Gatsby files
121+
122+
.cache/
123+
124+
# Comment in the public line in if your project uses Gatsby and not Next.js
125+
126+
# https://nextjs.org/blog/next-9-1#public-directory-support
127+
128+
# public
129+
130+
# vuepress build output
131+
132+
.vuepress/dist
133+
134+
# vuepress v2.x temp and cache directory
135+
136+
.temp
137+
.cache
138+
139+
# Docusaurus cache and generated files
140+
141+
.docusaurus
142+
143+
# Serverless directories
144+
145+
.serverless/
146+
147+
# FuseBox cache
148+
149+
.fusebox/
150+
151+
# DynamoDB Local files
152+
153+
.dynamodb/
154+
155+
# TernJS port file
156+
157+
.tern-port
158+
159+
# Stores VSCode versions used for testing VSCode extensions
160+
161+
.vscode-test
162+
163+
# yarn v2
164+
165+
.yarn/cache
166+
.yarn/unplugged
167+
.yarn/build-state.yml
168+
.yarn/install-state.gz
169+
.pnp.\*
170+
171+
# IntelliJ based IDEs
172+
.idea
173+
174+
# Finder (MacOS) folder config
175+
.DS_Store
176+

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2023 Gaurish Sethia
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# elysia-ip
2+
3+
Get the Client Ip Address in Elysia.
4+
5+
It supports Cloudflare, Fastly and other runtimes as well.
6+
7+
## Installation
8+
9+
```bash
10+
bun a elysia-ip
11+
```
12+
13+
## Documentation
14+
15+
Please check the wiki section.
16+
17+
## License
18+
MIT
19+
20+
## Author
21+
Copyright (c) 2023 Gaurish Sethia, All Rights Reserved.

bun.lockb

51.1 KB
Binary file not shown.

index.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Elysia } from 'elysia';
2+
3+
export type IPHeaders = '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' | (string & {})
4+
5+
export const headersToCheck: IPHeaders[] = [
6+
'x-real-ip', // Nginx proxy/FastCGI
7+
'x-client-ip', // Apache https://httpd.apache.org/docs/2.4/mod/mod_remoteip.html#page-header
8+
'cf-connecting-ip', // Cloudflare
9+
'fastly-client-ip', // Fastly
10+
'x-cluster-client-ip', // GCP
11+
'x-forwarded', // General Forwarded
12+
'forwarded-for', // RFC 7239
13+
'forwarded', // RFC 7239
14+
'x-forwarded', // RFC 7239
15+
'appengine-user-ip', // GCP
16+
'true-client-ip', // Akamai and Cloudflare
17+
'cf-pseudo-ipv4', // Cloudflare
18+
]
19+
20+
const getIP = (headers: Headers, checkHeaders: IPHeaders[] = headersToCheck) => {
21+
if (typeof checkHeaders === 'string' && headers.get(checkHeaders)) return headers.get(checkHeaders)
22+
// X-Forwarded-For is the de-facto standard header
23+
if (headers.get('x-forwarded-for')) return headers.get('x-forwarded-for')?.split(',')[0]
24+
let clientIP: string | undefined | null = null;
25+
if (!checkHeaders) return null;
26+
for (const header of checkHeaders) {
27+
clientIP = headers.get(header)
28+
if (clientIP) break
29+
}
30+
return clientIP
31+
}
32+
33+
export const ip = (config: {
34+
checkHeaders?: IPHeaders[]
35+
} = {}) => (app: Elysia) => {
36+
return app.derive(({ request }) => {
37+
const clientIP = getIP(request.headers, config.checkHeaders)
38+
return {
39+
ip: clientIP || app.server!.requestIP(request)
40+
}
41+
})
42+
}

package.json

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "elysia-ip",
3+
"version": "0.0.1",
4+
"type": "module",
5+
"author": {
6+
"name": "Gaurish Sethia",
7+
"url": "https://github.com/gaurishhs",
8+
"email": "gaurishsethia@yahoo.com"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/gaurishhs/elysia-ip"
13+
},
14+
"main": "./dist/index.js",
15+
"exports": {
16+
"require": "./dist/index.js",
17+
"import": "./dist/index.js",
18+
"node": "./dist/index.js",
19+
"default": "./dist/index.js"
20+
},
21+
"types": "./dist/index.d.ts",
22+
"bugs": "https://github.com/gaurishhs/elysia-ip/issues",
23+
"homepage": "https://github.com/gaurishhs/elysia-ip",
24+
"keywords": [
25+
"elysia",
26+
"typescript",
27+
"ip"
28+
],
29+
"scripts": {
30+
"build": "tsc --project tsconfig.esm.json",
31+
"publish": "npm run build && npm publish",
32+
"lint": "eslint . --ext .ts"
33+
},
34+
"license": "MIT",
35+
"devDependencies": {
36+
"@typescript-eslint/eslint-plugin": "^6.7.4",
37+
"@typescript-eslint/parser": "^6.7.4",
38+
"bun-types": "latest",
39+
"elysia": "^0.7.15",
40+
"eslint": "^8.50.0"
41+
},
42+
"peerDependencies": {
43+
"elysia": ">= 0.2.0"
44+
}
45+
}

0 commit comments

Comments
 (0)