-
-
Notifications
You must be signed in to change notification settings - Fork 90
feat: migrate from nodejs-mobile to termux-nodejs standalone process #623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| import java.security.MessageDigest | ||
| import java.net.URI | ||
| import org.apache.commons.compress.archivers.tar.TarArchiveInputStream | ||
| import org.apache.commons.compress.archivers.tar.TarArchiveEntry | ||
|
|
||
| buildscript { | ||
| repositories { | ||
| mavenCentral() | ||
| } | ||
| dependencies { | ||
| classpath("org.apache.commons:commons-compress:1.28.0") | ||
| classpath("org.tukaani:xz:1.12") | ||
| } | ||
| } | ||
|
|
||
| abstract class DownloadNodejsTask : DefaultTask() { | ||
|
|
||
| private val targetArchitectures = listOf("aarch64", "arm", "x86_64") | ||
| private val architectureMap = mapOf( | ||
| "aarch64" to "arm64-v8a", | ||
| "arm" to "armeabi-v7a", | ||
| "x86_64" to "x86_64", | ||
| ) | ||
|
|
||
| @TaskAction | ||
| fun run() { | ||
| val baseUrl = "https://github.com/nini22P/termux-nodejs/releases/latest/download" | ||
|
|
||
| val libnodeRoot = project.file("src/main/jniLibs") | ||
| val assetsDir = project.file("src/main/assets") | ||
|
|
||
| if (!assetsDir.exists()) assetsDir.mkdirs() | ||
|
|
||
| val sha256Content = try { | ||
| fetch("$baseUrl/sha256.txt") | ||
| } catch (e: Exception) { | ||
| val allExist = targetArchitectures.all { archKey -> | ||
| val abi = architectureMap[archKey]!! | ||
| File(libnodeRoot, "$abi/libnode.so").exists() | ||
| } | ||
| if (allExist) { | ||
| println("Offline mode: Node.js binaries already exist, skipping download.") | ||
| return | ||
| } | ||
| throw GradleException("Failed to fetch SHA256 and local binaries are missing: ${e.message}", e) | ||
| } | ||
| val expectedHashes = parseSha256Content(sha256Content, targetArchitectures) | ||
|
|
||
| for (archKey in targetArchitectures) { | ||
| val fileName = expectedHashes.keys.find { it.contains(archKey) } ?: continue | ||
| val expectedHash = expectedHashes[fileName]!! | ||
| val tarFile = File(assetsDir, fileName) | ||
|
|
||
| val abi = architectureMap[archKey]!! | ||
| val archDir = File(libnodeRoot, abi) | ||
|
|
||
| val isTarInvalid = !tarFile.exists() || calculateSha256(tarFile) != expectedHash | ||
| if (isTarInvalid) { | ||
| downloadFile("$baseUrl/$fileName", tarFile) | ||
| if (calculateSha256(tarFile) != expectedHash) throw GradleException("SHA256 failed: $fileName") | ||
| } | ||
|
|
||
| extractBin(tarFile, archDir) | ||
| } | ||
| } | ||
|
|
||
| private fun extractBin(tarFile: File, archDir: File) { | ||
| val binPrefix = "./data/data/com.termux/files/usr/bin/" | ||
|
|
||
| val inputStream = tarFile.inputStream().buffered() | ||
|
|
||
| val decompressed = when { | ||
| tarFile.name.endsWith(".xz") -> | ||
| org.apache.commons.compress.compressors.xz.XZCompressorInputStream(inputStream) | ||
| tarFile.name.endsWith(".gz") -> | ||
| java.util.zip.GZIPInputStream(inputStream) | ||
| else -> inputStream | ||
| } | ||
|
|
||
| decompressed.use { comp -> | ||
| TarArchiveInputStream(comp).use { tis -> | ||
| var entry: TarArchiveEntry? = tis.nextEntry | ||
| while (entry != null) { | ||
| val name = entry.name | ||
| if (name.startsWith(binPrefix) | ||
| && !entry.isDirectory | ||
| && !entry.isSymbolicLink | ||
| ) { | ||
| val fileName = name.substringAfterLast("/") | ||
| val targetFile = File(archDir, "lib$fileName.so") | ||
|
|
||
| val bytes = tis.readBytes() | ||
|
|
||
| val newHash = calculateSha256(bytes) | ||
|
|
||
| if (targetFile.exists()) { | ||
| val oldHash = calculateSha256(targetFile) | ||
|
|
||
| if (oldHash == newHash) { | ||
| entry = tis.nextEntry | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| archDir.mkdirs() | ||
| targetFile.writeBytes(bytes) | ||
|
|
||
| println("Extracted: ${tarFile} -> $targetFile") | ||
| } | ||
|
|
||
| entry = tis.nextEntry | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun fetch(url: String): String { | ||
| println("Fetching: $url") | ||
| val connection = URI.create(url).toURL().openConnection() | ||
| connection.connectTimeout = 15000 | ||
| connection.readTimeout = 15000 | ||
| return connection.getInputStream().bufferedReader().use { it.readText() } | ||
| } | ||
|
|
||
| private fun downloadFile(url: String, target: File) { | ||
| println("Downloading: $url") | ||
| val connection = URI.create(url).toURL().openConnection() | ||
| connection.connectTimeout = 15000 | ||
| connection.readTimeout = 15000 | ||
| connection.getInputStream().use { input -> | ||
| target.outputStream().use { output -> input.copyTo(output) } | ||
| } | ||
| } | ||
|
|
||
| private fun parseSha256Content(content: String, architectures: List<String>): Map<String, String> { | ||
| val hashes = mutableMapOf<String, String>() | ||
| content.lines().forEach { line -> | ||
| val parts = line.trim().split(Regex("\\s+")) | ||
| if (parts.size >= 2) { | ||
| val hash = parts[0] | ||
| val name = parts[1].removePrefix("*") | ||
| if (architectures.any { name.contains(it) }) hashes[name] = hash | ||
| } | ||
| } | ||
| return hashes | ||
| } | ||
|
|
||
| private fun calculateSha256(file: File): String { | ||
| val digest = MessageDigest.getInstance("SHA-256") | ||
| file.inputStream().use { fis -> | ||
| val buffer = ByteArray(8192) | ||
| var bytesRead = fis.read(buffer) | ||
| while (bytesRead != -1) { | ||
| digest.update(buffer, 0, bytesRead) | ||
| bytesRead = fis.read(buffer) | ||
| } | ||
| } | ||
| return digest.digest().joinToString("") { "%02x".format(it) } | ||
| } | ||
|
|
||
| private fun calculateSha256(data: ByteArray): String { | ||
| val digest = MessageDigest.getInstance("SHA-256") | ||
| return digest.digest(data).joinToString("") { "%02x".format(it) } | ||
| } | ||
| } | ||
|
|
||
| tasks.register<DownloadNodejsTask>("downloadNodejs") | ||
|
|
||
| tasks.named("preBuild") { | ||
| dependsOn("downloadNodejs") | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用
URI.create(url).toURL().openStream()进行网络请求和文件下载时,没有设置连接超时(Connect Timeout)和读取超时(Read Timeout)。如果网络连接不稳定或对端无响应,可能会导致 Gradle 构建无限期挂起。建议使用URLConnection并显式设置合理的超时时间。