Skip to content

Commit

Permalink
Merge branch 'master' into renovate/configure
Browse files Browse the repository at this point in the history
  • Loading branch information
nvuillam authored Sep 19, 2023
2 parents cd26d42 + 609bcb6 commit 75298ce
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"Vuillamy",
"aarch",
"adoptopenjdk",
"adoptium",
"dotenv",
"eslintcache",
"fileurl",
Expand Down
51 changes: 44 additions & 7 deletions lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ function extract (file) {
})
}

/**
* The API will decide if it needs to redirect from api.adoptopenjdk.net to
* api.adoptium.net before finally redirecting to the binary. This function
* handles the initial redirection if needed, otherwise it just returns the
* location url for the binary.
**/
function followToAdoptium (location) {
if (/api.adoptium.net/g.test(location)) {
return fetch(location, { redirect: 'manual' })
.then(response => response.headers.get('Location'))
} else return location
}

/**
* Installs a JRE copy for the app
* @param {number} [version = 8] - Java Version (`8`/`9`/`10`/`11`/`12`)
Expand All @@ -150,6 +163,7 @@ function extract (file) {
* @param {string} [options.release = latest] - Release
* @param {string} [options.type = jre] - Binary Type (`jre`/`jdk`)
* @param {string} [options.heap_size] - Heap Size (`normal`/`large`)
* @param {string} [options.vendor] - defaults to adoptopenjdk (`adoptopenjdk`/`eclipse`)
* @return Promise<string> - Resolves to the installation directory or rejects an error
* @example
* const njre = require('njre')
Expand All @@ -173,9 +187,24 @@ function extract (file) {
* })
*/
function install (version = 8, options = {}) {
const { openjdk_impl = 'hotspot', release = 'latest', type = 'jre' } = options
options = { ...options, openjdk_impl, release, type }
let url = 'https://api.adoptopenjdk.net/v2/info/releases/openjdk' + version + '?'
const {
openjdk_impl = 'hotspot',
release = 'latest',
type = 'jre',
heap_size = 'normal',
vendor = 'adoptopenjdk'
} = options

options = { ...options, openjdk_impl, release, type, heap_size, vendor }

let endpoint = null
if (options.vendor === 'adoptopenjdk') endpoint = 'api.adoptopenjdk.net'
else if (options.vendor === 'eclipse') endpoint = 'api.adoptium.net'
else return Promise.reject(new Error('Unsupported vendor. Use adoptopenjdk (default) or eclipse'))

const versionPath = (options.release === 'latest')
? 'latest/' + version + '/ga'
: 'version/' + options.release

if (!options.os) {
switch (process.platform) {
Expand Down Expand Up @@ -204,13 +233,21 @@ function install (version = 8, options = {}) {
else return Promise.reject(new Error('Unsupported architecture'))
}

Object.keys(options).forEach(key => { url += key + '=' + options[key] + '&' })
const url = 'https://' + endpoint + '/v3/binary/' +
versionPath + '/' +
options.os + '/' +
options.arch + '/' +
options.type + '/' +
options.openjdk_impl + '/' +
options.heap_size + '/' +
options.vendor

const tmpdir = path.join(os.tmpdir(), 'njre')

return fetch(url)
.then(response => response.json())
.then(json => downloadAll(tmpdir, json.binaries[0].binary_link))
return fetch(url, { redirect: 'manual' })
.then(response => response.headers.get('Location'))
.then(location => followToAdoptium(location))
.then(location => downloadAll(tmpdir, location))
.then(verify)
.then(move)
.then(extract)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"javaw",
"jdk",
"openjdk",
"adoptopenjdk"
"adoptopenjdk",
"adoptium"
],
"homepage": "https://github.com/raftario/njre",
"bugs": "https://github.com/raftario/njre/issues",
Expand Down
12 changes: 12 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ describe('Install', () => {
it('should install JRE with custom options without throwing an error', () => {
return njre.install(11, { os: 'aix', arch: 'ppc64', openjdk_impl: 'openj9' })
}).timeout(100000)

it('should install JDK with custom release without throwing an error', () => {
return njre.install(null, { release: 'jdk-21+34-ea-beta' })
}).timeout(100000)

it('should install JRE 14 from AdoptOpenJdk without throwing an error', () => {
return njre.install(14, { os: 'linux' })
}).timeout(100000)

it('should install JRE 20 from Eclipse Foundation without throwing an error', () => {
return njre.install(20, { vendor: 'eclipse' })
}).timeout(100000)
})

0 comments on commit 75298ce

Please sign in to comment.