Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
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";
/**
* Desktop variant of NodeRequestProvider that prefers Electron's `net` module
* for requests without an explicit proxy. Electron's net respects the system
* proxy configuration (PAC, environment), whereas Node's http(s) does not —
* so the sync client gets transparent system-proxy support out of the box.
*
* When an explicit proxy IS set (sync options), we fall through to Node's
* http(s) since Electron's net has no straightforward way to be configured
* with a custom proxy.
*/
export default class ElectronRequestProvider extends NodeRequestProvider {
protected async getClient(opts: ClientOpts): Promise<Client> {
if (!opts.proxy) {
// `electron.net` can only be used after `app.ready` has fired.
// The sync timer is scheduled inside `buildApp()` and could in
// principle fire before the renderer's `ready` event, so wait
// defensively. After ready, `whenReady()` resolves synchronously
// and adds no overhead.
if (!electron.app.isReady()) {
await electron.app.whenReady();
}
return electron.net as unknown as Client;
}
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);
});
}