2024-07-18 21:35:17 +03:00
|
|
|
import assetPath from "../services/asset_path.js";
|
2024-07-18 21:37:45 +03:00
|
|
|
import path from "path";
|
2024-07-19 00:18:35 +03:00
|
|
|
import { fileURLToPath } from "url";
|
2024-07-18 21:37:45 +03:00
|
|
|
import express from "express";
|
2025-02-08 00:20:55 +02:00
|
|
|
import { isDev, isElectron } from "../services/utils.js";
|
2025-01-13 23:18:10 +02:00
|
|
|
import type serveStatic from "serve-static";
|
2025-04-23 10:06:37 +03:00
|
|
|
import proxy from "express-http-proxy";
|
2024-12-14 10:05:38 +02:00
|
|
|
|
2025-03-08 16:01:53 +01:00
|
|
|
const persistentCacheStatic = (root: string, options?: serveStatic.ServeStaticOptions<express.Response<unknown, Record<string, unknown>>>) => {
|
2025-01-22 19:08:38 +01:00
|
|
|
if (!isDev) {
|
2023-05-07 15:23:46 +02:00
|
|
|
options = {
|
2025-01-09 18:07:02 +02:00
|
|
|
maxAge: "1y",
|
2023-05-07 15:23:46 +02:00
|
|
|
...options
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return express.static(root, options);
|
|
|
|
};
|
|
|
|
|
2024-12-19 18:16:46 +02:00
|
|
|
async function register(app: express.Application) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const srcRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
|
2025-04-24 13:18:06 +03:00
|
|
|
const distDir = path.dirname(process.argv[1]);
|
2025-01-22 19:08:38 +01:00
|
|
|
if (isDev) {
|
2025-04-23 10:24:05 +03:00
|
|
|
const publicUrl = process.env.TRILIUM_PUBLIC_SERVER;
|
|
|
|
if (!publicUrl) {
|
|
|
|
throw new Error("Missing TRILIUM_PUBLIC_SERVER");
|
|
|
|
}
|
|
|
|
|
|
|
|
const clientProxy = proxy(publicUrl);
|
2025-02-02 12:47:41 +02:00
|
|
|
app.use(`/${assetPath}/app/doc_notes`, persistentCacheStatic(path.join(srcRoot, "public/app/doc_notes")));
|
2025-04-23 10:08:02 +03:00
|
|
|
app.use(`/${assetPath}/app`, clientProxy);
|
|
|
|
app.use(`/${assetPath}/app-dist`, clientProxy);
|
2025-04-23 10:24:05 +03:00
|
|
|
app.use(`/${assetPath}/stylesheets`, proxy(publicUrl, {
|
|
|
|
proxyReqPathResolver: (req) => "/stylesheets" + req.url
|
|
|
|
}));
|
2025-04-23 11:40:23 +03:00
|
|
|
app.use(`/${assetPath}/libraries`, proxy(publicUrl, {
|
|
|
|
proxyReqPathResolver: (req) => "/libraries" + req.url
|
|
|
|
}));
|
2025-04-24 12:35:53 +03:00
|
|
|
app.use(`/${assetPath}/images`, persistentCacheStatic(path.join(srcRoot, "assets", "images")));
|
2024-12-14 10:10:10 +02:00
|
|
|
} else {
|
2025-04-24 13:18:06 +03:00
|
|
|
const clientStaticCache = persistentCacheStatic(path.join(distDir, "public"));
|
|
|
|
app.use(`/${assetPath}/app`, clientStaticCache);
|
|
|
|
app.use(`/${assetPath}/app-dist`, clientStaticCache);
|
|
|
|
app.use(`/${assetPath}/stylesheets`, persistentCacheStatic(path.join(distDir, "public", "stylesheets")));
|
2025-04-24 14:47:48 +03:00
|
|
|
app.use(`/${assetPath}/libraries`, persistentCacheStatic(path.join(distDir, "public", "libraries")));
|
2025-04-24 13:45:48 +03:00
|
|
|
app.use(`/${assetPath}/images`, persistentCacheStatic(path.join(distDir, "assets", "images")));
|
2024-12-14 10:10:10 +02:00
|
|
|
}
|
2025-01-09 18:07:02 +02:00
|
|
|
app.use(`/${assetPath}/fonts`, persistentCacheStatic(path.join(srcRoot, "public/fonts")));
|
|
|
|
app.use(`/assets/vX/fonts`, express.static(path.join(srcRoot, "public/fonts")));
|
2025-04-23 10:06:37 +03:00
|
|
|
app.use(`/assets/vX/images`, express.static(path.join(srcRoot, "..", "images")));
|
2025-01-09 18:07:02 +02:00
|
|
|
app.use(`/assets/vX/stylesheets`, express.static(path.join(srcRoot, "public/stylesheets")));
|
2025-04-18 21:59:14 +03:00
|
|
|
app.use(`/${assetPath}/libraries`, persistentCacheStatic(path.join(srcRoot, "public/libraries")));
|
2025-01-09 18:07:02 +02:00
|
|
|
app.use(`/assets/vX/libraries`, express.static(path.join(srcRoot, "..", "libraries")));
|
2025-04-18 13:31:59 +03:00
|
|
|
|
2025-04-24 13:18:06 +03:00
|
|
|
const nodeModulesDir = isDev ? path.join(srcRoot, "..", "node_modules") : path.join(distDir, "node_modules");
|
2025-04-18 13:31:59 +03:00
|
|
|
|
|
|
|
app.use(`/node_modules/@excalidraw/excalidraw/dist/fonts/`, express.static(path.join(nodeModulesDir, "@excalidraw/excalidraw/dist/prod/fonts/")));
|
|
|
|
app.use(`/${assetPath}/node_modules/@excalidraw/excalidraw/dist/fonts/`, persistentCacheStatic(path.join(nodeModulesDir, "@excalidraw/excalidraw/dist/prod/fonts/")));
|
2023-11-09 18:21:53 -03:00
|
|
|
|
|
|
|
// KaTeX
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/${assetPath}/node_modules/katex/dist/katex.min.js`, persistentCacheStatic(path.join(nodeModulesDir, "katex/dist/katex.min.js")));
|
|
|
|
app.use(`/${assetPath}/node_modules/katex/dist/contrib/mhchem.min.js`, persistentCacheStatic(path.join(nodeModulesDir, "katex/dist/contrib/mhchem.min.js")));
|
|
|
|
app.use(`/${assetPath}/node_modules/katex/dist/contrib/auto-render.min.js`, persistentCacheStatic(path.join(nodeModulesDir, "katex/dist/contrib/auto-render.min.js")));
|
2023-11-09 18:21:53 -03:00
|
|
|
// expose the whole dist folder
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/node_modules/katex/dist/`, express.static(path.join(nodeModulesDir, "katex/dist/")));
|
|
|
|
app.use(`/${assetPath}/node_modules/katex/dist/`, persistentCacheStatic(path.join(nodeModulesDir, "katex/dist/")));
|
2023-11-22 19:58:54 +01:00
|
|
|
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/${assetPath}/node_modules/boxicons/css/`, persistentCacheStatic(path.join(nodeModulesDir, "boxicons/css/")));
|
|
|
|
app.use(`/${assetPath}/node_modules/boxicons/fonts/`, persistentCacheStatic(path.join(nodeModulesDir, "boxicons/fonts/")));
|
2023-11-22 20:09:04 +01:00
|
|
|
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/${assetPath}/node_modules/jquery/dist/`, persistentCacheStatic(path.join(nodeModulesDir, "jquery/dist/")));
|
2023-11-22 20:22:16 +01:00
|
|
|
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/${assetPath}/node_modules/jquery-hotkeys/`, persistentCacheStatic(path.join(nodeModulesDir, "jquery-hotkeys/")));
|
2023-11-22 20:22:16 +01:00
|
|
|
|
2024-08-11 01:33:51 +02:00
|
|
|
// i18n
|
|
|
|
app.use(`/${assetPath}/translations/`, persistentCacheStatic(path.join(srcRoot, "public", "translations/")));
|
2024-07-20 09:32:56 +03:00
|
|
|
|
2024-08-12 17:41:33 +02:00
|
|
|
// Deprecated, https://www.npmjs.com/package/autocomplete.js?activeTab=readme
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/${assetPath}/node_modules/autocomplete.js/dist/`, persistentCacheStatic(path.join(nodeModulesDir, "autocomplete.js/dist/")));
|
2024-08-11 01:20:17 +02:00
|
|
|
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/${assetPath}/node_modules/normalize.css/`, persistentCacheStatic(path.join(nodeModulesDir, "normalize.css/")));
|
2024-08-11 01:59:50 +02:00
|
|
|
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/${assetPath}/node_modules/jquery.fancytree/dist/`, persistentCacheStatic(path.join(nodeModulesDir, "jquery.fancytree/dist/")));
|
2024-08-11 01:59:50 +02:00
|
|
|
|
2024-08-12 17:41:33 +02:00
|
|
|
// CodeMirror
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/${assetPath}/node_modules/codemirror/lib/`, persistentCacheStatic(path.join(nodeModulesDir, "codemirror/lib/")));
|
|
|
|
app.use(`/${assetPath}/node_modules/codemirror/addon/`, persistentCacheStatic(path.join(nodeModulesDir, "codemirror/addon/")));
|
|
|
|
app.use(`/${assetPath}/node_modules/codemirror/mode/`, persistentCacheStatic(path.join(nodeModulesDir, "codemirror/mode/")));
|
|
|
|
app.use(`/${assetPath}/node_modules/codemirror/keymap/`, persistentCacheStatic(path.join(nodeModulesDir, "codemirror/keymap/")));
|
2024-09-01 15:04:47 +03:00
|
|
|
|
2025-04-18 13:31:59 +03:00
|
|
|
app.use(`/${assetPath}/node_modules/@highlightjs/cdn-assets/`, persistentCacheStatic(path.join(nodeModulesDir, "@highlightjs/cdn-assets/")));
|
2023-11-22 19:58:54 +01:00
|
|
|
}
|
2023-05-07 15:23:46 +02:00
|
|
|
|
2024-07-18 21:42:44 +03:00
|
|
|
export default {
|
2023-05-07 15:23:46 +02:00
|
|
|
register
|
|
|
|
};
|