Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 31 additions & 0 deletions hooks/useDetectOs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copied from https://github.com/nodejs/nodejs.dev/blob/main/src/hooks/useDetectOs.ts
Comment thread
FHachez marked this conversation as resolved.
Outdated
import { useEffect, useState } from 'react';
import { UserOS, detectOS } from '../util/detectOS';
import downloadUrlByOS from '../util/downloadUrlByOS';

export const useDetectOs = () => {
const [userOS, setUserOS] = useState<UserOS>(UserOS.UNKNOWN);
const [bitness, setBitness] = useState('');

useEffect(() => {
setUserOS(detectOS);
Comment thread
FHachez marked this conversation as resolved.
Outdated

// 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)
// @ts-expect-error no types for "userAgentData" because this API is experimental
if (typeof navigator.userAgentData?.getHighEntropyValues === 'function') {
Comment thread
FHachez marked this conversation as resolved.
Outdated
// @ts-expect-error no types for "userAgentData" because this API is experimental
navigator.userAgentData
.getHighEntropyValues(['bitness'])
.then((ua: { bitness: string }) => setBitness(ua.bitness))
.catch();
}
}, []);

return {
userOS,
getDownloadLink: (version: string) =>
downloadUrlByOS(userOS, version, bitness),
};
};
39 changes: 39 additions & 0 deletions util/detectOS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// From https://github.com/nodejs/nodejs.dev/blob/main/src/util/detectOS.ts
// eslint-disable-next-line no-shadow
export enum UserOS {
Comment thread
FHachez marked this conversation as resolved.
Outdated
MAC = 'MAC',
WIN = 'WIN',
UNIX = 'UNIX',
LINUX = 'LINUX',
MOBILE = 'MOBILE',
UNKNOWN = 'UNKNOWN',
}

export function detectOS(): UserOS {
Comment thread
FHachez marked this conversation as resolved.
Outdated
return (
Comment thread
FHachez marked this conversation as resolved.
Outdated
// Since `navigator.appVersion` is deprecated, we first try to use the `userAgent``
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/appVersion
detectOsInString(navigator.userAgent) ||
Comment thread
FHachez marked this conversation as resolved.
Outdated
detectOsInString(navigator.appVersion) ||
UserOS.UNKNOWN
);
}

function detectOsInString(userAgent: string): UserOS {
const osMatch = userAgent.match(/(Win|Mac|Linux|X11|Mobi)/i);
const os = (osMatch && osMatch[1]) || '';
switch (os) {
Comment thread
FHachez marked this conversation as resolved.
Outdated
case 'Win':
return UserOS.WIN;
case 'Mac':
return UserOS.MAC;
case 'Linux':
return UserOS.LINUX;
case 'X11':
return UserOS.UNIX;
case 'Mobi':
return UserOS.MOBILE;
default:
return UserOS.UNKNOWN;
}
}
32 changes: 32 additions & 0 deletions util/downloadUrlByOS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copied From https://github.com/nodejs/nodejs.dev/blob/main/src/util/downloadUrlByOS.ts
import { UserOS } from './detectOS';

export default function downloadUrlByOS(
Comment thread
FHachez marked this conversation as resolved.
Outdated
userOS: UserOS,
version: string,
bitness?: string
): string {
const baseURL = `https://nodejs.org/dist/${version}`;

if (userOS === UserOS.MOBILE) {
Comment thread
FHachez marked this conversation as resolved.
Outdated
return baseURL;
}

if (userOS === UserOS.MAC) {
return `${baseURL}/node-${version}.pkg`;
}

if (userOS === UserOS.WIN) {
if (
bitness === '64' ||
Comment thread
FHachez marked this conversation as resolved.
Outdated
navigator.appVersion.includes('WOW64') ||
navigator.appVersion.includes('Win64')
) {
return `${baseURL}/node-${version}-x64.msi`;
}

return `${baseURL}/node-${version}-x86.msi`;
}

return `${baseURL}/node-${version}.tar.gz`;
}