Skip to content

Commit

Permalink
feat: call userAgentData to retrieve CPU architecture in order to hav…
Browse files Browse the repository at this point in the history
…e better defaults (Chrome-only)
  • Loading branch information
apostolos committed Nov 10, 2023
1 parent 8028913 commit 22df765
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
38 changes: 34 additions & 4 deletions client/routes/customize/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,46 @@ import lwjgl_332 from './versions/3.3.2';
import lwjgl_333 from './versions/3.3.3';
import lwjgl_nightly from './versions/nightly';
import { Native, BuildType, Mode, Version, Language, Preset, NATIVE_ALL, Addon } from './types';
import { getUserAgentData } from '~/services/userAgentData';

// Types
import type { BuildStore, BuildStoreSnapshot, BuildBindings, Binding } from './types';
type BuildBindingsReducer = (opt: BuildBindings) => BuildBindings;

export function getDefaultPlatform(): Native {
if (navigator.platform.indexOf('Mac') > -1 || navigator.platform.indexOf('iP') > -1) {
return Native.MacOS;
} else if (navigator.platform.indexOf('Linux') > -1) {
return Native.Linux;
let uaData = getUserAgentData();
let platform: string | null = '';
let arch: string | null = null;

if (uaData != null) {
platform = uaData.platform;
arch = uaData.architecture;
} else if (navigator.platform) {
platform = navigator.platform;
}

if (platform !== null) {
if (platform.toLowerCase().startsWith('mac') || platform.toLowerCase().startsWith('ip')) {
if (arch !== null && arch.indexOf('arm') > -1) {
return Native.MacOSARM64;
}
return Native.MacOS;
} else if (platform.toLowerCase().includes('linux')) {
if (arch !== null) {
if (arch.indexOf('arm') > -1) {
return Native.LinuxARM64;
} else if (arch.indexOf('ppc') > -1) {
return Native.LinuxPPC64LE;
} else if (arch.indexOf('risc') > -1) {
return Native.LinuxRISCV64;
}
}
return Native.Linux;
}
}

if (arch !== null && arch.indexOf('arm') > -1) {
return Native.WindowsARM64;
}

return Native.Windows;
Expand Down
5 changes: 4 additions & 1 deletion client/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, lazy } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import { PageError } from './PageError';
import { setUserAgentData } from '~/services/userAgentData';
// import { sleep } from '../services/sleep';

// function $R(fn: () => Promise<{ default: React.ComponentType<any> }>) {
Expand Down Expand Up @@ -39,7 +40,9 @@ function $R(fn: () => Promise<{ default: React.ComponentType<any> }>) {
export const Home = $R(() => import(/* webpackChunkName: "route-home" */ './home'));
export const Guide = $R(() => import(/* webpackChunkName: "route-guide" */ './guide'));
export const Download = $R(() => import(/* webpackChunkName: "route-download" */ './download'));
export const Customize = $R(() => import(/* webpackChunkName: "route-customize" */ './customize'));
export const Customize = $R(() =>
setUserAgentData().then(() => import(/* webpackChunkName: "route-customize" */ './customize')),
);
export const Browse = $R(() => import(/* webpackChunkName: "route-browse" */ './browse'));
export const Source = $R(() => import(/* webpackChunkName: "route-source" */ './source'));
export const License = $R(() => import(/* webpackChunkName: "route-license" */ './license'));
Expand Down
19 changes: 19 additions & 0 deletions client/services/userAgentData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let userAgentData: any | null | undefined = undefined;

export async function setUserAgentData(): Promise<void> {
//@ts-expect-error
if (userAgentData === undefined && navigator.userAgentData) {
try {
//@ts-expect-error
userAgentData = await navigator.userAgentData.getHighEntropyValues(['architecture']);
} catch (e) {
userAgentData = null;
// ignore
}
console.log(userAgentData);
}
}

export function getUserAgentData() {
return userAgentData;
}

0 comments on commit 22df765

Please sign in to comment.