From e70d09a1f8f72ef41658a3e9f0a079f0c7ae4229 Mon Sep 17 00:00:00 2001 From: Faisal Salman Date: Sat, 25 Mar 2023 07:21:17 +0700 Subject: [PATCH] Fix #489: ARM arch detection & create test that simulates HTTP headers sent from an Apple silicon --- src/ua-parser.js | 2 +- test/test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/ua-parser.js b/src/ua-parser.js index 04a43b6..3fb1e12 100755 --- a/src/ua-parser.js +++ b/src/ua-parser.js @@ -1056,7 +1056,7 @@ var archName = uaCH[ARCHITECTURE]; if (archName) { if (archName && uaCH[BITNESS] == '64') archName += '64'; - rgxMapper.call(this.data, archName, rgxMap); + rgxMapper.call(this.data, archName + ';', rgxMap); } break; case UA_DEVICE: diff --git a/test/test.js b/test/test.js index c744aa5..8077e82 100644 --- a/test/test.js +++ b/test/test.js @@ -483,6 +483,36 @@ describe('Map UA-CH headers', function () { assert.strictEqual(uap.os.name, "Linux"); assert.strictEqual(uap.os.version, "x86_64"); }); + + it('Can detect Apple silicon from client hints data', function () { + + // https://github.com/faisalman/ua-parser-js/issues/489#issuecomment-1479213579 + const httpHeadersFromAppleSilicon = { + 'sec-ch-ua-arch' : 'arm', + 'sec-ch-ua-platform' : 'macOS', + 'sec-ch-ua-mobile' : '?0', + 'sec-ch-ua' : '"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"', + 'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0' + }; + + UAParser(httpHeadersFromAppleSilicon).withClientHints().then(function (ua) { + + // Only works in Chrome + /* + if (ua.os.is("macOS") && + ua.cpu.is("arm") && + !ua.device.is("mobile") && + !ua.device.is("tablet")) { + // possibly an Apple silicon device + } + */ + + assert.strictEqual(ua.os.is("macOS"), true); + assert.strictEqual(ua.cpu.is("arm"), true); + assert.strictEqual(ua.device.is("mobile"), false); + assert.strictEqual(ua.device.is("tablet"), false); + }); + }); }); describe('Map UA-CH JS', () => {