diff --git a/lib/aliases.js b/lib/aliases.js index 8d65c433..f1c83bfe 100644 --- a/lib/aliases.js +++ b/lib/aliases.js @@ -4,7 +4,8 @@ const aliases = { deb: ['debian'], rpm: ['fedora'], AppImage: ['appimage'], - dmg: ['dmg'] + dmg: ['dmg'], + nupkg: ['nupkg'] } module.exports = platform => { diff --git a/lib/cache.js b/lib/cache.js index 4d297cf9..06fa96d4 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -37,19 +37,21 @@ module.exports = class Cache { async cacheReleaseList(url) { const { token } = this.config - const headers = { Accept: 'application/vnd.github.preview' } + const headers = { Accept: 'application/octet-stream' } if (token && typeof token === 'string' && token.length > 0) { headers.Authorization = `token ${token}` } - const { status, body } = await retry( + const { body } = await retry( async () => { const response = await fetch(url, { headers }) if (response.status !== 200) { throw new Error( - `Tried to cache RELEASES, but failed fetching ${url}, status ${status}` + `Tried to cache RELEASES, but failed fetching ${url}, status ${ + response.status + }` ) } @@ -67,10 +69,12 @@ module.exports = class Cache { ) } - for (let i = 0; i < matches.length; i += 1) { - const nuPKG = url.replace('RELEASES', matches[i]) - content = content.replace(matches[i], nuPKG) - } + content = content.replace( + matches[0], + `${this.config.url}/download/latest/${matches[0]}` + ) + console.log('content', content) + return content } @@ -127,6 +131,7 @@ module.exports = class Cache { this.latest.version = tag_name this.latest.notes = release.body this.latest.pub_date = release.published_at + this.latest.files = {} // Clear list of download links this.latest.platforms = {} @@ -136,12 +141,7 @@ module.exports = class Cache { if (name === 'RELEASES') { try { - if (!this.latest.files) { - this.latest.files = {} - } - this.latest.files.RELEASES = await this.cacheReleaseList( - browser_download_url - ) + this.latest.files.RELEASES = await this.cacheReleaseList(url) } catch (err) { console.error(err) } @@ -154,13 +154,16 @@ module.exports = class Cache { continue } - this.latest.platforms[platform] = { + const entry = { name, api_url: url, url: browser_download_url, content_type, size: Math.round(size / 1000000 * 10) / 10 } + + this.latest.platforms[platform] = entry + this.latest.files[entry.name] = entry } console.log(`Finished caching version ${tag_name}`) diff --git a/lib/index.js b/lib/index.js index 631abb7a..f8b17b18 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,7 +5,7 @@ const Cache = require('./cache') module.exports = config => { const router = Router() - let cache = null; + let cache = null try { cache = new Cache(config) @@ -14,14 +14,16 @@ module.exports = config => { if (code) { return (req, res) => { - res.statusCode = 400; - - res.end(JSON.stringify({ - error: { - code, - message - } - })) + res.statusCode = 400 + + res.end( + JSON.stringify({ + error: { + code, + message + } + }) + ) } } @@ -34,6 +36,7 @@ module.exports = config => { router.get('/', routes.overview) router.get('/download', routes.download) router.get('/download/:platform', routes.downloadPlatform) + router.get('/download/latest/:file', routes.downloadLatest) router.get('/update/:platform/:version', routes.update) router.get('/update/win32/:version/RELEASES', routes.releases) diff --git a/lib/platform.js b/lib/platform.js index 764d38f3..2a56df8f 100644 --- a/lib/platform.js +++ b/lib/platform.js @@ -11,6 +11,6 @@ module.exports = fileName => { return 'darwin' } - const directCache = ['exe', 'dmg', 'rpm', 'deb', 'AppImage'] + const directCache = ['exe', 'dmg', 'rpm', 'deb', 'AppImage', 'nupkg'] return directCache.find(ext => ext === extension) || false } diff --git a/lib/routes.js b/lib/routes.js index 73344626..3f0c5e57 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -1,5 +1,5 @@ // Native -const urlHelpers = require('url'); +const urlHelpers = require('url') // Packages const { send } = require('micro') @@ -109,6 +109,29 @@ module.exports = ({ cache, config }) => { res.end() } + exports.downloadLatest = async (req, res) => { + const { file } = req.params + + // Get the latest version from the cache + const latest = await loadCache() + + if (!latest.files || !latest.files[file]) { + send(res, 404, 'File not found') + return + } + + if (token && typeof token === 'string' && token.length > 0) { + proxyPrivateDownload(latest.files[file], req, res) + return + } + + res.writeHead(302, { + Location: latest.files[file].url + }) + + res.end() + } + exports.update = async (req, res) => { const { platform: platformName, version } = req.params diff --git a/lib/server.js b/lib/server.js index f7869eaa..df6db3e7 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,4 +1,4 @@ -const hazel = require('./index') +const hazel = require('.') const { INTERVAL: interval, diff --git a/package.json b/package.json index cb03f66c..a9538348 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ }, "lint-staged": { "*.js": [ - "yarn test && :", + "yarn test --passWithNoTests && :", "prettier --single-quote --no-semi --write --no-editorconfig", "git add" ]