From 294925ff2941d4de0b5f8b4ce8c0f668e02395a7 Mon Sep 17 00:00:00 2001 From: Peter Squicciarini Date: Thu, 9 May 2019 10:56:19 -0700 Subject: [PATCH 1/3] Try using route params --- api/update/index.js | 6 ++++++ now.json | 10 ++++++++++ 2 files changed, 16 insertions(+) create mode 100644 api/update/index.js create mode 100644 now.json diff --git a/api/update/index.js b/api/update/index.js new file mode 100644 index 0000000..a117ea5 --- /dev/null +++ b/api/update/index.js @@ -0,0 +1,6 @@ +const { parse } = require('url') + +module.exports = (req, res) => { + const parsed = parse(req.url, true) + res.end(JSON.stringify(parsed, null, 2)) +} diff --git a/now.json b/now.json new file mode 100644 index 0000000..bed196d --- /dev/null +++ b/now.json @@ -0,0 +1,10 @@ +{ + "version": 2, + "routes": [ + { + "src": "/api/update/(?[^/]+)/(?[^/]+)/(?[^/]+)", + "dest": "/api/update?platform=$platform&quality=$quality&commit=$commit" + } + ], + "builds": [{ "src": "index.js", "use": "@now/node" }] +} \ No newline at end of file From fb26d0e1edb9281dbd62df59200b6a8337ceae9b Mon Sep 17 00:00:00 2001 From: Peter Squicciarini Date: Thu, 9 May 2019 11:51:02 -0700 Subject: [PATCH 2/3] Add parsing logic --- api/update/index.js | 93 +++++++++++++++++++++++++++++++++++++++++++-- now.json | 2 +- 2 files changed, 91 insertions(+), 4 deletions(-) diff --git a/api/update/index.js b/api/update/index.js index a117ea5..be54f33 100644 --- a/api/update/index.js +++ b/api/update/index.js @@ -1,6 +1,93 @@ +/* +Should download the version JSON file from the VSCodium repo +the JSON file should conform to this schema: +{ + "url": "", + "name": "1.33.1", // the version number + "version": "51b0b28134d51361cf996d2f0a1c698247aeabd8", // the latest commit hash + "productVersion": "1.33.1", // the version number + "hash": "cb4109f196d23b9d1e8646ce43145c5bb62f55a8", // sha1 of the release download + "timestamp": 1554971059007, + "sha256hash": "ac2a1c8772501732cd5ff539a04bb4dc566b58b8528609d2b34bbf970d08cf01" // sha256 of the release download +} +The hashes can be ignored by this api/lambda -- we are only concerned with whether +the commit hash in the url parameter matches the "version" identifier in the above payload +*/ + + const { parse } = require('url') -module.exports = (req, res) => { - const parsed = parse(req.url, true) - res.end(JSON.stringify(parsed, null, 2)) +const STABLE = 'stable' + +const DARWIN = 'darwin' +const WINDOWS = 'win32' +const LINUX = 'linux' + +const IA32 = 'ia32' +const X64 = 'x64' + +const ARCHIVE = 'archive' +const USER = 'user' + +const QUALITIES = new Set([STABLE]) +const OS = new Set([DARWIN, WINDOWS, LINUX]) +const TYPES = new Set([ARCHIVE, USER]) +const ARCH = new Set([IA32, X64]) + +function getJSON ({ os, arch, type }) { + // TODO get os/arch/type specific JSON file from a repo where these files will be stored + return { + "url": "https://github.com/release/something.zip", + "name": "1.33.1", // the version number + "version": "51b0b28134d51361cf996d2f0a1c698247aeabd8", // the latest commit hash + "productVersion": "1.33.1", // the version number + "hash": "cb4109f196d23b9d1e8646ce43145c5bb62f55a8", // sha1 of the release download + "timestamp": 1554971059007, + "sha256hash": "ac2a1c8772501732cd5ff539a04bb4dc566b58b8528609d2b34bbf970d08cf01" // sha256 of the release download + } +} + +// returns false if invalid, or an object of os, arch, type if valid +function validateInput (platform, quality) { + // a bunch of validation rules for the different permutations + if (!QUALITIES.has(quality)) return false + + let [os, arch, type] = platform.split('-') + if (!OS.has(os)) return false + + if (os === WINDOWS && !type) { + type = arch + arch = IA32 + } + + if (os === WINDOWS || os === LINUX) { + if (!ARCH.has(arch)) return false + } + + if (os === WINDOWS && !TYPES.has(type)) return false + + return { os, arch, type } +} + +module.exports = (req, res) => { + const { query } = parse(req.url, true) + const { platform, quality, commit } = query + const input = validateInput(platform, quality) + if (!input) { + res.writeHead(404) + res.end() + return + } + + const { os, arch, type } = input + const latest = getJSON({ os, arch, type }) + + if (commit === latest.version) { + res.writeHead(204) + res.end() + return + } + res.setHeader('Content-Type', 'application/json') + res.write(JSON.stringify(latest)) + res.end() } diff --git a/now.json b/now.json index bed196d..662e9f1 100644 --- a/now.json +++ b/now.json @@ -6,5 +6,5 @@ "dest": "/api/update?platform=$platform&quality=$quality&commit=$commit" } ], - "builds": [{ "src": "index.js", "use": "@now/node" }] + "builds": [{ "src": "api/update/index.js", "use": "@now/node" }] } \ No newline at end of file From 458f0fa9d704bb9aeb5b8b110c0500112b1a83e1 Mon Sep 17 00:00:00 2001 From: Peter Squicciarini Date: Thu, 9 May 2019 12:06:25 -0700 Subject: [PATCH 3/3] Add alias and links in README --- README.md | 2 ++ now.json | 1 + 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index a917baf..66088d2 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ # update-api + +see the [project](https://github.com/VSCodium/vscodium/projects/1) and the [original issue](https://github.com/VSCodium/vscodium/issues/41) for more context \ No newline at end of file diff --git a/now.json b/now.json index 662e9f1..c712504 100644 --- a/now.json +++ b/now.json @@ -1,5 +1,6 @@ { "version": 2, + "alias": ["vscodium.now.sh"], "routes": [ { "src": "/api/update/(?[^/]+)/(?[^/]+)/(?[^/]+)",