doipjs/src/serviceproviders.js

139 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-10-24 07:11:14 -06:00
/*
2021-01-13 05:20:33 -07:00
Copyright 2021 Yarmo Mackenbach
2020-10-24 07:11:14 -06:00
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.
*/
2020-10-26 14:18:17 -06:00
const bent = require('bent')
const req = bent('GET')
2020-11-03 19:02:15 -07:00
const utils = require('./utils')
2020-10-26 14:18:17 -06:00
2020-10-24 12:33:34 -06:00
const list = [
2020-10-23 17:25:01 -06:00
'dns',
2021-03-05 07:39:17 -07:00
'irc',
2020-10-23 17:25:01 -06:00
'xmpp',
2020-10-24 07:33:51 -06:00
'twitter',
2020-10-25 16:20:22 -06:00
'reddit',
2020-11-04 14:38:03 -07:00
'liberapay',
2020-10-24 07:50:14 -06:00
'hackernews',
2020-10-24 17:16:13 -06:00
'lobsters',
2020-10-25 16:06:00 -06:00
'devto',
2020-10-25 16:32:30 -06:00
'gitea',
2020-10-26 15:33:05 -06:00
'gitlab',
2020-10-25 17:04:19 -06:00
'github',
2020-11-02 16:25:25 -07:00
'mastodon',
2020-11-02 16:41:03 -07:00
'fediverse',
2020-11-03 14:23:35 -07:00
'discourse',
2020-12-26 05:52:49 -07:00
'owncast',
2020-10-23 17:25:01 -06:00
]
const data = {
dns: require('./serviceproviders/dns'),
2021-03-05 07:39:17 -07:00
irc: require('./serviceproviders/irc'),
xmpp: require('./serviceproviders/xmpp'),
twitter: require('./serviceproviders/twitter'),
2020-10-25 16:20:22 -06:00
reddit: require('./serviceproviders/reddit'),
2020-11-04 14:38:03 -07:00
liberapay: require('./serviceproviders/liberapay'),
hackernews: require('./serviceproviders/hackernews'),
lobsters: require('./serviceproviders/lobsters'),
2020-10-25 16:06:00 -06:00
devto: require('./serviceproviders/devto'),
2020-10-25 16:32:30 -06:00
gitea: require('./serviceproviders/gitea'),
2020-10-26 15:33:05 -06:00
gitlab: require('./serviceproviders/gitlab'),
2020-10-25 17:04:19 -06:00
github: require('./serviceproviders/github'),
2020-11-02 16:25:25 -07:00
mastodon: require('./serviceproviders/mastodon'),
2020-11-02 16:41:03 -07:00
fediverse: require('./serviceproviders/fediverse'),
2020-11-03 14:23:35 -07:00
discourse: require('./serviceproviders/discourse'),
2020-12-26 05:52:49 -07:00
owncast: require('./serviceproviders/owncast'),
}
2020-10-24 11:10:28 -06:00
const match = (uri, opts) => {
2020-11-07 18:07:02 -07:00
let matches = [],
sp
2020-10-24 11:10:28 -06:00
2020-10-24 12:33:34 -06:00
list.forEach((spName, i) => {
sp = data[spName]
2020-10-24 11:10:28 -06:00
if (sp.reURI.test(uri)) {
matches.push(sp.processURI(uri, opts))
2020-10-24 11:10:28 -06:00
}
})
return matches
}
2020-10-24 12:33:34 -06:00
2020-12-05 15:14:09 -07:00
const directRequestHandler = (spData, opts) => {
return new Promise(async (resolve, reject) => {
const url = spData.proof.fetch ? spData.proof.fetch : spData.proof.uri
2021-03-05 08:59:28 -07:00
if (!url) {
reject('No valid URI provided')
return
}
2020-12-05 15:14:09 -07:00
let res
2020-11-02 10:21:56 -07:00
2020-12-05 15:14:09 -07:00
switch (spData.proof.format) {
case 'json':
req(url, null, {
2020-12-05 15:14:09 -07:00
Accept: 'application/json',
'User-Agent': `doipjs/${require('../package.json').version}`,
})
2020-12-20 15:19:07 -07:00
.then(async (res) => {
return await res.json()
})
.then((res) => {
resolve(res)
})
.catch((e) => {
reject(e)
})
2020-12-05 15:14:09 -07:00
break
case 'text':
req(url)
2020-12-20 15:19:07 -07:00
.then(async (res) => {
return await res.text()
})
.then((res) => {
resolve(res)
})
.catch((e) => {
reject(e)
})
2020-12-05 15:14:09 -07:00
break
default:
reject('No specified proof data format')
break
}
})
2020-10-26 14:18:17 -06:00
}
2020-12-05 15:14:09 -07:00
const proxyRequestHandler = (spData, opts) => {
return new Promise(async (resolve, reject) => {
const url = spData.proof.fetch ? spData.proof.fetch : spData.proof.uri
2020-12-20 15:19:07 -07:00
req(utils.generateProxyURL(spData.proof.format, url, opts), null, {
Accept: 'application/json',
})
2020-12-20 15:19:07 -07:00
.then(async (res) => {
return await res.json()
})
.then((res) => {
resolve(res.content)
})
.catch((e) => {
reject(e)
})
2020-12-05 15:14:09 -07:00
})
2020-10-26 14:18:17 -06:00
}
2020-10-24 12:33:34 -06:00
exports.list = list
exports.data = data
exports.match = match
2020-10-26 14:18:17 -06:00
exports.directRequestHandler = directRequestHandler
exports.proxyRequestHandler = proxyRequestHandler