Make jsenv detection conditional without return

This commit is contained in:
Yarmo Mackenbach 2021-04-26 12:28:43 +02:00
parent da59262c45
commit e0dc5f4b21
No known key found for this signature in database
GPG key ID: 37367F4AF4087AD1
3 changed files with 215 additions and 218 deletions

View file

@ -26,47 +26,46 @@ const jsEnv = require("browser-or-node")
module.exports.timeout = 5000 module.exports.timeout = 5000
if (!jsEnv.isNode) { if (!jsEnv.isNode) {
module.exports.fn = null const dns = require('dns')
return
}
const dns = require('dns') /**
* Execute a fetch request
* @function
* @async
* @param {object} data - Data used in the request
* @param {string} data.domain - The targeted domain
* @returns {object}
*/
module.exports.fn = async (data, opts) => {
let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(
() => reject(new Error('Request was timed out')),
data.fetcherTimeout ? data.fetcherTimeout : module.exports.timeout
)
})
/** const fetchPromise = new Promise((resolve, reject) => {
* Execute a fetch request dns.resolveTxt(data.domain, (err, records) => {
* @function if (err) {
* @async reject(err)
* @param {object} data - Data used in the request return
* @param {string} data.domain - The targeted domain }
* @returns {object}
*/
module.exports.fn = async (data, opts) => {
let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(
() => reject(new Error('Request was timed out')),
data.fetcherTimeout ? data.fetcherTimeout : module.exports.timeout
)
})
const fetchPromise = new Promise((resolve, reject) => { resolve({
dns.resolveTxt(data.domain, (err, records) => { domain: data.domain,
if (err) { records: {
reject(err) txt: records,
return },
} })
resolve({
domain: data.domain,
records: {
txt: records,
},
}) })
}) })
})
return Promise.race([fetchPromise, timeoutPromise]).then((result) => { return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
clearTimeout(timeoutHandle) clearTimeout(timeoutHandle)
return result return result
}) })
} }
} else {
module.exports.fn = null
}

View file

@ -25,71 +25,70 @@ const jsEnv = require("browser-or-node")
*/ */
module.exports.timeout = 20000 module.exports.timeout = 20000
if (!jsEnv.isNode) { if (jsEnv.isNode) {
const irc = require('irc-upd')
const validator = require('validator')
/**
* Execute a fetch request
* @function
* @async
* @param {object} data - Data used in the request
* @param {string} data.nick - The nick of the targeted account
* @param {string} data.domain - The domain on which the targeted account is registered
* @param {object} opts - Options used to enable the request
* @param {string} opts.claims.irc.nick - The nick to be used by the library to log in
* @returns {object}
*/
module.exports.fn = async (data, opts) => {
let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(
() => reject(new Error('Request was timed out')),
data.fetcherTimeout ? data.fetcherTimeout : module.exports.timeout
)
})
const fetchPromise = new Promise((resolve, reject) => {
try {
validator.isAscii(opts.claims.irc.nick)
} catch (err) {
throw new Error(`IRC fetcher was not set up properly (${err.message})`)
}
try {
const client = new irc.Client(data.domain, opts.claims.irc.nick, {
port: 6697,
secure: true,
channels: [],
})
const reKey = /[a-zA-Z0-9\-\_]+\s+:\s(openpgp4fpr\:.*)/
const reEnd = /End\sof\s.*\staxonomy./
let keys = []
client.addListener('registered', (message) => {
client.send(`PRIVMSG NickServ :TAXONOMY ${data.nick}`)
})
client.addListener('notice', (nick, to, text, message) => {
if (reKey.test(text)) {
const match = text.match(reKey)
keys.push(match[1])
}
if (reEnd.test(text)) {
client.disconnect()
resolve(keys)
}
})
} catch (error) {
reject(error)
}
})
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
clearTimeout(timeoutHandle)
return result
})
}
} else {
module.exports.fn = null module.exports.fn = null
return
}
const irc = require('irc-upd')
const validator = require('validator')
/**
* Execute a fetch request
* @function
* @async
* @param {object} data - Data used in the request
* @param {string} data.nick - The nick of the targeted account
* @param {string} data.domain - The domain on which the targeted account is registered
* @param {object} opts - Options used to enable the request
* @param {string} opts.claims.irc.nick - The nick to be used by the library to log in
* @returns {object}
*/
module.exports.fn = async (data, opts) => {
let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(
() => reject(new Error('Request was timed out')),
data.fetcherTimeout ? data.fetcherTimeout : module.exports.timeout
)
})
const fetchPromise = new Promise((resolve, reject) => {
try {
validator.isAscii(opts.claims.irc.nick)
} catch (err) {
throw new Error(`IRC fetcher was not set up properly (${err.message})`)
}
try {
const client = new irc.Client(data.domain, opts.claims.irc.nick, {
port: 6697,
secure: true,
channels: [],
})
const reKey = /[a-zA-Z0-9\-\_]+\s+:\s(openpgp4fpr\:.*)/
const reEnd = /End\sof\s.*\staxonomy./
let keys = []
client.addListener('registered', (message) => {
client.send(`PRIVMSG NickServ :TAXONOMY ${data.nick}`)
})
client.addListener('notice', (nick, to, text, message) => {
if (reKey.test(text)) {
const match = text.match(reKey)
keys.push(match[1])
}
if (reEnd.test(text)) {
client.disconnect()
resolve(keys)
}
})
} catch (error) {
reject(error)
}
})
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
clearTimeout(timeoutHandle)
return result
})
} }

View file

