From 5fac8be0ae68cbbdf3514973cbf925a31a765ef5 Mon Sep 17 00:00:00 2001 From: Ven Date: Sun, 23 Oct 2022 23:23:52 +0200 Subject: Vencord Standalone without git/node (#148) --- src/ipcMain/extensions.ts | 24 ++------- src/ipcMain/index.ts | 7 ++- src/ipcMain/simpleGet.ts | 37 ++++++++++++++ src/ipcMain/updater.ts | 113 ------------------------------------------ src/ipcMain/updater/common.ts | 59 ++++++++++++++++++++++ src/ipcMain/updater/git.ts | 74 +++++++++++++++++++++++++++ src/ipcMain/updater/http.ts | 86 ++++++++++++++++++++++++++++++++ src/ipcMain/updater/index.ts | 19 +++++++ 8 files changed, 281 insertions(+), 138 deletions(-) create mode 100644 src/ipcMain/simpleGet.ts delete mode 100644 src/ipcMain/updater.ts create mode 100644 src/ipcMain/updater/common.ts create mode 100644 src/ipcMain/updater/git.ts create mode 100644 src/ipcMain/updater/http.ts create mode 100644 src/ipcMain/updater/index.ts (limited to 'src/ipcMain') diff --git a/src/ipcMain/extensions.ts b/src/ipcMain/extensions.ts index d676f22..0e26ff1 100644 --- a/src/ipcMain/extensions.ts +++ b/src/ipcMain/extensions.ts @@ -19,33 +19,15 @@ import { session } from "electron"; import { unzip } from "fflate"; import { constants as fsConstants } from "fs"; -import { access,mkdir, rm, writeFile } from "fs/promises"; -import https from "https"; +import { access, mkdir, rm, writeFile } from "fs/promises"; import { join } from "path"; import { DATA_DIR } from "./constants"; import { crxToZip } from "./crxToZip"; +import { get } from "./simpleGet"; const extensionCacheDir = join(DATA_DIR, "ExtensionCache"); -function download(url: string) { - return new Promise((resolve, reject) => { - https.get(url, res => { - const { statusCode, statusMessage, headers } = res; - if (statusCode! >= 400) - return void reject(`${statusCode}: ${statusMessage} - ${url}`); - if (statusCode! >= 300) - return void resolve(download(headers.location!)); - - const chunks = [] as Buffer[]; - res.on("error", reject); - - res.on("data", chunk => chunks.push(chunk)); - res.once("end", () => resolve(Buffer.concat(chunks))); - }); - }); -} - async function extract(data: Buffer, outDir: string) { await mkdir(outDir, { recursive: true }); return new Promise((resolve, reject) => { @@ -86,7 +68,7 @@ export async function installExt(id: string) { await access(extDir, fsConstants.F_OK); } catch (err) { const url = `https://clients2.google.com/service/update2/crx?response=redirect&acceptformat=crx2,crx3&x=id%3D${id}%26uc&prodversion=32`; - const buf = await download(url); + const buf = await get(url); await extract(crxToZip(buf), extDir); } diff --git a/src/ipcMain/index.ts b/src/ipcMain/index.ts index 958728a..8a60bc6 100644 --- a/src/ipcMain/index.ts +++ b/src/ipcMain/index.ts @@ -18,19 +18,18 @@ import "./updater"; -import monacoHtml from "@fileContent/../components/monacoWin.html;base64"; -import { app, BrowserWindow, desktopCapturer, ipcMain, shell } from "electron"; +import { BrowserWindow, desktopCapturer, ipcMain, shell } from "electron"; import { mkdirSync, readFileSync, watch } from "fs"; import { open, readFile, writeFile } from "fs/promises"; import { join } from "path"; +import monacoHtml from "~fileContent/../components/monacoWin.html;base64"; + import { debounce } from "../utils/debounce"; import IpcEvents from "../utils/IpcEvents"; import { Queue } from "../utils/Queue"; import { ALLOWED_PROTOCOLS, QUICKCSS_PATH, SETTINGS_DIR, SETTINGS_FILE } from "./constants"; - - mkdirSync(SETTINGS_DIR, { recursive: true }); function readCss() { diff --git a/src/ipcMain/simpleGet.ts b/src/ipcMain/simpleGet.ts new file mode 100644 index 0000000..1a8302c --- /dev/null +++ b/src/ipcMain/simpleGet.ts @@ -0,0 +1,37 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2022 Vendicated and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +import https from "https"; + +export function get(url: string, options: https.RequestOptions = {}) { + return new Promise((resolve, reject) => { + https.get(url, options, res => { + const { statusCode, statusMessage, headers } = res; + if (statusCode! >= 400) + return void reject(`${statusCode}: ${statusMessage} - ${url}`); + if (statusCode! >= 300) + return void resolve(get(headers.location!, options)); + + const chunks = [] as Buffer[]; + res.on("error", reject); + + res.on("data", chunk => chunks.push(chunk)); + res.once("end", () => resolve(Buffer.concat(chunks))); + }); + }); +} diff --git a/src/ipcMain/updater.ts b/src/ipcMain/updater.ts deleted file mode 100644 index 6987916..0000000 --- a/src/ipcMain/updater.ts +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Vencord, a modification for Discord's desktop app - * Copyright (c) 2022 Vendicated and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . -*/ - -import { execFile as cpExecFile } from "child_process"; -import { createHash } from "crypto"; -import { ipcMain } from "electron"; -import { createReadStream } from "fs"; -import { join } from "path"; -import { promisify } from "util"; - -import IpcEvents from "../utils/IpcEvents"; - -const VENCORD_SRC_DIR = join(__dirname, ".."); - -const execFile = promisify(cpExecFile); - -function git(...args: string[]) { - return execFile("git", args, { - cwd: VENCORD_SRC_DIR - }); -} - -async function calculateHashes() { - const hashes = {} as Record; - - await Promise.all( - ["patcher.js", "preload.js", "renderer.js"].map(file => new Promise(r => { - const fis = createReadStream(join(__dirname, file)); - const hash = createHash("sha1", { encoding: "hex" }); - fis.once("end", () => { - hash.end(); - hashes[file] = hash.read(); - r(); - }); - fis.pipe(hash); - })) - ); - - return hashes; -} - -function serializeErrors(func: (...args: any[]) => any) { - return async function () { - try { - return { - ok: true, - value: await func(...arguments) - }; - } catch (e: any) { - return { - ok: false, - error: e instanceof Error ? { - // prototypes get lost, so turn error into plain object - ...e - } : e - }; - } - }; -} - -async function getRepo() { - const res = await git("remote", "get-url", "origin"); - return res.stdout.trim() - .replace(/git@(.+):/, "https://$1/") - .replace(/\.git$/, ""); -} - -async function calculateGitChanges() { - await git("fetch"); - - const res = await git("log", "HEAD...origin/main", "--pretty=format:%an/%h/%s"); - - const commits = res.stdout.trim(); - return commits ? commits.split("\n").map(line => { - const [author, hash, ...rest] = line.split("/"); - return { - hash, author, message: rest.join("/") - }; - }) : []; -} - -async function pull() { - const res = await git("pull"); - return res.stdout.includes("Fast-forward"); -} - -async function build() { - const res = await execFile("node", ["scripts/build/build.mjs"], { - cwd: VENCORD_SRC_DIR - }); - return !res.stderr.includes("Build failed"); -} - -ipcMain.handle(IpcEvents.GET_HASHES, serializeErrors(calculateHashes)); -ipcMain.handle(IpcEvents.GET_REPO, serializeErrors(getRepo)); -ipcMain.handle(IpcEvents.GET_UPDATES, serializeErrors(calculateGitChanges)); -ipcMain.handle(IpcEvents.UPDATE, serializeErrors(pull)); -ipcMain.handle(IpcEvents.BUILD, serializeErrors(build)); diff --git a/src/ipcMain/updater/common.ts b/src/ipcMain/updater/common.ts new file mode 100644 index 0000000..41f08e8 --- /dev/null +++ b/src/ipcMain/updater/common.ts @@ -0,0 +1,59 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2022 Vendicated and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +import { createHash } from "crypto"; +import { createReadStream } from "fs"; +import { join } from "path"; + +export async function calculateHashes() { + const hashes = {} as Record; + + await Promise.all( + ["patcher.js", "preload.js", "renderer.js"].map(file => new Promise(r => { + const fis = createReadStream(join(__dirname, file)); + const hash = createHash("sha1", { encoding: "hex" }); + fis.once("end", () => { + hash.end(); + hashes[file] = hash.read(); + r(); + }); + fis.pipe(hash); + })) + ); + + return hashes; +} + +export function serializeErrors(func: (...args: any[]) => any) { + return async function () { + try { + return { + ok: true, + value: await func(...arguments) + }; + } catch (e: any) { + return { + ok: false, + error: e instanceof Error ? { + // prototypes get lost, so turn error into plain object + ...e + } : e + }; + } + }; +} diff --git a/src/ipcMain/updater/git.ts b/src/ipcMain/updater/git.ts new file mode 100644 index 0000000..7e4176a --- /dev/null +++ b/src/ipcMain/updater/git.ts @@ -0,0 +1,74 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2022 Vendicated and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +import { execFile as cpExecFile } from "child_process"; +import { ipcMain } from "electron"; +import { join } from "path"; +import { promisify } from "util"; + +import IpcEvents from "../../utils/IpcEvents"; +import { calculateHashes, serializeErrors } from "./common"; + +const VENCORD_SRC_DIR = join(__dirname, ".."); + +const execFile = promisify(cpExecFile); + +function git(...args: string[]) { + return execFile("git", args, { + cwd: VENCORD_SRC_DIR + }); +} + +async function getRepo() { + const res = await git("remote", "get-url", "origin"); + return res.stdout.trim() + .replace(/git@(.+):/, "https://$1/") + .replace(/\.git$/, ""); +} + +async function calculateGitChanges() { + await git("fetch"); + + const res = await git("log", "HEAD...origin/main", "--pretty=format:%an/%h/%s"); + + const commits = res.stdout.trim(); + return commits ? commits.split("\n").map(line => { + const [author, hash, ...rest] = line.split("/"); + return { + hash, author, message: rest.join("/") + }; + }) : []; +} + +async function pull() { + const res = await git("pull"); + return res.stdout.includes("Fast-forward"); +} + +async function build() { + const res = await execFile("node", ["scripts/build/build.mjs"], { + cwd: VENCORD_SRC_DIR + }); + return !res.stderr.includes("Build failed"); +} + +ipcMain.handle(IpcEvents.GET_HASHES, serializeErrors(calculateHashes)); +ipcMain.handle(IpcEvents.GET_REPO, serializeErrors(getRepo)); +ipcMain.handle(IpcEvents.GET_UPDATES, serializeErrors(calculateGitChanges)); +ipcMain.handle(IpcEvents.UPDATE, serializeErrors(pull)); +ipcMain.handle(IpcEvents.BUILD, serializeErrors(build)); diff --git a/src/ipcMain/updater/http.ts b/src/ipcMain/updater/http.ts new file mode 100644 index 0000000..5b3f0ff --- /dev/null +++ b/src/ipcMain/updater/http.ts @@ -0,0 +1,86 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2022 Vendicated and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +import { ipcMain } from "electron"; +import { writeFile } from "fs/promises"; +import { join } from "path"; + +import gitHash from "~git-hash"; +import gitRemote from "~git-remote"; + +import { VENCORD_USER_AGENT } from "../../utils/constants"; +import IpcEvents from "../../utils/IpcEvents"; +import { get } from "../simpleGet"; +import { calculateHashes, serializeErrors } from "./common"; + +const API_BASE = `https://api.github.com/repos/${gitRemote}`; +let PendingUpdates = [] as [string, Buffer][]; + +async function githubGet(endpoint: string) { + return get(API_BASE + endpoint, { + headers: { + Accept: "application/vnd.github+json", + // "All API requests MUST include a valid User-Agent header. + // Requests with no User-Agent header will be rejected." + "User-Agent": VENCORD_USER_AGENT, + // todo: perhaps add support for (optional) api token? + // unauthorised rate limit is 60 reqs/h + // https://github.com/settings/tokens/new?description=Vencord%20Updater + } + }); +} + +async function calculateGitChanges() { + const res = await githubGet(`/compare/${gitHash}...HEAD`); + + const data = JSON.parse(res.toString("utf-8")); + return data.commits.map(c => ({ + // github api only sends the long sha + hash: c.sha.slice(0, 7), + author: c.author.login, + message: c.commit.message + })); +} + +async function fetchUpdates() { + const release = await githubGet("/releases/latest"); + + const data = JSON.parse(release.toString()); + const hash = data.name.slice(data.name.lastIndexOf(" ") + 1); + if (hash === gitHash) + return true; + + await Promise.all(data.assets.map(async ({ name, browser_download_url }) => { + if (["patcher.js", "preload.js", "renderer.js"].some(s => name.startsWith(s))) { + PendingUpdates.push([name, await get(browser_download_url)]); + } + })); + return true; +} + +async function applyUpdates() { + await Promise.all(PendingUpdates.map(([name, data]) => writeFile(join(__dirname, name), data))); + PendingUpdates = []; + return true; +} + +ipcMain.handle(IpcEvents.GET_HASHES, serializeErrors(calculateHashes)); +ipcMain.handle(IpcEvents.GET_REPO, serializeErrors(() => `https://github.com/${gitRemote}`)); +ipcMain.handle(IpcEvents.GET_UPDATES, serializeErrors(calculateGitChanges)); +ipcMain.handle(IpcEvents.UPDATE, serializeErrors(fetchUpdates)); +ipcMain.handle(IpcEvents.BUILD, serializeErrors(applyUpdates)); diff --git a/src/ipcMain/updater/index.ts b/src/ipcMain/updater/index.ts new file mode 100644 index 0000000..7036112 --- /dev/null +++ b/src/ipcMain/updater/index.ts @@ -0,0 +1,19 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2022 Vendicated and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +import(IS_STANDALONE ? "./http" : "./git"); -- cgit