mirror of
https://codeberg.org/keyoxide/doipjs.git
synced 2024-12-23 06:59: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')
|
||||
|
||||
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) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
|
@ -31,4 +39,9 @@ module.exports = async (hostname) => {
|
|||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
||||
clearTimeout(timeoutHandle)
|
||||
return result
|
||||
})
|
||||
}
|
||||
|
|
|
@ -17,4 +17,4 @@ exports.dns = require('./dns')
|
|||
exports.irc = require('./irc')
|
||||
exports.matrix = require('./matrix')
|
||||
exports.twitter = require('./twitter')
|
||||
exports.xmpp = require('./xmpp')
|
||||
exports.xmpp = require('./xmpp')
|
||||
|
|
|
@ -16,7 +16,15 @@ limitations under the License.
|
|||
const irc = require('irc-upd')
|
||||
|
||||
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 {
|
||||
const client = new irc.Client(opts.hostname, opts.nick, {
|
||||
port: 6697,
|
||||
|
@ -44,4 +52,9 @@ module.exports = async (nickQuery, opts) => {
|
|||
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')
|
||||
|
||||
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}`
|
||||
|
||||
return bentReq(url, null, {
|
||||
const fetchPromise = bentReq(url, null, {
|
||||
Accept: 'application/json',
|
||||
})
|
||||
.then(async (data) => {
|
||||
|
@ -31,4 +39,9 @@ module.exports = async (roomId, eventId, opts) => {
|
|||
.catch((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')
|
||||
|
||||
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`,
|
||||
null,
|
||||
{
|
||||
|
@ -34,4 +42,9 @@ module.exports = async (tweetId, opts) => {
|
|||
.catch((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) => {
|
||||
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) {
|
||||
const xmppStartRes = await xmppStart(
|
||||
opts.service,
|
||||
|
@ -55,20 +63,16 @@ module.exports = async (id, data, opts) => {
|
|||
}
|
||||
|
||||
const response = await iqCaller.request(
|
||||
xml(
|
||||
'iq',
|
||||
{ type: 'get', to: id },
|
||||
xml('vCard', 'vcard-temp')
|
||||
),
|
||||
xml('iq', { type: 'get', to: id }, xml('vCard', 'vcard-temp')),
|
||||
30 * 1000
|
||||
)
|
||||
|
||||
|
||||
const vcardRow = response.getChild('vCard', 'vcard-temp').toString()
|
||||
const dom = new jsdom.JSDOM(vcardRow)
|
||||
|
||||
|
||||
try {
|
||||
let vcard
|
||||
|
||||
|
||||
switch (data.toLowerCase()) {
|
||||
case 'desc':
|
||||
case 'note':
|
||||
|
@ -82,10 +86,9 @@ module.exports = async (id, data, opts) => {
|
|||
throw new Error('No DESC or NOTE field found in vCard')
|
||||
}
|
||||
break
|
||||
|
||||
|
||||
default:
|
||||
vcard = dom.window.document.querySelector(data)
|
||||
.textContent
|
||||
vcard = dom.window.document.querySelector(data).textContent
|
||||
break
|
||||
}
|
||||
xmpp.stop()
|
||||
|
@ -94,4 +97,9 @@ module.exports = async (id, data, opts) => {
|
|||
reject(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
||||
clearTimeout(timeoutHandle)
|
||||
return result
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue