66 lines
9.0 KiB
JavaScript
66 lines
9.0 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.parseUserAgent = exports.extractMetaInfo = exports.calculatePrettyUserAgent = void 0;
|
|
const bowser_1 = __importDefault(require("bowser"));
|
|
const DEFAULT_NAME = 'Other';
|
|
const DEFAULT_VERSION = '0.0';
|
|
const DEFAULT_PLATFORM_TYPE = DEFAULT_NAME.toLowerCase();
|
|
const EMPTY_PARSED_USER_AGENT = bowser_1.default.parse(' ');
|
|
const META_INFO_REGEX = /\([^(|^)]*\)/;
|
|
function calculateBrowser(browserDetails) {
|
|
return {
|
|
name: browserDetails.name || DEFAULT_NAME,
|
|
version: browserDetails.version || DEFAULT_VERSION,
|
|
};
|
|
}
|
|
function calculateOs(parsedOsDetails) {
|
|
const name = parsedOsDetails.name || DEFAULT_NAME;
|
|
let version = DEFAULT_VERSION;
|
|
// NOTE: a 'versionName' property value is more readable in the case of Windows (GH-481):
|
|
// Windows 8.1: os.version: "NT 6.3", os.versionName: "8.1".
|
|
if (name.toLowerCase() === 'windows') {
|
|
if (parsedOsDetails.versionName)
|
|
version = parsedOsDetails.versionName;
|
|
}
|
|
else if (parsedOsDetails.version)
|
|
version = parsedOsDetails.version;
|
|
return { name, version };
|
|
}
|
|
function calculateEngine(engineDetails) {
|
|
return {
|
|
name: engineDetails.name || DEFAULT_NAME,
|
|
version: engineDetails.version || DEFAULT_VERSION,
|
|
};
|
|
}
|
|
function calculatePrettyUserAgent(browser, os) {
|
|
return `${browser.name} ${browser.version} / ${os.name} ${os.version}`;
|
|
}
|
|
exports.calculatePrettyUserAgent = calculatePrettyUserAgent;
|
|
function extractMetaInfo(prettyUserAgent) {
|
|
const parenthesisExpressions = prettyUserAgent.match(META_INFO_REGEX);
|
|
if (!parenthesisExpressions || !parenthesisExpressions.length)
|
|
return '';
|
|
return parenthesisExpressions[parenthesisExpressions.length - 1].replace(/[()]/g, '');
|
|
}
|
|
exports.extractMetaInfo = extractMetaInfo;
|
|
function parseUserAgent(userAgent = '') {
|
|
const parsedUserAgent = userAgent ? bowser_1.default.parse(userAgent) : EMPTY_PARSED_USER_AGENT;
|
|
const browser = calculateBrowser(parsedUserAgent.browser);
|
|
const os = calculateOs(parsedUserAgent.os);
|
|
const engine = calculateEngine(parsedUserAgent.engine);
|
|
const prettyUserAgent = calculatePrettyUserAgent(browser, os);
|
|
return {
|
|
name: browser.name,
|
|
version: browser.version,
|
|
platform: parsedUserAgent.platform.type || DEFAULT_PLATFORM_TYPE,
|
|
os,
|
|
engine,
|
|
prettyUserAgent: prettyUserAgent,
|
|
userAgent,
|
|
};
|
|
}
|
|
exports.parseUserAgent = parseUserAgent;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGFyc2UtdXNlci1hZ2VudC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy91dGlscy9wYXJzZS11c2VyLWFnZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7OztBQUFBLG9EQUE0QjtBQUU1QixNQUFNLFlBQVksR0FBYyxPQUFPLENBQUM7QUFDeEMsTUFBTSxlQUFlLEdBQVcsS0FBSyxDQUFDO0FBQ3RDLE1BQU0scUJBQXFCLEdBQUssWUFBWSxDQUFDLFdBQVcsRUFBRSxDQUFDO0FBQzNELE1BQU0sdUJBQXVCLEdBQUcsZ0JBQU0sQ0FBQyxLQUFLLENBQUMsR0FBRyxDQUFDLENBQUM7QUFDbEQsTUFBTSxlQUFlLEdBQUcsY0FBYyxDQUFDO0FBaUJ2QyxTQUFTLGdCQUFnQixDQUFFLGNBQTRDO0lBQ25FLE9BQU87UUFDSCxJQUFJLEVBQUssY0FBYyxDQUFDLElBQUksSUFBSSxZQUFZO1FBQzVDLE9BQU8sR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