-
-
Notifications
You must be signed in to change notification settings - Fork 35.8k
lib: brand-check Navigator getters to throw ERR_INVALID_THIS #63601
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
Changes from 1 commit
a8d88e8
57fda3b
a35e81b
d0a014b
19c4154
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,7 @@ const { | |||||
|
|
||||||
| const { | ||||||
| ERR_ILLEGAL_CONSTRUCTOR, | ||||||
| ERR_INVALID_THIS, | ||||||
| } = require('internal/errors').codes; | ||||||
|
|
||||||
| const { | ||||||
|
|
@@ -79,7 +80,9 @@ function getNavigatorPlatform(arch, platform) { | |||||
| } | ||||||
|
|
||||||
| class Navigator { | ||||||
| // Private properties are used to avoid brand validations. | ||||||
| // Private properties also serve as a brand check via the `#field in obj` | ||||||
| // operator in each getter, so callers get a proper TypeError instead of | ||||||
| // an internal "private field" error when invoked on a non-Navigator `this`. | ||||||
| #availableParallelism; | ||||||
| #locks; | ||||||
| #userAgent; | ||||||
|
|
@@ -97,6 +100,7 @@ class Navigator { | |||||
| * @returns {number} | ||||||
| */ | ||||||
| get hardwareConcurrency() { | ||||||
| if (!(#availableParallelism in this)) throw new ERR_INVALID_THIS('Navigator'); | ||||||
| this.#availableParallelism ??= getAvailableParallelism(); | ||||||
| return this.#availableParallelism; | ||||||
| } | ||||||
|
|
@@ -105,6 +109,7 @@ class Navigator { | |||||
| * @returns {LockManager} | ||||||
| */ | ||||||
| get locks() { | ||||||
| if (!(#locks in this)) throw new ERR_INVALID_THIS('Navigator'); | ||||||
| this.#locks ??= require('internal/locks').locks; | ||||||
| return this.#locks; | ||||||
| } | ||||||
|
|
@@ -113,6 +118,7 @@ class Navigator { | |||||
| * @returns {string} | ||||||
| */ | ||||||
| get language() { | ||||||
| if (!(#languages in this)) throw new ERR_INVALID_THIS('Navigator'); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't do this. We explicitly avoided doing this because of performance in the past. We went through deprecation cycles multiple times because of this.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't not throwing here go against the specs?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accessing this.#attribute already throws
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This particular getter doesn't use private field anymore so currently it doesn't throw at all.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @anonrig you can try it yourself:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aduh95 your suggestion is the correct approach |
||||||
| // The default locale might be changed dynamically, so always invoke the | ||||||
| // binding. | ||||||
| return getDefaultLocale() || 'en-US'; | ||||||
|
|
@@ -122,6 +128,7 @@ class Navigator { | |||||
| * @returns {Array<string>} | ||||||
| */ | ||||||
| get languages() { | ||||||
| if (!(#languages in this)) throw new ERR_INVALID_THIS('Navigator'); | ||||||
| this.#languages ??= ObjectFreeze([this.language]); | ||||||
| return this.#languages; | ||||||
| } | ||||||
|
|
@@ -130,6 +137,7 @@ class Navigator { | |||||
| * @returns {string} | ||||||
| */ | ||||||
| get userAgent() { | ||||||
| if (!(#userAgent in this)) throw new ERR_INVALID_THIS('Navigator'); | ||||||
| this.#userAgent ??= `Node.js/${StringPrototypeSlice(nodeVersion, 1, StringPrototypeIndexOf(nodeVersion, '.'))}`; | ||||||
| return this.#userAgent; | ||||||
| } | ||||||
|
|
@@ -138,6 +146,7 @@ class Navigator { | |||||
| * @returns {string} | ||||||
| */ | ||||||
| get platform() { | ||||||
| if (!(#platform in this)) throw new ERR_INVALID_THIS('Navigator'); | ||||||
| this.#platform ??= getNavigatorPlatform(arch, platform); | ||||||
| return this.#platform; | ||||||
| } | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.