Skip to content

Commit

Permalink
refactor: better PCancelable.fn integration (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats authored Jun 3, 2023
1 parent 14a729c commit 9eb6273
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ const addHtml = require('./html')

const REQ_TIMEOUT = 8000

const fetch = (
url,
{ reflect = false, toEncode, timeout = REQ_TIMEOUT, ...opts }
) =>
new PCancelable(async (resolve, reject, onCancel) => {
const fetch = PCancelable.fn(
async (
url,
{ reflect = false, toEncode, timeout = REQ_TIMEOUT, ...opts },
onCancel
) => {
const reqTimeout = reflect ? timeout / 2 : timeout

const req = got(url, {
Expand All @@ -36,26 +37,27 @@ const fetch = (

try {
const res = await req
return resolve({
return {
headers: res.headers,
html: await toEncode(res.body, res.headers['content-type']),
mode: 'fetch',
url: res.url,
statusCode: res.statusCode
})
}
} catch (error) {
debug('fetch:error', { url, message: error.message || error, reflect })
return reflect
? resolve({ isRejected: true, error })
: resolve({
url,
html: '',
mode: 'fetch',
headers: error.response ? error.response.headers : {},
statusCode: error.response ? error.response.statusCode : undefined
})
? { isRejected: true, error }
: {
url,
html: '',
mode: 'fetch',
headers: error.response ? error.response.headers : {},
statusCode: error.response ? error.response.statusCode : undefined
}
}
})
}
)

const prerender = async (
url,
Expand Down

0 comments on commit 9eb6273

Please sign in to comment.