Skip to content

Commit

Permalink
Merge pull request #164 from markslater/allow-insecure-protocol
Browse files Browse the repository at this point in the history
Allow insecure protocol
  • Loading branch information
deepy authored Dec 13, 2021
2 parents e4b9b15 + d2e6052 commit bd6884a
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ node {
// Or set to null if you want to add the repository on your own.
distBaseUrl = "https://nodejs.org/dist"
// Specifies whether it is acceptable to communicate with the Node.js repository over an insecure HTTP connection.
// Only used if download is true
// Change it to true if you use a mirror that uses HTTP rather than HTTPS
// Or set to null if you want to use Gradle's default behaviour.
allowInsecureProtocol = null
// The npm command executed by the npmInstall task
// By default it is install but it can be changed to ci
npmInstallCommand = "install"
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/com/github/gradle/node/NodeExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ open class NodeExtension(project: Project) {
*/
val distBaseUrl = project.objects.property<String>()

/**
* Specifies whether it is acceptable to communicate with the Node.js repository over an insecure HTTP connection.
* Only used if download is true
* Change it to true if you use a mirror that uses HTTP rather than HTTPS
* Or set to null if you want to use Gradle's default behaviour.
*/
val allowInsecureProtocol = project.objects.property<Boolean>()

val npmCommand = project.objects.property<String>().convention("npm")
val npxCommand = project.objects.property<String>().convention("npx")
val yarnCommand = project.objects.property<String>().convention("yarn")
Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/com/github/gradle/node/NodePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.gradle.api.Project
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.register
import org.gradle.util.GradleVersion
import java.io.File

class NodePlugin : Plugin<Project> {
Expand All @@ -30,7 +31,7 @@ class NodePlugin : Plugin<Project> {
addYarnRule()
project.afterEvaluate {
if (nodeExtension.download.get()) {
nodeExtension.distBaseUrl.orNull?.let { addRepository(it) }
nodeExtension.distBaseUrl.orNull?.let { addRepository(it, nodeExtension.allowInsecureProtocol.orNull) }
configureNodeSetupTask(nodeExtension)
}
}
Expand Down Expand Up @@ -86,7 +87,7 @@ class NodePlugin : Plugin<Project> {
}
}

private fun addRepository(distUrl: String) {
private fun addRepository(distUrl: String, allowInsecureProtocol: Boolean?) {
project.repositories.ivy {
name = "Node.js"
setUrl(distUrl)
Expand All @@ -99,6 +100,9 @@ class NodePlugin : Plugin<Project> {
content {
includeModule("org.nodejs", "node")
}
if (GradleVersion.current() >= GradleVersion.version("6.0")) {
allowInsecureProtocol?.let { isAllowInsecureProtocol = it }
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class NodeExtensionTest extends AbstractProjectTest {
nodeExtension.npmCommand.get() == 'npm'
nodeExtension.npxCommand.get() == 'npx'
nodeExtension.distBaseUrl.get() == 'https://nodejs.org/dist'
nodeExtension.allowInsecureProtocol.orNull == null
nodeExtension.workDir.get() != null
nodeExtension.nodeProjectDir.get() != null
nodeExtension.version.get() == DEFAULT_NODE_VERSION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,31 @@ class NodeTask_integTest extends AbstractIntegTest {
result.task(":nodeSetup").outcome == TaskOutcome.SKIPPED
result.task(":hello").outcome == TaskOutcome.SUCCESS
}

def 'make sure build works with allowInsecureProtocol false using a custom repository'() {
given:
Assume.assumeFalse(gradleVersion < GradleVersion.version("6.8"))
copyResources("fixtures/node-disallow-insecure-protocol")

when:
def result = build("hello")

then:
result.task(":nodeSetup").outcome == TaskOutcome.SUCCESS
result.task(":hello").outcome == TaskOutcome.SUCCESS
}

def 'make sure build works with allowInsecureProtocol true using a custom repository'() {
given:
Assume.assumeFalse(gradleVersion < GradleVersion.version("6.8"))
copyResources("fixtures/node-allow-insecure-protocol")

when:
def result = build("hello")

then:
result.task(":nodeSetup").outcome == TaskOutcome.SUCCESS
result.task(":hello").outcome == TaskOutcome.SUCCESS
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id "com.github.node-gradle.node"
}

node {
version = "12.13.0"
distBaseUrl = "http://nodejs.org/dist/"
download = true
allowInsecureProtocol = true
workDir = file("build/node")
}

task hello(type: NodeTask) {
script = file("simple.js")
args = []
outputs.upToDateWhen {
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello World");
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id "com.github.node-gradle.node"
}

node {
version = "12.13.0"
distBaseUrl = "https://nodejs.org/dist/"
download = true
allowInsecureProtocol = false
workDir = file("build/node")
}

task hello(type: NodeTask) {
script = file("simple.js")
args = []
outputs.upToDateWhen {
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello World");

0 comments on commit bd6884a

Please sign in to comment.