@@ -11,6 +11,7 @@ const {
1111
1212const {
1313 ERR_ILLEGAL_CONSTRUCTOR ,
14+ ERR_INVALID_THIS ,
1415} = require ( 'internal/errors' ) . codes ;
1516
1617const {
@@ -79,7 +80,9 @@ function getNavigatorPlatform(arch, platform) {
7980}
8081
8182class Navigator {
82- // Private properties are used to avoid brand validations.
83+ // Private properties also serve as a brand check via the `#field in obj`
84+ // operator in each getter, so callers get a proper TypeError instead of
85+ // an internal "private field" error when invoked on a non-Navigator `this`.
8386 #availableParallelism;
8487 #locks;
8588 #userAgent;
@@ -97,6 +100,7 @@ class Navigator {
97100 * @returns {number }
98101 */
99102 get hardwareConcurrency ( ) {
103+ if ( ! ( #availableParallelism in this ) ) throw new ERR_INVALID_THIS ( 'Navigator' ) ;
100104 this . #availableParallelism ??= getAvailableParallelism ( ) ;
101105 return this . #availableParallelism;
102106 }
@@ -105,6 +109,7 @@ class Navigator {
105109 * @returns {LockManager }
106110 */
107111 get locks ( ) {
112+ if ( ! ( #locks in this ) ) throw new ERR_INVALID_THIS ( 'Navigator' ) ;
108113 this . #locks ??= require ( 'internal/locks' ) . locks ;
109114 return this . #locks;
110115 }
@@ -113,6 +118,7 @@ class Navigator {
113118 * @returns {string }
114119 */
115120 get language ( ) {
121+ if ( ! ( #languages in this ) ) throw new ERR_INVALID_THIS ( 'Navigator' ) ;
116122 // The default locale might be changed dynamically, so always invoke the
117123 // binding.
118124 return getDefaultLocale ( ) || 'en-US' ;
@@ -122,6 +128,7 @@ class Navigator {
122128 * @returns {Array<string> }
123129 */
124130 get languages ( ) {
131+ if ( ! ( #languages in this ) ) throw new ERR_INVALID_THIS ( 'Navigator' ) ;
125132 this . #languages ??= ObjectFreeze ( [ this . language ] ) ;
126133 return this . #languages;
127134 }
@@ -130,6 +137,7 @@ class Navigator {
130137 * @returns {string }
131138 */
132139 get userAgent ( ) {
140+ if ( ! ( #userAgent in this ) ) throw new ERR_INVALID_THIS ( 'Navigator' ) ;
133141 this . #userAgent ??= `Node.js/${ StringPrototypeSlice ( nodeVersion , 1 , StringPrototypeIndexOf ( nodeVersion , '.' ) ) } ` ;
134142 return this . #userAgent;
135143 }
@@ -138,6 +146,7 @@ class Navigator {
138146 * @returns {string }
139147 */
140148 get platform ( ) {
149+ if ( ! ( #platform in this ) ) throw new ERR_INVALID_THIS ( 'Navigator' ) ;
141150 this . #platform ??= getNavigatorPlatform ( arch , platform ) ;
142151 return this . #platform;
143152 }
0 commit comments