mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-03-13 19:16:52 +01:00
Merge pull request #7564 from BlueWallet/contr
FIX: Lndhub lacked timeouts
This commit is contained in:
commit
e92eb7eae0
6 changed files with 37 additions and 27 deletions
|
@ -6,6 +6,7 @@ import { checkNotifications, requestNotifications, RESULTS } from 'react-native-
|
|||
import PushNotification from 'react-native-push-notification';
|
||||
import loc from '../loc';
|
||||
import { groundControlUri } from './constants';
|
||||
import { fetch } from '../util/fetch';
|
||||
|
||||
const PUSH_TOKEN = 'PUSH_TOKEN';
|
||||
const GROUNDCONTROL_BASE_URI = 'GROUNDCONTROL_BASE_URI';
|
||||
|
@ -385,10 +386,6 @@ export const configureNotifications = async onProcessNotifications => {
|
|||
});
|
||||
};
|
||||
|
||||
const _sleep = async ms => {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates whether the provided GroundControl URI is valid by pinging it.
|
||||
*
|
||||
|
@ -396,15 +393,13 @@ const _sleep = async ms => {
|
|||
* @returns {Promise<boolean>} TRUE if valid, FALSE otherwise
|
||||
*/
|
||||
export const isGroundControlUriValid = async uri => {
|
||||
let response;
|
||||
try {
|
||||
response = await Promise.race([fetch(`${uri}/ping`, { headers: _getHeaders() }), _sleep(2000)]);
|
||||
} catch (_) {}
|
||||
|
||||
if (!response) return false;
|
||||
|
||||
const json = await response.json();
|
||||
return !!json.description;
|
||||
const response = await fetch(`${uri}/ping`, { headers: _getHeaders() });
|
||||
const json = await response.json();
|
||||
return !!json.description;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const isNotificationsCapable = hasGmsSync() || hasHmsSync() || Platform.OS !== 'android';
|
||||
|
@ -430,24 +425,21 @@ const getLevels = async () => {
|
|||
const pushToken = await getPushToken();
|
||||
if (!pushToken || !pushToken.token || !pushToken.os) return;
|
||||
|
||||
let response;
|
||||
try {
|
||||
response = await Promise.race([
|
||||
fetch(`${baseURI}/getTokenConfiguration`, {
|
||||
method: 'POST',
|
||||
headers: _getHeaders(),
|
||||
body: JSON.stringify({
|
||||
token: pushToken.token,
|
||||
os: pushToken.os,
|
||||
}),
|
||||
const response = await fetch(`${baseURI}/getTokenConfiguration`, {
|
||||
method: 'POST',
|
||||
headers: _getHeaders(),
|
||||
body: JSON.stringify({
|
||||
token: pushToken.token,
|
||||
os: pushToken.os,
|
||||
}),
|
||||
_sleep(3000),
|
||||
]);
|
||||
} catch (_) {}
|
||||
});
|
||||
|
||||
if (!response) return {};
|
||||
|
||||
return await response.json();
|
||||
if (!response) return {};
|
||||
return await response.json();
|
||||
} catch (_) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import URL from 'url';
|
||||
import { fetch } from '../util/fetch';
|
||||
|
||||
export default class Azteco {
|
||||
/**
|
||||
|
|
|
@ -6,6 +6,7 @@ import CryptoJS from 'crypto-js';
|
|||
// @ts-ignore theres no types for secp256k1
|
||||
import secp256k1 from 'secp256k1';
|
||||
import { parse } from 'url'; // eslint-disable-line n/no-deprecated-api
|
||||
import { fetch } from '../util/fetch';
|
||||
|
||||
const ONION_REGEX = /^(http:\/\/[^/:@]+\.onion(?::\d{1,5})?)(\/.*)?$/; // regex for onion URL
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import bolt11 from 'bolt11';
|
||||
import { BitcoinUnit, Chain } from '../../models/bitcoinUnits';
|
||||
import { LegacyWallet } from './legacy-wallet';
|
||||
import { fetch } from '../../util/fetch';
|
||||
|
||||
export class LightningCustodianWallet extends LegacyWallet {
|
||||
static readonly type = 'lightningCustodianWallet';
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import assert from 'assert';
|
||||
|
||||
import { LightningCustodianWallet } from '../../class';
|
||||
import { fetch } from '../../util/fetch';
|
||||
|
||||
jest.setTimeout(200 * 1000);
|
||||
const baseUri = 'https://lndhub-staging.herokuapp.com';
|
||||
|
|
14
util/fetch.ts
Normal file
14
util/fetch.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
const DEFAULT_TIMEOUT = 10000; // default timeout in ms
|
||||
|
||||
// protection against calling itself recursively
|
||||
const nativeFetch = globalThis.fetch.bind(globalThis);
|
||||
|
||||
export function fetch(input: RequestInfo | URL, init: RequestInit & { timeout?: number } = {}): Promise<Response> {
|
||||
if (__DEV__) {
|
||||
console.log('fetch wrapper: ', input, init);
|
||||
}
|
||||
const { timeout = DEFAULT_TIMEOUT, ...rest } = init;
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), timeout);
|
||||
return nativeFetch(input, { ...rest, signal: controller.signal }).finally(() => clearTimeout(timer));
|
||||
}
|
Loading…
Add table
Reference in a new issue