-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
lib: add the Navigator object #39581
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
Closed
Closed
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -361,5 +361,6 @@ module.exports = { | |
btoa: 'readable', | ||
atob: 'readable', | ||
performance: 'readable', | ||
navigator: 'readable', | ||
}, | ||
}; |
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
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
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,67 @@ | ||
'use strict'; | ||
|
||
const { | ||
ObjectDefineProperties, | ||
ObjectSetPrototypeOf, | ||
MathClz32, | ||
} = primordials; | ||
|
||
const { | ||
codes: { | ||
ERR_ILLEGAL_CONSTRUCTOR, | ||
} | ||
} = require('internal/errors'); | ||
|
||
const { | ||
getCPUs, | ||
getOSInformation, | ||
getTotalMem, | ||
} = internalBinding('os'); | ||
|
||
class Navigator { | ||
constructor() { | ||
throw new ERR_ILLEGAL_CONSTRUCTOR(); | ||
} | ||
} | ||
|
||
class InternalNavigator {} | ||
InternalNavigator.prototype.constructor = Navigator.prototype.constructor; | ||
ObjectSetPrototypeOf(InternalNavigator.prototype, Navigator.prototype); | ||
|
||
function getDeviceMemory() { | ||
const mem = getTotalMem() / 1024 / 1024; | ||
if (mem <= 0.25 * 1024) return 0.25; | ||
if (mem >= 8 * 1024) return 8; | ||
const lowerBound = 1 << 31 - MathClz32(mem - 1); | ||
const upperBound = lowerBound * 2; | ||
return mem - lowerBound <= upperBound - mem ? | ||
lowerBound / 1024 : | ||
upperBound / 1024; | ||
} | ||
|
||
function getPlatform() { | ||
if (process.platform === 'win32') return 'Win32'; | ||
if (process.platform === 'android') return 'Android'; | ||
return getOSInformation()[0]; | ||
} | ||
|
||
const cpuData = getCPUs(); | ||
ObjectDefineProperties(Navigator.prototype, { | ||
deviceMemory: { | ||
configurable: true, | ||
enumerable: true, | ||
value: getDeviceMemory(), | ||
}, | ||
hardwareConcurrency: { | ||
configurable: true, | ||
enumerable: true, | ||
value: cpuData ? cpuData.length / 7 : 1, | ||
}, | ||
platform: { | ||
configurable: true, | ||
enumerable: true, | ||
value: getPlatform(), | ||
}, | ||
}); | ||
|
||
module.exports = new InternalNavigator(); |
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
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
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
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,3 @@ | ||
spec: https://w3c.github.io/device-memory/ | ||
suggested_reviewers: | ||
- npm1 |
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,8 @@ | ||
test(function() { | ||
assert_equals(typeof navigator.deviceMemory, "number", | ||
"navigator.deviceMemory returns a number"); | ||
assert_true(navigator.deviceMemory >= 0, | ||
"navigator.deviceMemory returns a positive value"); | ||
assert_true([0.25, 0.5, 1, 2, 4, 8].includes(navigator.deviceMemory), | ||
"navigator.deviceMemory returns a power of 2 between 0.25 and 8"); | ||
}, "navigator.deviceMemory is a positive number, a power of 2, between 0.25 and 8"); |
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 @@ | ||
// META: script=/resources/WebIDLParser.js | ||
// META: script=/resources/idlharness.js | ||
// META: timeout=long | ||
|
||
// https://w3c.github.io/device-memory/ | ||
|
||
"use strict"; | ||
|
||
idl_test( | ||
['device-memory'], | ||
['html'], | ||
async idl_array => { | ||
if (self.GLOBAL.isWorker()) { | ||
idl_array.add_objects({ WorkerNavigator: ['navigator'] }); | ||
} else { | ||
idl_array.add_objects({ Navigator: ['navigator'] }); | ||
} | ||
} | ||
); |
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,14 @@ | ||
// GENERATED CONTENT - DO NOT EDIT | ||
// Content was automatically extracted by Reffy into webref | ||
// (https://github.com/w3c/webref) | ||
// Source: Device Memory 1 (https://w3c.github.io/device-memory/) | ||
|
||
[ | ||
SecureContext, | ||
Exposed=(Window,Worker) | ||
] interface mixin NavigatorDeviceMemory { | ||
readonly attribute double deviceMemory; | ||
}; | ||
|
||
Navigator includes NavigatorDeviceMemory; | ||
WorkerNavigator includes NavigatorDeviceMemory; |
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
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
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,7 @@ | ||
'use strict'; | ||
/* eslint-disable no-global-assign */ | ||
require('../common'); | ||
const { notStrictEqual } = require('assert'); | ||
|
||
performance = undefined; | ||
notStrictEqual(globalThis.performance, undefined); |
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,21 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const { cpus } = require('os'); | ||
const { platform } = require('process'); | ||
|
||
assert.ok(navigator.deviceMemory >= 0.25 && navigator.deviceMemory <= 8); | ||
|
||
assert.strictEqual(navigator.platform, { | ||
aix: 'AIX', | ||
android: 'Android', | ||
darwin: 'Darwin', | ||
freebsd: 'FreeBSD', | ||
linux: 'Linux', | ||
openbsd: 'OpenBSD', | ||
sunos: 'SunOS', | ||
win32: 'Win32', | ||
}[platform]); | ||
|
||
assert.ok(navigator.hardwareConcurrency >= 1); | ||
assert.strictEqual(navigator.hardwareConcurrency, cpus().length); |
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,5 @@ | ||
{ | ||
"idlharness.https.any.js": { | ||
"skip": "idlharness cannot recognize Node.js environment" | ||
} | ||
} |
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,7 @@ | ||
'use strict'; | ||
require('../common'); | ||
const { WPTRunner } = require('../common/wpt'); | ||
|
||
const runner = new WPTRunner('device-memory'); | ||
|
||
runner.runJsTests(); |
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.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The specification does not indicate any deprecation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm 😕
Confused... Seems like MDN and TypeScript have incorrectly marked it as deprecated?
https://stackoverflow.com/questions/38506517/deprecated-javascript-os-detection-techniques
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jimmywarting MDN takes the term "deprecated" to mean "use of this API is discouraged" - not "this API is on the way to removal from the spec". Under MDN's meaning of the term this marking as deprecated is correct because most of the properties/methods return incorrect and/or inconsistent information. There are more reliable ways to get the information.
Of course it would be good if the reasoning was provided along with the tagging.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that around https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-oscpu, the spec adds this big red warning about the entire set of
NavigatorID
members (includingplatform
):That — along with the fact that most of the members are useless because they’re required by the spec to either not ever return anything useful or to not reliably/deterministically return anything useful — is why we have it flagged as deprecated in MDN: As a “don’t use this” warning to developers, so we don’t end up causing developers to mistakenly try to use any of it.
For example, in the case of
platform
, the spec requirements are:So, given that browsers can choose to always return the empty string for it, that means developers cannot and should not write code which assumes it returns "
MacIntel
", "Win32
", "FreeBSD i386
", "WebTV OS
" or anything else actually useful. So it’s flagged as deprecated in MDN to help developers avoid making that mistake.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless the spec have marked it as deprecated or removed it then i think you should use the term "Legacy" instead of "Deprecated" like NodeJS docs dose or something like that.
Think "Deprecated" is a too strong word for what it actually means.
Say something in terms of: "Legacy discouraged fingerprinting, use feature detection instead"