From bbf746b011dcd2a2f6a0bf5cf25dd75a42d9e4f8 Mon Sep 17 00:00:00 2001 From: Marcos Rodriguez Velez Date: Sun, 16 Feb 2025 22:26:03 -0400 Subject: [PATCH] REF: fetch to timeoutFetch --- blue_modules/notifications.js | 13 ++++----- class/azteco.ts | 3 ++- class/wallets/lightning-custodian-wallet.ts | 27 ++++++++++--------- .../lightning-custodian-wallet.test.js | 11 ++++---- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/blue_modules/notifications.js b/blue_modules/notifications.js index c8cc2a5f7..75bc47dbe 100644 --- a/blue_modules/notifications.js +++ b/blue_modules/notifications.js @@ -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 { timeoutFetch } from '../util/fetch'; const PUSH_TOKEN = 'PUSH_TOKEN'; const GROUNDCONTROL_BASE_URI = 'GROUNDCONTROL_BASE_URI'; @@ -149,7 +150,7 @@ export const majorTomToGroundControl = async (addresses, hashes, txids) => { let response; try { console.debug('majorTomToGroundControl: Sending request to:', `${baseURI}/majorTomToGroundControl`); - response = await fetch(`${baseURI}/majorTomToGroundControl`, { + response = await timeoutFetch(`${baseURI}/majorTomToGroundControl`, { method: 'POST', headers: _getHeaders(), body: requestBody, @@ -212,7 +213,7 @@ export const setLevels = async levelAll => { if (!pushToken || !pushToken.token || !pushToken.os) return; try { - const response = await fetch(`${baseURI}/setTokenConfiguration`, { + const response = await timeoutFetch(`${baseURI}/setTokenConfiguration`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -276,7 +277,7 @@ const postTokenConfig = async () => { const appVersion = getSystemName() + ' ' + getSystemVersion() + ';' + getApplicationName() + ' ' + getVersion(); console.debug('postTokenConfig: Posting configuration', { lang, appVersion }); - await fetch(`${baseURI}/setTokenConfiguration`, { + await timeoutFetch(`${baseURI}/setTokenConfiguration`, { method: 'POST', headers: _getHeaders(), body: JSON.stringify({ @@ -398,7 +399,7 @@ const _sleep = async ms => { export const isGroundControlUriValid = async uri => { let response; try { - response = await Promise.race([fetch(`${uri}/ping`, { headers: _getHeaders() }), _sleep(2000)]); + response = await Promise.race([timeoutFetch(`${uri}/ping`, { headers: _getHeaders() }), _sleep(2000)]); } catch (_) {} if (!response) return false; @@ -433,7 +434,7 @@ const getLevels = async () => { let response; try { response = await Promise.race([ - fetch(`${baseURI}/getTokenConfiguration`, { + timeoutFetch(`${baseURI}/getTokenConfiguration`, { method: 'POST', headers: _getHeaders(), body: JSON.stringify({ @@ -478,7 +479,7 @@ export const unsubscribe = async (addresses, hashes, txids) => { }); try { - const response = await fetch(`${baseURI}/unsubscribe`, { + const response = await timeoutFetch(`${baseURI}/unsubscribe`, { method: 'POST', headers: _getHeaders(), body, diff --git a/class/azteco.ts b/class/azteco.ts index 7aa35ed14..8646c9f3f 100644 --- a/class/azteco.ts +++ b/class/azteco.ts @@ -1,4 +1,5 @@ import URL from 'url'; +import { timeoutFetch } from '../util/fetch'; export default class Azteco { /** @@ -14,7 +15,7 @@ export default class Azteco { const url = `${baseURI}blue_despatch.php?CODE_1=${voucher[0]}&CODE_2=${voucher[1]}&CODE_3=${voucher[2]}&CODE_4=${voucher[3]}&ADDRESS=${address}`; try { - const response = await fetch(url, { + const response = await timeoutFetch(url, { method: 'GET', }); return response && response.status === 200; diff --git a/class/wallets/lightning-custodian-wallet.ts b/class/wallets/lightning-custodian-wallet.ts index 6aaed076c..061e5fa3f 100644 --- a/class/wallets/lightning-custodian-wallet.ts +++ b/class/wallets/lightning-custodian-wallet.ts @@ -2,6 +2,7 @@ import bolt11 from 'bolt11'; import { BitcoinUnit, Chain } from '../../models/bitcoinUnits'; import { LegacyWallet } from './legacy-wallet'; import loc from '../../loc'; +import { timeoutFetch } from '../../util/fetch'; export class LightningCustodianWallet extends LegacyWallet { static readonly type = 'lightningCustodianWallet'; @@ -99,7 +100,7 @@ export class LightningCustodianWallet extends LegacyWallet { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), 10000); try { - const response = await fetch(this.baseURI + '/create', { + const response = await timeoutFetch(this.baseURI + '/create', { method: 'POST', body: JSON.stringify({ partnerid: 'bluewallet', accounttype: (isTest && 'test') || 'common' }), headers: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' }, @@ -129,7 +130,7 @@ export class LightningCustodianWallet extends LegacyWallet { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), 10000); try { - const response = await fetch(this.baseURI + '/payinvoice', { + const response = await timeoutFetch(this.baseURI + '/payinvoice', { method: 'POST', body: JSON.stringify({ invoice, amount: freeAmount }), headers: { @@ -167,7 +168,7 @@ export class LightningCustodianWallet extends LegacyWallet { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), 10000); try { - const response = await fetch(this.baseURI + '/getuserinvoices' + limitString, { + const response = await timeoutFetch(this.baseURI + '/getuserinvoices' + limitString, { method: 'GET', headers: { 'Access-Control-Allow-Origin': '*', @@ -238,7 +239,7 @@ export class LightningCustodianWallet extends LegacyWallet { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), 10000); try { - const response = await fetch(this.baseURI + '/addinvoice', { + const response = await timeoutFetch(this.baseURI + '/addinvoice', { method: 'POST', body: JSON.stringify({ amt: amt + '', memo }), headers: { @@ -287,7 +288,7 @@ export class LightningCustodianWallet extends LegacyWallet { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), 10000); try { - const response = await fetch(this.baseURI + '/auth?type=auth', { + const response = await timeoutFetch(this.baseURI + '/auth?type=auth', { method: 'POST', body: JSON.stringify({ login, password }), headers: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' }, @@ -343,7 +344,7 @@ export class LightningCustodianWallet extends LegacyWallet { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), 10000); try { - const response = await fetch(this.baseURI + '/auth?type=refresh_token', { + const response = await timeoutFetch(this.baseURI + '/auth?type=refresh_token', { method: 'POST', body: JSON.stringify({ refresh_token: this.refresh_token }), headers: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' }, @@ -377,7 +378,7 @@ export class LightningCustodianWallet extends LegacyWallet { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), 10000); try { - const response = await fetch(this.baseURI + '/getbtc', { + const response = await timeoutFetch(this.baseURI + '/getbtc', { method: 'GET', headers: { 'Access-Control-Allow-Origin': '*', @@ -467,7 +468,7 @@ export class LightningCustodianWallet extends LegacyWallet { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), 10000); try { - const response = await fetch(this.baseURI + '/getpending', { + const response = await timeoutFetch(this.baseURI + '/getpending', { method: 'GET', headers: { 'Access-Control-Allow-Origin': '*', @@ -504,7 +505,7 @@ export class LightningCustodianWallet extends LegacyWallet { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), 10000); try { - const response = await fetch(this.baseURI + '/gettxs' + queryRes, { + const response = await timeoutFetch(this.baseURI + '/gettxs' + queryRes, { method: 'GET', headers: { 'Access-Control-Allow-Origin': '*', @@ -545,7 +546,7 @@ export class LightningCustodianWallet extends LegacyWallet { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), 10000); try { - const response = await fetch(this.baseURI + '/balance', { + const response = await timeoutFetch(this.baseURI + '/balance', { method: 'GET', headers: { 'Access-Control-Allow-Origin': '*', @@ -642,7 +643,7 @@ export class LightningCustodianWallet extends LegacyWallet { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), 10000); try { - const response = await fetch(this.baseURI + '/getinfo', { + const response = await timeoutFetch(this.baseURI + '/getinfo', { method: 'GET', headers: { 'Access-Control-Allow-Origin': '*', @@ -676,7 +677,7 @@ export class LightningCustodianWallet extends LegacyWallet { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), 10000); try { - const response = await fetch(normalizedAddress.toString(), { + const response = await timeoutFetch(normalizedAddress.toString(), { method: 'GET', headers: { 'Access-Control-Allow-Origin': '*', @@ -731,7 +732,7 @@ export class LightningCustodianWallet extends LegacyWallet { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), 10000); try { - const response = await fetch(this.baseURI + '/decodeinvoice?invoice=' + invoice, { + const response = await timeoutFetch(this.baseURI + '/decodeinvoice?invoice=' + invoice, { method: 'GET', headers: { 'Access-Control-Allow-Origin': '*', diff --git a/tests/integration/lightning-custodian-wallet.test.js b/tests/integration/lightning-custodian-wallet.test.js index 2f9bebe88..c19a4ad39 100644 --- a/tests/integration/lightning-custodian-wallet.test.js +++ b/tests/integration/lightning-custodian-wallet.test.js @@ -1,6 +1,7 @@ import assert from 'assert'; import { LightningCustodianWallet } from '../../class'; +import { timeoutFetch } from '../../util/fetch'; jest.setTimeout(200 * 1000); const baseUri = 'https://lndhub-staging.herokuapp.com'; @@ -164,7 +165,7 @@ describe.skip('LightningCustodianWallet', () => { return; } - const response = await fetch('https://api.opennode.co/v1/charges', { + const response = await timeoutFetch('https://api.opennode.co/v1/charges', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -218,7 +219,7 @@ describe.skip('LightningCustodianWallet', () => { return; } - const response = await fetch('https://api.strike.acinq.co/api/v1/charges', { + const response = await timeoutFetch('https://api.strike.acinq.co/api/v1/charges', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', @@ -289,7 +290,7 @@ describe.skip('LightningCustodianWallet', () => { return; } - const response = await fetch(`https://api-bitrefill.com/v1/lnurl_pay/${process.env.BITREFILL}/callback?amount=1000`, { + const response = await timeoutFetch(`https://api-bitrefill.com/v1/lnurl_pay/${process.env.BITREFILL}/callback?amount=1000`, { method: 'GET', headers: { 'Content-Type': 'application/json', @@ -460,7 +461,7 @@ describe.skip('LightningCustodianWallet', () => { // fetching invoice from tippin.me : - const response = await fetch('https://tippin.me/lndreq/newinvoice.php', { + const response = await timeoutFetch('https://tippin.me/lndreq/newinvoice.php', { method: 'POST', headers: { Origin: 'https://tippin.me', @@ -568,7 +569,7 @@ describe.skip('LightningCustodianWallet', () => { // fetching invoice from tippin.me : - const response = await fetch('https://tippin.me/lndreq/newinvoice.php', { + const response = await timeoutFetch('https://tippin.me/lndreq/newinvoice.php', { method: 'POST', headers: { Origin: 'https://tippin.me',