From bb5f3ccea24bd09c4734652058f1c8b9654d92ad Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Mon, 12 Apr 2021 16:14:18 +0200 Subject: [PATCH] Update fetchers, add gitlab fetcher --- src/fetcher/dns.js | 6 ++-- src/fetcher/gitlab.js | 70 ++++++++++++++++++++++++++++++++++++++++++ src/fetcher/http.js | 8 ++--- src/fetcher/index.js | 1 + src/fetcher/irc.js | 6 ++-- src/fetcher/matrix.js | 12 ++++---- src/fetcher/twitter.js | 4 +-- src/fetcher/xmpp.js | 6 ++-- 8 files changed, 92 insertions(+), 21 deletions(-) create mode 100644 src/fetcher/gitlab.js diff --git a/src/fetcher/dns.js b/src/fetcher/dns.js index 503e142..a2fd886 100644 --- a/src/fetcher/dns.js +++ b/src/fetcher/dns.js @@ -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, }, diff --git a/src/fetcher/gitlab.js b/src/fetcher/gitlab.js new file mode 100644 index 0000000..874c781 --- /dev/null +++ b/src/fetcher/gitlab.js @@ -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 + }) +} diff --git a/src/fetcher/http.js b/src/fetcher/http.js index eef874d..43c65d9 100644 --- a/src/fetcher/http.js +++ b/src/fetcher/http.js @@ -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() }) diff --git a/src/fetcher/index.js b/src/fetcher/index.js index b613cef..ae41e0d 100644 --- a/src/fetcher/index.js +++ b/src/fetcher/index.js @@ -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') diff --git a/src/fetcher/irc.js b/src/fetcher/irc.js index ae72f95..1fd4218 100644 --- a/src/fetcher/irc.js +++ b/src/fetcher/irc.js @@ -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)) { diff --git a/src/fetcher/matrix.js b/src/fetcher/matrix.js index c0be324..00c84e2 100644 --- a/src/fetcher/matrix.js +++ b/src/fetcher/matrix.js @@ -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) diff --git a/src/fetcher/twitter.js b/src/fetcher/twitter.js index 2f1a993..23041a4 100644 --- a/src/fetcher/twitter.js +++ b/src/fetcher/twitter.js @@ -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', diff --git a/src/fetcher/xmpp.js b/src/fetcher/xmpp.js index 4ad6aef..fa4c4db 100644 --- a/src/fetcher/xmpp.js +++ b/src/fetcher/xmpp.js @@ -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')