UNPKG

@maxmind/geoip2-node

Version:

Node.js API for GeoIP2 webservice client and database reader

88 lines (87 loc) 3.55 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const mmdb = __importStar(require("maxmind")); const errors_1 = require("./errors"); const models = __importStar(require("./models")); const utils_1 = require("./utils"); class ReaderModel { constructor(mmdbReader) { this.mmdbReader = mmdbReader; } anonymousIP(ipAddress) { return this.modelFor(models.AnonymousIP, 'GeoIP2-Anonymous-IP', ipAddress, 'anonymousIP()'); } city(ipAddress) { return this.modelFor(models.City, 'City', ipAddress, 'city()'); } country(ipAddress) { return this.modelFor(models.Country, 'Country', ipAddress, 'country()'); } asn(ipAddress) { return this.modelFor(models.Asn, 'ASN', ipAddress, 'asn()'); } connectionType(ipAddress) { return this.modelFor(models.ConnectionType, 'Connection-Type', ipAddress, 'connectionType()'); } domain(ipAddress) { return this.modelFor(models.Domain, 'Domain', ipAddress, 'domain()'); } isp(ipAddress) { return this.modelFor(models.Isp, 'ISP', ipAddress, 'isp()'); } enterprise(ipAddress) { return this.modelFor(models.Enterprise, 'Enterprise', ipAddress, 'enterprise()'); } getRecord(dbType, ipAddress, fnName) { const metaDbType = this.mmdbReader.metadata.databaseType; if (!mmdb.validate(ipAddress)) { throw new errors_1.ValueError(`${ipAddress} is invalid`); } if (!metaDbType.includes(dbType)) { throw new errors_1.BadMethodCallError(`The ${fnName} method cannot be used with the ${metaDbType} database`); } const [record, prefixLength] = this.mmdbReader.getWithPrefixLength(ipAddress); if (!record) { throw new errors_1.AddressNotFoundError(`The address ${ipAddress} is not in the database`); } return [record, (0, utils_1.toCidr)(ipAddress, prefixLength)]; } modelFor(modelClass, dbType, ipAddress, fnName) { const [record, network] = this.getRecord(dbType, ipAddress, fnName); return new modelClass(record, ipAddress, network); } } exports.default = ReaderModel;