@@ -105,6 +105,9 @@ function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, interna
105
105
}
106
106
const pjsonPath = fileURLToPath ( pjsonUrl ) ;
107
107
const double = RegExpPrototypeExec ( doubleSlashRegEx , isTarget ? target : request ) !== null ;
108
+ console . trace ( {
109
+ target, request
110
+ } )
108
111
process . emitWarning (
109
112
`Use of deprecated ${ double ? 'double slash' :
110
113
'leading or trailing slash matching' } resolving "${ target } " for module ` +
@@ -355,6 +358,7 @@ const patternRegEx = /\*/g;
355
358
* @param {boolean } internal - Whether the target is internal to the package.
356
359
* @param {boolean } isPathMap - Whether the target is a path map.
357
360
* @param {string[] } conditions - The import conditions.
361
+ * @param {boolean } mustBeInternalTarget - If target must be in the package boundary.
358
362
* @returns {URL } - The resolved URL object.
359
363
* @throws {ERR_INVALID_PACKAGE_TARGET } - If the target is invalid.
360
364
* @throws {ERR_INVALID_SUBPATH } - If the subpath is invalid.
@@ -369,14 +373,15 @@ function resolvePackageTargetString(
369
373
internal ,
370
374
isPathMap ,
371
375
conditions ,
376
+ mustBeInternalTarget = true ,
372
377
) {
373
378
374
379
if ( subpath !== '' && ! pattern && target [ target . length - 1 ] !== '/' ) {
375
380
throw invalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
376
381
}
377
382
378
- if ( ! StringPrototypeStartsWith ( target , './' ) ) {
379
- if ( internal && ! StringPrototypeStartsWith ( target , '../' ) &&
383
+ if ( ! StringPrototypeStartsWith ( target , './' ) && ! StringPrototypeStartsWith ( target , '../' ) ) {
384
+ if ( internal &&
380
385
! StringPrototypeStartsWith ( target , '/' ) ) {
381
386
// No need to convert target to string, since it's already presumed to be
382
387
if ( ! URLCanParse ( target ) ) {
@@ -390,8 +395,17 @@ function resolvePackageTargetString(
390
395
throw invalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
391
396
}
392
397
393
- if ( RegExpPrototypeExec ( invalidSegmentRegEx , StringPrototypeSlice ( target , 2 ) ) !== null ) {
394
- if ( RegExpPrototypeExec ( deprecatedInvalidSegmentRegEx , StringPrototypeSlice ( target , 2 ) ) === null ) {
398
+ // skip ../ and ./ prefixes when looking for invalid segments
399
+ const skipPrefix = target . length > 1 && target [ 0 ] === '.' ?
400
+ target [ 1 ] === '/' ?
401
+ 2 :
402
+ target . length > 2 && target [ 1 ] === '.' && target [ 2 ] === '/' ?
403
+ 3 :
404
+ 0
405
+ :
406
+ 0
407
+ if ( RegExpPrototypeExec ( invalidSegmentRegEx , StringPrototypeSlice ( target , skipPrefix ) ) !== null ) {
408
+ if ( RegExpPrototypeExec ( deprecatedInvalidSegmentRegEx , StringPrototypeSlice ( target , skipPrefix ) ) === null ) {
395
409
if ( ! isPathMap ) {
396
410
const request = pattern ?
397
411
StringPrototypeReplace ( match , '*' , ( ) => subpath ) :
@@ -410,7 +424,8 @@ function resolvePackageTargetString(
410
424
const resolvedPath = resolved . pathname ;
411
425
const packagePath = new URL ( '.' , packageJSONUrl ) . pathname ;
412
426
413
- if ( ! StringPrototypeStartsWith ( resolvedPath , packagePath ) ) {
427
+ // if (mustBeInternalTarget && !StringPrototypeStartsWith(resolvedPath, packagePath)) {
428
+ if ( mustBeInternalTarget && ! StringPrototypeStartsWith ( resolvedPath , packagePath ) ) {
414
429
throw invalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
415
430
}
416
431
@@ -461,14 +476,15 @@ function isArrayIndex(key) {
461
476
* @param {boolean } internal - Whether the package is internal.
462
477
* @param {boolean } isPathMap - Whether the package is a path map.
463
478
* @param {Set<string> } conditions - The conditions to match.
479
+ * @param {boolean } mustBeInternalTarget - If the target must be in the package boundary.
464
480
* @returns {URL | null | undefined } - The resolved target, or null if not found, or undefined if not resolvable.
465
481
*/
466
482
function resolvePackageTarget ( packageJSONUrl , target , subpath , packageSubpath ,
467
- base , pattern , internal , isPathMap , conditions ) {
483
+ base , pattern , internal , isPathMap , conditions , mustBeInternalTarget ) {
468
484
if ( typeof target === 'string' ) {
469
485
return resolvePackageTargetString (
470
486
target , subpath , packageSubpath , packageJSONUrl , base , pattern , internal ,
471
- isPathMap , conditions ) ;
487
+ isPathMap , conditions , mustBeInternalTarget ) ;
472
488
} else if ( ArrayIsArray ( target ) ) {
473
489
if ( target . length === 0 ) {
474
490
return null ;
@@ -481,7 +497,7 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
481
497
try {
482
498
resolveResult = resolvePackageTarget (
483
499
packageJSONUrl , targetItem , subpath , packageSubpath , base , pattern ,
484
- internal , isPathMap , conditions ) ;
500
+ internal , isPathMap , conditions , mustBeInternalTarget ) ;
485
501
} catch ( e ) {
486
502
lastException = e ;
487
503
if ( e . code === 'ERR_INVALID_PACKAGE_TARGET' ) {
@@ -518,7 +534,7 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
518
534
const conditionalTarget = target [ key ] ;
519
535
const resolveResult = resolvePackageTarget (
520
536
packageJSONUrl , conditionalTarget , subpath , packageSubpath , base ,
521
- pattern , internal , isPathMap , conditions ) ;
537
+ pattern , internal , isPathMap , conditions , mustBeInternalTarget ) ;
522
538
if ( resolveResult === undefined ) { continue ; }
523
539
return resolveResult ;
524
540
}
@@ -583,6 +599,7 @@ function packageExportsResolve(
583
599
const resolveResult = resolvePackageTarget (
584
600
packageJSONUrl , target , '' , packageSubpath , base , false , false , false ,
585
601
conditions ,
602
+ true ,
586
603
) ;
587
604
588
605
if ( resolveResult == null ) {
@@ -635,7 +652,7 @@ function packageExportsResolve(
635
652
true ,
636
653
false ,
637
654
StringPrototypeEndsWith ( packageSubpath , '/' ) ,
638
- conditions ) ;
655
+ conditions , true ) ;
639
656
640
657
if ( resolveResult == null ) {
641
658
throw exportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
@@ -693,6 +710,7 @@ function packageImportsResolve(name, base, conditions) {
693
710
const resolveResult = resolvePackageTarget (
694
711
packageJSONUrl , imports [ name ] , '' , name , base , false , true , false ,
695
712
conditions ,
713
+ false ,
696
714
) ;
697
715
if ( resolveResult != null ) {
698
716
return resolveResult ;
@@ -725,7 +743,8 @@ function packageImportsResolve(name, base, conditions) {
725
743
const resolveResult = resolvePackageTarget ( packageJSONUrl , target ,
726
744
bestMatchSubpath ,
727
745
bestMatch , base , true ,
728
- true , false , conditions ) ;
746
+ true , false , conditions ,
747
+ false ) ;
729
748
if ( resolveResult != null ) {
730
749
return resolveResult ;
731
750
}
0 commit comments