Skip to content

Build your personal knowledge base with Trilium Notes

chore(desktop): drop the asar read-stream workaround

Elian Doran1590d1bd4a0f
diff --git a/apps/desktop/src/main.spec.ts b/apps/desktop/src/main.spec.ts
index 471d7fa6c2..177a82b865 100644
--- a/apps/desktop/src/main.spec.ts
+++ b/apps/desktop/src/main.spec.ts
@@ -231,7 +231,6 @@ 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 44b842b301..8c8ae15152 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -28,7 +28,6 @@ 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";
@@ -65,7 +64,6 @@ 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
deleted file mode 100644
index 91fed12779..0000000000
--- a/apps/desktop/src/services/asar_read_stream.spec.ts
+++ /dev/null
@@ -1,64 +0,0 @@
-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
deleted file mode 100644
index ec435ea611..0000000000
--- a/apps/desktop/src/services/asar_read_stream.ts
+++ /dev/null
@@ -1,53 +0,0 @@
-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 });
-}