Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): read asar files without Electron's temp copies (closes #9748)

Elian Doran5b91f87778ec
diff --git a/apps/desktop/src/main.spec.ts b/apps/desktop/src/main.spec.ts
index 917ea15240..118da24c3c 100644
--- a/apps/desktop/src/main.spec.ts
+++ b/apps/desktop/src/main.spec.ts
@@ -231,6 +231,7 @@ vi.mock("./services/window", () => ({
vi.mock("./ipc_messaging_provider", () => ({ default: class { init = vi.fn(); } }));
vi.mock("./platform_provider", () => ({ default: class {} }));
vi.mock("./protocol", () => ({ registerTriliumAppScheme: vi.fn(), setupTriliumAppProtocol: vi.fn() }));
+vi.mock("./services/asar_read_stream", () => ({ setupAsarReadStreams: vi.fn() }));
vi.mock("./services/custom_dictionary", () => ({ setupCustomDictionary: vi.fn() }));
vi.mock("./services/printing", () => ({ setupPrintingHandlers: vi.fn() }));
vi.mock("./services/tray", () => ({ setupSystemTray: vi.fn() }));
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index cfda4b1f84..a8b5d328e8 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -28,6 +28,7 @@ import CompositeMessagingProvider from "./composite_messaging_provider";
import IpcMessagingProvider from "./ipc_messaging_provider";
import DesktopPlatformProvider from "./platform_provider";
import { registerTriliumAppScheme, setupTriliumAppProtocol } from "./protocol";
+import { setupAsarReadStreams } from "./services/asar_read_stream";
import { applyLaunchOnStartup, setupAutoLaunch, wasLaunchedHidden } from "./services/auto_launch";
import { setupCustomDictionary } from "./services/custom_dictionary";
import { setupReferer } from "./services/referer";
@@ -63,6 +64,7 @@ export async function main() {
}
registerTriliumAppScheme();
+ setupAsarReadStreams();
const userDataPath = getUserData();
app.setPath("userData", userDataPath);
diff --git a/apps/desktop/src/services/asar_read_stream.spec.ts b/apps/desktop/src/services/asar_read_stream.spec.ts
new file mode 100644
index 0000000000..91fed12779
--- /dev/null
+++ b/apps/desktop/src/services/asar_read_stream.spec.ts
@@ -0,0 +1,64 @@
+import fs from "fs";
+import { mkdtempSync, rmSync, writeFileSync } from "fs";
+import { tmpdir } from "os";
+import { join } from "path";
+import { text } from "stream/consumers";
+import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
+
+vi.mock("electron", () => ({ app: { getAppPath: () => "/opt/trilium/resources/app" } }));
+
+const { setupAsarReadStreams, wrapCreateReadStream } = await import("./asar_read_stream.js");
+
+const archive = mkdtempSync(join(tmpdir(), "trilium-asar-"));
+const file = join(archive, "index.html");
+writeFileSync(file, "0123456789");
+
+afterAll(() => rmSync(archive, { recursive: true, force: true }));
+
+describe("wrapCreateReadStream", () => {
+ it("reads files under the archive without opening them, within the range", async () => {
+ const original = vi.fn(fs.createReadStream);
+ const createReadStream = wrapCreateReadStream(original, archive);
+
+ expect(await text(createReadStream(file))).toBe("0123456789");
+ expect(await text(createReadStream(file, { start: 2, end: 4 }))).toBe("234");
+ expect(await text(createReadStream(file, { start: 7 }))).toBe("789");
+ expect(original).not.toHaveBeenCalled();
+ });
+
+ it("fails the stream with the read error for a missing entry", async () => {
+ const original = vi.fn(fs.createReadStream);
+ const createReadStream = wrapCreateReadStream(original, archive);
+
+ const missing = createReadStream(join(archive, "missing.js"));
+ await expect(text(missing)).rejects.toMatchObject({ code: "ENOENT" });
+ expect(original).not.toHaveBeenCalled();
+ });
+
+ it("passes other paths and options through to the original", () => {
+ const stream = {} as fs.ReadStream;
+ const original = vi.fn(() => stream);
+ const createReadStream = wrapCreateReadStream(original, archive);
+
+ expect(createReadStream(`${archive}.unpacked/addon.node`)).toBe(stream);
+ expect(createReadStream(file, "utf-8")).toBe(stream);
+ expect(createReadStream(file, { encoding: "utf-8" })).toBe(stream);
+ expect(createReadStream(Buffer.from(file))).toBe(stream);
+ expect(original).toHaveBeenCalledTimes(4);
+ });
+});
+
+describe("setupAsarReadStreams", () => {
+ const original = fs.createReadStream;
+ afterEach(() => {
+ fs.createReadStream = original;
+ });
+
+ it("installs the wrapper only when the app runs from an asar archive", () => {
+ setupAsarReadStreams();
+ expect(fs.createReadStream).toBe(original);
+
+ setupAsarReadStreams("/opt/trilium/resources/app.asar");
+ expect(fs.createReadStream).not.toBe(original);
+ });
+});
diff --git a/apps/desktop/src/services/asar_read_stream.ts b/apps/desktop/src/services/asar_read_stream.ts
new file mode 100644
index 0000000000..ec435ea611
--- /dev/null
+++ b/apps/desktop/src/services/asar_read_stream.ts
@@ -0,0 +1,53 @@
+import { app } from "electron";
+import fs from "fs";
+import { sep } from "path";
+import { Readable } from "stream";
+
+type CreateReadStream = typeof fs.createReadStream;
+
+/**
+ * Makes `fs.createReadStream()` read files inside the app's asar archive with `readFile()`.
+ *
+ * Electron opens an asar entry through a copy in the OS temp directory, made once per process.
+ * macOS deletes temp files that nobody accessed for three days, after which `send` (behind
+ * `express.static()` and `res.sendFile()`) fails with `ENOENT`. `readFile()` reads the archive.
+ *
+ * TODO: Remove this module when upgrading to Electron 45, which reads asar entries in place
+ * (electron/electron#52833, fixing electron/electron#52804).
+ */
+export function setupAsarReadStreams(appPath = app.getAppPath()) {
+ if (!appPath.endsWith(".asar")) {
+ return;
+ }
+ fs.createReadStream = wrapCreateReadStream(fs.createReadStream, appPath);
+}
+
+/** Wraps `createReadStream` so that paths under `asarPath` are read with `readFile()`. */
+export function wrapCreateReadStream(
+ createReadStream: CreateReadStream,
+ asarPath: string
+): CreateReadStream {
+ const prefix = asarPath + sep;
+ return function (this: unknown, path, options) {
+ if (typeof path !== "string" || !path.startsWith(prefix) || !isPlainRange(options)) {
+ return createReadStream.call(this, path, options);
+ }
+ const stream = readFromArchive(path, options?.start ?? 0, options?.end ?? Infinity);
+ return stream as fs.ReadStream;
+ } as CreateReadStream;
+}
+
+type Range = { start?: number; end?: number };
+
+function isPlainRange(options: Parameters<CreateReadStream>[1]): options is Range | undefined {
+ return options === undefined
+ || (typeof options === "object" && options.fd === undefined && options.encoding == null);
+}
+
+/** Streams the bytes from `start` to `end`, both inclusive as in `fs.createReadStream()`. */
+function readFromArchive(path: string, start: number, end: number) {
+ return Readable.from((async function* () {
+ const content = await fs.promises.readFile(path);
+ yield content.subarray(start, end + 1);
+ })(), { objectMode: false });
+}