1
1
'use strict' ;
2
2
3
+ const util = require ( 'util' ) ;
3
4
const isWindows = process . platform === 'win32' ;
4
5
6
+ function assertPath ( path ) {
7
+ if ( typeof path !== 'string' ) {
8
+ throw new TypeError ( 'Path must be a string. Received ' +
9
+ util . inspect ( path ) ) ;
10
+ }
11
+ }
12
+
5
13
// resolves . and .. elements in a path array with directory names there
6
14
// must be no slashes or device names (c:\) in the array
7
15
// (so also no leading and trailing slashes - it does not distinguish
@@ -84,10 +92,10 @@ win32.resolve = function() {
84
92
}
85
93
}
86
94
87
- // Skip empty and invalid entries
88
- if ( typeof path !== 'string' ) {
89
- throw new TypeError ( 'Arguments to path.resolve must be strings' ) ;
90
- } else if ( ! path ) {
95
+ assertPath ( path ) ;
96
+
97
+ // Skip empty entries
98
+ if ( path === '' ) {
91
99
continue ;
92
100
}
93
101
@@ -137,6 +145,8 @@ win32.resolve = function() {
137
145
138
146
139
147
win32 . normalize = function ( path ) {
148
+ assertPath ( path ) ;
149
+
140
150
var result = splitDeviceRe . exec ( path ) ,
141
151
device = result [ 1 ] || '' ,
142
152
isUnc = device && device . charAt ( 1 ) !== ':' ,
@@ -165,6 +175,8 @@ win32.normalize = function(path) {
165
175
166
176
167
177
win32 . isAbsolute = function ( path ) {
178
+ assertPath ( path ) ;
179
+
168
180
var result = splitDeviceRe . exec ( path ) ,
169
181
device = result [ 1 ] || '' ,
170
182
isUnc = ! ! device && device . charAt ( 1 ) !== ':' ;
@@ -210,6 +222,9 @@ win32.join = function() {
210
222
// to = 'C:\\orandea\\impl\\bbb'
211
223
// The output of the function should be: '..\\..\\impl\\bbb'
212
224
win32 . relative = function ( from , to ) {
225
+ assertPath ( from ) ;
226
+ assertPath ( to ) ;
227
+
213
228
from = win32 . resolve ( from ) ;
214
229
to = win32 . resolve ( to ) ;
215
230
@@ -287,6 +302,8 @@ win32._makeLong = function(path) {
287
302
288
303
289
304
win32 . dirname = function ( path ) {
305
+ assertPath ( path ) ;
306
+
290
307
var result = win32SplitPath ( path ) ,
291
308
root = result [ 0 ] ,
292
309
dir = result [ 1 ] ;
@@ -306,6 +323,11 @@ win32.dirname = function(path) {
306
323
307
324
308
325
win32 . basename = function ( path , ext ) {
326
+ assertPath ( path ) ;
327
+
328
+ if ( ext !== undefined && typeof ext !== 'string' )
329
+ throw new TypeError ( 'ext must be a string' ) ;
330
+
309
331
var f = win32SplitPath ( path ) [ 2 ] ;
310
332
// TODO: make this comparison case-insensitive on windows?
311
333
if ( ext && f . substr ( - 1 * ext . length ) === ext ) {
@@ -316,6 +338,7 @@ win32.basename = function(path, ext) {
316
338
317
339
318
340
win32 . extname = function ( path ) {
341
+ assertPath ( path ) ;
319
342
return win32SplitPath ( path ) [ 3 ] ;
320
343
} ;
321
344
@@ -351,11 +374,8 @@ win32.format = function(pathObject) {
351
374
352
375
353
376
win32 . parse = function ( pathString ) {
354
- if ( typeof pathString !== 'string' ) {
355
- throw new TypeError (
356
- "Parameter 'pathString' must be a string, not " + typeof pathString
357
- ) ;
358
- }
377
+ assertPath ( pathString ) ;
378
+
359
379
var allParts = win32SplitPath ( pathString ) ;
360
380
if ( ! allParts || allParts . length !== 4 ) {
361
381
throw new TypeError ( "Invalid path '" + pathString + "'" ) ;
@@ -395,10 +415,10 @@ posix.resolve = function() {
395
415
for ( var i = arguments . length - 1 ; i >= - 1 && ! resolvedAbsolute ; i -- ) {
396
416
var path = ( i >= 0 ) ? arguments [ i ] : process . cwd ( ) ;
397
417
398
- // Skip empty and invalid entries
399
- if ( typeof path !== 'string' ) {
400
- throw new TypeError ( 'Arguments to path.resolve must be strings' ) ;
401
- } else if ( ! path ) {
418
+ assertPath ( path ) ;
419
+
420
+ // Skip empty entries
421
+ if ( path === '' ) {
402
422
continue ;
403
423
}
404
424
@@ -419,6 +439,8 @@ posix.resolve = function() {
419
439
// path.normalize(path)
420
440
// posix version
421
441
posix . normalize = function ( path ) {
442
+ assertPath ( path ) ;
443
+
422
444
var isAbsolute = posix . isAbsolute ( path ) ,
423
445
trailingSlash = path . substr ( - 1 ) === '/' ;
424
446
@@ -437,6 +459,7 @@ posix.normalize = function(path) {
437
459
438
460
// posix version
439
461
posix . isAbsolute = function ( path ) {
462
+ assertPath ( path ) ;
440
463
return path . charAt ( 0 ) === '/' ;
441
464
} ;
442
465
@@ -463,6 +486,9 @@ posix.join = function() {
463
486
// path.relative(from, to)
464
487
// posix version
465
488
posix . relative = function ( from , to ) {
489
+ assertPath ( from ) ;
490
+ assertPath ( to ) ;
491
+
466
492
from = posix . resolve ( from ) . substr ( 1 ) ;
467
493
to = posix . resolve ( to ) . substr ( 1 ) ;
468
494
@@ -510,6 +536,8 @@ posix._makeLong = function(path) {
510
536
511
537
512
538
posix . dirname = function ( path ) {
539
+ assertPath ( path ) ;
540
+
513
541
var result = posixSplitPath ( path ) ,
514
542
root = result [ 0 ] ,
515
543
dir = result [ 1 ] ;
@@ -529,8 +557,13 @@ posix.dirname = function(path) {
529
557
530
558
531
559
posix . basename = function ( path , ext ) {
560
+ assertPath ( path ) ;
561
+
562
+ if ( ext !== undefined && typeof ext !== 'string' )
563
+ throw new TypeError ( 'ext must be a string' ) ;
564
+
532
565
var f = posixSplitPath ( path ) [ 2 ] ;
533
- // TODO: make this comparison case-insensitive on windows?
566
+
534
567
if ( ext && f . substr ( - 1 * ext . length ) === ext ) {
535
568
f = f . substr ( 0 , f . length - ext . length ) ;
536
569
}
@@ -539,6 +572,7 @@ posix.basename = function(path, ext) {
539
572
540
573
541
574
posix . extname = function ( path ) {
575
+ assertPath ( path ) ;
542
576
return posixSplitPath ( path ) [ 3 ] ;
543
577
} ;
544
578
@@ -566,11 +600,8 @@ posix.format = function(pathObject) {
566
600
567
601
568
602
posix . parse = function ( pathString ) {
569
- if ( typeof pathString !== 'string' ) {
570
- throw new TypeError (
571
- "Parameter 'pathString' must be a string, not " + typeof pathString
572
- ) ;
573
- }
603
+ assertPath ( pathString ) ;
604
+
574
605
var allParts = posixSplitPath ( pathString ) ;
575
606
if ( ! allParts || allParts . length !== 4 ) {
576
607
throw new TypeError ( "Invalid path '" + pathString + "'" ) ;
0 commit comments