Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Prune desktop build comments

Manav Rathicb487e53e050
diff --git a/desktop/.gitignore b/desktop/.gitignore
index 583286b838..ee9fddbf32 100644
--- a/desktop/.gitignore
+++ b/desktop/.gitignore
@@ -1,26 +1,12 @@
-# Local env files
.env
.env.*.local
-# tsc transpiles src/**/*.ts and emits the generated JS into app
app/
-
-# out is a symlink to the photos web app's out dir, which contains the built up
-# photos app.
out
-
-# electron-builder
dist/
-# We download it on demand, if needed for the particular OS/arch.
build/vips*
-
-# Staged by scripts/ort.js for the particular OS/arch.
build/onnxruntime/
-
-# Rust N-API addons staged for packaging by scripts/napi.js.
build/napi/
-# The Rust ML addon built by scripts/napi.js, and the declarations generated by
-# `cargo codegen napi`.
rust-bindings/
diff --git a/desktop/electron-builder.yml b/desktop/electron-builder.yml
index 9cd676777f..1604d32650 100644
--- a/desktop/electron-builder.yml
+++ b/desktop/electron-builder.yml
@@ -42,7 +42,6 @@ mac:
category: public.app-category.photography
hardenedRuntime: true
mergeASARs: false
- # The Rust N-API addon and the ONNX Runtime library are shipped as separate
- # per-arch Mach-Os (in both slices of the universal build) instead of fat
- # binaries; the app picks the one matching the process arch at runtime.
+ # Both macOS build passes contain separate x64 and arm64 binaries. Copy them
+ # into the universal app unchanged; the app selects one at runtime.
x64ArchFiles: "**/{napi,onnxruntime}/**"
diff --git a/desktop/eslint.config.mjs b/desktop/eslint.config.mjs
index 25d3785a50..5e030ec051 100644
--- a/desktop/eslint.config.mjs
+++ b/desktop/eslint.config.mjs
@@ -8,7 +8,6 @@ export default ts.config(
...ts.configs.strictTypeChecked,
...ts.configs.stylisticTypeChecked,
{
- // typescript-eslint needs this enabling type checked rules.
languageOptions: {
parserOptions: {
project: true,
@@ -17,8 +16,7 @@ export default ts.config(
},
},
{
- // The list of (minimatch) globs to ignore. This needs to be the only
- // key in this configuration object.
+ // ESLint ignores these paths globally only when this object has no other keys.
ignores: [
"eslint.config.mjs",
"scripts/*.js",
@@ -30,28 +28,21 @@ export default ts.config(
],
},
{
- // Rule customizations.
rules: {
- // Allow numbers to be used in template literals.
"@typescript-eslint/restrict-template-expressions": [
"error",
{ allowNumber: true },
],
- // Allow void expressions as the entire body of an arrow function.
"@typescript-eslint/no-confusing-void-expression": [
"error",
{ ignoreArrowShorthand: true },
],
- // Allow free standing ternary expressions.
"@typescript-eslint/no-unused-expressions": [
"error",
{ allowTernary: true },
],
- // Allow force unwrapping potentially optional values.
- //
- // See: [Note: non-null-assertions have better stack trace]
+ // Prefer native undefined access errors to custom assertions.
"@typescript-eslint/no-non-null-assertion": "off",
- // Allow `while(true)` etc.
"@typescript-eslint/no-unnecessary-condition": [
"error",
{ allowConstantLoopConditions: true },
diff --git a/desktop/scripts/beforeBuild.js b/desktop/scripts/beforeBuild.js
index cd8ee81711..3576e09b4a 100755
--- a/desktop/scripts/beforeBuild.js
+++ b/desktop/scripts/beforeBuild.js
@@ -2,49 +2,16 @@ const fsp = require("fs/promises");
const { stageNapiAddons } = require("./napi");
const { stageONNXRuntime } = require("./ort");
-/**
- * This hook is invoked during the initial build (e.g. when triggered by
- * "npm run build"), and importantly, on each rebuild for a different
- * architecture during the build. We use it to stage the vips binary, the ONNX
- * Runtime library, and the Rust N-API addon for the current architecture.
- * See "[Note: vips]", "[Note: ONNX Runtime binaries]" and "[Note: Packaging
- * the N-API addon]" for more details.
- *
- * The documentation for this hook is at:
- * https://www.electron.build/app-builder-lib.interface.configuration#beforebuild
- *
- * > The function to be run before dependencies are installed or rebuilt.
- *
- * Here is an example of the context that it gets
- * https://www.electron.build/app-builder-lib.interface.beforebuildcontext
- *
- * appDir: '/path/to/ente/desktop',
- * platform: Platform {
- * name: 'mac',
- * buildConfigurationKey: 'mac',
- * nodeName: 'darwin'
- * },
- * arch: 'arm64'
- *
- * Note that we must not return falsey from this function, because:
- *
- * > Resolving to false will skip dependencies install or rebuild.
- *
- */
+// Electron Builder skips its dependency rebuild after any falsy return.
module.exports = async (context) => {
const { appDir, platform, arch } = context;
- // Stage the already-downloaded ONNX Runtime library, then build and stage
- // the release Rust addon for the architecture being packaged.
await stageONNXRuntime(platform.nodeName, arch, appDir);
await stageNapiAddons(appDir, platform.nodeName, arch);
// The arch used by Electron Builder is not the same as the arch used by
// Node's process, but for the two cases that we care about, "x64" and
// "arm64", both of them use the string constant and thus can be compared.
- //
- // https://github.com/electron-userland/electron-builder/blob/master/packages/builder-util/src/arch.ts#L9
- // https://nodejs.org/api/process.html#processarch
if (arch == process.arch) {
// `vips.js` would've already downloaded the file, nothing to do.
return true;
diff --git a/desktop/scripts/napi.js b/desktop/scripts/napi.js
index 1f90444a2d..cdd74c2ffe 100644
--- a/desktop/scripts/napi.js
+++ b/desktop/scripts/napi.js
@@ -1,19 +1,3 @@
-/**
- * [Note: Packaging the N-API addon]
- *
- * This script owns the two napi-rs operations used by desktop:
- *
- * - `node scripts/napi.js dts <typeDefDir> <outPath>` renders declarations
- * emitted by the `cargo check` performed by `cargo codegen napi`.
- * - `node scripts/napi.js build` builds the host addon into the gitignored
- * `rust-bindings/` directory for development.
- *
- * Packaging calls {@link stageNapiAddons} from `beforeBuild.js`. Every addon
- * build goes through {@link buildNapiAddon}, which always uses Cargo's
- * release profile. This script neither downloads nor installs ONNX Runtime;
- * that is owned by `ort.js` and postinstall.
- */
-
const fsp = require("node:fs/promises");
const path = require("node:path");
const { execFileSync, execSync } = require("node:child_process");
@@ -21,7 +5,6 @@ const { execFileSync, execSync } = require("node:child_process");
const archesForTarget = (platform, arch) =>
platform == "darwin" ? ["arm64", "x64"] : [arch];
-/** napi-cli's name for the addon built for the given platform-arch. */
const napiTriple = (platform, arch) => {
switch (`${platform}-${arch}`) {
case "darwin-arm64":
@@ -41,7 +24,6 @@ const napiTriple = (platform, arch) => {
}
};
-/** The Rust target triple for the given platform-arch. */
const rustTriple = (platform, arch) => {
switch (`${platform}-${arch}`) {
case "darwin-arm64":
@@ -61,7 +43,6 @@ const rustTriple = (platform, arch) => {
}
};
-/** Install the given Rust target for the active toolchain if it is missing. */
const ensureRustTarget = (target, appDir) => {
const installed = execFileSync(
"rustup",
@@ -77,14 +58,6 @@ const ensureRustTarget = (target, appDir) => {
});
};
-/**
- * Build the addon for the given platform-arch into `rust-bindings/`.
- *
- * The build always runs so that edits to the Rust sources cannot be missed
- * when packaging; Cargo's incremental compilation makes this cheap when
- * nothing has changed. Linux always uses napi-cli's cross toolchain, including
- * for the host architecture, to keep the addon's glibc requirement at 2.17.
- */
const buildNapiAddon = async (appDir, platform, arch) => {
if (arch != process.arch)
ensureRustTarget(rustTriple(platform, arch), appDir);
@@ -116,13 +89,7 @@ const buildNapiAddon = async (appDir, platform, arch) => {
return addonPath;
};
-/**
- * Ensure the release addon(s) for a package target are built and staged into
- * `build/napi/`.
- */
const stageNapiAddons = async (appDir, platform, arch) => {
- // On macOS the packaged app is universal, so both architectures must be
- // staged regardless of which arch this build pass is for.
const arches = archesForTarget(platform, arch);
const stageDir = path.join(appDir, "build", "napi");
@@ -138,14 +105,11 @@ const stageNapiAddons = async (appDir, platform, arch) => {
);
}
- // Remove addons staged by earlier passes for other platforms or
- // architectures so that they don't get copied into this package.
for (const entry of await fsp.readdir(stageDir))
if (!wanted.includes(entry))
await fsp.rm(path.join(stageDir, entry), { recursive: true });
};
-/** Render the napi macro output into the declaration file used by TypeScript. */
const renderTypeDefinitions = async (typeDefDir, outPath) => {
const { generateTypeDef } = require("@napi-rs/cli");
const { dts } = await generateTypeDef({ typeDefDir, cwd: process.cwd() });
diff --git a/desktop/scripts/ort.js b/desktop/scripts/ort.js
index f8b292cd41..bccdf8a552 100644
--- a/desktop/scripts/ort.js
+++ b/desktop/scripts/ort.js
@@ -1,21 +1,3 @@
-/**
- * [Note: ONNX Runtime binaries]
- *
- * The Rust ML addon (see [Note: ML with Rust]) loads ONNX Runtime dynamically
- * at runtime. We use Ente's pinned custom builds β€” CoreML-enabled on macOS and
- * WebGPU-capable elsewhere β€” published at
- * https://github.com/ente/ort-packaging/releases
- *
- * This script is the only place that downloads those builds. Postinstall runs
- * it once to acquire both architectures for the host platform. The extracted
- * libraries are kept under `node_modules/.cache/ente-onnxruntime/`, where the
- * development app loads them directly.
- *
- * During packaging, `beforeBuild.js` calls {@link stageONNXRuntime} to copy the
- * already-installed libraries needed by the current target into
- * `build/onnxruntime/`. That step performs no network access.
- */
-
const crypto = require("node:crypto");
const fsp = require("node:fs/promises");
const path = require("node:path");
@@ -23,15 +5,11 @@ const { execFileSync } = require("node:child_process");
const ortVersion = "1.28.0-r1";
+// Our packaging enables CoreML on macOS and WebGPU elsewhere.
const ortReleaseURL = `https://github.com/ente/ort-packaging/releases/download/ort-${ortVersion}`;
-/**
- * SHA-256 checksums of the release assets, pinned here so that the library we
- * package cannot change without a corresponding (reviewed) change to this
- * file. When bumping {@link ortVersion}, update these with the values from the
- * release's `.sha256` sidecar files after verifying them against the built
- * archives.
- */
+// When changing ortVersion, verify the archives and update these from the
+// release's .sha256 sidecars.
const ortAssetSHA256s = {
"darwin-arm64":
"de08b6c23398f1d6639c66519b44b06f7cd0f7c44a5ad7bade3c4d8b049c5428",
@@ -66,7 +44,6 @@ const archesForTarget = (platform, arch) =>
const installDir = (appDir) =>
path.join(appDir, "node_modules", ".cache", "ente-onnxruntime");
-/** Download and extract one pinned ONNX Runtime build, if it is not current. */
const downloadONNXRuntimeIfNeeded = async (platform, arch, appDir) => {
const asset = ortAssetName(platform, arch);
const outDir = path.join(installDir(appDir), arch);
@@ -95,17 +72,12 @@ const downloadONNXRuntimeIfNeeded = async (platform, arch, appDir) => {
await fsp.mkdir(outDir, { recursive: true });
const archivePath = path.join(outDir, asset);
await fsp.writeFile(archivePath, archive);
- // tar is available on all the platforms we build on (bsdtar on macOS and
- // Windows, GNU tar on Linux), and handles both .tar.gz and .zip.
+ // Windows ships bsdtar, which also extracts zip archives.
execFileSync("tar", ["-xf", asset], { cwd: outDir });
await fsp.rm(archivePath);
await fsp.writeFile(stampPath, asset);
};
-/**
- * Copy the already-installed ONNX Runtime libraries required by a build target
- * into the resources staging directory. This deliberately never downloads.
- */
const stageONNXRuntime = async (platform, arch, appDir) => {
const arches = archesForTarget(platform, arch);
const sourceRoot = installDir(appDir);
@@ -137,7 +109,6 @@ const stageONNXRuntime = async (platform, arch, appDir) => {
});
};
-/** Acquire every architecture needed by desktop packaging on this platform. */
const installONNXRuntime = async (platform, appDir) => {
for (const arch of ["arm64", "x64"])
await downloadONNXRuntimeIfNeeded(platform, arch, appDir);
diff --git a/desktop/scripts/postinstall.mjs b/desktop/scripts/postinstall.mjs
index 3fea964cb7..3e0373649f 100644
--- a/desktop/scripts/postinstall.mjs
+++ b/desktop/scripts/postinstall.mjs
@@ -1,23 +1,10 @@
-/**
- * Fetch binary dependencies and rebuild native modules.
- *
- * Installs skip dependency lifecycle scripts (`ignore-scripts` in `.npmrc`),
- * so this postinstall step doesn't run automatically on install and needs to
- * be invoked explicitly (`npm run postinstall`).
- *
- * With `--if-needed`, do nothing if we've already run for the currently
- * installed dependency tree. The stamp records a hash of npm's hidden
- * lockfile (`node_modules/.package-lock.json`), which reflects what is
- * actually installed, so both `npm ci` (which recreates `node_modules`) and
- * incremental `npm install`s that change packages invalidate it.
- */
-
import { execSync } from "node:child_process";
import { createHash } from "node:crypto";
import { readFileSync, writeFileSync } from "node:fs";
const stamp = "node_modules/.postinstall-stamp";
+// npm's hidden lockfile represents the dependency tree installed in node_modules.
const treeHash = () =>
createHash("sha256")
.update(readFileSync("node_modules/.package-lock.json"))
diff --git a/desktop/scripts/vips.js b/desktop/scripts/vips.js
index 59ededcdbd..6176a8448b 100755
--- a/desktop/scripts/vips.js
+++ b/desktop/scripts/vips.js
@@ -1,40 +1,8 @@
-/**
- * [Note: vips]
- *
- * For use within our Electron app we need static builds for Linux and Windows
- * for both x64 and ARM. For this, we need a custom workflow because (as of
- * writing) upstream doesn't publish these.
- *
- * This is our custom workflow, which runs on a fork of upstream:
- * https://github.com/ente/libvips-packaging/commit/a298aff3e1f25f713508d31d0c3a55a4f828fdd3
- *
- * The binaries it creates are available at
- * https://github.com/ente/libvips-packaging/releases/tag/v8.16.0
- *
- * To integrate this binary, we need to modify two places:
- *
- * 1. This script, `vips.js`, runs during "npm install" (it is set as the
- * "prepare" step in our `package.json`). It downloads the relevant binary
- * for the current OS/arch combination and places it in the `build` folder,
- * allowing it to be used during development.
- *
- * 2. The sibling script, `beforeBuild.js`, runs during "npm run build" (it is set
- * as the beforeBuild script in `electrons-builder.yml`). It downloads the
- * relevant binary for the OS/arch combination being built.
- *
- * Note that `vips.js` would've already run once `beforeBuild.js` is run, but on
- * our CI we prepare builds for multiple architectures in one go, so we need to
- * unconditionally replace the binary with the relevant one for the current
- * architecture being built (which might be different from the one we're running
- * on). `beforeBuild.js` runs for each architecture being built.
- *
- * On macOS, we don't need `vips` since there we use the native `sips`.
- */
-
const fs = require("fs");
const fsp = require("fs/promises");
const main = () => {
+ // macOS uses sips
switch (`${process.platform}-${process.arch}`) {
case "linux-x64":
return downloadIfNeeded("vips-x64", "vips");
@@ -42,7 +10,7 @@ const main = () => {
return downloadIfNeeded("vips-arm64", "vips");
case "win32-x64":
return downloadIfNeeded("vips-x86_64.exe", "vips.exe");
- case "linux-arm64":
+ case "win32-arm64":
return downloadIfNeeded("vips-aarch64.exe", "vips.exe");
}
};
@@ -51,8 +19,7 @@ const downloadIfNeeded = (downloadName, outputName) => {
const out = `build/${outputName}`;
try {
- // Making the file executable is the last step, so if the file exists at
- // this path and is executable, we assume it is the correct one.
+ // chmod is the final download step, so executable means complete.
fs.accessSync(out, fs.constants.X_OK);
return;
} catch {}
diff --git a/desktop/tsconfig.json b/desktop/tsconfig.json
index 3f1a78dd09..cd5b31a7d5 100644
--- a/desktop/tsconfig.json
+++ b/desktop/tsconfig.json
@@ -1,18 +1,6 @@
{
- /* TSConfig for a set of vanilla TypeScript files that need to be transpiled
- into JavaScript that'll then be loaded and run by the main (node) process
- of our Electron app. */
-
- /*
- * Recommended target, lib and other settings for code running in the
- * version of Node.js bundled with Electron.
- *
- * Currently, with Electron 42, this is Node.js 24.15.0.
- * https://releases.electronjs.org/release/v42.0.0
- */
- /* TSConfig docs: https://aka.ms/tsconfig.json */
"compilerOptions": {
- /* Mirror the runtime baseline of Electron 42. */
+ /* Match Electron's embedded Node.js runtime. */
"lib": [
"ES2024",
"ESNext.Array",
@@ -28,25 +16,16 @@
"moduleResolution": "node16",
"types": ["node"],
- /* Emit the generated JS into `app/` */
"rootDir": "src",
"outDir": "app",
- /* Require the `type` modifier when importing types */
- /* We want this, but it causes "ESM syntax is not allowed in a CommonJS
- module when 'verbatimModuleSyntax' is enabled" currently */
- /* "verbatimModuleSyntax": true, */
-
"strict": true,
- /* Stricter than strict */
"noImplicitReturns": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"noFallthroughCasesInSwitch": true,
- /* e.g. makes array indexing returns undefined */
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true
},
- /* Include all `.ts` files in `src/` */
"include": ["src"]
}