which-bot
Version:
detect which bot is from user-agent and is trustable
56 lines • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RULES = exports.is = exports.parse = void 0;
const rules_1 = require("./rules");
exports.RULES = rules_1.default;
const util_1 = require("./util");
const MOBILE_REGEX = /mobile/i;
function parse(ua, ip) {
var _a;
if (!ua) {
return false;
}
const res = (0, util_1.match)(ua, rules_1.default);
if (!res) {
return false;
}
const data = {
name: res.name,
company: res.company,
mobile: !!ua.match(MOBILE_REGEX),
};
if (!ip || !((_a = res.hosts) === null || _a === void 0 ? void 0 : _a.length)) {
return data;
}
return (0, util_1.isValidIp)(res.hosts, ip).then((valid) => {
return Object.assign(Object.assign({}, data), { valid });
});
}
exports.parse = parse;
function is(name, ua, ip) {
var _a;
if (!name || !ua) {
return false;
}
const rules = rules_1.default.filter(item => item.company === name);
if (!rules.length) {
return false;
}
const res = (0, util_1.match)(ua, rules_1.default);
if (!res) {
return false;
}
const data = {
name: res.name,
company: res.company,
mobile: !!ua.match(MOBILE_REGEX),
};
if (!ip || !((_a = res.hosts) === null || _a === void 0 ? void 0 : _a.length)) {
return data;
}
return (0, util_1.isValidIp)(res.hosts, ip).then((valid) => {
return Object.assign(Object.assign({}, data), { valid });
});
}
exports.is = is;
//# sourceMappingURL=index.js.map