Skip to content

Commit 503d265

Browse files
rhlshahbmuenzenmeyerovflowd
authored
fix: select arm64 by default (#6439)
* Fixed Issue #6364 * Fix arm64 select by default v2 * Fix arm64 select by default v2 * Update util/__tests__/getUserBitnessByArchitecture.test.mjs Co-authored-by: Brian Muenzenmeyer <[email protected]> Signed-off-by: Rahil Shah <[email protected]> * Update util/getUserBitnessByArchitecture.ts Co-authored-by: Brian Muenzenmeyer <[email protected]> Signed-off-by: Rahil Shah <[email protected]> * Update getUserBitnessByArchitecture.ts Signed-off-by: Claudio W <[email protected]> * chore: fixed lint --------- Signed-off-by: Rahil Shah <[email protected]> Signed-off-by: Claudio W <[email protected]> Co-authored-by: Brian Muenzenmeyer <[email protected]> Co-authored-by: Claudio W <[email protected]>
1 parent a8efa11 commit 503d265

File tree

6 files changed

+74
-15
lines changed

6 files changed

+74
-15
lines changed

components/Downloads/Release/BitnessDropdown.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@ import Select from '@/components/Common/Select';
99
import { useDetectOS } from '@/hooks/react-client';
1010
import { ReleaseContext } from '@/providers/releaseProvider';
1111
import { bitnessItems, formatDropdownItems } from '@/util/downloadUtils';
12+
import { getUserBitnessByArchitecture } from '@/util/getUserBitnessByArchitecture';
1213

1314
const parseNumericBitness = (bitness: string) =>
1415
/^\d+$/.test(bitness) ? Number(bitness) : bitness;
1516

1617
const BitnessDropdown: FC = () => {
17-
const { bitness: userBitness } = useDetectOS();
18+
const { bitness: userBitness, architecture: userArchitecture } =
19+
useDetectOS();
1820
const { bitness, os, release, setBitness } = useContext(ReleaseContext);
1921
const t = useTranslations();
2022

2123
// we also reset the bitness when the OS changes, because different OSs have
2224
// different bitnesses available
23-
useEffect(() => setBitness(userBitness), [setBitness, userBitness]);
25+
26+
useEffect(() => {
27+
setBitness(getUserBitnessByArchitecture(userArchitecture, userBitness));
28+
// we shouldn't update the effect on setter state change
29+
// eslint-disable-next-line react-hooks/exhaustive-deps
30+
}, [userArchitecture, userBitness]);
2431

2532
// @TODO: We should have a proper utility that gives
2633
// disabled OSs, Platforms, based on specific criteria

hooks/react-client/__tests__/useDetectOS.test.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe('useDetectOS', () => {
3535
expect(result.current).toStrictEqual({
3636
os: 'WIN',
3737
bitness: 64,
38+
architecture: '',
3839
});
3940
});
4041
});
@@ -53,6 +54,7 @@ describe('useDetectOS', () => {
5354
expect(result.current).toStrictEqual({
5455
os: 'WIN',
5556
bitness: 64,
57+
architecture: '',
5658
});
5759
});
5860
});
@@ -71,6 +73,7 @@ describe('useDetectOS', () => {
7173
expect(result.current).toStrictEqual({
7274
os: 'MAC',
7375
bitness: 86,
76+
architecture: '',
7477
});
7578
});
7679
});

hooks/react-client/useDetectOS.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,40 @@ import { useEffect, useState } from 'react';
44

55
import type { UserOS } from '@/types/userOS';
66
import { detectOS } from '@/util/detectOS';
7+
import { getArchitecture } from '@/util/getArchitecture';
78
import { getBitness } from '@/util/getBitness';
89

910
type UserOSState = {
1011
os: UserOS;
1112
bitness: number;
13+
architecture: string;
1214
};
1315

1416
const useDetectOS = () => {
1517
const [userOSState, setUserOSState] = useState<UserOSState>({
1618
os: 'OTHER',
1719
bitness: 86,
20+
architecture: 'ARM',
1821
});
1922

2023
useEffect(() => {
21-
getBitness().then(bitness => {
22-
const userAgent = navigator?.userAgent;
23-
24-
setUserOSState({
25-
os: detectOS(),
26-
bitness:
27-
bitness === '64' ||
28-
userAgent?.includes('WOW64') ||
29-
userAgent?.includes('Win64')
30-
? 64
31-
: 86,
32-
});
33-
});
24+
Promise.all([getBitness(), getArchitecture()]).then(
25+
([bitness, architecture]) => {
26+
const userAgent: string | undefined =
27+
(typeof navigator === 'object' && navigator.userAgent) || '';
28+
const defaultBitness: number = 86; // Default bitness if unable to determine
29+
setUserOSState({
30+
os: detectOS(),
31+
bitness:
32+
bitness === '64' ||
33+
userAgent?.includes('WOW64') ||
34+
userAgent?.includes('Win64')
35+
? 64
36+
: defaultBitness,
37+
architecture: architecture ? architecture : '',
38+
});
39+
}
40+
);
3441
}, []);
3542

3643
return userOSState;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getUserBitnessByArchitecture } from '@/util/getUserBitnessByArchitecture';
2+
3+
describe('getUserBitnessByArchitecture', () => {
4+
it('should return "arm64" for arm architecture and 64-bit', () => {
5+
const result = getUserBitnessByArchitecture('arm', 64);
6+
expect(result).toBe('arm64');
7+
});
8+
9+
it('should return the user bitness for other architectures', () => {
10+
const result = getUserBitnessByArchitecture('x86', 32);
11+
expect(result).toBe(32);
12+
});
13+
});

util/getArchitecture.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference types="user-agent-data-types" />
2+
3+
export const getArchitecture = async () => {
4+
// This is necessary to detect Windows 11 on Edge.
5+
// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorUAData/getHighEntropyValues)
6+
// [MSFT](https://learn.microsoft.com/en-us/microsoft-edge/web-platform/how-to-detect-win11)
7+
if (typeof navigator?.userAgentData?.getHighEntropyValues === 'function') {
8+
const entropyValues = navigator.userAgentData.getHighEntropyValues([
9+
'architecture',
10+
]);
11+
12+
// Apparently in some cases this is not a Promise, we can Promisify it.
13+
return Promise.resolve(entropyValues)
14+
.then(({ architecture }) => architecture)
15+
.catch(() => undefined);
16+
}
17+
18+
return undefined;
19+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const getUserBitnessByArchitecture = (
2+
userArchitecture: string,
3+
userBitness: number
4+
) => {
5+
if (userArchitecture === 'arm' && userBitness === 64) {
6+
return 'arm64';
7+
}
8+
9+
return userBitness;
10+
};

0 commit comments

Comments
 (0)