|
| 1 | +/** |
| 2 | + * @typedef {object} BrowserAndOtherReturn |
| 3 | + * @property {object} browser |
| 4 | + * @property {object} device |
| 5 | + */ |
| 6 | +/** |
| 7 | + * @typedef {object} OtherReturn |
| 8 | + * @property {object} browser |
| 9 | + * @property {object} device |
| 10 | + */ |
| 11 | +const Browser = require('../model/Browser'); |
| 12 | +const Version = require('../model/Version'); |
| 13 | +/** |
| 14 | + * Applications utility |
| 15 | + * |
| 16 | + * @internal |
| 17 | + */ |
| 18 | +class Applications { |
| 19 | + /** |
| 20 | + * |
| 21 | + * @param {string} ua the User-Agent string |
| 22 | + * |
| 23 | + * @return {BrowserAndOtherReturn} |
| 24 | + */ |
| 25 | + static identifyBrowser(ua = '') { |
| 26 | + if (ua.match(Applications.BROWSERS_REGEX)) { |
| 27 | + for (let type of Object.keys(Applications.BROWSERS)) { |
| 28 | + for (let item of Applications.BROWSERS[type]) { |
| 29 | + let match; |
| 30 | + if ((match = ua.match(item.regexp))) { |
| 31 | + return { |
| 32 | + browser: { |
| 33 | + name: item.name, |
| 34 | + hidden: item.hidden || false, |
| 35 | + stock: false, |
| 36 | + channel: '', |
| 37 | + type: type, |
| 38 | + version: match[1] ? new Version({ |
| 39 | + value: match[1], |
| 40 | + details: item.details || null, |
| 41 | + }) : null, |
| 42 | + }, |
| 43 | + device: typeof item.type !== 'undefined' ? {type: item.type} : null, |
| 44 | + }; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * |
| 53 | + * @param {string} ua the User-Agent string |
| 54 | + * |
| 55 | + * @return {BrowserAndOtherReturn} |
| 56 | + */ |
| 57 | + static identifyOther(ua = '') { |
| 58 | + if (ua.match(Applications.OTHERS_REGEX)) { |
| 59 | + for (let type of Object.keys(Applications.OTHERS)) { |
| 60 | + for (let item of Applications.OTHERS[type]) { |
| 61 | + let match; |
| 62 | + if ((match = ua.match(item.regexp))) { |
| 63 | + return { |
| 64 | + browser: { |
| 65 | + name: item.name, |
| 66 | + hidden: item.hidden || false, |
| 67 | + stock: false, |
| 68 | + channel: '', |
| 69 | + type: type, |
| 70 | + version: match[1] ? new Version({ |
| 71 | + value: match[1], |
| 72 | + details: item.details || null, |
| 73 | + }) : null, |
| 74 | + }, |
| 75 | + device: typeof item.type !== 'undefined' ? {type: item.type} : null, |
| 76 | + }; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * |
| 85 | + * @param {string} ua the User-Agent string |
| 86 | + * |
| 87 | + * @return {Browser} |
| 88 | + */ |
| 89 | + static identifyBot(ua = '') { |
| 90 | + if (ua.match(Applications.BOTS_REGEX)) { |
| 91 | + for (let type of Object.keys(Applications.BOTS)) { |
| 92 | + for (let item of Applications.BOTS[type]) { |
| 93 | + let match; |
| 94 | + if ((match = ua.match(item.regexp))) { |
| 95 | + return new Browser({ |
| 96 | + name: item.name, |
| 97 | + stock: false, |
| 98 | + version: match[1] ? new Version({ |
| 99 | + value: match[1], |
| 100 | + details: item.details || null, |
| 101 | + }) : null, |
| 102 | + }); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +Applications.BOTS = require('../../data/applications-bots'); |
| 111 | +Applications.BOTS_REGEX = require('../../data/regexes/applications-bots'); |
| 112 | + |
| 113 | +Applications.BROWSERS = require('../../data/applications-browsers'); |
| 114 | +Applications.BROWSERS_REGEX = require('../../data/regexes/applications-browsers'); |
| 115 | + |
| 116 | +Applications.OTHERS = require('../../data/applications-others'); |
| 117 | +Applications.OTHERS_REGEX = require('../../data/regexes/applications-others'); |
| 118 | + |
| 119 | +module.exports = Applications; |
0 commit comments