From 05af218202b0e6cbf3aa4017d105da430fa5555b Mon Sep 17 00:00:00 2001 From: Mark Heiges Date: Sat, 16 Sep 2023 08:06:02 -0400 Subject: [PATCH] use adoptopenjdk API by default --- lib/install.js | 31 +++++++++++++++++++++++++++++-- tests/test.js | 12 ++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/lib/install.js b/lib/install.js index bd8cf4e..1e0634d 100644 --- a/lib/install.js +++ b/lib/install.js @@ -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`) @@ -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 - Resolves to the installation directory or rejects an error * @example * const njre = require('njre') @@ -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 @@ -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 + '/' @@ -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) diff --git a/tests/test.js b/tests/test.js index a79b79d..9803798 100644 --- a/tests/test.js +++ b/tests/test.js @@ -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) })