@@ -29,13 +29,30 @@ const url = require('url');
2929
3030 // Missing server:
3131 assert . throws ( ( ) => url . pathToFileURL ( '\\\\\\no-server' ) , {
32- code : 'ERR_INVALID_ARG_VALUE'
32+ code : 'ERR_INVALID_ARG_VALUE' ,
3333 } ) ;
3434
3535 // Missing share or resource:
3636 assert . throws ( ( ) => url . pathToFileURL ( '\\\\host' ) , {
37- code : 'ERR_INVALID_ARG_VALUE'
37+ code : 'ERR_INVALID_ARG_VALUE' ,
3838 } ) ;
39+
40+ // Regression test for direct String.prototype.startsWith call
41+ assert . throws ( ( ) => url . pathToFileURL ( [
42+ '\\\\' ,
43+ { [ Symbol . toPrimitive ] : ( ) => 'blep\\blop' } ,
44+ ] ) , {
45+ code : 'ERR_INVALID_ARG_TYPE' ,
46+ } ) ;
47+ assert . throws ( ( ) => url . pathToFileURL ( [ '\\\\' , 'blep\\blop' ] ) , {
48+ code : 'ERR_INVALID_ARG_TYPE' ,
49+ } ) ;
50+ assert . throws ( ( ) => url . pathToFileURL ( {
51+ [ Symbol . toPrimitive ] : ( ) => '\\\\blep\\blop' ,
52+ } ) , {
53+ code : 'ERR_INVALID_ARG_TYPE' ,
54+ } ) ;
55+
3956 } else {
4057 // UNC paths on posix are considered a single path that has backslashes:
4158 const fileURL = url . pathToFileURL ( '\\\\nas\\share\\path.txt' ) . href ;
@@ -144,3 +161,19 @@ const url = require('url');
144161 assert . strictEqual ( actual , expected ) ;
145162 }
146163}
164+
165+ // Test for non-string parameter
166+ {
167+ for ( const badPath of [
168+ undefined , null , true , 42 , 42n , Symbol ( '42' ) , NaN , { } , [ ] , ( ) => { } ,
169+ Promise . resolve ( 'foo' ) ,
170+ new Date ( ) ,
171+ new String ( 'notPrimitive' ) ,
172+ { toString ( ) { return 'amObject' ; } } ,
173+ { [ Symbol . toPrimitive ] : ( hint ) => 'amObject' } ,
174+ ] ) {
175+ assert . throws ( ( ) => url . pathToFileURL ( badPath ) , {
176+ code : 'ERR_INVALID_ARG_TYPE' ,
177+ } ) ;
178+ }
179+ }
0 commit comments