Skip to content

Commit

Permalink
Detect ARM64 macOS running on Apple Silicon
Browse files Browse the repository at this point in the history
Also distinguishes whether 64-bit Node is running because we're actually on an Intel Mac or if it's just Rosetta 2 pretending to be one.
  • Loading branch information
jakejarvis committed Mar 10, 2021
1 parent af5b7e1 commit 6a6c1f9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
3 changes: 1 addition & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

declare function arch(): 'x64' | 'x86';
declare function arch(): 'x64' | 'x86' | 'arm64';

export = arch;
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ module.exports = function arch () {
}

/**
* All recent versions of Mac OS are 64-bit.
* On macOS, we need to detect if x64 Node is running because the CPU is truly
* an Intel chip, or if it's running on Apple Silicon via Rosetta 2:
* https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment
*/
if (process.platform === 'darwin') {
return 'x64'
var nativeArm = process.arch === 'arm64'
var rosettaArm = cp.execSync('sysctl -in sysctl.proc_translated', { encoding: 'utf8' }) === '1\n'

return (nativeArm || rosettaArm) ? 'arm64' : 'x64'
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var arch = require('../')
var test = require('tape')

test('returns x86 or x64', function (t) {
test('returns a valid architecture', function (t) {
var str = arch()
t.ok(str === 'x86' || str === 'x64')
t.ok(str === 'x86' || str === 'x64' || str === 'arm64')
t.end()
})

0 comments on commit 6a6c1f9

Please sign in to comment.