@@ -49,10 +49,42 @@ export async function extractPurlsFromPnpmLockfile(
4949 visit ( pkgPath )
5050 }
5151 return Array . from ( seen ) . map ( p =>
52- idToNpmPurl ( stripPnpmPeerSuffix ( stripLeadingPnpmDepPathSlash ( p ) ) ) ,
52+ idToNpmPurl (
53+ stripPnpmPeerSuffixFromDepPath ( stripLeadingPnpmDepPathSlash ( p ) ) ,
54+ ) ,
5355 )
5456}
5557
58+ function stripPnpmPeerSuffixFromDepPath ( depPath : string ) : string {
59+ // Modern pnpm paths use name@version; older lockfiles used name/version.
60+ // Peer suffix delimiters belong to the version component, since npm names
61+ // themselves may contain underscores.
62+ const scoped = depPath . startsWith ( '@' )
63+ const nameSlash = scoped ? depPath . indexOf ( '/' ) : - 1
64+ const atIndex = scoped
65+ ? depPath . indexOf ( '@' , nameSlash + 1 )
66+ : depPath . indexOf ( '@' )
67+ const legacySlash = scoped
68+ ? depPath . indexOf ( '/' , nameSlash + 1 )
69+ : depPath . indexOf ( '/' )
70+ if ( atIndex !== - 1 && ( legacySlash === - 1 || atIndex < legacySlash ) ) {
71+ return `${ depPath . slice ( 0 , atIndex + 1 ) } ${ stripPnpmPeerSuffix ( depPath . slice ( atIndex + 1 ) ) } `
72+ }
73+
74+ // Legacy pnpm v5 paths use name/version, including /@scope/name/version.
75+ const versionSlash = legacySlash
76+ if ( versionSlash !== - 1 ) {
77+ const name = depPath . slice ( 0 , versionSlash )
78+ const version = stripPnpmPeerSuffix ( depPath . slice ( versionSlash + 1 ) )
79+ if ( semver . valid ( version ) ) {
80+ return `${ name } @${ version } `
81+ }
82+ }
83+
84+ // Some lockfiles contain package IDs without versions; leave those intact.
85+ return depPath
86+ }
87+
5688export function isPnpmDepPath ( maybeDepPath : string ) : boolean {
5789 return maybeDepPath . length > 0 && maybeDepPath . charCodeAt ( 0 ) === 47 /*'/'*/
5890}
0 commit comments