Skip to content

Build your personal knowledge base with Trilium Notes

feat(resources): download through the system proxy on desktop

Elian Doran85ebbb0a2e66
diff --git a/apps/desktop/src/main.spec.ts b/apps/desktop/src/main.spec.ts
index b2b2a20d5d..471d7fa6c2 100644
--- a/apps/desktop/src/main.spec.ts
+++ b/apps/desktop/src/main.spec.ts
@@ -215,7 +215,7 @@ vi.mock("@triliumnext/server/src/services/i18n.js", () => ({ initializeTranslati
vi.mock("@triliumnext/server/src/services/image_provider.js", () => ({ serverImageProvider: {} }));
vi.mock("@triliumnext/server/src/www.js", () => ({ default: vi.fn(() => h.startServer()) }));
-vi.mock("./services/request", () => ({ default: class {} }));
+vi.mock("./services/request", () => ({ default: class {}, setupResourceDownloads: vi.fn() }));
vi.mock("./services/window", () => ({
default: {
getLastFocusedWindow: () => h.lastFocusedWindow,
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index 6ef7bbfd48..44b842b301 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -42,7 +42,7 @@ import {
} from "./services/backup_passphrase";
import { setupOneNoteHandlers } from "./services/onenote";
import { setupPrintingHandlers } from "./services/printing";
-import ElectronRequestProvider from "./services/request";
+import ElectronRequestProvider, { setupResourceDownloads } from "./services/request";
import { setupRestoreHandlers } from "./services/restore";
import { getSecuritySettings, registerSecurityIpcHandlers } from "./services/security_settings";
import { setupShellHandlers } from "./services/shell";
@@ -146,6 +146,7 @@ export async function main() {
registerSecurityIpcHandlers();
registerBackupPassphraseIpcHandlers();
setupStartupMetricsIpc();
+ setupResourceDownloads();
setupTranscriptionHandlers();
app.on("will-quit", () => {
diff --git a/apps/desktop/src/services/request.spec.ts b/apps/desktop/src/services/request.spec.ts
index 36020fb762..c6b89d97e6 100644
--- a/apps/desktop/src/services/request.spec.ts
+++ b/apps/desktop/src/services/request.spec.ts
@@ -1,6 +1,8 @@
import { afterEach, describe, expect, it, vi } from "vitest";
-const netSentinel = { request: () => ({ on() {}, end() {} }) };
+const netFetch = vi.fn(async (_url: string, _init: RequestInit) => new Response("ok"));
+const netSentinel = { request: () => ({ on() {}, end() {} }), fetch: netFetch };
+const setFetch = vi.fn();
const isReady = vi.fn(() => true);
const whenReady = vi.fn(() => Promise.resolve());
@@ -14,7 +16,9 @@ vi.mock("electron", () => ({
}
}));
-const { default: ElectronRequestProvider } = await import("./request.js");
+vi.mock("@triliumnext/server/src/services/resources.js", () => ({ default: { setFetch } }));
+
+const { default: ElectronRequestProvider, setupResourceDownloads } = await import("./request.js");
// `getClient` is protected; expose it through a thin subclass for testing.
class TestableProvider extends ElectronRequestProvider {
@@ -55,3 +59,21 @@ describe("ElectronRequestProvider", () => {
expect(typeof client.request).toBe("function");
});
});
+
+describe("setupResourceDownloads", () => {
+ afterEach(() => {
+ vi.clearAllMocks();
+ isReady.mockReturnValue(true);
+ });
+
+ it("downloads resources through electron.net, once the app is ready", async () => {
+ setupResourceDownloads();
+ const [ [ fetch ] ] = setFetch.mock.calls;
+ const init = { headers: { Range: "bytes=10-" } };
+
+ isReady.mockReturnValue(false);
+ await fetch("https://example.com/model.onnx", init);
+ expect(whenReady).toHaveBeenCalledOnce();
+ expect(netFetch).toHaveBeenCalledExactlyOnceWith("https://example.com/model.onnx", init);
+ });
+});
diff --git a/apps/desktop/src/services/request.ts b/apps/desktop/src/services/request.ts
index 2a635cab5e..ffa937ff35 100644
--- a/apps/desktop/src/services/request.ts
+++ b/apps/desktop/src/services/request.ts
@@ -1,4 +1,5 @@
import NodeRequestProvider, { type Client, type ClientOpts } from "@triliumnext/server/src/services/request.js";
+import resources from "@triliumnext/server/src/services/resources.js";
import electron from "electron";
/**
@@ -27,3 +28,16 @@ export default class ElectronRequestProvider extends NodeRequestProvider {
return super.getClient(opts);
}
}
+
+/**
+ * Sends the resource manager's downloads through `electron.net.fetch()`, which follows the system
+ * proxy as the sync client does, where Node's `fetch()` would ignore it.
+ */
+export function setupResourceDownloads() {
+ resources.setFetch(async (url, init) => {
+ if (!electron.app.isReady()) {
+ await electron.app.whenReady();
+ }
+ return electron.net.fetch(url, init);
+ });
+}
diff --git a/apps/server/src/services/resources.spec.ts b/apps/server/src/services/resources.spec.ts
index adec6a10f2..049a6c7afd 100644
--- a/apps/server/src/services/resources.spec.ts
+++ b/apps/server/src/services/resources.spec.ts
@@ -5,7 +5,7 @@ import http from "node:http";
import type { AddressInfo } from "node:net";
import os from "node:os";
import path from "node:path";
-import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { type ResourceDefinition, type ResourceFile, ResourceManager } from "./resources.js";
@@ -174,6 +174,17 @@ describe("ResourceManager", () => {
expect((await impatient.getStatus([ "test.model" ])).resources[0].downloadedSize).toBe(STALL_AFTER);
});
+ it("downloads through the fetch it is given, such as one that follows the system proxy", async () => {
+ const proxied = vi.fn((url: string, init: RequestInit) => fetch(url, init));
+ manager.setFetch(proxied);
+ manager.register(defineModel());
+
+ manager.startDownload([ "test.model" ]);
+ expect((await waitForEnd()).status).toBe("succeeded");
+ expect(proxied.mock.calls.map(([ url ]) => new URL(url).pathname))
+ .toEqual([ "/model.bin", "/tokens.txt" ]);
+ });
+
it("joins a running download, and cancels it without leaving an empty partial file", async () => {
manager.register(defineModel({ tokensUrl: "/stall" }));
diff --git a/apps/server/src/services/resources.ts b/apps/server/src/services/resources.ts
index 49ac64b7de..18a7283596 100644
--- a/apps/server/src/services/resources.ts
+++ b/apps/server/src/services/resources.ts
@@ -34,6 +34,15 @@ export interface ResourceDefinition {
files: ResourceFile[];
}
+/** The part of `fetch()` a download uses. */
+export type ResourceFetch = (url: string, init: RequestInit) => Promise<Response>;
+
+interface DownloadOptions {
+ /** Node's `fetch()` by default, which ignores the system proxy and the `HTTP(S)_PROXY` variables. */
+ fetch?: ResourceFetch;
+ stallTimeoutMs?: number;
+}
+
interface Download {
progress: ResourceDownloadProgress;
controller: AbortController;
@@ -69,9 +78,14 @@ export class ResourceManager {
constructor(
private readonly baseDir: string,
private readonly broadcast: (progress: ResourceDownloadProgress) => void,
- private readonly options: { stallTimeoutMs?: number } = {}
+ private readonly options: DownloadOptions = {}
) {}
+ /** Downloads through `fetch` from now on, as desktop does with Electron's, which follows the system proxy. */
+ setFetch(fetch: ResourceFetch) {
+ this.options.fetch = fetch;
+ }
+
register(...definitions: ResourceDefinition[]) {
for (const definition of definitions) {
validateDefinition(definition);
@@ -195,7 +209,7 @@ export class ResourceManager {
await downloadFile(file, destination, controller.signal, (bytes) => {
receive(bytes);
report(false);
- }, this.options.stallTimeoutMs);
+ }, this.options);
}
const { size, mtimeMs } = await fs.promises.stat(destination);
verified[file.path] = { size, mtimeMs };
@@ -337,7 +351,8 @@ export class ResourceManager {
* nothing for `stallTimeoutMs`, before the response or within it, fails the download.
*/
export async function downloadFile(file: ResourceFile, destination: string, signal: AbortSignal,
- onProgress: (bytes: number) => void, stallTimeoutMs = STALL_TIMEOUT_MS) {
+ onProgress: (bytes: number) => void,
+ { fetch = globalThis.fetch, stallTimeoutMs = STALL_TIMEOUT_MS }: DownloadOptions = {}) {
const partial = `${destination}.partial`;
const name = path.basename(file.path);
const stall = new AbortController();