-
Notifications
You must be signed in to change notification settings - Fork 3
/
lookup.js
69 lines (62 loc) · 1.92 KB
/
lookup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// TODO: replace with a LRU cache
const cache = {}
const get = require('simple-get')
module.exports = (username) =>
fetch()
.then(res => res.json())
.then(json => {
return json.map(data => data.raw_key)
})
async function usernames2keys(usernames) {
const all = await Promise.all(
usernames.map(username => new Promise((resolve, reject) => {
get.concat({
url: `https://api.github.com/users/${username}/gpg_keys`,
json: true,
headers: {
'user-agent': 'GitHub PGP KeyFinder'
}
}, (err, res, data) => {
if (err) return reject(err)
return resolve(data.map(i => i.raw_key))
})
}))
)
return all.reduce((a, b) => a.concat(b)).filter(Boolean)
}
async function email2usernames(email) {
return new Promise((resolve, reject) => {
get.concat({
url: `https://api.github.com/search/users?q=${email}+in:email`,
json: true,
headers: {
'user-agent': 'GitHub PGP KeyFinder'
}
}, (err, res, data) => {
if (err) return reject(err)
if (data.total_count === 0) {
return reject(new Error(`Could not find the GitHub user publicly associated with the email address "${email}"`))
} else if (data.total_count > 0) {
return resolve(data.items.map(i => i.login))
} else {
return reject('Unexpected value for data.total_count returned by GitHub API')
}
})
})
}
async function lookup(email) {
if (cache[email]) return cache[email]
const usernames = await email2usernames(email)
const keys = await usernames2keys(usernames)
cache[email] = keys
return cache[email]
}
function demote(email, key) {
const i = cache[email].indexOf(key)
cache[email].push(cache[email].splice(i, 1)[0])
}
module.exports.lookup = lookup
module.exports.demote = demote
if (!module.parent) {
lookup('wmhilton@gmail.com').then(console.log).then(() => lookup('wmhilton@gmail.com')).then(console.log)
}