Skip to content

Commit

Permalink
Merge pull request #22 from jakejarvis/detect-arm
Browse files Browse the repository at this point in the history
  • Loading branch information
feross authored Feb 12, 2024
2 parents af5b7e1 + 6a6c1f9 commit 0ed871e
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 0ed871e

Please sign in to comment.