From d068710d8693a81af7df5e82aff3e58badcf1496 Mon Sep 17 00:00:00 2001 From: backroot Date: Sun, 4 Oct 2020 12:56:04 +0900 Subject: [PATCH] Support for Vercel and GitHub Private Repo (#62 #39) --- .gitignore | 3 +++ .nowignore => .vercelignore | 0 lib/index.js | 1 + lib/platform.js | 4 ++-- lib/routes.js | 19 ++++++++++++++++++- now.json | 16 ---------------- package.json | 5 ++++- vercel.json | 21 +++++++++++++++++++++ 8 files changed, 49 insertions(+), 20 deletions(-) rename .nowignore => .vercelignore (100%) delete mode 100644 now.json create mode 100644 vercel.json diff --git a/.gitignore b/.gitignore index be81578..2e03c68 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ coverage # dependencies node_modules + +# vercel +.vercel diff --git a/.nowignore b/.vercelignore similarity index 100% rename from .nowignore rename to .vercelignore diff --git a/lib/index.js b/lib/index.js index 631abb7..119bc81 100644 --- a/lib/index.js +++ b/lib/index.js @@ -35,6 +35,7 @@ module.exports = config => { router.get('/download', routes.download) router.get('/download/:platform', routes.downloadPlatform) router.get('/update/:platform/:version', routes.update) + router.get('/update/:platform/:version/:targetFile', routes.update) router.get('/update/win32/:version/RELEASES', routes.releases) return (req, res) => { diff --git a/lib/platform.js b/lib/platform.js index 764d38f..8f4a1ec 100644 --- a/lib/platform.js +++ b/lib/platform.js @@ -6,11 +6,11 @@ module.exports = fileName => { if ( (fileName.includes('mac') || fileName.includes('darwin')) && - extension === 'zip' + (extension === 'yml') ) { return 'darwin' } - const directCache = ['exe', 'dmg', 'rpm', 'deb', 'AppImage'] + const directCache = ['exe', 'dmg', 'rpm', 'deb', 'AppImage', 'zip'] return directCache.find(ext => ext === extension) || false } diff --git a/lib/routes.js b/lib/routes.js index 7334462..e1055d7 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -1,5 +1,6 @@ // Native const urlHelpers = require('url'); +const { extname } = require('path'); // Packages const { send } = require('micro') @@ -81,6 +82,10 @@ module.exports = ({ cache, config }) => { platform = 'dmg' } + if (platform === 'darwin') { + platform = 'dmg' + } + // Get the latest version from the cache const latest = await loadCache() @@ -110,7 +115,7 @@ module.exports = ({ cache, config }) => { } exports.update = async (req, res) => { - const { platform: platformName, version } = req.params + const { platform: platformName, version, targetFile } = req.params if (!valid(version)) { send(res, 500, { @@ -142,6 +147,18 @@ module.exports = ({ cache, config }) => { return } + if (targetFile) { + if (shouldProxyPrivateDownload) { + for (let p in latest.platforms) { + if (latest.platforms[p].name === targetFile) { + proxyPrivateDownload(latest.platforms[p], req, res) + break; + } + } + 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 diff --git a/now.json b/now.json deleted file mode 100644 index 5d79df6..0000000 --- a/now.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "version": 2, - "name": "hazel", - "builds": [ - { - "src": "lib/server.js", - "use": "@now/node" - } - ], - "routes": [ - { - "src": ".*", - "dest": "/lib/server.js" - } - ] -} diff --git a/package.json b/package.json index cb03f66..3adb762 100644 --- a/package.json +++ b/package.json @@ -29,12 +29,14 @@ ] }, "dependencies": { + "@vercel/node": "^1.8.2", "async-retry": "1.2.3", "date-fns": "1.29.0", "express-useragent": "1.0.12", "fetch": "1.1.0", "finalhandler": "1.1.0", "handlebars": "4.0.11", + "install": "^0.13.0", "jest": "24.0.0", "micro": "9.3.3", "ms": "2.1.1", @@ -42,7 +44,8 @@ "router": "1.3.2", "semver": "5.5.0", "stream-to-string": "1.1.0", - "test-listen": "1.1.0" + "test-listen": "1.1.0", + "vercel": "^20.1.0" }, "devDependencies": { "eslint-config-prettier": "2.9.0", diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..10c151f --- /dev/null +++ b/vercel.json @@ -0,0 +1,21 @@ +{ + "version": 2, + "env": { + "ACCOUNT": "Your GitHub Account", + "REPOSITORY": "Your GirHub Repository", + "TOKEN": "Your GitHub Token", + "URL": "http://localhost:3000" + }, + "builds": [ + { + "src": "lib/*.js", + "use": "@vercel/node" + } + ], + "routes": [ + { + "src": ".*", + "dest": "lib/server.js" + } + ] +}