UNPKG

ip-location-api

Version:
2 lines (1 loc) 1.74 kB
var IpLookup=function(){"use strict";const n=n=>((n=n.split(/\./))[0]<<24|n[1]<<16|n[2]<<8|n[3])>>>0,t=document.currentScript.src.split("/").slice(0,-1).join("/")+"/",r=async(n,t=3)=>fetch(n,{cache:"no-cache"}).then((async e=>{return e.ok?e.arrayBuffer():404===e.status?null:t?(await(i=100*(4-t)*(4-t),new Promise((n=>setTimeout(n,i)))),r(n,t-1)):null;var i})),e=r,i={},l={4:t,6:t},s={4:e(t+"4.idx").then((n=>{if(n)return i[4]=new Uint32Array(n)})),6:e(t+"4.idx").then((n=>{if(n)return i[6]=new BigUint64Array(n)}))};return async t=>{var e,u,c=!0;t.includes(":")?6===(u=(e=(t=>{if(t.includes("."))return n(t.split(":").pop());var r,e=0n;if((t=t.split(/:/)).length-1<7){const n=t.indexOf("");if(n<4){const e=8-t.length,i=n+e;for(r=7;r>=n;r--)t[r]=r>i?t[r-e]:0}}for(r=0;r<4;r++)t[r]&&(e+=BigInt(parseInt(t[r],16))<<BigInt(16*(3-r)));return e})(t)).constructor===BigInt?6:4)&&(c=!1):(e=n(t),u=4);const a=i[u]||await s[u];if(!a)return null;if(!(e>=a[0]))return null;for(var o,f=0,g=a.length-1;;)if(e<a[o=f+g>>1]){if(g-f<2)return null;g=o-1}else{if(f===o){g>o&&e>=a[g]&&(o=g);break}f=o}const h=((n,t)=>n.length>t?n:"_".repeat(t-n.length)+n)(o.toString(36),2);const y=await r(l[u]+u+"/"+h);if(!y)return null;const p=2*(u-2),w=8+2*p,d=y.byteLength/w,A=c?new Uint32Array(y.slice(0,4*d)):new BigUint64Array(y.slice(0,8*d));for(f=0,g=d-1;;)if(e<A[o=f+g>>1]){if(g-f<2)return null;g=o-1}else{if(f===o){g>o&&e>=A[g]&&(o=g);break}f=o}const B=c?new Uint32Array(y.slice((d+o)*p,(d+o+1)*p))[0]:new BigUint64Array(y.slice((d+o)*p,(d+o+1)*p))[0];if(e>=A[o]&&e<=B){const n=new Int32Array(y.slice(d*p*2+8*o,d*p*2+8*(o+1))),t=(n=>String.fromCharCode(65+(n/26|0),n%26+65))(1023&n[0]);return{latitude:(n[0]>>10)/1e4,longitude:n[1]/1e4,country:t}}return null}}();