Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect ARM64 macOS running on Apple Silicon #22

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})