Skip to content

Commit

Permalink
refactor(node): Turn fixNpmDownloadUrl() into an extension
Browse files Browse the repository at this point in the history
While at it, make it an expression. This allows to turn transform the
code in one of the callers to a one-liner and to switch from `var` to
`val`.

Signed-off-by: Frank Viernau <frank_viernau@epam.com>
  • Loading branch information
fviernau committed Sep 11, 2024
1 parent b4205ba commit 3382b5b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion plugins/package-managers/node/src/main/kotlin/Npm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ open class Npm(
}
}

downloadUrl = fixNpmDownloadUrl(downloadUrl)
downloadUrl = downloadUrl.fixNpmDownloadUrl()

val vcsFromDownloadUrl = VcsHost.parseUrl(downloadUrl)
if (vcsFromDownloadUrl.url != downloadUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,18 @@ internal fun expandNpmShortcutUrl(url: String): String {
}

/**
* Do various replacements in [downloadUrl]. Return the transformed URL.
* Return the result of doing various replacements in this URL.
*/
internal fun fixNpmDownloadUrl(downloadUrl: String): String {
internal fun String.fixNpmDownloadUrl(): String =
@Suppress("HttpUrlsUsage")
return downloadUrl
// Work around the issue described at
// https://npm.community/t/some-packages-have-dist-tarball-as-http-and-not-https/285/19.
.replace("http://registry.npmjs.org/", "https://registry.npmjs.org/")
// Work around the issue described at
// https://npm.community/t/some-packages-have-dist-tarball-as-http-and-not-https/285/19.
replace("http://registry.npmjs.org/", "https://registry.npmjs.org/")
// Work around Artifactory returning API URLs instead of download URLs.
// See these somewhat related issues:
// - https://www.jfrog.com/jira/browse/RTFACT-8727
// - https://www.jfrog.com/jira/browse/RTFACT-18463
.replace(ARTIFACTORY_API_PATH_PATTERN, "$1/$2")
}

private val ARTIFACTORY_API_PATH_PATTERN = Regex("(.*artifactory.*)/api/npm/(.*)")

Expand Down
5 changes: 1 addition & 4 deletions plugins/package-managers/node/src/main/kotlin/yarn2/Yarn2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,7 @@ class Yarn2(
val vcsFromPackage = parseNpmVcsInfo(packageJson)
val homepage = packageJson.homepage.orEmpty()
val author = parseNpmAuthors(packageJson.author)

var downloadUrl = packageJson.dist?.tarball.orEmpty()

downloadUrl = fixNpmDownloadUrl(downloadUrl)
val downloadUrl = packageJson.dist?.tarball.orEmpty().fixNpmDownloadUrl()

val hash = Hash.create(packageJson.dist?.shasum.orEmpty())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ class NpmSupportTest : WordSpec({

"fixNpmDownloadUrl()" should {
"replace HTTP with HTTPS for the NPM registry only" {
fixNpmDownloadUrl("http://registry.npmjs.org/babel-cli/-/babel-cli-6.26.0.tgz") shouldBe
"http://registry.npmjs.org/babel-cli/-/babel-cli-6.26.0.tgz".fixNpmDownloadUrl() shouldBe
"https://registry.npmjs.org/babel-cli/-/babel-cli-6.26.0.tgz"
fixNpmDownloadUrl("http://oss-review-toolkit.org/") shouldBe "http://oss-review-toolkit.org/"
"http://oss-review-toolkit.org/".fixNpmDownloadUrl() shouldBe "http://oss-review-toolkit.org/"
}

"correct Artifactory API URLS" {
fixNpmDownloadUrl("http://my.repo/artifactory/api/npm/npm/all") shouldBe
"http://my.repo/artifactory/api/npm/npm/all".fixNpmDownloadUrl() shouldBe
"http://my.repo/artifactory/npm/all"
}
}
Expand Down

0 comments on commit 3382b5b

Please sign in to comment.