@ -25,119 +25,118 @@ const jsEnv = require("browser-or-node")
*/ */
module.exports.timeout = 5000 module.exports.timeout = 5000
if (!jsEnv.isNode) { if (jsEnv.isNode) {
module.exports.fn = null const jsdom = require('jsdom')
return const { client, xml } = require('@xmpp/client')
} const debug = require('@xmpp/debug')
const validator = require('validator')
const jsdom = require('jsdom')
const { client, xml } = require('@xmpp/client') let xmpp = null,
const debug = require('@xmpp/debug') iqCaller = null
const validator = require('validator')
const xmppStart = async (service, username, password) => {
let xmpp = null, return new Promise((resolve, reject) => {
iqCaller = null const xmpp = client({
service: service,
const xmppStart = async (service, username, password) => { username: username,
return new Promise((resolve, reject) => { password: password,
const xmpp = client({ })
service: service, if (process.env.NODE_ENV !== 'production') {
username: username, debug(xmpp, true)
password: password,
})
if (process.env.NODE_ENV !== 'production') {
debug(xmpp, true)
}
const { iqCaller } = xmpp
xmpp.start()
xmpp.on('online', (address) => {
resolve({ xmpp: xmpp, iqCaller: iqCaller })
})
xmpp.on('error', (error) => {
reject(error)
})
})
}
/**
* Execute a fetch request
* @function
* @async
* @param {object} data - Data used in the request
* @param {string} data.id - The identifier of the targeted account
* @param {string} data.field - The vCard field to return (should be "note")
* @param {object} opts - Options used to enable the request
* @param {string} opts.claims.xmpp.service - The server hostname on which the library can log in
* @param {string} opts.claims.xmpp.username - The username used to log in
* @param {string} opts.claims.xmpp.password - The password used to log in
* @returns {object}
*/
module.exports.fn = async (data, opts) => {
let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(
() => reject(new Error('Request was timed out')),
data.fetcherTimeout ? data.fetcherTimeout : module.exports.timeout
)
})
const fetchPromise = new Promise(async (resolve, reject) => {
try {
validator.isFQDN(opts.claims.xmpp.service)
validator.isAscii(opts.claims.xmpp.username)
validator.isAscii(opts.claims.xmpp.password)
} catch (err) {
throw new Error(`XMPP fetcher was not set up properly (${err.message})`)
}
if (!xmpp || xmpp.status !== 'online') {
const xmppStartRes = await xmppStart(
opts.claims.xmpp.service,
opts.claims.xmpp.username,
opts.claims.xmpp.password
)
xmpp = xmppStartRes.xmpp
iqCaller = xmppStartRes.iqCaller
}
const response = await iqCaller.request(
xml('iq', { type: 'get', to: data.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.field.toLowerCase()) {
case 'desc':
case 'note':
vcard = dom.window.document.querySelector('note text')
if (!vcard) {
vcard = dom.window.document.querySelector('DESC')
}
if (vcard) {
vcard = vcard.textContent
} else {
throw new Error('No DESC or NOTE field found in vCard')
}
break
default:
vcard = dom.window.document.querySelector(data).textContent
break
} }
xmpp.stop() const { iqCaller } = xmpp
resolve(vcard) xmpp.start()
} catch (error) { xmpp.on('online', (address) => {
reject(error) resolve({ xmpp: xmpp, iqCaller: iqCaller })
} })
}) xmpp.on('error', (error) => {
reject(error)
return Promise.race([fetchPromise, timeoutPromise]).then((result) => { })
clearTimeout(timeoutHandle) })
return result }
})
/**
* Execute a fetch request
* @function
* @async
* @param {object} data - Data used in the request
* @param {string} data.id - The identifier of the targeted account
* @param {string} data.field - The vCard field to return (should be "note")
* @param {object} opts - Options used to enable the request
* @param {string} opts.claims.xmpp.service - The server hostname on which the library can log in
* @param {string} opts.claims.xmpp.username - The username used to log in
* @param {string} opts.claims.xmpp.password - The password used to log in
* @returns {object}
*/
module.exports.fn = async (data, opts) => {
let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(
() => reject(new Error('Request was timed out')),
data.fetcherTimeout ? data.fetcherTimeout : module.exports.timeout
)
})
const fetchPromise = new Promise(async (resolve, reject) => {
try {
validator.isFQDN(opts.claims.xmpp.service)
validator.isAscii(opts.claims.xmpp.username)
validator.isAscii(opts.claims.xmpp.password)
} catch (err) {
throw new Error(`XMPP fetcher was not set up properly (${err.message})`)
}
if (!xmpp || xmpp.status !== 'online') {
const xmppStartRes = await xmppStart(
opts.claims.xmpp.service,
opts.claims.xmpp.username,
opts.claims.xmpp.password
)
xmpp = xmppStartRes.xmpp
iqCaller = xmppStartRes.iqCaller
}
const response = await iqCaller.request(
xml('iq', { type: 'get', to: data.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.field.toLowerCase()) {
case 'desc':
case 'note':
vcard = dom.window.document.querySelector('note text')
if (!vcard) {
vcard = dom.window.document.querySelector('DESC')
}
if (vcard) {
vcard = vcard.textContent
} else {
throw new Error('No DESC or NOTE field found in vCard')
}
break
default:
vcard = dom.window.document.querySelector(data).textContent
break
}
xmpp.stop()
resolve(vcard)
} catch (error) {
reject(error)
}
})
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
clearTimeout(timeoutHandle)
return result
})
}
} else {
module.exports.fn = null
} }