From 006f46c35d24512446b5aae792afd55085c75233 Mon Sep 17 00:00:00 2001 From: backroot Date: Sun, 4 Oct 2020 12:56:04 +0900 Subject: [PATCH 1/2] 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 | 20 ++++++++++++++++++-- lib/server.js | 2 +- now.json | 16 ---------------- package.json | 8 ++++++-- vercel.json | 21 +++++++++++++++++++++ 9 files changed, 52 insertions(+), 23 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..2dcb1a3 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') @@ -81,6 +81,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 +114,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 +146,18 @@ module.exports = ({ cache, config }) => { return } + if (targetFile) { + if (shouldProxyPrivateDownload) { + for (const platform in latest.platforms) { + if (latest.platforms[platform].name === targetFile) { + proxyPrivateDownload(latest.platforms[platform], 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/lib/server.js b/lib/server.js index f7869ea..df6db3e 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/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..8d8f943 100644 --- a/package.json +++ b/package.json @@ -23,18 +23,20 @@ }, "lint-staged": { "*.js": [ - "yarn test && :", + "yarn test --passWithNoTests && :", "prettier --single-quote --no-semi --write --no-editorconfig", "git add" ] }, "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,9 @@ "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", + "yarn": "^1.22.10" }, "devDependencies": { "eslint-config-prettier": "2.9.0", diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..572b3fc --- /dev/null +++ b/vercel.json @@ -0,0 +1,21 @@ +{ + "version": 2, + "env": { + "ACCOUNT": "Your GitHub Account", + "REPOSITORY": "Yout GitHub Repository", + "TOKEN": "Your GitHub Token", + "URL": "http://localhost:3000" + }, + "builds": [ + { + "src": "lib/*.js", + "use": "@vercel/node" + } + ], + "routes": [ + { + "src": ".*", + "dest": "lib/server.js" + } + ] +} From 1ca24da52e8518523063081232f1432caf97ef7a Mon Sep 17 00:00:00 2001 From: backroot Date: Sat, 10 Oct 2020 12:13:30 +0900 Subject: [PATCH 2/2] Update README --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8d3852e..319fd8a 100644 --- a/README.md +++ b/README.md @@ -13,14 +13,14 @@ The result will be faster and more lightweight than any other solution out there - Refreshes the cache every **15 minutes** (custom interval [possible](#options)) - When asked for an update, it returns the link to the GitHub asset directly (saves bandwidth) - Supports **macOS** and **Windows** apps -- Scales very nicely across multiple [Now](https://zeit.co/now) instances +- Scales very nicely across multiple [Vercel](https://vercel.com) instances ## Usage -With [Now CLI](https://zeit.co/download), you can easily deploy an update server. As the first step, clone the repository: +With [Vercel CLI](https://vercel.com/download), you can easily deploy an update server. As the first step, clone the repository: ```bash -git clone https://github.com/zeit/hazel +git clone https://github.com/vercel/hazel ``` Next, move into the directory: @@ -32,7 +32,7 @@ cd hazel Inside the directory, create a new deployment: ```bash -now -e ACCOUNT="" -e REPOSITORY="" +vercel -e ACCOUNT="" -e REPOSITORY="" ``` On the command above, you can define the following environment variables: @@ -63,7 +63,7 @@ The following environment variables can be used optionally: - `INTERVAL`: Refreshes the cache every x minutes ([restrictions](https://developer.github.com/changes/2012-10-14-rate-limit-changes/)) - `PRE`: When defined with a value of `1`, only pre-releases will be cached - `TOKEN`: Your GitHub token (for private repos) -- `URL`: The server's URL (for private repos - when running on [Now](https://zeit.co/now), this field is filled with the URL of the deployment automatically) +- `URL`: The server's URL (for private repos - when running on [Vercel](https://vercel.com), this field is filled with the URL of the deployment automatically) ## Statistics