-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6576 from whymarrh/cancellable-fetch
Add custom fetch wrapper with abort on timeout
- Loading branch information
Showing
6 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* global AbortController */ | ||
|
||
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import assert from 'assert' | ||
import nock from 'nock' | ||
|
||
import http from './fetch' | ||
|
||
describe('custom fetch fn', () => { | ||
it('fetches a url', async () => { | ||
nock('https://api.infura.io') | ||
.get('/money') | ||
.reply(200, '{"hodl": false}') | ||
|
||
const fetch = http() | ||
const response = await (await fetch('https://api.infura.io/money')).json() | ||
assert.deepEqual(response, { | ||
hodl: false, | ||
}) | ||
}) | ||
|
||
it('throws when the request hits a custom timeout', async () => { | ||
nock('https://api.infura.io') | ||
.get('/moon') | ||
.delay(2000) | ||
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}') | ||
|
||
const fetch = http({ | ||
timeout: 123, | ||
}) | ||
|
||
try { | ||
await fetch('https://api.infura.io/moon').then(r => r.json()) | ||
assert.fail('Request should throw') | ||
} catch (e) { | ||
assert.ok(e) | ||
} | ||
}) | ||
|
||
it('should abort the request when the custom timeout is hit', async () => { | ||
nock('https://api.infura.io') | ||
.get('/moon') | ||
.delay(2000) | ||
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}') | ||
|
||
const fetch = http({ | ||
timeout: 123, | ||
}) | ||
|
||
try { | ||
await fetch('https://api.infura.io/moon').then(r => r.json()) | ||
assert.fail('Request should be aborted') | ||
} catch (e) { | ||
assert.deepEqual(e.message, 'Aborted') | ||
} | ||
}) | ||
}) |