|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright (c) 2022, Oracle and/or its affiliates. |
| 4 | + * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. |
| 5 | + */ |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +const HttpsProxyAgent = require('https-proxy-agent'); |
| 9 | +const fetch = require('node-fetch'); |
| 10 | + |
| 11 | +// WARNING: This file contains functions that are called by build scripts |
| 12 | +// (where this code is not running in Electron). As such, do not |
| 13 | +// require files like userSettings.js, wktLogging.js, or |
| 14 | +// wktTools.js, all of which execute code during that requires an |
| 15 | +// electron environment to function properly. |
| 16 | +// |
| 17 | +async function getReleaseVersions(name, baseUrl, options = undefined) { |
| 18 | + const proxyAgent = await getProxyAgent(options); |
| 19 | + return new Promise((resolve, reject) => { |
| 20 | + const releasesUrl = `${baseUrl}/releases`; |
| 21 | + fetch(releasesUrl, getFetchOptions(proxyAgent)).then(res => { |
| 22 | + res.json().then(fetchResults => { |
| 23 | + const results = fetchResults.map(fetchResult => { |
| 24 | + return { |
| 25 | + name: fetchResult.name, |
| 26 | + tag: fetchResult.tag_name, |
| 27 | + }; |
| 28 | + }); |
| 29 | + resolve(results); |
| 30 | + }); |
| 31 | + }).catch(err => reject(new Error(`Failed to get release versions for ${name} from ${releasesUrl}: ${err}`))); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +async function getLatestReleaseObject(name, baseUrl, options = undefined) { |
| 36 | + const proxyAgent = await getProxyAgent(options); |
| 37 | + return new Promise((resolve, reject) => { |
| 38 | + const latestUrl = baseUrl + '/releases/latest'; |
| 39 | + fetch(latestUrl, getFetchOptions(proxyAgent)).then(res => { |
| 40 | + const results = res.json(); |
| 41 | + resolve(results); |
| 42 | + }).catch(err => reject(new Error(`Failed to get the latest release of ${name} from ${latestUrl}: ${err}`))); |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +async function getSpecifiedReleaseObject(name, tag, baseUrl, options = undefined) { |
| 47 | + const proxyAgent = await getProxyAgent(options); |
| 48 | + return new Promise((resolve, reject) => { |
| 49 | + const releaseUrl = baseUrl + '/releases/tags/' + tag; |
| 50 | + fetch(releaseUrl, getFetchOptions(proxyAgent)) |
| 51 | + .then(res => resolve(res.json())) |
| 52 | + .catch(err => reject(new Error(`Failed to get the ${tag} release of ${name} from ${releaseUrl}: ${err}`))); |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +async function getProxyOptionsFromPreferences() { |
| 57 | + return new Promise((resolve, reject) => { |
| 58 | + try { |
| 59 | + // This require statement for userSettings should be safe since the build |
| 60 | + // scripts always pass a non-empty options object, which means that |
| 61 | + // getProxyAgent() will never call this function in a build scripts context. |
| 62 | + // |
| 63 | + const httpsProxyUrl = require('./userSettings').getHttpsProxyUrl(); |
| 64 | + if (httpsProxyUrl) { |
| 65 | + resolve({ httpsProxyUrl: httpsProxyUrl }); |
| 66 | + } else { |
| 67 | + resolve(); |
| 68 | + } |
| 69 | + } catch (err) { |
| 70 | + reject(err); |
| 71 | + } |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +async function getProxyAgent(options = undefined) { |
| 76 | + const proxyOptions = options || await getProxyOptionsFromPreferences(); |
| 77 | + const httpsProxyUrl = _getHttpsProxyUrl(proxyOptions); |
| 78 | + |
| 79 | + let proxyAgent; |
| 80 | + if (httpsProxyUrl) { |
| 81 | + proxyAgent = new HttpsProxyAgent(httpsProxyUrl); |
| 82 | + } |
| 83 | + return proxyAgent; |
| 84 | +} |
| 85 | + |
| 86 | +function getFetchOptions(proxyAgent) { |
| 87 | + let options = {}; |
| 88 | + if (proxyAgent) { |
| 89 | + options = { |
| 90 | + agent: proxyAgent |
| 91 | + }; |
| 92 | + } |
| 93 | + return options; |
| 94 | +} |
| 95 | + |
| 96 | +function _getHttpsProxyUrl(options) { |
| 97 | + const myOptions = _getOptions(options, {httpsProxyUrl: undefined}); |
| 98 | + return myOptions.httpsProxyUrl; |
| 99 | +} |
| 100 | + |
| 101 | +function _getOptions(options, defaultOptions) { |
| 102 | + if (options === null || options === undefined || typeof options === 'function') { |
| 103 | + return defaultOptions; |
| 104 | + } |
| 105 | + |
| 106 | + if (typeof options === 'string') { |
| 107 | + defaultOptions = {...defaultOptions}; |
| 108 | + defaultOptions.httpsProxy = options; |
| 109 | + options = defaultOptions; |
| 110 | + } else if (typeof options !== 'object') { |
| 111 | + throw new Error(`Invalid options argument type: ${typeof options}`); |
| 112 | + } |
| 113 | + return options; |
| 114 | +} |
| 115 | + |
| 116 | +module.exports = { |
| 117 | + getFetchOptions, |
| 118 | + getLatestReleaseObject, |
| 119 | + getProxyOptionsFromPreferences, |
| 120 | + getProxyAgent, |
| 121 | + getReleaseVersions, |
| 122 | + getSpecifiedReleaseObject, |
| 123 | +}; |
0 commit comments