@rzl-zone/utils-js
Version:
A modern, lightweight set of JavaScript utility functions for everyday development, crafted to enhance code readability and maintainability.
1 lines • 835 B
JavaScript
;require('server-only');var server=require('next/server');var n=t=>typeof t=="boolean";var i=t=>typeof t=="function";var m=(t,o=true)=>{if(!i(server.NextRequest))throw new Error("`getClientIpOrUrl` is designed to be used in a `Next.js` environment.");if(!(t instanceof server.NextRequest))throw new TypeError("Argument `request` must be an instance of NextRequest.");if(!n(o))throw new TypeError("Expected `includeFullUrl` to be a boolean.");let e=(t.headers.get("x-forwarded-for")??"127.0.0.1").trim().split(",");(e[0]==="::ffff:127.0.0.1"||e[0]==="::1")&&(e[0]="127.0.0.1");let r=e.length>1?e[e.length-1].trim():e[0];if(!o)return r;let p=t.headers.get("x-forwarded-proto")||"http",f=t.headers.get("x-forwarded-port")||"3000";return `${process.env.NODE_ENV=="production"?p:"http"}://${r}:${f}`};exports.getClientIpOrUrl=m;