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

fix: select arm64 by default #6439

Merged
merged 7 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 7 additions & 2 deletions components/Downloads/Release/BitnessDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ import Select from '@/components/Common/Select';
import { useDetectOS } from '@/hooks/react-client';
import { ReleaseContext } from '@/providers/releaseProvider';
import { bitnessItems, formatDropdownItems } from '@/util/downloadUtils';
import { getUserBitnessByArchitecture } from '@/util/getUserBitnessByArchitecture';

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

const BitnessDropdown: FC = () => {
const { bitness: userBitness } = useDetectOS();
const { bitness: userBitness, architecture: userArchitecture } =
useDetectOS();
const { bitness, os, release, setBitness } = useContext(ReleaseContext);
const t = useTranslations();

// we also reset the bitness when the OS changes, because different OSs have
// different bitnesses available
useEffect(() => setBitness(userBitness), [setBitness, userBitness]);

useEffect(() => {
setBitness(getUserBitnessByArchitecture(userArchitecture, userBitness));
}, [userArchitecture, userBitness]);

// @TODO: We should have a proper utility that gives
// disabled OSs, Platforms, based on specific criteria
Expand Down
3 changes: 3 additions & 0 deletions hooks/react-client/__tests__/useDetectOS.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('useDetectOS', () => {
expect(result.current).toStrictEqual({
os: 'WIN',
bitness: 64,
architecture: '',
});
});
});
Expand All @@ -53,6 +54,7 @@ describe('useDetectOS', () => {
expect(result.current).toStrictEqual({
os: 'WIN',
bitness: 64,
architecture: '',
});
});
});
Expand All @@ -71,6 +73,7 @@ describe('useDetectOS', () => {
expect(result.current).toStrictEqual({
os: 'MAC',
bitness: 86,
architecture: '',
});
});
});
Expand Down
33 changes: 20 additions & 13 deletions hooks/react-client/useDetectOS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,40 @@ import { useEffect, useState } from 'react';

import type { UserOS } from '@/types/userOS';
import { detectOS } from '@/util/detectOS';
import { getArchitecture } from '@/util/getArchitecture';
import { getBitness } from '@/util/getBitness';

type UserOSState = {
os: UserOS;
bitness: number;
architecture: string;
};

const useDetectOS = () => {
const [userOSState, setUserOSState] = useState<UserOSState>({
os: 'OTHER',
bitness: 86,
architecture: 'ARM',
});

useEffect(() => {
getBitness().then(bitness => {
const userAgent = navigator?.userAgent;

setUserOSState({
os: detectOS(),
bitness:
bitness === '64' ||
userAgent?.includes('WOW64') ||
userAgent?.includes('Win64')
? 64
: 86,
});
});
Promise.all([getBitness(), getArchitecture()]).then(
ovflowd marked this conversation as resolved.
Show resolved Hide resolved
([bitness, architecture]) => {
const userAgent: string | undefined =
(typeof navigator === 'object' && navigator.userAgent) || '';
const defaultBitness: number = 86; // Default bitness if unable to determine
setUserOSState({
os: detectOS(),
bitness:
bitness === '64' ||
userAgent?.includes('WOW64') ||
userAgent?.includes('Win64')
? 64
: defaultBitness,
architecture: architecture ? architecture : '',
});
}
);
}, []);

return userOSState;
Expand Down
13 changes: 13 additions & 0 deletions util/__tests__/getUserBitnessByArchitecture.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getUserBitnessByArchitecture } from '@/util/getUserBitnessByArchitecture';

describe('getUserBitnessByArchitecture', () => {
it('should return "arm64" for arm architecture and 64-bit', () => {
const result = getUserBitnessByArchitecture('arm', 64);
expect(result).toBe('arm64');
});

it('should return the user bitness for other architectures', () => {
const result = getUserBitnessByArchitecture('x86', 32);
expect(result).toBe(32);
});
});
19 changes: 19 additions & 0 deletions util/getArchitecture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference types="user-agent-data-types" />
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved
ovflowd marked this conversation as resolved.
Show resolved Hide resolved

export const getArchitecture = async () => {
// This is necessary to detect Windows 11 on Edge.
// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorUAData/getHighEntropyValues)
// [MSFT](https://learn.microsoft.com/en-us/microsoft-edge/web-platform/how-to-detect-win11)
if (typeof navigator?.userAgentData?.getHighEntropyValues === 'function') {
const entropyValues = navigator.userAgentData.getHighEntropyValues([
'architecture',
]);

// Apparently in some cases this is not a Promise, we can Promisify it.
return Promise.resolve(entropyValues)
.then(({ architecture }) => architecture)
.catch(() => undefined);
}

return undefined;
};
10 changes: 10 additions & 0 deletions util/getUserBitnessByArchitecture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const getUserBitnessByArchitecture = (
userArchitecture: string,
userBitness: number
) => {
if (userArchitecture === 'arm' && userBitness === 64) {
return 'arm64';
}

return userBitness;
};
Loading