diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-08-15 14:41:14 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-08-15 14:41:14 +0200 |
commit | 4f990821f7ededd6a255928d6ed420e084dbed71 (patch) | |
tree | 06c7e9f4d971a462e90899c34ef617383a25cacb /util | |
parent | :bug: fix types (diff) | |
download | server-4f990821f7ededd6a255928d6ed420e084dbed71.tar.xz |
:construction: auto update
Diffstat (limited to 'util')
-rw-r--r-- | util/src/util/AutoUpdate.ts | 80 | ||||
-rw-r--r-- | util/src/util/Config.ts | 2 |
2 files changed, 82 insertions, 0 deletions
diff --git a/util/src/util/AutoUpdate.ts b/util/src/util/AutoUpdate.ts new file mode 100644 index 00000000..a2ce73c2 --- /dev/null +++ b/util/src/util/AutoUpdate.ts @@ -0,0 +1,80 @@ +import "missing-native-js-functions"; +import fetch from "node-fetch"; +import readline from "readline"; +import fs from "fs/promises"; +import path from "path"; + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +export function enableAutoUpdate(opts: { + checkInterval: number | boolean; + packageJsonLink: string; + path: string; + downloadUrl: string; + downloadType?: "zip"; +}) { + if (!opts.checkInterval) return; + var interval = 1000 * 60 * 60 * 24; + if (typeof opts.checkInterval === "number") opts.checkInterval = 1000 * interval; + + const i = setInterval(async () => { + const currentVersion = await getCurrentVersion(opts.path); + const latestVersion = await getLatestVersion(opts.packageJsonLink); + if (currentVersion !== latestVersion) { + clearInterval(i); + console.log(`[Auto Update] Current version (${currentVersion}) is out of date, updating ...`); + await download(opts.downloadUrl, opts.path); + } + }, interval); + setImmediate(async () => { + const currentVersion = await getCurrentVersion(opts.path); + const latestVersion = await getLatestVersion(opts.packageJsonLink); + if (currentVersion !== latestVersion) { + rl.question( + `[Auto Update] Current version (${currentVersion}) is out of date, would you like to update? (yes/no)`, + (answer) => { + if (answer.toBoolean()) { + console.log(`[Auto update] updating ...`); + download(opts.downloadUrl, opts.path); + } else { + } + } + ); + } + }); +} + +async function download(url: string, dir: string) { + try { + // TODO: use file stream instead of buffer (to prevent crash because of high memory usage for big files) + // TODO check file hash + const response = await fetch(url); + const buffer = await response.buffer(); + const tempDir = await fs.mkdtemp("fosscord"); + fs.writeFile(path.join(tempDir, "Fosscord.zip"), buffer); + } catch (error) { + console.error(`[Auto Update] download failed`, error); + } +} + +async function getCurrentVersion(dir: string) { + try { + const content = await fs.readFile(path.join(dir, "package.json"), { encoding: "utf8" }); + return JSON.parse(content).version; + } catch (error) { + throw new Error("[Auto update] couldn't get current version in " + dir); + } +} + +async function getLatestVersion(url: string) { + try { + const response = await fetch(url); + const content = await response.json(); + return content.version; + } catch (error) { + throw new Error("[Auto update] check failed for " + url); + } +} diff --git a/util/src/util/Config.ts b/util/src/util/Config.ts index 09a42564..ec1efc6c 100644 --- a/util/src/util/Config.ts +++ b/util/src/util/Config.ts @@ -99,6 +99,7 @@ export interface DefaultOptions { }; }; security: { + autoUpdate: boolean | number; requestSignature: string; jwtSecret: string; forwadedFor: string | null; // header to get the real user ip address @@ -230,6 +231,7 @@ export const DefaultOptions: DefaultOptions = { }, }, security: { + autoUpdate: true, requestSignature: crypto.randomBytes(32).toString("base64"), jwtSecret: crypto.randomBytes(256).toString("base64"), forwadedFor: null, |