Update fetchers, add gitlab fetcher

This commit is contained in:
Yarmo Mackenbach 2021-04-12 16:14:18 +02:00
parent 228f924ece
commit bb5f3ccea2
No known key found for this signature in database
GPG key ID: 37367F4AF4087AD1
8 changed files with 92 additions and 21 deletions

View file

@ -15,7 +15,7 @@ limitations under the License.
*/
const dns = require('dns')
module.exports = async (hostname) => {
module.exports = async (data, opts) => {
let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(
@ -25,14 +25,14 @@ module.exports = async (hostname) => {
})
const fetchPromise = new Promise((resolve, reject) => {
dns.resolveTxt(hostname, (err, records) => {
dns.resolveTxt(data.domain, (err, records) => {
if (err) {
reject(err)
return
}
resolve({
hostname: hostname,
domain: data.domain,
records: {
txt: records,
},

70
src/fetcher/gitlab.js Normal file
View file

@ -0,0 +1,70 @@
/*
Copyright 2021 Yarmo Mackenbach
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const bent = require('bent')
const req = bent('GET')
module.exports = async (data, opts) => {
let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(
() => reject(new Error('Request was timed out')),
5000
)
})
const fetchPromise = new Promise((resolve, reject) => {
const urlUser = `https://${data.domain}/api/v4/users?username=${data.username}`
let resUser
try {
resUser = await req(urlUser, null, { Accept: 'application/json' })
} catch (e) {
resUser = await req(utils.generateProxyURL('web', urlUser, opts), null, {
Accept: 'application/json',
})
}
const jsonUser = await resUser.json()
const user = jsonUser.find((user) => user.username === match[2])
if (!user) {
reject(`No user with username ${match[2]}`)
}
const urlProject = `https://${data.domain}/api/v4/users/${user.id}/projects`
let resProject
try {
resProject = await req(urlProject, null, { Accept: 'application/json' })
} catch (e) {
resProject = await req(
utils.generateProxyURL('web', urlProject, opts),
null,
{ Accept: 'application/json' }
)
}
const jsonProject = await resProject.json()
const project = jsonProject.find((proj) => proj.path === 'gitlab_proof')
if (!project) {
reject(`No project at ${spData.proof.uri}`)
}
resolve(project)
})
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
clearTimeout(timeoutHandle)
return result
})
}

View file

@ -16,7 +16,7 @@ limitations under the License.
const bent = require('bent')
const req = bent('GET')
module.exports = async (url, format) => {
module.exports = async (data, opts) => {
let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(
@ -26,14 +26,14 @@ module.exports = async (url, format) => {
})
const fetchPromise = new Promise((resolve, reject) => {
if (!url) {
if (!data.url) {
reject('No valid URI provided')
return
}
switch (format) {
case 'json':
req(url, null, {
req(data.url, null, {
Accept: 'application/json',
'User-Agent': `doipjs/${require('../package.json').version}`,
})
@ -48,7 +48,7 @@ module.exports = async (url, format) => {
})
break
case 'text':
req(url)
req(data.url)
.then(async (res) => {
return await res.text()
})

View file

@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
exports.dns = require('./dns')
exports.gitlab = require('./gitlab')
exports.http = require('./http')
exports.irc = require('./irc')
exports.matrix = require('./matrix')

View file

@ -15,7 +15,7 @@ limitations under the License.
*/
const irc = require('irc-upd')
module.exports = async (hostname, nickQuery, opts) => {
module.exports = async (data, opts) => {
let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(
@ -26,7 +26,7 @@ module.exports = async (hostname, nickQuery, opts) => {
const fetchPromise = new Promise((resolve, reject) => {
try {
const client = new irc.Client(hostname, opts.nick, {
const client = new irc.Client(data.domain, opts.nick, {
port: 6697,
secure: true,
channels: [],
@ -36,7 +36,7 @@ module.exports = async (hostname, nickQuery, opts) => {
let keys = []
client.addListener('registered', (message) => {
client.send(`PRIVMSG NickServ :TAXONOMY ${nickQuery}`)
client.send(`PRIVMSG NickServ :TAXONOMY ${data.nick}`)
})
client.addListener('notice', (nick, to, text, message) => {
if (reKey.test(text)) {

View file

@ -16,7 +16,7 @@ limitations under the License.
const bent = require('bent')
const bentReq = bent('GET')
module.exports = async (roomId, eventId, opts) => {
module.exports = async (data, opts) => {
let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(
@ -25,17 +25,17 @@ module.exports = async (roomId, eventId, opts) => {
)
})
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/${data.roomId}/event/${data.eventId}?access_token=${opts.accessToken}`
const fetchPromise = new Promise((resolve, reject) => {
bentReq(url, null, {
Accept: 'application/json',
})
.then(async (data) => {
return await data.json()
.then(async (res) => {
return await res.json()
})
.then((data) => {
resolve(data)
.then((res) => {
resolve(res)
})
.catch((error) => {
reject(error)

View file

@ -16,7 +16,7 @@ limitations under the License.
const bent = require('bent')
const bentReq = bent('GET')
module.exports = async (tweetId, opts) => {
module.exports = async (data, opts) => {
let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(
@ -27,7 +27,7 @@ module.exports = async (tweetId, opts) => {
const fetchPromise = new Promise((resolve, reject) => {
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=${data.tweetId}&tweet_mode=extended`,
null,
{
Accept: 'application/json',

View file

@ -42,7 +42,7 @@ const xmppStart = async (service, username, password) => {
})
}
module.exports = async (id, data, opts) => {
module.exports = async (data, opts) => {
let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout(
@ -63,7 +63,7 @@ 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: data.id }, xml('vCard', 'vcard-temp')),
30 * 1000
)
@ -73,7 +73,7 @@ module.exports = async (id, data, opts) => {
try {
let vcard
switch (data.toLowerCase()) {
switch (data.field.toLowerCase()) {
case 'desc':
case 'note':
vcard = dom.window.document.querySelector('note text')