From f7a78169230b448b502d2f13020ab9be36dee5b9 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Sun, 31 Mar 2019 19:36:33 +0200 Subject: [PATCH] fix: ensure external requests has early timeout (#162) * fix: avoid hang code * build: increase timeout * build: update timeout * build: ensure timeout early * build: add timeout * build: fix identation --- packages/metascraper-clearbit-logo/index.js | 2 +- packages/metascraper-logo-favicon/index.js | 2 +- .../src/get-media/twitter-info.js | 30 ++++++++----------- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/metascraper-clearbit-logo/index.js b/packages/metascraper-clearbit-logo/index.js index 128359a82..031b18b49 100644 --- a/packages/metascraper-clearbit-logo/index.js +++ b/packages/metascraper-clearbit-logo/index.js @@ -17,7 +17,7 @@ module.exports = opts => { const logoUrl = apiUrl(url, opts) try { - await got.head(logoUrl) + await got.head(logoUrl, { retry: 0, timeout: 1000 }) return logoUrl } catch (err) { return null diff --git a/packages/metascraper-logo-favicon/index.js b/packages/metascraper-logo-favicon/index.js index 456b0ae50..7c5b115fc 100644 --- a/packages/metascraper-logo-favicon/index.js +++ b/packages/metascraper-logo-favicon/index.js @@ -62,7 +62,7 @@ module.exports = () => ({ const logoUrl = resolveUrl(origin, 'favicon.ico') try { - await got.head(logoUrl) + await got.head(logoUrl, { retry: 0, timeout: 10000 }) return logoUrl } catch (err) { return null diff --git a/packages/metascraper-media-provider/src/get-media/twitter-info.js b/packages/metascraper-media-provider/src/get-media/twitter-info.js index 9855addd2..43c31ef1a 100644 --- a/packages/metascraper-media-provider/src/get-media/twitter-info.js +++ b/packages/metascraper-media-provider/src/get-media/twitter-info.js @@ -15,6 +15,8 @@ const TWITTER_BEARER_TOKEN = const TWITTER_HOSTNAMES = ['twitter.com', 'mobile.twitter.com'] +const TOKEN_TIMEOUT = 6000 + const isTweet = url => url.includes('/status/') const isTwitterHost = url => TWITTER_HOSTNAMES.includes(new URL(url).hostname) @@ -33,23 +35,20 @@ const agent = PROXY_HOST proxy: { host: PROXY_HOST, port: PROXY_PORT, - proxyAuth: - PROXY_USER && PROXY_PASS ? `${PROXY_USER}:${PROXY_PASS}` : null + proxyAuth: PROXY_USER && PROXY_PASS ? `${PROXY_USER}:${PROXY_PASS}` : null } }) : null const getGuestToken = async (url = '', opts = {}) => { - const { body } = await got.post( - 'https://api.twitter.com/1.1/guest/activate.json', - { - headers: { Authorization: TWITTER_BEARER_TOKEN }, - json: true, - retry: 0, - agent, - ...opts - } - ) + const { body } = await got.post('https://api.twitter.com/1.1/guest/activate.json', { + headers: { Authorization: TWITTER_BEARER_TOKEN }, + json: true, + timeout: TOKEN_TIMEOUT / 2, + retry: 0, + agent, + ...opts + }) return get(body, 'guest_token') } @@ -61,6 +60,7 @@ const getTwitterInfo = ({ getToken, ...opts }) => async url => { got(apiUrl, { agent, retry: 0, + timeout: TOKEN_TIMEOUT, json: true, headers: { authorization: TWITTER_BEARER_TOKEN, @@ -72,11 +72,7 @@ const getTwitterInfo = ({ getToken, ...opts }) => async url => { const body = get(res, 'value.body') - const id = get( - body, - `globalObjects.tweets.${tweetId}.retweeted_status_id_str`, - tweetId - ) + const id = get(body, `globalObjects.tweets.${tweetId}.retweeted_status_id_str`, tweetId) const tweetObj = get(body, `globalObjects.tweets.${id}`)