@@ -17,12 +17,26 @@ func timeoutDetection(err error) {
1717 // check if error is a timeout error
1818}
1919
20+ // VersionExists checks if a package version exists on npm registry
21+ func VersionExists (dependency string , version string ) bool {
22+ normalizedVersion := strings .Trim (version , "/^\" " )
23+ url := "https://registry.npmjs.org/" + dependency + "/" + normalizedVersion
24+
25+ resp , err := http .Head (url )
26+ if err != nil {
27+ return false
28+ }
29+ defer resp .Body .Close ()
30+
31+ return resp .StatusCode == 200
32+ }
33+
2034// get all versions if no version is specified
2135func GetNPMRegistry (pkg RegistryRequest ) (* http.Response , error ) {
2236 var req * http.Response
2337 var err error
2438
25- normalizedVersion := strings .Trim (pkg .Version , "^ \" " ) // remove quotes if present
39+ normalizedVersion := strings .Trim (pkg .Version , "/ " ) // remove quotes if present
2640
2741 if pkg .Version != "" {
2842 req , err = http .Get ("https://registry.npmjs.org/" + pkg .Dependency + "/" + normalizedVersion )
@@ -45,8 +59,7 @@ func GetCratesRegistry(pkg RegistryRequest) (*http.Response, error) {
4559 var err error
4660
4761 if pkg .Version != "" {
48- normalizedVersion := strings .Trim (pkg .Version , "^\" " ) // remove quotes if present
49- req , err = http .Get ("https://crates.io/api/v1/crates/" + pkg .Dependency + "/" + normalizedVersion )
62+ req , err = http .Get ("https://crates.io/api/v1/crates/" + pkg .Dependency + "/" + pkg .Version )
5063 } else {
5164 req , err = http .Get ("https://crates.io/api/v1/crates/" + pkg .Dependency )
5265 }
0 commit comments