Skip to content

Commit

Permalink
fix: ensure external requests has early timeout (#162)
Browse files Browse the repository at this point in the history
* fix: avoid hang code

* build: increase timeout

* build: update timeout

* build: ensure timeout early

* build: add timeout

* build: fix identation
  • Loading branch information
Kikobeats authored Mar 31, 2019
1 parent 99bfc73 commit f7a7816
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/metascraper-clearbit-logo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/metascraper-logo-favicon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 13 additions & 17 deletions packages/metascraper-media-provider/src/get-media/twitter-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')
}

Expand All @@ -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,
Expand All @@ -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}`)

Expand Down

0 comments on commit f7a7816

Please sign in to comment.