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/api/update/index.js b/api/update/index.js new file mode 100644 index 0000000..be54f33 --- /dev/null +++ b/api/update/index.js @@ -0,0 +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') + +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 new file mode 100644 index 0000000..c712504 --- /dev/null +++ b/now.json @@ -0,0 +1,11 @@ +{ + "version": 2, + "alias": ["vscodium.now.sh"], + "routes": [ + { + "src": "/api/update/(?[^/]+)/(?[^/]+)/(?[^/]+)", + "dest": "/api/update?platform=$platform&quality=$quality&commit=$commit" + } + ], + "builds": [{ "src": "api/update/index.js", "use": "@now/node" }] +} \ No newline at end of file