Skip to content

Commit

Permalink
use adoptopenjdk API by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Heiges committed Sep 16, 2023
1 parent 3a72486 commit 05af218
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
31 changes: 29 additions & 2 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 eclipse (`eclipse`/`adoptopenjdk`)
* @return Promise<string> - Resolves to the installation directory or rejects an error
* @example
* const njre = require('njre')
Expand All @@ -173,9 +187,21 @@ function extract (file) {
* })
*/
function install (version = 8, options = {}) {
const { openjdk_impl = 'hotspot', release = 'latest', type = 'jre', heap_size = 'normal', vendor = 'eclipse' } = options
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'))

let version_path = (options.release === 'latest')
? 'latest/' + version + '/ga'
: 'version/' + options.release
Expand Down Expand Up @@ -207,7 +233,7 @@ function install (version = 8, options = {}) {
else return Promise.reject(new Error('Unsupported architecture'))
}

let url = 'https://api.adoptium.net/v3/binary/'
let url = 'https://' + endpoint + '/v3/binary/'
+ version_path + '/'
+ options.os + '/'
+ options.arch + '/'
Expand All @@ -220,6 +246,7 @@ function install (version = 8, options = {}) {

return fetch(url, {redirect: 'manual'})
.then(response => response.headers.get('Location'))
.then(location => followToAdoptium(location))
.then(location => downloadAll(tmpdir, location))
.then(verify)
.then(move)
Expand Down
12 changes: 10 additions & 2 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ describe('Install', () => {
return njre.install()
}).timeout(100000)

it('should install JDK with custom options without throwing an error', () => {
return njre.install(11, { os: 'aix', arch: 'ppc64', openjdk_impl: 'hotspot', type: 'jdk' })
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 05af218

Please sign in to comment.