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') const dns = require('dns')
module.exports = async (hostname) => { module.exports = async (data, opts) => {
let timeoutHandle let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => { const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout( timeoutHandle = setTimeout(
@ -25,14 +25,14 @@ module.exports = async (hostname) => {
}) })
const fetchPromise = new Promise((resolve, reject) => { const fetchPromise = new Promise((resolve, reject) => {
dns.resolveTxt(hostname, (err, records) => { dns.resolveTxt(data.domain, (err, records) => {
if (err) { if (err) {
reject(err) reject(err)
return return
} }
resolve({ resolve({
hostname: hostname, domain: data.domain,
records: { records: {
txt: 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 bent = require('bent')
const req = bent('GET') const req = bent('GET')
module.exports = async (url, format) => { module.exports = async (data, opts) => {
let timeoutHandle let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => { const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout( timeoutHandle = setTimeout(
@ -26,14 +26,14 @@ module.exports = async (url, format) => {
}) })
const fetchPromise = new Promise((resolve, reject) => { const fetchPromise = new Promise((resolve, reject) => {
if (!url) { if (!data.url) {
reject('No valid URI provided') reject('No valid URI provided')
return return
} }
switch (format) { switch (format) {
case 'json': case 'json':
req(url, null, { req(data.url, null, {
Accept: 'application/json', Accept: 'application/json',
'User-Agent': `doipjs/${require('../package.json').version}`, 'User-Agent': `doipjs/${require('../package.json').version}`,
}) })
@ -48,7 +48,7 @@ module.exports = async (url, format) => {
}) })
break break
case 'text': case 'text':
req(url) req(data.url)
.then(async (res) => { .then(async (res) => {
return await res.text() return await res.text()
}) })

View file

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

View file

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

View file

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

View file

@ -16,7 +16,7 @@ limitations under the License.
const bent = require('bent') const bent = require('bent')
const bentReq = bent('GET') const bentReq = bent('GET')
module.exports = async (tweetId, opts) => { module.exports = async (data, opts) => {
let timeoutHandle let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => { const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout( timeoutHandle = setTimeout(
@ -27,7 +27,7 @@ module.exports = async (tweetId, opts) => {
const fetchPromise = new Promise((resolve, reject) => { const fetchPromise = new Promise((resolve, reject) => {
bentReq( 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, null,
{ {
Accept: 'application/json', 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 let timeoutHandle
const timeoutPromise = new Promise((resolve, reject) => { const timeoutPromise = new Promise((resolve, reject) => {
timeoutHandle = setTimeout( timeoutHandle = setTimeout(
@ -63,7 +63,7 @@ module.exports = async (id, data, opts) => {
} }
const response = await iqCaller.request( 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 30 * 1000
) )
@ -73,7 +73,7 @@ module.exports = async (id, data, opts) => {
try { try {
let vcard let vcard
switch (data.toLowerCase()) { switch (data.field.toLowerCase()) {
case 'desc': case 'desc':
case 'note': case 'note':
vcard = dom.window.document.querySelector('note text') vcard = dom.window.document.querySelector('note text')