-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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 all 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,66 @@ | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import { useDetectOS } from '../useDetectOS'; | ||
|
||
const mockNavigator = { | ||
userAgent: | ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36', | ||
userAgentData: { | ||
getHighEntropyValues: jest.fn().mockResolvedValue({ bitness: '64' }), | ||
}, | ||
}; | ||
|
||
const originalNavigator = global.navigator; | ||
|
||
describe('useDetectOS', () => { | ||
afterEach(() => { | ||
// Reset the navigator global to the original value | ||
Object.defineProperty(global, 'navigator', { | ||
value: originalNavigator, | ||
writable: true, | ||
}); | ||
}); | ||
|
||
it('should detect the user OS and bitness', async () => { | ||
Object.defineProperty(global, 'navigator', { | ||
value: mockNavigator, | ||
// Allow us to change the value of navigator for the other tests | ||
writable: true, | ||
FHachez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
const { result } = renderHook(() => useDetectOS()); | ||
|
||
await waitFor(() => { | ||
expect(result.current.userOS).toBe('WIN'); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.getDownloadLink('v18.16.0')).toBe( | ||
'https://nodejs.org/dist/v18.16.0/node-v18.16.0-x64.msi' | ||
ovflowd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
}); | ||
|
||
expect( | ||
mockNavigator.userAgentData.getHighEntropyValues | ||
).toHaveBeenCalledWith(['bitness']); | ||
}); | ||
|
||
it('should return the default url if global.navigator does not exist', async () => { | ||
Object.defineProperty(global, 'navigator', { | ||
value: undefined, | ||
// Allow us to change the value of navigator for the other tests | ||
writable: true, | ||
}); | ||
|
||
const { result } = renderHook(() => useDetectOS()); | ||
|
||
await waitFor(() => { | ||
expect(result.current.userOS).toBe('OTHER'); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.getDownloadLink('v18.16.0')).toBe( | ||
'https://nodejs.org/dist/v18.16.0/node-v18.16.0.tar.gz' | ||
); | ||
}); | ||
}); | ||
}); |
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,37 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { detectOS } from '../util/detectOS'; | ||
import { downloadUrlByOS } from '../util/downloadUrlByOS'; | ||
|
||
import type { UserOS } from '../types/userOS'; | ||
|
||
export const useDetectOS = () => { | ||
const [userOS, setUserOS] = useState<UserOS>('OTHER'); | ||
const [bitness, setBitness] = useState(''); | ||
|
||
useEffect(() => { | ||
setUserOS(detectOS()); | ||
|
||
// 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') { | ||
// @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({ | ||
userAgent: navigator?.userAgent, | ||
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 @@ | ||
export type UserOS = 'MAC' | 'WIN' | 'LINUX' | 'OTHER'; |
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 @@ | ||
import { detectOsInUserAgent } from '../detectOS'; | ||
|
||
describe('detectOsInUserAgent', () => { | ||
it.each([ | ||
[ | ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246', | ||
'WIN', | ||
], | ||
[ | ||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36', | ||
'MAC', | ||
], | ||
[ | ||
'Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36', | ||
'OTHER', | ||
], | ||
[ | ||
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1', | ||
'LINUX', | ||
], | ||
[ | ||
'Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.3 Mobile/15E148 Safari/604.1', | ||
'MAC', | ||
], | ||
['', 'OTHER'], | ||
['OTHERAgent/1.0', 'OTHER'], | ||
[undefined, 'OTHER'], | ||
])('detectOsInUserAgent(%s) returns %s', (os, expected) => { | ||
expect(detectOsInUserAgent(os)).toBe(expected); | ||
}); | ||
}); |
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,72 @@ | ||
import { downloadUrlByOS } from '../downloadUrlByOS'; | ||
|
||
const version = 'v18.16.0'; | ||
describe('downloadUrlByOS', () => { | ||
it('returns the correct download URL for Mac', () => { | ||
const userAgent = | ||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9'; | ||
const userOS = 'MAC'; | ||
const expectedUrl = 'https://nodejs.org/dist/v18.16.0/node-v18.16.0.pkg'; | ||
|
||
expect(downloadUrlByOS({ userAgent, userOS, version })).toBe(expectedUrl); | ||
}); | ||
|
||
it('returns the correct download URL for Windows (32-bit)', () => { | ||
const userAgent = | ||
'Mozilla/5.0 (Windows NT 10.0; Win32; x86) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36'; | ||
const userOS = 'WIN'; | ||
const bitness = '32'; | ||
const expectedUrl = | ||
'https://nodejs.org/dist/v18.16.0/node-v18.16.0-x86.msi'; | ||
|
||
expect(downloadUrlByOS({ userAgent, userOS, version, bitness })).toBe( | ||
expectedUrl | ||
); | ||
}); | ||
|
||
it('returns the correct download URL for Windows (64-bit) because the userAgent contains Win64', () => { | ||
const userAgent = | ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'; | ||
const userOS = 'WIN'; | ||
const bitness = ''; | ||
const expectedUrl = | ||
'https://nodejs.org/dist/v18.16.0/node-v18.16.0-x64.msi'; | ||
|
||
expect(downloadUrlByOS({ userAgent, userOS, version, bitness })).toBe( | ||
expectedUrl | ||
); | ||
}); | ||
|
||
it('returns the correct download URL for Windows (64-bit) because the userAgent contains WOW64', () => { | ||
const userAgent = | ||
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36'; | ||
const userOS = 'WIN'; | ||
const bitness = ''; | ||
const expectedUrl = | ||
'https://nodejs.org/dist/v18.16.0/node-v18.16.0-x64.msi'; | ||
|
||
expect(downloadUrlByOS({ userAgent, userOS, version, bitness })).toBe( | ||
expectedUrl | ||
); | ||
}); | ||
|
||
it('returns the correct download URL for Windows (64-bit) because bitness = 64', () => { | ||
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'; | ||
const userOS = 'WIN'; | ||
const bitness = '64'; | ||
const expectedUrl = | ||
'https://nodejs.org/dist/v18.16.0/node-v18.16.0-x64.msi'; | ||
|
||
expect(downloadUrlByOS({ userAgent, userOS, version, bitness })).toBe( | ||
expectedUrl | ||
); | ||
}); | ||
|
||
it('returns the default download URL for other operating systems', () => { | ||
const userAgent = 'Mozilla/5.0 (Linux; Android 11; SM-G975U1)'; | ||
const userOS = 'OTHER'; | ||
const expectedUrl = 'https://nodejs.org/dist/v18.16.0/node-v18.16.0.tar.gz'; | ||
|
||
expect(downloadUrlByOS({ userAgent, userOS, version })).toBe(expectedUrl); | ||
}); | ||
}); |
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,19 @@ | ||
import type { UserOS } from '../types/userOS'; | ||
|
||
export const detectOsInUserAgent = (userAgent: string | undefined): UserOS => { | ||
const osMatch = userAgent?.match(/(Win|Mac|Linux)/); | ||
switch (osMatch && osMatch[1]) { | ||
case 'Win': | ||
return 'WIN'; | ||
case 'Mac': | ||
return 'MAC'; | ||
case 'Linux': | ||
return 'LINUX'; | ||
default: | ||
return 'OTHER'; | ||
} | ||
}; | ||
|
||
// Since `navigator.appVersion` is deprecated, we use the `userAgent`` | ||
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/appVersion | ||
export const detectOS = (): UserOS => detectOsInUserAgent(navigator?.userAgent); |
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,30 @@ | ||
import type { UserOS } from '../types/userOS'; | ||
|
||
type DownloadUrlByOS = { | ||
userAgent?: string | undefined; | ||
userOS: UserOS; | ||
version: string; | ||
bitness?: string; | ||
}; | ||
|
||
export const downloadUrlByOS = ({ | ||
userAgent, | ||
userOS, | ||
version, | ||
bitness, | ||
}: DownloadUrlByOS): string => { | ||
const baseURL = `https://nodejs.org/dist/${version}`; | ||
const is64Bit = | ||
bitness === '64' || | ||
userAgent?.includes('WOW64') || | ||
userAgent?.includes('Win64'); | ||
|
||
switch (userOS) { | ||
case 'MAC': | ||
return `${baseURL}/node-${version}.pkg`; | ||
case 'WIN': | ||
return `${baseURL}/node-${version}-x${is64Bit ? 64 : 86}.msi`; | ||
default: | ||
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.