1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 01:39:44 +01:00

Combine fetch-with-timeout implementations (#7084)

There were two competing utility functions for calling fetch with a
timeout. They have been combined into one.
This commit is contained in:
Mark Stacey 2019-09-04 17:00:11 -03:00 committed by GitHub
parent 3e9d247d4b
commit 1e7b37d1cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 54 deletions

View File

@ -2,7 +2,8 @@ const ObservableStore = require('obs-store')
const log = require('loglevel')
const BN = require('bn.js')
const createId = require('../lib/random-id')
const { bnToHex, fetchWithTimeout } = require('../lib/util')
const { bnToHex } = require('../lib/util')
import fetchWithTimeout from '../lib/fetch-with-timeout'
const {
MAINNET_CODE,
ROPSTEN_CODE,

View File

@ -0,0 +1,23 @@
const fetchWithTimeout = ({ timeout = 120000 } = {}) => {
return async function _fetch (url, opts) {
const abortController = new AbortController()
const abortSignal = abortController.signal
const f = fetch(url, {
...opts,
signal: abortSignal,
})
const timer = setTimeout(() => abortController.abort(), timeout)
try {
const res = await f
clearTimeout(timer)
return res
} catch (e) {
clearTimeout(timer)
throw e
}
}
}
export default fetchWithTimeout

View File

@ -144,29 +144,6 @@ function removeListeners (listeners, emitter) {
})
}
function fetchWithTimeout ({ timeout = 120000 } = {}) {
return async function _fetch (url, opts) {
const abortController = new AbortController()
const abortSignal = abortController.signal
const f = fetch(url, {
...opts,
signal: abortSignal,
})
const timer = setTimeout(() => abortController.abort(), timeout)
try {
const res = await f
clearTimeout(timer)
return res
} catch (e) {
clearTimeout(timer)
throw e
}
}
}
module.exports = {
removeListeners,
applyListeners,
@ -177,5 +154,4 @@ module.exports = {
hexToBn,
bnToHex,
BnMultiplyByFraction,
fetchWithTimeout,
}

View File

@ -1,15 +1,15 @@
import assert from 'assert'
import nock from 'nock'
import http from './fetch'
import fetchWithTimeout from '../../../app/scripts/lib/fetch-with-timeout'
describe('custom fetch fn', () => {
describe('fetchWithTimeout', () => {
it('fetches a url', async () => {
nock('https://api.infura.io')
.get('/money')
.reply(200, '{"hodl": false}')
const fetch = http()
const fetch = fetchWithTimeout()
const response = await (await fetch('https://api.infura.io/money')).json()
assert.deepEqual(response, {
hodl: false,
@ -22,7 +22,7 @@ describe('custom fetch fn', () => {
.delay(2000)
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}')
const fetch = http({
const fetch = fetchWithTimeout({
timeout: 123,
})
@ -40,7 +40,7 @@ describe('custom fetch fn', () => {
.delay(2000)
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}')
const fetch = http({
const fetch = fetchWithTimeout({
timeout: 123,
})

View File

@ -2,7 +2,7 @@ import {
loadLocalStorageData,
saveLocalStorageData,
} from '../../../lib/local-storage-helpers'
import fetchWithTimeout from './fetch'
import fetchWithTimeout from '../../../../app/scripts/lib/fetch-with-timeout'
const fetchWithCache = async (url, fetchOptions = {}, { cacheRefreshTime = 360000, timeout = 30000 } = {}) => {
if (fetchOptions.body || (fetchOptions.method && fetchOptions.method !== 'GET')) {

View File

@ -1,23 +0,0 @@
export default function ({ timeout = 120000 } = {}) {
return function _fetch (url, opts) {
return new Promise(async (resolve, reject) => {
const abortController = new AbortController()
const abortSignal = abortController.signal
const f = fetch(url, {
...opts,
signal: abortSignal,
})
const timer = setTimeout(() => abortController.abort(), timeout)
try {
const res = await f
clearTimeout(timer)
return resolve(res)
} catch (e) {
clearTimeout(timer)
return reject(e)
}
})
}
}