From f3e39925c0d6f852af6242ca43fd03d98d8e88ef Mon Sep 17 00:00:00 2001 From: Cory Smith Date: Tue, 25 Jan 2022 13:42:34 -0900 Subject: [PATCH] Add response timeout settings --- index.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 847cf3c..7f70ca2 100644 --- a/index.js +++ b/index.js @@ -41,7 +41,10 @@ const fetchCycle = async (cycle = 'Current') => { const response = await superagent .get(BASE_URL) .set('Accept', ACCEPT) - .timeout({ deadline: 30000 }) + .timeout({ + response: 30e3, + deadline: 60e3 + }) .retry(3) const $ = cheerio.load(response.text) @@ -62,12 +65,12 @@ const fetchCycle = async (cycle = 'Current') => { terminalProcedures.fetchCycle = fetchCycle terminalProcedures.getCycleEffectiveDates = async (cycle = 'Current') => { - const { text: currentCycle, } = await fetchCycle(cycle) - if (!currentCycle) { - console.warn(`Could not retrieve ${cycle} cycle effective dates`) + const currentCycle = await fetchCycle(cycle) + if (!currentCycle || !currentCycle.text) { + console.warn(`${cycle} cycle effective dates not found or not available.`) return } - return parseEffectiveDates(currentCycle.replace(/(\n|\t)/gm, '')) + return parseEffectiveDates(currentCycle.text.replace(/(\n|\t)/gm, '')) } terminalProcedures.currentCycleEffectiveDates = async () => { @@ -79,8 +82,8 @@ terminalProcedures.currentCycleEffectiveDates = async () => { */ const fetchCurrentCycleCode = async () => { const cycle = await fetchCycle('Current') - if (!cycle) { - console.warn('Current cycle not found or not available.') + if (!cycle || !cycle.val ) { + console.warn('Current cycle code not found or not available.') return null } return cycle.val @@ -93,8 +96,8 @@ terminalProcedures.fetchCurrentCycleCode = fetchCurrentCycleCode */ const fetchNextCycleCode = async () => { const cycle = await fetchCycle('Next') - if (!cycle) { - console.warn('Next cycle not found or not available. The Next cycle is available 19 days before the end of the current cycle.') + if (!cycle || !cycle.val) { + console.warn('Next cycle code not found or not available. The Next cycle is available 19 days before the end of the current cycle.') return null } return cycle.val @@ -197,7 +200,10 @@ const listOne = async (icao, options) => { const getProcedures = async url => superagent .get(url) .set('Accept', ACCEPT) - .timeout({ deadline: 30000 }) + .timeout({ + response: 10e3, + deadline: 60e3 + }) .retry(3) .then(res => parse(res.text))