@sanely/request
v0.1.0
Published
Read the client IP address and location from a request
Readme
@sanely/request reads what the network and the hosting platform report about a request: the client's IP address and, on platforms that send it, the client's location.
Features
- Headers a client cannot forge. Name the platform the app runs on, such as Vercel, Cloudflare, or Fly.io, and
ipreads only the headers that platform sets. - Location.
georeads the country, city, and other fields that the platform reports. - Your own proxies. List their addresses with
parseProxies, andipskips them. - No platform. With
platform: "none",ipreads the connection and falls back to common forwarding headers such asX-Forwarded-For. - Any request. Both functions accept a Fetch
Requestor a Node.js-style request. - No external dependencies.
Install
npm install @sanely/requestThe package needs Node.js 20 or later.
Get started
import { geo, ip } from "@sanely/request"
const options = { platform: "vercel" } as const
ip(request, options) // "8.8.8.8"
geo(request, options).country // "US"Set platform to the platform the app runs on. ip then reads only that platform's headers, which a client cannot forge. With platform: "none", ip reads the connection and falls back to the common forwarding headers, such as X-Forwarded-For, which a client can forge unless a proxy in front of the app replaces them.
Behind your own proxies or load balancers, list their addresses so that ip skips them. Parse the list once at startup, so a bad entry fails before the app serves a request:
import { ip, parseProxies } from "@sanely/request"
const proxies = parseProxies(["10.0.0.0/8", "198.51.100.7"])
ip(request, { platform: "none", proxies })Documentation
- Reference:
ip,parseProxies,geo, platforms, and types
Contributing
Read the architecture, testing guide, and root contributor guide before changing the package.
License
MIT. See LICENSE.
