diff --git a/package-lock.json b/package-lock.json index 7ad5aa71..bde7d4c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "node-device-detector", - "version": "2.1.1", + "version": "2.1.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "node-device-detector", - "version": "2.1.1", + "version": "2.1.4", "license": "MIT", "dependencies": { "js-yaml": "^4.1.0" diff --git a/parser/device/device-trusted.js b/parser/device/device-trusted.js index 39ac01af..b8b74988 100644 --- a/parser/device/device-trusted.js +++ b/parser/device/device-trusted.js @@ -1,5 +1,5 @@ const helper = require('../helper'); -const {attr} = helper; +const { attr } = helper; /** * check screen size @@ -9,21 +9,43 @@ const {attr} = helper; * @param {string} metaHeight * @return {boolean} */ -const isDeviceSizeValid = (deviceWidth, deviceHeight, metaWidth, metaHeight) => { - +const isDeviceSizeValid = ( + deviceWidth, + deviceHeight, + metaWidth, + metaHeight +) => { if (deviceWidth && deviceHeight && metaWidth && metaHeight) { const minMetaWidth = parseInt(metaWidth) - 1; const maxMetaWidth = parseInt(metaWidth) + 1; const minMetaHeight = parseInt(metaHeight) - 1; const maxMetaHeight = parseInt(metaHeight) + 1; - const isWidthValid = helper.fuzzyBetweenNumber(parseInt(deviceWidth), minMetaWidth, maxMetaWidth) - && helper.fuzzyBetweenNumber(parseInt(deviceHeight), minMetaHeight, maxMetaHeight); - - const isHeightValid = helper.fuzzyBetweenNumber(parseInt(deviceWidth), minMetaHeight, maxMetaHeight) - && helper.fuzzyBetweenNumber(parseInt(deviceHeight), minMetaWidth, maxMetaWidth); - - return (isWidthValid || isHeightValid); + const isWidthValid = + helper.fuzzyBetweenNumber( + parseInt(deviceWidth), + minMetaWidth, + maxMetaWidth + ) && + helper.fuzzyBetweenNumber( + parseInt(deviceHeight), + minMetaHeight, + maxMetaHeight + ); + + const isHeightValid = + helper.fuzzyBetweenNumber( + parseInt(deviceWidth), + minMetaHeight, + maxMetaHeight + ) && + helper.fuzzyBetweenNumber( + parseInt(deviceHeight), + minMetaWidth, + maxMetaWidth + ); + + return isWidthValid || isHeightValid; } return true; @@ -50,7 +72,7 @@ const extractGpuName = (metaGPU) => { const gpuMap = [ { regex: /Adreno \(TM\) ([^$,]+)/i, name: 'Qualcomm Adreno $1' }, { regex: /Mali-([^$,]+)/i, name: 'ARM Mali-$1' }, - { regex: /PowerVR Rogue ([^$,]+)/i, name: 'PowerVR $1' } + { regex: /PowerVR Rogue ([^$,]+)/i, name: 'PowerVR $1' }, ]; for (let i = 0, l = gpuMap.length; i < l; i++) { @@ -63,7 +85,6 @@ const extractGpuName = (metaGPU) => { return metaGPU; }; - /** * @param {ResultDevice} deviceData * @param {Object} clientHints @@ -75,17 +96,26 @@ const checkDisplaySize = (deviceData, clientHints) => { let deviceInfo = deviceData.info; if (deviceInfo) { - if (deviceInfo.display && typeof deviceInfo.display.resolution === 'string') { + if ( + deviceInfo.display && + typeof deviceInfo.display.resolution === 'string' + ) { let resolution = deviceInfo.display.resolution.split('x'); deviceWidth = resolution[0]; deviceHeight = resolution[1]; } else { - deviceWidth = deviceInfo.display && deviceInfo.display.resolution && deviceInfo.display.resolution.width - ? deviceInfo.display.resolution.width - : null; - deviceHeight = deviceInfo.display && deviceInfo.display.resolution && deviceInfo.display.resolution.height - ? deviceInfo.display.resolution.height - : null; + deviceWidth = + deviceInfo.display && + deviceInfo.display.resolution && + deviceInfo.display.resolution.width + ? deviceInfo.display.resolution.width + : null; + deviceHeight = + deviceInfo.display && + deviceInfo.display.resolution && + deviceInfo.display.resolution.height + ? deviceInfo.display.resolution.height + : null; } let metaWidth = attr(clientHints.meta, 'width', null); @@ -99,12 +129,16 @@ const checkDisplaySize = (deviceData, clientHints) => { }; const isGpuExistForClientHints = (clientHints) => { - return clientHints.meta && clientHints.meta.gpu && clientHints.meta.gpu.length > 0; -} + return ( + clientHints.meta && clientHints.meta.gpu && clientHints.meta.gpu.length > 0 + ); +}; const isAppleBrandForDeviceData = (deviceData) => { - return deviceData.brand === 'Apple' - || (deviceData.brand === '' && /ip(?:hone|[ao]d)/i.test(deviceData.code)) + return ( + deviceData.brand === 'Apple' || + (deviceData.brand === '' && /ip(?:hone|[ao]d)/i.test(deviceData.code)) + ); }; /** @@ -114,9 +148,10 @@ const isAppleBrandForDeviceData = (deviceData) => { */ const checkGpu = (deviceData, clientHints) => { let deviceInfo = deviceData.info; - let deviceGPU = deviceInfo && deviceInfo.hardware && deviceInfo.hardware.gpu - ? deviceInfo.hardware.gpu.name - : null; + let deviceGPU = + deviceInfo && deviceInfo.hardware && deviceInfo.hardware.gpu + ? deviceInfo.hardware.gpu.name + : null; let metaGPU = attr(clientHints.meta, 'gpu', null); @@ -125,15 +160,13 @@ const checkGpu = (deviceData, clientHints) => { } if (metaGPU !== null && deviceGPU !== null) { - return isDeviceGpuValid(deviceGPU, metaGPU) + return isDeviceGpuValid(deviceGPU, metaGPU); } return true; }; - class DeviceTrusted { - /** * @param {ResultOs} osData * @param {ResultClient} clientData @@ -142,19 +175,31 @@ class DeviceTrusted { * @return {boolean|null} */ static check(osData, clientData, deviceData, clientHints) { - const regexAppleGpu = /GPU Apple/i; const isGpuExist = isGpuExistForClientHints(clientHints); const isAppleBrand = isAppleBrandForDeviceData(deviceData); - // is Apple and lack of client-hints - if (isAppleBrand && clientHints.client.brands.length > 0) { + if ( + isAppleBrand && + clientHints && + clientHints.client && + clientHints.client.brands && + clientHints.client.brands.length > 0 + ) { return false; } // is Apple and check correct gpu name - if (isAppleBrand && isGpuExist && !regexAppleGpu.test(clientHints.meta.gpu)) { + if ( + isAppleBrand && + isGpuExist && + !regexAppleGpu.test(clientHints.meta.gpu) + ) { return false; - } else if (isAppleBrand && isGpuExist && regexAppleGpu.test(clientHints.meta.gpu)) { + } else if ( + isAppleBrand && + isGpuExist && + regexAppleGpu.test(clientHints.meta.gpu) + ) { return true; } @@ -175,4 +220,4 @@ class DeviceTrusted { } } -module.exports = DeviceTrusted; \ No newline at end of file +module.exports = DeviceTrusted; diff --git a/tests/fixtures/oss.yml b/tests/fixtures/oss.yml index 5cb6861e..f77d9643 100644 --- a/tests/fixtures/oss.yml +++ b/tests/fixtures/oss.yml @@ -5198,6 +5198,14 @@ version: 10.15.7 platform: "" family: Mac +- + user_agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 + os: + name: Mac + short_name: MAC + version: 10.15.7 + platform: "" + family: Mac - user_agent: Mozilla/5.0 (iPhone; CPU iPhone OS 15_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) (Ecosia ios@4.0.6.795) os: