mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 13:39:01 +01:00 
			
		
		
		
	Merge pull request #1073 from TriliumNext/refactor_utils-isPlatform
refactor(server/utils): turn isMac/isWin/isElectron/isDev into boolean
This commit is contained in:
		
						commit
						6fab899898
					
				@ -35,7 +35,7 @@ app.use((req, res, next) => {
 | 
				
			|||||||
    return next();
 | 
					    return next();
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if (!utils.isElectron()) {
 | 
					if (!utils.isElectron) {
 | 
				
			||||||
    app.use(compression()); // HTTP compression
 | 
					    app.use(compression()); // HTTP compression
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -77,7 +77,7 @@ await import("./services/scheduler.js");
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
startScheduledCleanup();
 | 
					startScheduledCleanup();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if (utils.isElectron()) {
 | 
					if (utils.isElectron) {
 | 
				
			||||||
    (await import("@electron/remote/main/index.js")).initialize();
 | 
					    (await import("@electron/remote/main/index.js")).initialize();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -195,7 +195,7 @@ function processContent(images: Image[], note: BNote, content: string) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function openNote(req: Request) {
 | 
					function openNote(req: Request) {
 | 
				
			||||||
    if (utils.isElectron()) {
 | 
					    if (utils.isElectron) {
 | 
				
			||||||
        ws.sendMessageToAllClients({
 | 
					        ws.sendMessageToAllClients({
 | 
				
			||||||
            type: "openNote",
 | 
					            type: "openNote",
 | 
				
			||||||
            noteId: req.params.noteId
 | 
					            noteId: req.params.noteId
 | 
				
			||||||
 | 
				
			|||||||
@ -181,7 +181,7 @@ function saveToTmpDir(fileName: string, content: string | Buffer, entityType: st
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    log.info(`Saved temporary file ${tmpObj.name}`);
 | 
					    log.info(`Saved temporary file ${tmpObj.name}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (utils.isElectron()) {
 | 
					    if (utils.isElectron) {
 | 
				
			||||||
        chokidar.watch(tmpObj.name).on("change", (path, stats) => {
 | 
					        chokidar.watch(tmpObj.name).on("change", (path, stats) => {
 | 
				
			||||||
            ws.sendMessageToAllClients({
 | 
					            ws.sendMessageToAllClients({
 | 
				
			||||||
                type: "openedFileUpdated",
 | 
					                type: "openedFileUpdated",
 | 
				
			||||||
 | 
				
			|||||||
@ -2,11 +2,11 @@ import assetPath from "../services/asset_path.js";
 | 
				
			|||||||
import path from "path";
 | 
					import path from "path";
 | 
				
			||||||
import { fileURLToPath } from "url";
 | 
					import { fileURLToPath } from "url";
 | 
				
			||||||
import express from "express";
 | 
					import express from "express";
 | 
				
			||||||
import env from "../services/env.js";
 | 
					import { isDev } from "../services/utils.js";
 | 
				
			||||||
import type serveStatic from "serve-static";
 | 
					import type serveStatic from "serve-static";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const persistentCacheStatic = (root: string, options?: serveStatic.ServeStaticOptions<express.Response<any, Record<string, any>>>) => {
 | 
					const persistentCacheStatic = (root: string, options?: serveStatic.ServeStaticOptions<express.Response<any, Record<string, any>>>) => {
 | 
				
			||||||
    if (!env.isDev()) {
 | 
					    if (!isDev) {
 | 
				
			||||||
        options = {
 | 
					        options = {
 | 
				
			||||||
            maxAge: "1y",
 | 
					            maxAge: "1y",
 | 
				
			||||||
            ...options
 | 
					            ...options
 | 
				
			||||||
@ -17,7 +17,7 @@ const persistentCacheStatic = (root: string, options?: serveStatic.ServeStaticOp
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
async function register(app: express.Application) {
 | 
					async function register(app: express.Application) {
 | 
				
			||||||
    const srcRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
 | 
					    const srcRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
 | 
				
			||||||
    if (env.isDev()) {
 | 
					    if (isDev) {
 | 
				
			||||||
        const webpack = (await import("webpack")).default;
 | 
					        const webpack = (await import("webpack")).default;
 | 
				
			||||||
        const webpackMiddleware = (await import("webpack-dev-middleware")).default;
 | 
					        const webpackMiddleware = (await import("webpack-dev-middleware")).default;
 | 
				
			||||||
        const productionConfig = (await import("../../webpack.config.js")).default;
 | 
					        const productionConfig = (await import("../../webpack.config.js")).default;
 | 
				
			||||||
 | 
				
			|||||||
@ -8,7 +8,7 @@ const doubleCsrfUtilities = doubleCsrf({
 | 
				
			|||||||
        path: "", // empty, so cookie is valid only for the current path
 | 
					        path: "", // empty, so cookie is valid only for the current path
 | 
				
			||||||
        secure: false,
 | 
					        secure: false,
 | 
				
			||||||
        sameSite: "strict",
 | 
					        sameSite: "strict",
 | 
				
			||||||
        httpOnly: !isElectron() // set to false for Electron, see https://github.com/TriliumNext/Notes/pull/966
 | 
					        httpOnly: !isElectron // set to false for Electron, see https://github.com/TriliumNext/Notes/pull/966
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    cookieName: "_csrf"
 | 
					    cookieName: "_csrf"
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
@ -5,8 +5,7 @@ import attributeService from "../services/attributes.js";
 | 
				
			|||||||
import config from "../services/config.js";
 | 
					import config from "../services/config.js";
 | 
				
			||||||
import optionService from "../services/options.js";
 | 
					import optionService from "../services/options.js";
 | 
				
			||||||
import log from "../services/log.js";
 | 
					import log from "../services/log.js";
 | 
				
			||||||
import env from "../services/env.js";
 | 
					import { isDev, isElectron } from "../services/utils.js";
 | 
				
			||||||
import utils from "../services/utils.js";
 | 
					 | 
				
			||||||
import protectedSessionService from "../services/protected_session.js";
 | 
					import protectedSessionService from "../services/protected_session.js";
 | 
				
			||||||
import packageJson from "../../package.json" with { type: "json" };
 | 
					import packageJson from "../../package.json" with { type: "json" };
 | 
				
			||||||
import assetPath from "../services/asset_path.js";
 | 
					import assetPath from "../services/asset_path.js";
 | 
				
			||||||
@ -19,7 +18,7 @@ import type BNote from "../becca/entities/bnote.js";
 | 
				
			|||||||
function index(req: Request, res: Response) {
 | 
					function index(req: Request, res: Response) {
 | 
				
			||||||
    const options = optionService.getOptionMap();
 | 
					    const options = optionService.getOptionMap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const view = !utils.isElectron() && req.cookies["trilium-device"] === "mobile" ? "mobile" : "desktop";
 | 
					    const view = !isElectron && req.cookies["trilium-device"] === "mobile" ? "mobile" : "desktop";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //'overwrite' set to false (default) => the existing token will be re-used and validated
 | 
					    //'overwrite' set to false (default) => the existing token will be re-used and validated
 | 
				
			||||||
    //'validateOnReuse' set to false => if validation fails, generate a new token instead of throwing an error
 | 
					    //'validateOnReuse' set to false => if validation fails, generate a new token instead of throwing an error
 | 
				
			||||||
@ -34,7 +33,6 @@ function index(req: Request, res: Response) {
 | 
				
			|||||||
    const theme = options.theme;
 | 
					    const theme = options.theme;
 | 
				
			||||||
    const themeNote = attributeService.getNoteWithLabel("appTheme", theme);
 | 
					    const themeNote = attributeService.getNoteWithLabel("appTheme", theme);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const isElectron = utils.isElectron();
 | 
					 | 
				
			||||||
    res.render(view, {
 | 
					    res.render(view, {
 | 
				
			||||||
        device: view,
 | 
					        device: view,
 | 
				
			||||||
        csrfToken: csrfToken,
 | 
					        csrfToken: csrfToken,
 | 
				
			||||||
@ -53,7 +51,7 @@ function index(req: Request, res: Response) {
 | 
				
			|||||||
        maxEntityChangeSyncIdAtLoad: sql.getValue("SELECT COALESCE(MAX(id), 0) FROM entity_changes WHERE isSynced = 1"),
 | 
					        maxEntityChangeSyncIdAtLoad: sql.getValue("SELECT COALESCE(MAX(id), 0) FROM entity_changes WHERE isSynced = 1"),
 | 
				
			||||||
        instanceName: config.General ? config.General.instanceName : null,
 | 
					        instanceName: config.General ? config.General.instanceName : null,
 | 
				
			||||||
        appCssNoteIds: getAppCssNoteIds(),
 | 
					        appCssNoteIds: getAppCssNoteIds(),
 | 
				
			||||||
        isDev: env.isDev(),
 | 
					        isDev,
 | 
				
			||||||
        isMainWindow: view === "mobile" ? true : !req.query.extraWindow,
 | 
					        isMainWindow: view === "mobile" ? true : !req.query.extraWindow,
 | 
				
			||||||
        isProtectedSessionAvailable: protectedSessionService.isProtectedSessionAvailable(),
 | 
					        isProtectedSessionAvailable: protectedSessionService.isProtectedSessionAvailable(),
 | 
				
			||||||
        maxContentWidth: Math.max(640, parseInt(options.maxContentWidth)),
 | 
					        maxContentWidth: Math.max(640, parseInt(options.maxContentWidth)),
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,6 @@
 | 
				
			|||||||
"use strict";
 | 
					"use strict";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import utils from "../services/utils.js";
 | 
					import { isElectron } from "../services/utils.js";
 | 
				
			||||||
import multer from "multer";
 | 
					import multer from "multer";
 | 
				
			||||||
import log from "../services/log.js";
 | 
					import log from "../services/log.js";
 | 
				
			||||||
import express from "express";
 | 
					import express from "express";
 | 
				
			||||||
@ -280,7 +280,7 @@ function register(app: express.Application) {
 | 
				
			|||||||
    apiRoute(DEL, "/api/etapi-tokens/:etapiTokenId", etapiTokensApiRoutes.deleteToken);
 | 
					    apiRoute(DEL, "/api/etapi-tokens/:etapiTokenId", etapiTokensApiRoutes.deleteToken);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // in case of local electron, local calls are allowed unauthenticated, for server they need auth
 | 
					    // in case of local electron, local calls are allowed unauthenticated, for server they need auth
 | 
				
			||||||
    const clipperMiddleware = utils.isElectron() ? [] : [auth.checkEtapiToken];
 | 
					    const clipperMiddleware = isElectron ? [] : [auth.checkEtapiToken];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    route(GET, "/api/clipper/handshake", clipperMiddleware, clipperRoute.handshake, apiResultHandler);
 | 
					    route(GET, "/api/clipper/handshake", clipperMiddleware, clipperRoute.handshake, apiResultHandler);
 | 
				
			||||||
    route(PST, "/api/clipper/clippings", clipperMiddleware, clipperRoute.addClipping, apiResultHandler);
 | 
					    route(PST, "/api/clipper/clippings", clipperMiddleware, clipperRoute.addClipping, apiResultHandler);
 | 
				
			||||||
 | 
				
			|||||||
@ -2,14 +2,14 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import sqlInit from "../services/sql_init.js";
 | 
					import sqlInit from "../services/sql_init.js";
 | 
				
			||||||
import setupService from "../services/setup.js";
 | 
					import setupService from "../services/setup.js";
 | 
				
			||||||
import utils from "../services/utils.js";
 | 
					import { isElectron } from "../services/utils.js";
 | 
				
			||||||
import assetPath from "../services/asset_path.js";
 | 
					import assetPath from "../services/asset_path.js";
 | 
				
			||||||
import appPath from "../services/app_path.js";
 | 
					import appPath from "../services/app_path.js";
 | 
				
			||||||
import type { Request, Response } from "express";
 | 
					import type { Request, Response } from "express";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function setupPage(req: Request, res: Response) {
 | 
					function setupPage(req: Request, res: Response) {
 | 
				
			||||||
    if (sqlInit.isDbInitialized()) {
 | 
					    if (sqlInit.isDbInitialized()) {
 | 
				
			||||||
        if (utils.isElectron()) {
 | 
					        if (isElectron) {
 | 
				
			||||||
            handleElectronRedirect();
 | 
					            handleElectronRedirect();
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            res.redirect(".");
 | 
					            res.redirect(".");
 | 
				
			||||||
 | 
				
			|||||||
@ -23,7 +23,7 @@ Terminal=false
 | 
				
			|||||||
 * We overwrite this file during every run as it might have been updated.
 | 
					 * We overwrite this file during every run as it might have been updated.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function installLocalAppIcon() {
 | 
					function installLocalAppIcon() {
 | 
				
			||||||
    if (!isElectron() || ["win32", "darwin"].includes(os.platform()) || (config.General && config.General.noDesktopIcon)) {
 | 
					    if (!isElectron || ["win32", "darwin"].includes(os.platform()) || (config.General && config.General.noDesktopIcon)) {
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,4 @@
 | 
				
			|||||||
import assetPath from "./asset_path.js";
 | 
					import assetPath from "./asset_path.js";
 | 
				
			||||||
import env from "./env.js";
 | 
					import { isDev } from "./utils.js";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default env.isDev() ? assetPath + "/app" : assetPath + "/app-dist";
 | 
					export default isDev ? assetPath + "/app" : assetPath + "/app-dist";
 | 
				
			||||||
@ -14,7 +14,7 @@ const noAuthentication = config.General && config.General.noAuthentication === t
 | 
				
			|||||||
function checkAuth(req: Request, res: Response, next: NextFunction) {
 | 
					function checkAuth(req: Request, res: Response, next: NextFunction) {
 | 
				
			||||||
    if (!sqlInit.isDbInitialized()) {
 | 
					    if (!sqlInit.isDbInitialized()) {
 | 
				
			||||||
        res.redirect("setup");
 | 
					        res.redirect("setup");
 | 
				
			||||||
    } else if (!req.session.loggedIn && !isElectron() && !noAuthentication) {
 | 
					    } else if (!req.session.loggedIn && !isElectron && !noAuthentication) {
 | 
				
			||||||
        res.redirect("login");
 | 
					        res.redirect("login");
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        next();
 | 
					        next();
 | 
				
			||||||
@ -24,7 +24,7 @@ function checkAuth(req: Request, res: Response, next: NextFunction) {
 | 
				
			|||||||
// for electron things which need network stuff
 | 
					// for electron things which need network stuff
 | 
				
			||||||
//  currently, we're doing that for file upload because handling form data seems to be difficult
 | 
					//  currently, we're doing that for file upload because handling form data seems to be difficult
 | 
				
			||||||
function checkApiAuthOrElectron(req: Request, res: Response, next: NextFunction) {
 | 
					function checkApiAuthOrElectron(req: Request, res: Response, next: NextFunction) {
 | 
				
			||||||
    if (!req.session.loggedIn && !isElectron() && !noAuthentication) {
 | 
					    if (!req.session.loggedIn && !isElectron && !noAuthentication) {
 | 
				
			||||||
        reject(req, res, "Logged in session not found");
 | 
					        reject(req, res, "Logged in session not found");
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        next();
 | 
					        next();
 | 
				
			||||||
@ -48,7 +48,7 @@ function checkAppInitialized(req: Request, res: Response, next: NextFunction) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function checkPasswordSet(req: Request, res: Response, next: NextFunction) {
 | 
					function checkPasswordSet(req: Request, res: Response, next: NextFunction) {
 | 
				
			||||||
    if (!isElectron() && !passwordService.isPasswordSet()) {
 | 
					    if (!isElectron && !passwordService.isPasswordSet()) {
 | 
				
			||||||
        res.redirect("set-password");
 | 
					        res.redirect("set-password");
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        next();
 | 
					        next();
 | 
				
			||||||
@ -56,7 +56,7 @@ function checkPasswordSet(req: Request, res: Response, next: NextFunction) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function checkPasswordNotSet(req: Request, res: Response, next: NextFunction) {
 | 
					function checkPasswordNotSet(req: Request, res: Response, next: NextFunction) {
 | 
				
			||||||
    if (!isElectron() && passwordService.isPasswordSet()) {
 | 
					    if (!isElectron && passwordService.isPasswordSet()) {
 | 
				
			||||||
        res.redirect("login");
 | 
					        res.redirect("login");
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        next();
 | 
					        next();
 | 
				
			||||||
 | 
				
			|||||||
@ -8,8 +8,7 @@ import fs from "fs";
 | 
				
			|||||||
import themeNames from "./code_block_theme_names.json" with { type: "json" };
 | 
					import themeNames from "./code_block_theme_names.json" with { type: "json" };
 | 
				
			||||||
import { t } from "i18next";
 | 
					import { t } from "i18next";
 | 
				
			||||||
import { join } from "path";
 | 
					import { join } from "path";
 | 
				
			||||||
import { isElectron, getResourceDir } from "./utils.js";
 | 
					import { isDev, isElectron, getResourceDir } from "./utils.js";
 | 
				
			||||||
import env from "./env.js";
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Represents a color scheme for the code block syntax highlight.
 | 
					 * Represents a color scheme for the code block syntax highlight.
 | 
				
			||||||
@ -46,7 +45,7 @@ export function listSyntaxHighlightingThemes() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getStylesDirectory() {
 | 
					function getStylesDirectory() {
 | 
				
			||||||
    if (isElectron() && !env.isDev()) {
 | 
					    if (isElectron && !isDev) {
 | 
				
			||||||
        return "styles";
 | 
					        return "styles";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,7 +0,0 @@
 | 
				
			|||||||
function isDev() {
 | 
					 | 
				
			||||||
    return !!(process.env.TRILIUM_ENV && process.env.TRILIUM_ENV === "dev");
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export default {
 | 
					 | 
				
			||||||
    isDev
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
@ -2,13 +2,10 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import optionService from "./options.js";
 | 
					import optionService from "./options.js";
 | 
				
			||||||
import log from "./log.js";
 | 
					import log from "./log.js";
 | 
				
			||||||
import { isElectron as getIsElectron, isMac as getIsMac } from "./utils.js";
 | 
					import { isElectron, isMac } from "./utils.js";
 | 
				
			||||||
import type { KeyboardShortcut } from "./keyboard_actions_interface.js";
 | 
					import type { KeyboardShortcut } from "./keyboard_actions_interface.js";
 | 
				
			||||||
import { t } from "i18next";
 | 
					import { t } from "i18next";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const isMac = getIsMac();
 | 
					 | 
				
			||||||
const isElectron = getIsElectron();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function getDefaultKeyboardActions() {
 | 
					function getDefaultKeyboardActions() {
 | 
				
			||||||
    if (!t("keyboard_actions.note-navigation")) {
 | 
					    if (!t("keyboard_actions.note-navigation")) {
 | 
				
			||||||
        throw new Error("Keyboard actions loaded before translations.");
 | 
					        throw new Error("Keyboard actions loaded before translations.");
 | 
				
			||||||
 | 
				
			|||||||
@ -17,7 +17,7 @@ const MINUTE = 60 * SECOND;
 | 
				
			|||||||
const HOUR = 60 * MINUTE;
 | 
					const HOUR = 60 * MINUTE;
 | 
				
			||||||
const DAY = 24 * HOUR;
 | 
					const DAY = 24 * HOUR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const NEW_LINE = isWindows() ? "\r\n" : "\n";
 | 
					const NEW_LINE = isWindows ? "\r\n" : "\n";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let todaysMidnight!: Date;
 | 
					let todaysMidnight!: Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -77,7 +77,7 @@ const defaultOptions: DefaultOption[] = [
 | 
				
			|||||||
    { name: "revisionSnapshotTimeInterval", value: "600", isSynced: true },
 | 
					    { name: "revisionSnapshotTimeInterval", value: "600", isSynced: true },
 | 
				
			||||||
    { name: "revisionSnapshotNumberLimit", value: "-1", isSynced: true },
 | 
					    { name: "revisionSnapshotNumberLimit", value: "-1", isSynced: true },
 | 
				
			||||||
    { name: "protectedSessionTimeout", value: "600", isSynced: true },
 | 
					    { name: "protectedSessionTimeout", value: "600", isSynced: true },
 | 
				
			||||||
    { name: "zoomFactor", value: isWindows() ? "0.9" : "1.0", isSynced: false },
 | 
					    { name: "zoomFactor", value: isWindows ? "0.9" : "1.0", isSynced: false },
 | 
				
			||||||
    { name: "overrideThemeFonts", value: "false", isSynced: false },
 | 
					    { name: "overrideThemeFonts", value: "false", isSynced: false },
 | 
				
			||||||
    { name: "mainFontFamily", value: "theme", isSynced: false },
 | 
					    { name: "mainFontFamily", value: "theme", isSynced: false },
 | 
				
			||||||
    { name: "mainFontSize", value: "100", isSynced: false },
 | 
					    { name: "mainFontSize", value: "100", isSynced: false },
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,5 @@
 | 
				
			|||||||
import config from "./config.js";
 | 
					import config from "./config.js";
 | 
				
			||||||
import { isElectron } from "./utils.js";
 | 
					import { isDev, isElectron } from "./utils.js";
 | 
				
			||||||
import env from "./env.js";
 | 
					 | 
				
			||||||
import dataDir from "./data_dir.js";
 | 
					import dataDir from "./data_dir.js";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function parseAndValidate(portStr: string, source: string) {
 | 
					function parseAndValidate(portStr: string, source: string) {
 | 
				
			||||||
@ -18,8 +17,8 @@ let port: number;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
if (process.env.TRILIUM_PORT) {
 | 
					if (process.env.TRILIUM_PORT) {
 | 
				
			||||||
    port = parseAndValidate(process.env.TRILIUM_PORT, "environment variable TRILIUM_PORT");
 | 
					    port = parseAndValidate(process.env.TRILIUM_PORT, "environment variable TRILIUM_PORT");
 | 
				
			||||||
} else if (isElectron()) {
 | 
					} else if (isElectron) {
 | 
				
			||||||
    port = env.isDev() ? 37740 : 37840;
 | 
					    port = isDev ? 37740 : 37840;
 | 
				
			||||||
} else {
 | 
					} else {
 | 
				
			||||||
    port = parseAndValidate(config["Network"]["port"] || "3000", `Network.port in ${dataDir.CONFIG_INI_PATH}`);
 | 
					    port = parseAndValidate(config["Network"]["port"] || "3000", `Network.port in ${dataDir.CONFIG_INI_PATH}`);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -206,7 +206,7 @@ async function getProxyAgent(opts: ClientOpts) {
 | 
				
			|||||||
async function getClient(opts: ClientOpts): Promise<Client> {
 | 
					async function getClient(opts: ClientOpts): Promise<Client> {
 | 
				
			||||||
    // it's not clear how to explicitly configure proxy (as opposed to system proxy),
 | 
					    // it's not clear how to explicitly configure proxy (as opposed to system proxy),
 | 
				
			||||||
    // so in that case, we always use node's modules
 | 
					    // so in that case, we always use node's modules
 | 
				
			||||||
    if (isElectron() && !opts.proxy) {
 | 
					    if (isElectron && !opts.proxy) {
 | 
				
			||||||
        return (await import("electron")).net as Client;
 | 
					        return (await import("electron")).net as Client;
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        const { protocol } = url.parse(opts.url);
 | 
					        const { protocol } = url.parse(opts.url);
 | 
				
			||||||
 | 
				
			|||||||
@ -37,7 +37,7 @@ function isDbInitialized() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
async function initDbConnection() {
 | 
					async function initDbConnection() {
 | 
				
			||||||
    if (!isDbInitialized()) {
 | 
					    if (!isDbInitialized()) {
 | 
				
			||||||
        log.info(`DB not initialized, please visit setup page` + (isElectron() ? "" : ` - http://[your-server-host]:${port} to see instructions on how to initialize Trilium.`));
 | 
					        log.info(`DB not initialized, please visit setup page` + (isElectron ? "" : ` - http://[your-server-host]:${port} to see instructions on how to initialize Trilium.`));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -8,11 +8,18 @@ import sanitize from "sanitize-filename";
 | 
				
			|||||||
import mimeTypes from "mime-types";
 | 
					import mimeTypes from "mime-types";
 | 
				
			||||||
import path from "path";
 | 
					import path from "path";
 | 
				
			||||||
import { fileURLToPath } from "url";
 | 
					import { fileURLToPath } from "url";
 | 
				
			||||||
import env from "./env.js";
 | 
					 | 
				
			||||||
import { dirname, join } from "path";
 | 
					import { dirname, join } from "path";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const randtoken = generator({ source: "crypto" });
 | 
					const randtoken = generator({ source: "crypto" });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const isMac = process.platform === "darwin";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const isWindows = process.platform === "win32";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const isElectron = !!process.versions["electron"];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const isDev = !!(process.env.TRILIUM_ENV && process.env.TRILIUM_ENV === "dev");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function newEntityId() {
 | 
					export function newEntityId() {
 | 
				
			||||||
    return randomString(12);
 | 
					    return randomString(12);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -58,10 +65,6 @@ export function hmac(secret: any, value: any) {
 | 
				
			|||||||
    return hmac.digest("base64");
 | 
					    return hmac.digest("base64");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function isElectron() {
 | 
					 | 
				
			||||||
    return !!process.versions["electron"];
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export function hash(text: string) {
 | 
					export function hash(text: string) {
 | 
				
			||||||
    text = text.normalize();
 | 
					    text = text.normalize();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -128,7 +131,7 @@ export function escapeRegExp(str: string) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function crash() {
 | 
					export async function crash() {
 | 
				
			||||||
    if (isElectron()) {
 | 
					    if (isElectron) {
 | 
				
			||||||
        (await import("electron")).app.exit(1);
 | 
					        (await import("electron")).app.exit(1);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        process.exit(1);
 | 
					        process.exit(1);
 | 
				
			||||||
@ -314,21 +317,13 @@ export function envToBoolean(val: string | undefined) {
 | 
				
			|||||||
 * @returns the resource dir.
 | 
					 * @returns the resource dir.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export function getResourceDir() {
 | 
					export function getResourceDir() {
 | 
				
			||||||
    if (isElectron() && !env.isDev()) {
 | 
					    if (isElectron && !isDev) {
 | 
				
			||||||
        return process.resourcesPath;
 | 
					        return process.resourcesPath;
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        return join(dirname(fileURLToPath(import.meta.url)), "..", "..");
 | 
					        return join(dirname(fileURLToPath(import.meta.url)), "..", "..");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function isMac() {
 | 
					 | 
				
			||||||
    return process.platform === "darwin";
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export function isWindows() {
 | 
					 | 
				
			||||||
    return process.platform === "win32";
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export default {
 | 
					export default {
 | 
				
			||||||
    randomSecureToken,
 | 
					    randomSecureToken,
 | 
				
			||||||
    randomString,
 | 
					    randomString,
 | 
				
			||||||
 | 
				
			|||||||
@ -2,7 +2,6 @@ import path from "path";
 | 
				
			|||||||
import url from "url";
 | 
					import url from "url";
 | 
				
			||||||
import port from "./port.js";
 | 
					import port from "./port.js";
 | 
				
			||||||
import optionService from "./options.js";
 | 
					import optionService from "./options.js";
 | 
				
			||||||
import env from "./env.js";
 | 
					 | 
				
			||||||
import log from "./log.js";
 | 
					import log from "./log.js";
 | 
				
			||||||
import sqlInit from "./sql_init.js";
 | 
					import sqlInit from "./sql_init.js";
 | 
				
			||||||
import cls from "./cls.js";
 | 
					import cls from "./cls.js";
 | 
				
			||||||
@ -10,7 +9,7 @@ import keyboardActionsService from "./keyboard_actions.js";
 | 
				
			|||||||
import remoteMain from "@electron/remote/main/index.js";
 | 
					import remoteMain from "@electron/remote/main/index.js";
 | 
				
			||||||
import type { App, BrowserWindow, BrowserWindowConstructorOptions, WebContents } from "electron";
 | 
					import type { App, BrowserWindow, BrowserWindowConstructorOptions, WebContents } from "electron";
 | 
				
			||||||
import { ipcMain } from "electron";
 | 
					import { ipcMain } from "electron";
 | 
				
			||||||
import { isMac, isWindows } from "./utils.js";
 | 
					import { isDev, isMac, isWindows } from "./utils.js";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { fileURLToPath } from "url";
 | 
					import { fileURLToPath } from "url";
 | 
				
			||||||
import { dirname } from "path";
 | 
					import { dirname } from "path";
 | 
				
			||||||
@ -116,10 +115,10 @@ function getWindowExtraOpts() {
 | 
				
			|||||||
    const extraOpts: Partial<BrowserWindowConstructorOptions> = {};
 | 
					    const extraOpts: Partial<BrowserWindowConstructorOptions> = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!optionService.getOptionBool("nativeTitleBarVisible")) {
 | 
					    if (!optionService.getOptionBool("nativeTitleBarVisible")) {
 | 
				
			||||||
        if (isMac()) {
 | 
					        if (isMac) {
 | 
				
			||||||
            extraOpts.titleBarStyle = "hiddenInset";
 | 
					            extraOpts.titleBarStyle = "hiddenInset";
 | 
				
			||||||
            extraOpts.titleBarOverlay = true;
 | 
					            extraOpts.titleBarOverlay = true;
 | 
				
			||||||
        } else if (isWindows()) {
 | 
					        } else if (isWindows) {
 | 
				
			||||||
            extraOpts.titleBarStyle = "hidden";
 | 
					            extraOpts.titleBarStyle = "hidden";
 | 
				
			||||||
            extraOpts.titleBarOverlay = true;
 | 
					            extraOpts.titleBarOverlay = true;
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
@ -129,7 +128,7 @@ function getWindowExtraOpts() {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Window effects (Mica)
 | 
					    // Window effects (Mica)
 | 
				
			||||||
    if (optionService.getOptionBool("backgroundEffects") && isWindows()) {
 | 
					    if (optionService.getOptionBool("backgroundEffects") && isWindows) {
 | 
				
			||||||
        extraOpts.backgroundMaterial = "auto";
 | 
					        extraOpts.backgroundMaterial = "auto";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -169,7 +168,7 @@ function configureWebContents(webContents: WebContents, spellcheckEnabled: boole
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getIcon() {
 | 
					function getIcon() {
 | 
				
			||||||
    return path.join(dirname(fileURLToPath(import.meta.url)), "../../images/app-icons/png/256x256" + (env.isDev() ? "-dev" : "") + ".png");
 | 
					    return path.join(dirname(fileURLToPath(import.meta.url)), "../../images/app-icons/png/256x256" + (isDev ? "-dev" : "") + ".png");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function createSetupWindow() {
 | 
					async function createSetupWindow() {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,5 @@
 | 
				
			|||||||
import { WebSocketServer as WebSocketServer, WebSocket } from "ws";
 | 
					import { WebSocketServer as WebSocketServer, WebSocket } from "ws";
 | 
				
			||||||
import { isElectron, randomString } from "./utils.js";
 | 
					import { isDev, isElectron, randomString } from "./utils.js";
 | 
				
			||||||
import log from "./log.js";
 | 
					import log from "./log.js";
 | 
				
			||||||
import sql from "./sql.js";
 | 
					import sql from "./sql.js";
 | 
				
			||||||
import cls from "./cls.js";
 | 
					import cls from "./cls.js";
 | 
				
			||||||
@ -9,16 +9,15 @@ import protectedSessionService from "./protected_session.js";
 | 
				
			|||||||
import becca from "../becca/becca.js";
 | 
					import becca from "../becca/becca.js";
 | 
				
			||||||
import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.js";
 | 
					import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.js";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import env from "./env.js";
 | 
					 | 
				
			||||||
import type { IncomingMessage, Server as HttpServer } from "http";
 | 
					import type { IncomingMessage, Server as HttpServer } from "http";
 | 
				
			||||||
import type { EntityChange } from "./entity_changes_interface.js";
 | 
					import type { EntityChange } from "./entity_changes_interface.js";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if (env.isDev()) {
 | 
					if (isDev) {
 | 
				
			||||||
    const chokidar = (await import("chokidar")).default;
 | 
					    const chokidar = (await import("chokidar")).default;
 | 
				
			||||||
    const debounce = (await import("debounce")).default;
 | 
					    const debounce = (await import("debounce")).default;
 | 
				
			||||||
    const debouncedReloadFrontend = debounce(() => reloadFrontend("source code change"), 200);
 | 
					    const debouncedReloadFrontend = debounce(() => reloadFrontend("source code change"), 200);
 | 
				
			||||||
    chokidar
 | 
					    chokidar
 | 
				
			||||||
        .watch(isElectron() ? "dist/src/public" : "src/public")
 | 
					        .watch(isElectron ? "dist/src/public" : "src/public")
 | 
				
			||||||
        .on("add", debouncedReloadFrontend)
 | 
					        .on("add", debouncedReloadFrontend)
 | 
				
			||||||
        .on("change", debouncedReloadFrontend)
 | 
					        .on("change", debouncedReloadFrontend)
 | 
				
			||||||
        .on("unlink", debouncedReloadFrontend);
 | 
					        .on("unlink", debouncedReloadFrontend);
 | 
				
			||||||
@ -62,7 +61,7 @@ function init(httpServer: HttpServer, sessionParser: SessionParser) {
 | 
				
			|||||||
    webSocketServer = new WebSocketServer({
 | 
					    webSocketServer = new WebSocketServer({
 | 
				
			||||||
        verifyClient: (info, done) => {
 | 
					        verifyClient: (info, done) => {
 | 
				
			||||||
            sessionParser(info.req, {}, () => {
 | 
					            sessionParser(info.req, {}, () => {
 | 
				
			||||||
                const allowed = isElectron() || (info.req as any).session.loggedIn || (config.General && config.General.noAuthentication);
 | 
					                const allowed = isElectron || (info.req as any).session.loggedIn || (config.General && config.General.noAuthentication);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (!allowed) {
 | 
					                if (!allowed) {
 | 
				
			||||||
                    log.error("WebSocket connection not allowed because session is neither electron nor logged in.");
 | 
					                    log.error("WebSocket connection not allowed because session is neither electron nor logged in.");
 | 
				
			||||||
 | 
				
			|||||||
@ -53,7 +53,7 @@ async function startTrilium() {
 | 
				
			|||||||
     * its startup is slower than focusing the existing process/window. So in the end, it works out without having
 | 
					     * its startup is slower than focusing the existing process/window. So in the end, it works out without having
 | 
				
			||||||
     * to do a complex evaluation.
 | 
					     * to do a complex evaluation.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    if (utils.isElectron()) {
 | 
					    if (utils.isElectron) {
 | 
				
			||||||
        (await import("electron")).app.requestSingleInstanceLock();
 | 
					        (await import("electron")).app.requestSingleInstanceLock();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -71,7 +71,7 @@ async function startTrilium() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    ws.init(httpServer, sessionParser as any); // TODO: Not sure why session parser is incompatible.
 | 
					    ws.init(httpServer, sessionParser as any); // TODO: Not sure why session parser is incompatible.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (utils.isElectron()) {
 | 
					    if (utils.isElectron) {
 | 
				
			||||||
        const electronRouting = await import("./routes/electron.js");
 | 
					        const electronRouting = await import("./routes/electron.js");
 | 
				
			||||||
        electronRouting.default(app);
 | 
					        electronRouting.default(app);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -146,7 +146,7 @@ function startHttpServer() {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (utils.isElectron()) {
 | 
					        if (utils.isElectron) {
 | 
				
			||||||
            import("electron").then(({ app, dialog }) => {
 | 
					            import("electron").then(({ app, dialog }) => {
 | 
				
			||||||
                // Not all situations require showing an error dialog. When Trilium is already open,
 | 
					                // Not all situations require showing an error dialog. When Trilium is already open,
 | 
				
			||||||
                // clicking the shortcut, the software icon, or the taskbar icon, or when creating a new window,
 | 
					                // clicking the shortcut, the software icon, or the taskbar icon, or when creating a new window,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user