forked from vercel/hazel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
224 lines (175 loc) · 5.58 KB
/
routes.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
// Native
const urlHelpers = require('url');
// Packages
const { send } = require('micro')
const { valid, compare } = require('semver')
const { parse } = require('express-useragent')
const fetch = require('node-fetch')
const distanceInWordsToNow = require('date-fns/distance_in_words_to_now')
// Utilities
const checkAlias = require('./aliases')
const prepareView = require('./view')
module.exports = ({ cache, config }) => {
const { loadCache } = cache
const exports = {}
const { token, url } = config
const shouldProxyPrivateDownload =
token && typeof token === 'string' && token.length > 0
// Helpers
const proxyPrivateDownload = (asset, req, res) => {
const redirect = 'manual'
const headers = { Accept: 'application/octet-stream' }
const options = { headers, redirect }
const { api_url: rawUrl } = asset
const finalUrl = rawUrl.replace(
'https://api.github.com/',
`https://${token}@api.github.com/`
)
fetch(finalUrl, options).then(assetRes => {
res.setHeader('Location', assetRes.headers.get('Location'))
send(res, 302)
})
}
exports.download = async (req, res) => {
const userAgent = parse(req.headers['user-agent'])
const params = urlHelpers.parse(req.url, true).query
const isUpdate = params && params.update
let platform
if (userAgent.isMac && isUpdate) {
platform = 'darwin'
} else if (userAgent.isMac && !isUpdate) {
platform = 'dmg'
} else if (userAgent.isWindows) {
platform = 'exe'
}
// Get the latest version from the cache
const { platforms } = await loadCache()
if (!platform || !platforms || !platforms[platform]) {
send(res, 404, 'No download available for your platform!')
return
}
if (shouldProxyPrivateDownload) {
proxyPrivateDownload(platforms[platform], req, res)
return
}
res.writeHead(302, {
Location: platforms[platform].url
})
res.end()
}
exports.downloadPlatform = async (req, res) => {
const params = urlHelpers.parse(req.url, true).query
const isUpdate = params && params.update
let { platform } = req.params
if (platform === 'mac' && !isUpdate) {
platform = 'dmg'
}
// Get the latest version from the cache
const latest = await loadCache()
// Check platform for appropiate aliases
platform = checkAlias(platform)
if (!platform) {
send(res, 500, 'The specified platform is not valid')
return
}
if (!latest.platforms || !latest.platforms[platform]) {
send(res, 404, 'No download available for your platform')
return
}
if (token && typeof token === 'string' && token.length > 0) {
proxyPrivateDownload(latest.platforms[platform], req, res)
return
}
res.writeHead(302, {
Location: latest.platforms[platform].url
})
res.end()
}
exports.update = async (req, res) => {
const { platform: platformName, version } = req.params
if (!valid(version)) {
send(res, 500, {
error: 'version_invalid',
message: 'The specified version is not SemVer-compatible'
})
return
}
const platform = checkAlias(platformName)
if (!platform) {
send(res, 500, {
error: 'invalid_platform',
message: 'The specified platform is not valid'
})
return
}
// Get the latest version from the cache
const latest = await loadCache()
if (!latest.platforms || !latest.platforms[platform]) {
res.statusCode = 204
res.end()
return
}
// Previously, we were checking if the latest version is
// greater than the one on the client. However, we
// only need to compare if they're different (even if
// lower) in order to trigger an update.
// This allows developers to downgrade their users
// to a lower version in the case that a major bug happens
// that will take a long time to fix and release
// a patch update.
if (compare(latest.version, version) !== 0) {
const { notes, pub_date } = latest
send(res, 200, {
name: latest.version,
notes,
pub_date,
url: shouldProxyPrivateDownload
? `${url}/download/${platformName}?update=true`
: latest.platforms[platform].url
})
return
}
res.statusCode = 204
res.end()
}
exports.releases = async (req, res) => {
// Get the latest version from the cache
const latest = await loadCache()
if (!latest.files || !latest.files.RELEASES) {
res.statusCode = 204
res.end()
return
}
const content = latest.files.RELEASES
res.writeHead(200, {
'content-length': Buffer.byteLength(content, 'utf8'),
'content-type': 'application/octet-stream'
})
res.end(content)
}
exports.overview = async (req, res) => {
const latest = await loadCache()
try {
const render = await prepareView()
const details = {
account: config.account,
repository: config.repository,
date: distanceInWordsToNow(latest.pub_date, { addSuffix: true }),
files: latest.platforms,
version: latest.version,
releaseNotes: `https://github.com/${config.account}/${
config.repository
}/releases/tag/${latest.version}`,
allReleases: `https://github.com/${config.account}/${
config.repository
}/releases`,
github: `https://github.com/${config.account}/${config.repository}`
}
send(res, 200, render(details))
} catch (err) {
console.error(err)
send(res, 500, 'Error reading overview file')
}
}
return exports
}