Update JDK handling to account for AdoptOpenJDK migration to Adoptium.

Fix bug with AdmZip by replacing it for the jdk extraction task. It will have to eventually be replaced for everything else.
Remove disclaimer about Oracle JDK, as it is no longer used.
This commit is contained in:
Daniel Scalzi
2021-10-14 23:17:40 -04:00
parent f9e4fd8561
commit 430e840514
4 changed files with 257 additions and 372 deletions

View File

@@ -5,6 +5,7 @@ const child_process = require('child_process')
const crypto = require('crypto')
const EventEmitter = require('events')
const fs = require('fs-extra')
const StreamZip = require('node-stream-zip')
const path = require('path')
const Registry = require('winreg')
const request = require('request')
@@ -222,42 +223,6 @@ class JavaGuard extends EventEmitter {
this.mcVersion = mcVersion
}
// /**
// * @typedef OracleJREData
// * @property {string} uri The base uri of the JRE.
// * @property {{major: string, update: string, build: string}} version Object containing version information.
// */
// /**
// * Resolves the latest version of Oracle's JRE and parses its download link.
// *
// * @returns {Promise.<OracleJREData>} Promise which resolved to an object containing the JRE download data.
// */
// static _latestJREOracle(){
// const url = 'https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html'
// const regex = /https:\/\/.+?(?=\/java)\/java\/jdk\/([0-9]+u[0-9]+)-(b[0-9]+)\/([a-f0-9]{32})?\/jre-\1/
// return new Promise((resolve, reject) => {
// request(url, (err, resp, body) => {
// if(!err){
// const arr = body.match(regex)
// const verSplit = arr[1].split('u')
// resolve({
// uri: arr[0],
// version: {
// major: verSplit[0],
// update: verSplit[1],
// build: arr[2]
// }
// })
// } else {
// resolve(null)
// }
// })
// })
// }
/**
* @typedef OpenJDKData
* @property {string} uri The base uri of the JRE.
@@ -281,30 +246,41 @@ class JavaGuard extends EventEmitter {
if(process.platform === 'darwin') {
return this._latestCorretto(major)
} else {
return this._latestAdoptOpenJDK(major)
return this._latestAdoptium(major)
}
}
static _latestAdoptOpenJDK(major) {
static _latestAdoptium(major) {
const majorNum = Number(major)
const sanitizedOS = process.platform === 'win32' ? 'windows' : (process.platform === 'darwin' ? 'mac' : process.platform)
const url = `https://api.adoptium.net/v3/assets/latest/${major}/hotspot?vendor=eclipse`
const url = `https://api.adoptopenjdk.net/v2/latestAssets/nightly/openjdk${major}?os=${sanitizedOS}&arch=x64&heap_size=normal&openjdk_impl=hotspot&type=jre`
return new Promise((resolve, reject) => {
request({url, json: true}, (err, resp, body) => {
if(!err && body.length > 0){
resolve({
uri: body[0].binary_link,
size: body[0].binary_size,
name: body[0].binary_name
const targetBinary = body.find(entry => {
return entry.version.major === majorNum
&& entry.binary.os === sanitizedOS
&& entry.binary.image_type === 'jdk'
&& entry.binary.architecture === 'x64'
})
if(targetBinary != null) {
resolve({
uri: targetBinary.binary.package.link,
size: targetBinary.binary.package.size,
name: targetBinary.binary.package.name
})
} else {
resolve(null)
}
} else {
resolve(null)
}
})
})
}
static _latestCorretto(major) {
@@ -839,6 +815,7 @@ class JavaGuard extends EventEmitter {
pathSet1 = new Set([
...pathSet1,
...(await JavaGuard._scanFileSystem('C:\\Program Files\\Java')),
...(await JavaGuard._scanFileSystem('C:\\Program Files\\Eclipse Foundation')),
...(await JavaGuard._scanFileSystem('C:\\Program Files\\AdoptOpenJDK'))
])
}
@@ -1583,21 +1560,7 @@ class AssetGuard extends EventEmitter {
this.java = new DLTracker([jre], jre.size, (a, self) => {
if(verData.name.endsWith('zip')){
const zip = new AdmZip(a.to)
const pos = path.join(dataDir, zip.getEntries()[0].entryName)
zip.extractAllToAsync(dataDir, true, (err) => {
if(err){
console.log(err)
self.emit('complete', 'java', JavaGuard.javaExecFromRoot(pos))
} else {
fs.unlink(a.to, err => {
if(err){
console.log(err)
}
self.emit('complete', 'java', JavaGuard.javaExecFromRoot(pos))
})
}
})
this._extractJdkZip(a.to, dataDir, self)
} else {
// Tar.gz
@@ -1638,6 +1601,32 @@ class AssetGuard extends EventEmitter {
}
async _extractJdkZip(zipPath, runtimeDir, self) {
const zip = new StreamZip.async({
file: zipPath,
storeEntries: true
})
let pos = ''
try {
const entries = await zip.entries()
pos = path.join(runtimeDir, Object.keys(entries)[0])
console.log('Extracting jdk..')
await zip.extract(null, runtimeDir)
console.log('Cleaning up..')
await fs.remove(zipPath)
console.log('Jdk extraction complete.')
} catch(err) {
console.log(err)
} finally {
zip.close()
self.emit('complete', 'java', JavaGuard.javaExecFromRoot(pos))
}
}
// _enqueueOracleJRE(dataDir){
// return new Promise((resolve, reject) => {
// JavaGuard._latestJREOracle().then(verData => {