-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Migrate detect os hook #5322
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
Merged
ovflowd
merged 18 commits into
nodejs:major/website-redesign
from
FHachez:migrate_dected_os_hook
Apr 24, 2023
Merged
Migrate detect os hook #5322
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a7610db
feat(detectOs): copy files from node.dev
FHachez d8926e6
feat(detectOs): detect based on the userAgent or appVersion
FHachez 93f7b80
test(detectOs): add userAgent tests
FHachez 7c56950
fix(detectOs): use arrow function
FHachez e70eb07
refactor(detectOs): create a type userOS
FHachez d60552b
feat(downloadUrlByOS): use a switch
FHachez 9dde224
test(downloadUrlByOs): create the tests
FHachez 84f456b
test(downloadUrlByOs): use an existing version to test the links
FHachez 137ae18
test(useDetectOS): create test for the hook
FHachez 010014c
refactor(downloadUrlByOS): extract bitness into constant
FHachez f6dd69a
Merge branch 'major/website-redesign' into migrate_dected_os_hook
e8a43ac
fix(useDetectOS): case sensitivity issue
FHachez cdf2186
fix(useDetectOS): case sensitivity issue
FHachez b22e0fa
fix(useDetectOS): tackle PR comments
FHachez fbc26b2
refactor(useDetectOS): use OTHER os instead of UNKNOWN
FHachez e8bc600
fix(useDetectOS): tackle PR comments
FHachez b2c6ccb
test(useDetectOS): test when the navigator doesn't exist
FHachez 6592300
Merge branch 'major/website-redesign' into migrate_dected_os_hook
aymen94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| 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); | ||
|
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') { | ||
|
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), | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
|
FHachez marked this conversation as resolved.
Outdated
|
||
| MAC = 'MAC', | ||
| WIN = 'WIN', | ||
| UNIX = 'UNIX', | ||
| LINUX = 'LINUX', | ||
| MOBILE = 'MOBILE', | ||
| UNKNOWN = 'UNKNOWN', | ||
| } | ||
|
|
||
| export function detectOS(): UserOS { | ||
|
FHachez marked this conversation as resolved.
Outdated
|
||
| return ( | ||
|
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) || | ||
|
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) { | ||
|
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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( | ||
|
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) { | ||
|
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' || | ||
|
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`; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.