-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
195 lines (161 loc) · 5.53 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const path = require('path')
const express = require('express')
const serveStatic = require('serve-static')
const cache = require('memory-cache')
const nconf = require('nconf')
const { getDependencies } = require('./lib/starbuck')
const { getBadge } = require('./lib/util')
const Github = require('./lib/github')
const Gitlab = require('./lib/gitlab')
const supportedServices = ['gitlab', 'github']
const supportedBadges = ['dev-status', 'status', 'peer-status']
module.exports = function starbuck (config, port) {
return new Promise(function (resolve, reject) {
try {
nconf
.argv()
.env({
separator: '__',
lowerCase: true
})
.defaults({
'github': {
'url': 'https://api.github.com',
'token': ''
},
'gitlab': {
'url': 'https://gitlab.com/api',
'token': ''
},
'npm': {
'url': 'http://registry.npmjs.org'
}
})
.overrides(config)
const github = Github(nconf.get('github'))
const gitlab = Gitlab(nconf.get('gitlab'))
const app = express()
const asyncMiddleware = (fn) => {
return (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next)
}
}
const cacheMiddleware = (req, res, next) => {
const cached = cache.get(req.url)
if (cached) {
res.set(cached.headers)
res.send(cached.content)
} else {
res.cache = {
write: res.write,
end: res.end,
content: ''
}
res.end = function (content, encoding) {
if (res.statusCode !== 200) return res.cache.end.apply(this, arguments) // don't cache
cache.put(req.url, {
headers: res._headers,
content,
encoding
}, 900000)
return res.cache.end.apply(this, arguments)
}
next()
}
}
app.use(serveStatic('dist'))
app.get('/api/:service/:owner/repos', cacheMiddleware, asyncMiddleware(async (req, res) => {
const { service, owner } = req.params
let repos = {}
if (service === 'github') {
repos = await github.getRepos(owner)
} else {
repos = await gitlab.getRepos(owner)
}
res.send(repos)
}))
app.get('/api/:service/:owner/:repo', asyncMiddleware(async (req, res) => {
const { service, owner, repo } = req.params
if (supportedServices.indexOf(service) === -1) {
return res.status(500).send({ error: 'service not supported' })
}
if (cache.get(`${service}:${owner}:${repo}`)) {
return res.status(200).send(JSON.stringify(cache.get(`${service}:${owner}:${repo}`)))
}
let pack = {}
let dependencies = {}
try {
if (service === 'github') {
pack = await github.getPackage(owner, repo)
} else {
pack = await gitlab.getPackage(owner, repo)
}
dependencies = await getDependencies(pack, {
npm: nconf.get('npm').url
})
} catch (ex) {
return res.status(500).send(JSON.stringify({
error: 'could not find package'
}))
}
const response = Object.assign({ starbuck: dependencies }, pack)
cache.put(`${service}:${owner}:${repo}`, response, 900000)
res.status(200).send(JSON.stringify(response))
}))
app.get('/badge/:service/:owner/:repo/:status.svg', cacheMiddleware, asyncMiddleware(async (req, res) => {
const { service, owner, repo, status } = req.params
async function get () {
let pack = {}
if (service === 'github') {
pack = await github.getPackage(owner, repo)
} else {
pack = await gitlab.getPackage(owner, repo)
}
const dependencies = await getDependencies(pack, {
npm: nconf.get('npm').url
})
return Object.assign({ starbuck: dependencies }, pack)
}
try {
if (supportedServices.indexOf(service) === -1) {
return res.status(500).send({ error: 'service not supported' })
}
if (supportedBadges.indexOf(status) === -1) {
return res.send(await getBadge('unknown', 'invalid'))
}
const response = await get()
let dep = {
'dev-status': 'devDependencies',
'status': 'dependencies',
'peer-status': 'peerDependencies'
}[status]
let dependencies = response.starbuck[dep]
let type
if (Object.keys(dependencies).length === 0) {
type = 'none'
} else if (Object.keys(dependencies).filter((d) => dependencies[d].needsUpdating).length > 0) {
type = 'notsouptodate'
} else {
type = 'uptodate'
}
res.setHeader('Content-Type', 'image/svg+xml')
res.send(await getBadge(type, dep))
} catch (ex) {
return res.send(await getBadge('unknown', status))
}
}))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'dist', 'index.html'))
})
app.use((error, req, res) => {
res.status(500).send({ error: error.toString() })
})
app.listen(port, () => {
console.log(`starbuck listening at http://localhost:${port}`); // eslint-disable-line
resolve()
})
} catch (ex) {
reject(ex)
}
})
}