forked from electron/update.electronjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
243 lines (214 loc) · 6.62 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'use strict'
const http = require('http')
const fetch = require('node-fetch')
const semver = require('semver')
const assert = require('assert')
const log = require('pino')()
const crypto = require('crypto')
const requestIp = require('request-ip')
const { NODE_ENV: env } = process.env
if (env === 'test') log.level = 'error'
class Updates {
constructor ({ token, cache } = {}) {
assert(cache, '.cache required')
this.token = token
this.cache = cache
}
listen (port, cb) {
if (typeof port === 'function') {
;[port, cb] = [undefined, port]
}
const server = http.createServer((req, res) => {
const start = new Date()
this.handle(req, res)
.catch(err => {
log.error(err)
res.statusCode = err.statusCode || 500
const msg = env === 'production' ? 'Internal Server Error' : err.stack
res.end(msg)
})
.then(() => {
log.info(
{
method: req.method,
url: req.url,
status: res.statusCode,
ipHash: this.hashIp(requestIp.getClientIp(req)),
duration: new Date() - start
},
'request'
)
})
})
server.listen(port, cb)
return server
}
async handle (req, res) {
let segs = req.url.split(/[/?]/).filter(Boolean)
const [account, repository, platform, version, file] = segs
if (!account || !repository || !platform || !version) {
redirect(res, 'https://github.com/electron/update.electronjs.org')
} else if (platform !== 'darwin' && platform !== 'win32') {
const message = `Unsupported platform: "${platform}". Supported: darwin, win32.`
notFound(res, message)
} else if (version && !semver.valid(version)) {
badRequest(res, `Invalid SemVer: "${version}"`)
} else if (file === 'RELEASES') {
await this.handleReleases(res, account, repository)
} else {
await this.handleUpdate(res, account, repository, platform, version)
}
}
async handleReleases (res, account, repository) {
const latest = await this.cachedGetLatest(account, repository, 'win32')
if (!latest || !latest.RELEASES) return notFound(res)
res.end(latest.RELEASES)
}
async handleUpdate (res, account, repository, platform, version) {
const latest = await this.cachedGetLatest(account, repository, platform)
if (!latest) {
const message =
platform === 'darwin'
? 'No updates found (needs asset matching *{mac,darwin,osx}*.zip in public repository)'
: 'No updates found (needs asset containing win32-x64 or .exe in public repository)'
notFound(res, message)
} else if (semver.eq(latest.version, version)) {
log.info({ account, repository, platform, version }, 'up to date')
noContent(res)
} else {
log.info(
{
account,
repository,
platform,
version,
latest: latest.name || latest.version
},
'update available'
)
json(res, {
name: latest.name || latest.version,
notes: latest.notes,
url: latest.url
})
}
}
async cachedGetLatest (account, repository, platform) {
const key = `${account}/${repository}`
let latest = await this.cache.get(key)
if (latest) {
log.info({ key }, 'cache hit')
return latest[platform] ? latest[platform] : null
}
let lock
if (this.cache.lock) {
log.debug({ key }, 'lock acquiring')
lock = await this.cache.lock(key)
log.debug({ key }, 'lock acquired')
latest = await this.cache.get(key)
if (latest) {
log.info({ key }, 'cache hit after lock')
return latest[platform] ? latest[platform] : null
}
}
latest = await this.getLatest(account, repository)
if (latest) {
await this.cache.set(key, latest)
} else {
await this.cache.set(key, {})
}
if (lock) {
log.debug({ key }, 'lock releasing')
await lock.unlock()
log.debug({ key }, 'lock released')
}
return latest && latest[platform]
}
async getLatest (account, repository) {
account = encodeURIComponent(account)
repository = encodeURIComponent(repository)
const url = `https://api.github.com/repos/${account}/${repository}/releases?per_page=100`
const headers = { Accept: 'application/vnd.github.preview' }
if (this.token) headers.Authorization = `token ${this.token}`
const res = await fetch(url, { headers })
log.info({ account, repository, status: res.status }, 'github releases api')
if (res.status === 403) {
console.error('Rate Limited!')
return
}
if (res.status >= 400) {
return
}
const latest = {}
const releases = await res.json()
for (const release of releases) {
if (
!semver.valid(release.tag_name) ||
release.draft ||
release.prerelease
) {
continue
}
for (const asset of release.assets) {
const platform = assetPlatform(asset.name)
if (platform && !latest[platform]) {
latest[platform] = {
name: release.name,
version: release.tag_name,
url: asset.browser_download_url,
notes: release.body
}
}
if (latest.darwin && latest.win32) break
}
if (latest.darwin && latest.win32) break
}
if (latest.win32) {
const rurl = `https://github.com/${account}/${repository}/releases/download/${
latest.win32.version
}/RELEASES`
const rres = await fetch(rurl)
if (rres.status < 400) {
const body = await rres.text()
const matches = body.match(/[^ ]*\.nupkg/gim)
const nuPKG = rurl.replace('RELEASES', matches[0])
latest.win32.RELEASES = body.replace(matches[0], nuPKG)
}
}
return latest.darwin || latest.win32 ? latest : null
}
hashIp (ip) {
if (!ip) return
return crypto
.createHash('sha256')
.update(ip)
.digest('hex')
}
}
const assetPlatform = fileName => {
if (/.*(mac|darwin|osx).*\.zip/i.test(fileName)) return 'darwin'
if (/win32-x64|(\.exe$)/.test(fileName)) return 'win32'
return false
}
const notFound = (res, message = 'Not found') => {
res.statusCode = 404
res.end(message)
}
const badRequest = (res, message) => {
res.statusCode = 400
res.end(message)
}
const noContent = res => {
res.statusCode = 204
res.end()
}
const json = (res, obj) => {
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(obj))
}
const redirect = (res, url) => {
res.statusCode = 302
res.setHeader('Location', url)
res.end(url)
}
module.exports = Updates