UNPKG

powr-sdk-api

Version:

Shared API core library for PowrStack projects. Zero dependencies - works with Express, Next.js API routes, and other frameworks. All features are optional and install only what you need.

21 lines (20 loc) 649 B
"use strict"; /** * Best-effort client IP from the incoming HTTP request * (uses X-Forwarded-For, X-Real-IP, req.ip, or socket address). */ function getClientIp(req) { const forwarded = req.headers["x-forwarded-for"]; if (typeof forwarded === "string" && forwarded.length > 0) { const first = forwarded.split(",")[0].trim(); if (first) return first; } const realIp = req.headers["x-real-ip"]; if (typeof realIp === "string" && realIp.trim()) return realIp.trim(); if (req.ip) return req.ip; if (req.socket && req.socket.remoteAddress) return req.socket.remoteAddress; return null; } module.exports = { getClientIp };