From 63110ca6fd8453b5136fdabc851ef9918cb1dc3a Mon Sep 17 00:00:00 2001 From: Ruben Nogueira Date: Fri, 17 Jan 2025 15:16:37 +0000 Subject: [PATCH] fix: arm64 detection --- browser.js | 32 +++++++++++++++++++++++++++++++- index.js | 8 ++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/browser.js b/browser.js index 9ee7072..157a6ca 100644 --- a/browser.js +++ b/browser.js @@ -20,15 +20,45 @@ module.exports = function arch () { return 'x64' } + if ([ + 'ARM64', + 'AArch64' + ].some(function (str) { + return userAgent.indexOf(str) > -1 + })) { + return 'arm64' + } + /** * Platform strings that indicate a 64-bit OS. * See: http://stackoverflow.com/a/19883965/292185 */ var platform = navigator.platform - if (platform === 'MacIntel' || platform === 'Linux x86_64') { + if (platform === 'Linux x86_64' || platform === 'Linux x86_64 X11') { return 'x64' } + if (platform === 'Linux aarch64' || platform === 'Linux armv8l') { + return 'arm64' + } + + /** + * + * Detect if the browser is running on an Mac ARM64 architecture. + * See: https://stackoverflow.com/a/65412357 + */ + if (platform === 'MacIntel') { + var canvas = OffscreenCanvas(1, 1); + var w = canvas.getContext("webgl"); + var d = w.getExtension('WEBGL_debug_renderer_info'); + var g = d && w.getParameter(d.UNMASKED_RENDERER_WEBGL) || ""; + if (g.match(/Apple/) && !g.match(/Apple GPU/)) { + return 'arm64' + } else { + return 'x64' + } + } + /** * CPU class strings that indicate a 64-bit OS. * See: http://stackoverflow.com/a/6267019/292185 diff --git a/index.js b/index.js index 956b622..b7133cf 100644 --- a/index.js +++ b/index.js @@ -34,6 +34,10 @@ module.exports = function arch () { * See: https://twitter.com/feross/status/776949077208510464 */ if (process.platform === 'win32') { + if (process.arch === 'arm64') { + return 'arm64' + } + var useEnv = false try { useEnv = !!(process.env.SYSTEMROOT && fs.statSync(process.env.SYSTEMROOT)) @@ -54,6 +58,10 @@ module.exports = function arch () { * On Linux, use the `getconf` command to get the architecture. */ if (process.platform === 'linux') { + if (process.arch === 'arm64') { + return 'arm64' + } + var output = cp.execSync('getconf LONG_BIT', { encoding: 'utf8' }) return output === '64\n' ? 'x64' : 'x86' }