UNPKG

ipdb

Version:

IP lookup using IPIP.net database (Node.js & Browser)

3 lines (2 loc) 3.74 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("fs")):"function"==typeof define&&define.amd?define(["fs"],e):(t="undefined"!=typeof globalThis?globalThis:t||self).IPDB=e(t.require$$0)}(this,function(t){"use strict";function e(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var n,r={exports:{}};var s=(n||(n=1,function(e,n){class r{constructor(e,n={}){let r;if("string"==typeof e){if("undefined"==typeof process||!process.versions||!process.versions.node)throw new Error("File path is only supported in Node.js environment. In browser, please pass ArrayBuffer or Uint8Array.");r=t.readFileSync(e)}else if((s=e)instanceof Uint8Array||null!==s&&null!==s.constructor&&"function"==typeof s.constructor.isBuffer&&s.constructor.isBuffer(s))r=e;else if(e instanceof ArrayBuffer)r=new Uint8Array(e);else{if(!(e instanceof Uint8Array))throw new TypeError("Invalid input type. Expected: file path (Node.js only), Buffer, ArrayBuffer, or Uint8Array");r=e}var s;const o=(f=0,(i=r).readInt32BE?i.readInt32BE(f):new DataView(i.buffer,i.byteOffset+f,4).getInt32(0,!1));var i,f;const a=r.slice(4,4+o);let d;d="undefined"==typeof TextDecoder?a.toString():(new TextDecoder).decode(a),this.meta=JSON.parse(d),this.data=r.slice(4+o),n.patches&&(this.patches=n.patches),void 0===this.meta.v4node&&1&this.meta.ip_version&&this._initv4node()}_initv4node(){let t=0;for(let e=0;e<80;e+=1)t=this._node(t,0);for(let e=80;e<96;e+=1)t=this._node(t,1);this.meta.v4node=t}find(t,e={}){const n={};try{if(n.data=this._find(t,e.language),n.code=0,this.patches||e.patches){const t=e.patches||this.patches;for(const e of t)e(n.data)}}catch(t){n.code=-1,n.message=t.message}return n}_find(t,e){const n=this._toBits(t);let r=32===n.length?this.meta.v4node:0,s=0;for(;s<n.length&&(r=this._node(r,n[s]),s+=1,!(r>=this.meta.node_count)););const o=this._resolve(r);let i=0;this.meta.languages[e]&&(i=this.meta.languages[e]);const f={};for(let t=0;t<this.meta.fields.length;t+=1)f[this.meta.fields[t]]=o[i+t];return f.ip=t,f.bitmask=s,f}_resolve(t){const e=t+(this.meta.node_count<<3)-this.meta.node_count,n=function(t,e){return t.readUInt16BE?t.readUInt16BE(e):new DataView(t.buffer,t.byteOffset+e,2).getUint16(0,!1)}(this.data,e),r=this.data.slice(e+2,e+2+n);let s;return s="undefined"==typeof TextDecoder?r.toString():(new TextDecoder).decode(r),s.split("\t")}_node(t,e){return n=this.data,r=t<<3|e<<2,n.readUInt32BE?n.readUInt32BE(r):new DataView(n.buffer,n.byteOffset+r,4).getUint32(0,!1);var n,r}_toBits(t){const e=function(t){if(/^(25[0-5]|2[0-4]\d|[01]?\d{1,2})\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})$/.test(t)){const e=t.split(".");for(const t of e)if(parseInt(t,10)>255)return 0;return 4}if(t.includes(":")){const e=t.split(":"),n=t.includes("::");if(!n&&8!==e.length)return 0;if(n&&e.length>8)return 0;for(const t of e)if(""!==t&&!/^[\da-f]{1,4}$/i.test(t))return 0;return 6}return 0}(t);if(!e)throw new Error("ip format error");if(4===e&&!(1&this.meta.ip_version))throw new Error("no support ipv4");if(6===e&&!(2&this.meta.ip_version))throw new Error("no support ipv6");return 4===e?this._toBits4(t):this._toBits6(t)}_toBits4(t){const e=[],n=t.split(".");for(const t of n){const n=parseInt(t,10);for(let t=7;t>=0;t-=1)e.push(n>>t&1)}return e}_toBits6(t){const e=[[],[]],n=t.split("::",2);for(let t=0;t<2;t+=1)if(n[t]){const r=n[t].split(":");for(const n of r){const r=parseInt(n,16);for(let n=15;n>=0;n-=1)e[t].push(r>>n&1)}}const r=128-e[0].length-e[1].length;return[...e[0],...new Array(r).fill(0),...e[1]]}}e.exports=r,n.default=r,n.Parse=r}(r,r.exports)),r.exports);return e(s)}); //# sourceMappingURL=ipdb.umd.min.js.map