mirror of
https://codeberg.org/keyoxide/doipjs.git
synced 2025-01-10 22:49:29 -07:00
Add timeout to fetcher functions
This commit is contained in:
parent
5ea67c1e6f
commit
e6e25a65e7
6 changed files with 82 additions and 22 deletions
|
@ -16,7 +16,15 @@ limitations under the License.
|
||||||
const dns = require('dns')
|
const dns = require('dns')
|
||||||
|
|
||||||
module.exports = async (hostname) => {
|
module.exports = async (hostname) => {
|
||||||
return new Promise((resolve, reject) => {
|
let timeoutHandle
|
||||||
|
const timeoutPromise = new Promise((resolve, reject) => {
|
||||||
|
timeoutHandle = setTimeout(
|
||||||
|
() => reject(new Error('Request was timed out')),
|
||||||
|
5000
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
const fetchPromise = new Promise((resolve, reject) => {
|
||||||
dns.resolveTxt(hostname, (err, records) => {
|
dns.resolveTxt(hostname, (err, records) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err)
|
reject(err)
|
||||||
|
@ -31,4 +39,9 @@ module.exports = async (hostname) => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
||||||
|
clearTimeout(timeoutHandle)
|
||||||
|
return result
|
||||||
|
})
|
||||||
}
|
}
|
|
@ -16,7 +16,15 @@ limitations under the License.
|
||||||
const irc = require('irc-upd')
|
const irc = require('irc-upd')
|
||||||
|
|
||||||
module.exports = async (nickQuery, opts) => {
|
module.exports = async (nickQuery, opts) => {
|
||||||
return new Promise((resolve, reject) => {
|
let timeoutHandle
|
||||||
|
const timeoutPromise = new Promise((resolve, reject) => {
|
||||||
|
timeoutHandle = setTimeout(
|
||||||
|
() => reject(new Error('Request was timed out')),
|
||||||
|
5000
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
const fetchPromise = new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
const client = new irc.Client(opts.hostname, opts.nick, {
|
const client = new irc.Client(opts.hostname, opts.nick, {
|
||||||
port: 6697,
|
port: 6697,
|
||||||
|
@ -44,4 +52,9 @@ module.exports = async (nickQuery, opts) => {
|
||||||
reject(error)
|
reject(error)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
||||||
|
clearTimeout(timeoutHandle)
|
||||||
|
return result
|
||||||
|
})
|
||||||
}
|
}
|
|
@ -17,9 +17,17 @@ const bent = require('bent')
|
||||||
const bentReq = bent('GET')
|
const bentReq = bent('GET')
|
||||||
|
|
||||||
module.exports = async (roomId, eventId, opts) => {
|
module.exports = async (roomId, eventId, opts) => {
|
||||||
|
let timeoutHandle
|
||||||
|
const timeoutPromise = new Promise((resolve, reject) => {
|
||||||
|
timeoutHandle = setTimeout(
|
||||||
|
() => reject(new Error('Request was timed out')),
|
||||||
|
5000
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
const url = `https://${opts.instance}/_matrix/client/r0/rooms/${roomId}/event/${eventId}?access_token=${opts.accessToken}`
|
const url = `https://${opts.instance}/_matrix/client/r0/rooms/${roomId}/event/${eventId}?access_token=${opts.accessToken}`
|
||||||
|
|
||||||
return bentReq(url, null, {
|
const fetchPromise = bentReq(url, null, {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
})
|
})
|
||||||
.then(async (data) => {
|
.then(async (data) => {
|
||||||
|
@ -31,4 +39,9 @@ module.exports = async (roomId, eventId, opts) => {
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
return error
|
return error
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
||||||
|
clearTimeout(timeoutHandle)
|
||||||
|
return result
|
||||||
|
})
|
||||||
}
|
}
|
|
@ -17,7 +17,15 @@ const bent = require('bent')
|
||||||
const bentReq = bent('GET')
|
const bentReq = bent('GET')
|
||||||
|
|
||||||
module.exports = async (tweetId, opts) => {
|
module.exports = async (tweetId, opts) => {
|
||||||
return bentReq(
|
let timeoutHandle
|
||||||
|
const timeoutPromise = new Promise((resolve, reject) => {
|
||||||
|
timeoutHandle = setTimeout(
|
||||||
|
() => reject(new Error('Request was timed out')),
|
||||||
|
5000
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
const fetchPromise = bentReq(
|
||||||
`https://api.twitter.com/1.1/statuses/show.json?id=${tweetId}&tweet_mode=extended`,
|
`https://api.twitter.com/1.1/statuses/show.json?id=${tweetId}&tweet_mode=extended`,
|
||||||
null,
|
null,
|
||||||
{
|
{
|
||||||
|
@ -34,4 +42,9 @@ module.exports = async (tweetId, opts) => {
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
return error
|
return error
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
||||||
|
clearTimeout(timeoutHandle)
|
||||||
|
return result
|
||||||
|
})
|
||||||
}
|
}
|
|
@ -43,7 +43,15 @@ const xmppStart = async (service, username, password) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = async (id, data, opts) => {
|
module.exports = async (id, data, opts) => {
|
||||||
return new Promise(async (resolve, reject) => {
|
let timeoutHandle
|
||||||
|
const timeoutPromise = new Promise((resolve, reject) => {
|
||||||
|
timeoutHandle = setTimeout(
|
||||||
|
() => reject(new Error('Request was timed out')),
|
||||||
|
5000
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
const fetchPromise = new Promise(async (resolve, reject) => {
|
||||||
if (!xmpp) {
|
if (!xmpp) {
|
||||||
const xmppStartRes = await xmppStart(
|
const xmppStartRes = await xmppStart(
|
||||||
opts.service,
|
opts.service,
|
||||||
|
@ -55,11 +63,7 @@ module.exports = async (id, data, opts) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await iqCaller.request(
|
const response = await iqCaller.request(
|
||||||
xml(
|
xml('iq', { type: 'get', to: id }, xml('vCard', 'vcard-temp')),
|
||||||
'iq',
|
|
||||||
{ type: 'get', to: id },
|
|
||||||
xml('vCard', 'vcard-temp')
|
|
||||||
),
|
|
||||||
30 * 1000
|
30 * 1000
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -84,8 +88,7 @@ module.exports = async (id, data, opts) => {
|
||||||
break
|
break
|
||||||
|
|
||||||
default:
|
default:
|
||||||
vcard = dom.window.document.querySelector(data)
|
vcard = dom.window.document.querySelector(data).textContent
|
||||||
.textContent
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
xmpp.stop()
|
xmpp.stop()
|
||||||
|
@ -94,4 +97,9 @@ module.exports = async (id, data, opts) => {
|
||||||
reject(error)
|
reject(error)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
||||||
|
clearTimeout(timeoutHandle)
|
||||||
|
return result
|
||||||
|
})
|
||||||
}
|
}
|
Loading…
Reference in a new issue