Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion lib/internal/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {

const {
ERR_ILLEGAL_CONSTRUCTOR,
ERR_INVALID_THIS,
} = require('internal/errors').codes;

const {
Expand Down Expand Up @@ -79,7 +80,8 @@ function getNavigatorPlatform(arch, platform) {
}

class Navigator {
// Private properties are used to avoid brand validations.
// Private properties are used to avoid brand validations: reading a private
// field from a non-Navigator receiver throws a TypeError on its own.
#availableParallelism;
#locks;
#userAgent;
Expand Down Expand Up @@ -113,6 +115,10 @@ class Navigator {
* @returns {string}
*/
get language() {
// `language` does not read a private field, so brand-check explicitly
// to keep parity with the other getters when called on a non-Navigator
// receiver (e.g. `Navigator.prototype.language`).
if (!(#languages in this)) throw new ERR_INVALID_THIS('Navigator');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't not throwing here go against the specs?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing this.#attribute already throws

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anonrig you can try it yourself: node -p Navigator.prototype.language exits with code 0 (while accessing the other properties e.g. node -p Navigator.prototype.languages has the expected TypeError)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!(#languages in this)) throw new ERR_INVALID_THIS('Navigator');
this.#languages; // eslint-disable-line no-unused-expressions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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';
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,19 @@ Object.defineProperty(navigator, 'language', { value: 'for-testing' });
assert.strictEqual(navigator.language, 'for-testing');
assert.strictEqual(navigator.languages.length, 1);
assert.strictEqual(navigator.languages[0] !== 'for-testing', true);

{
const { Navigator } = globalThis;
for (const name of Object.keys(Navigator.prototype)) {
assert.throws(
() => Navigator.prototype[name],
{ name: 'TypeError' },
`expected TypeError when reading ${name} on Navigator.prototype`,
);
assert.throws(
() => Reflect.get(Navigator.prototype, name, {}),
{ name: 'TypeError' },
`expected TypeError when reading ${name} on a plain object`,
);
}
}
Loading