replace with custom superagent based on fetch API
This commit is contained in:
+31
-51
@@ -12,10 +12,32 @@ const assert = require('assert'),
|
||||
constants = require('../constants.js'),
|
||||
debug = require('debug')('box:network/generic'),
|
||||
safe = require('safetydance'),
|
||||
superagent = require('superagent');
|
||||
superagent = require('../superagent.js');
|
||||
|
||||
const gCache = { ipv4: {}, ipv6: {} }; // each has { timestamp, value, request }
|
||||
|
||||
async function getIP(type) {
|
||||
const url = `https://${type}.api.cloudron.io/api/v1/helper/public_ip`;
|
||||
|
||||
gCache[type].value = null; // clear the obsolete value
|
||||
|
||||
debug(`getIP: querying ${url} to get ${type}`);
|
||||
const [networkError, response] = await safe(superagent.get(url).timeout(30 * 1000).retry(2).ok(() => true));
|
||||
|
||||
if (networkError || response.status !== 200) {
|
||||
debug(`getIP: Error getting IP. ${networkError.message}`);
|
||||
throw new BoxError(BoxError.EXTERNAL_ERROR, `Unable to detect ${type}. API server (${type}.api.cloudron.io) unreachable`);
|
||||
}
|
||||
|
||||
if (!response.body && !response.body.ip) {
|
||||
debug('get: Unexpected answer. No "ip" found in response body.', response.body);
|
||||
throw new BoxError(BoxError.EXTERNAL_ERROR, `Unable to detect ${type}. No IP found in response`);
|
||||
}
|
||||
|
||||
gCache[type].value = response.body.ip;
|
||||
gCache[type].timestamp = Date.now();
|
||||
}
|
||||
|
||||
async function getIPv4(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
@@ -23,31 +45,10 @@ async function getIPv4(config) {
|
||||
|
||||
if (gCache.ipv4.value && (Date.now() - gCache.ipv4.timestamp <= 5 * 60 * 1000)) return gCache.ipv4.value;
|
||||
|
||||
let request = gCache.ipv4.request; // allow reuse for parallel requests
|
||||
if (!request) {
|
||||
debug('getIPv4: querying ipv4.api.cloudron.io to get server IPv4');
|
||||
request = superagent.get('https://ipv4.api.cloudron.io/api/v1/helper/public_ip').timeout(30 * 1000).retry(2).ok(() => true);
|
||||
gCache.ipv4.request = request;
|
||||
}
|
||||
|
||||
gCache.ipv4.value = null;
|
||||
|
||||
const [networkError, response] = await safe(request);
|
||||
gCache.ipv4.request = null;
|
||||
|
||||
if (networkError || response.status !== 200) {
|
||||
debug('getIPv4: Error getting IP. %o', networkError);
|
||||
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IPv4. API server (ipv4.api.cloudron.io) unreachable');
|
||||
}
|
||||
|
||||
if (!response.body && !response.body.ip) {
|
||||
debug('getIPv4: Unexpected answer. No "ip" found in response body.', response.body);
|
||||
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IPv4. No IP found in response');
|
||||
}
|
||||
|
||||
gCache.ipv4.value = response.body.ip;
|
||||
gCache.ipv4.timestamp = Date.now();
|
||||
return response.body.ip;
|
||||
if (!gCache.request) gCache.request = getIP('ipv4');
|
||||
await gCache.request;
|
||||
gCache.request = null;
|
||||
return gCache.ipv4.value;
|
||||
}
|
||||
|
||||
async function getIPv6(config) {
|
||||
@@ -57,31 +58,10 @@ async function getIPv6(config) {
|
||||
|
||||
if (gCache.ipv6.value && (Date.now() - gCache.ipv6.timestamp <= 5 * 60 * 1000)) return gCache.ipv6.value;
|
||||
|
||||
let request = gCache.ipv6.request; // allow reuse for parallel requests
|
||||
if (!request) {
|
||||
debug('getIPv6: querying ipv6.api.cloudron.io to get server IPv6');
|
||||
request = superagent.get('https://ipv6.api.cloudron.io/api/v1/helper/public_ip').timeout(30 * 1000).retry(2).ok(() => true);
|
||||
gCache.ipv6.request = request;
|
||||
}
|
||||
|
||||
gCache.ipv6.value = null;
|
||||
|
||||
const [networkError, response] = await safe(request);
|
||||
gCache.ipv6.request = null;
|
||||
|
||||
if (networkError || response.status !== 200) {
|
||||
debug('getIPv6: Error getting IP. %o', networkError);
|
||||
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IPv6. API server (ipv6.api.cloudron.io) unreachable');
|
||||
}
|
||||
|
||||
if (!response.body && !response.body.ip) {
|
||||
debug('getIPv6: Unexpected answer. No "ip" found in response body.', response.body);
|
||||
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IPv6. No IP found in response');
|
||||
}
|
||||
|
||||
gCache.ipv6.value = response.body.ip;
|
||||
gCache.ipv6.timestamp = Date.now();
|
||||
return response.body.ip;
|
||||
if (!gCache.request) gCache.request = getIP('ipv6');
|
||||
await gCache.request;
|
||||
gCache.request = null;
|
||||
return gCache.ipv6.value;
|
||||
}
|
||||
|
||||
async function testIPv4Config(config) {
|
||||
|
||||
Reference in New Issue
Block a user