mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-29 19:12:27 +08:00
refactor(server/utils): isElectron - replace fn with boolean
this values cannot change during runtime, => there is no need to have these checks as dynamic function, instead just export the boolean value directly
This commit is contained in:
parent
94411cf418
commit
ca2bb94200
@ -35,7 +35,7 @@ app.use((req, res, next) => {
|
||||
return next();
|
||||
});
|
||||
|
||||
if (!utils.isElectron()) {
|
||||
if (!utils.isElectron) {
|
||||
app.use(compression()); // HTTP compression
|
||||
}
|
||||
|
||||
@ -77,7 +77,7 @@ await import("./services/scheduler.js");
|
||||
|
||||
startScheduledCleanup();
|
||||
|
||||
if (utils.isElectron()) {
|
||||
if (utils.isElectron) {
|
||||
(await import("@electron/remote/main/index.js")).initialize();
|
||||
}
|
||||
|
||||
|
@ -195,7 +195,7 @@ function processContent(images: Image[], note: BNote, content: string) {
|
||||
}
|
||||
|
||||
function openNote(req: Request) {
|
||||
if (utils.isElectron()) {
|
||||
if (utils.isElectron) {
|
||||
ws.sendMessageToAllClients({
|
||||
type: "openNote",
|
||||
noteId: req.params.noteId
|
||||
|
@ -181,7 +181,7 @@ function saveToTmpDir(fileName: string, content: string | Buffer, entityType: st
|
||||
|
||||
log.info(`Saved temporary file ${tmpObj.name}`);
|
||||
|
||||
if (utils.isElectron()) {
|
||||
if (utils.isElectron) {
|
||||
chokidar.watch(tmpObj.name).on("change", (path, stats) => {
|
||||
ws.sendMessageToAllClients({
|
||||
type: "openedFileUpdated",
|
||||
|
@ -8,7 +8,7 @@ const doubleCsrfUtilities = doubleCsrf({
|
||||
path: "", // empty, so cookie is valid only for the current path
|
||||
secure: false,
|
||||
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"
|
||||
});
|
||||
|
@ -6,7 +6,7 @@ import config from "../services/config.js";
|
||||
import optionService from "../services/options.js";
|
||||
import log from "../services/log.js";
|
||||
import env from "../services/env.js";
|
||||
import utils from "../services/utils.js";
|
||||
import { isElectron } from "../services/utils.js";
|
||||
import protectedSessionService from "../services/protected_session.js";
|
||||
import packageJson from "../../package.json" with { type: "json" };
|
||||
import assetPath from "../services/asset_path.js";
|
||||
@ -19,7 +19,7 @@ import type BNote from "../becca/entities/bnote.js";
|
||||
function index(req: Request, res: Response) {
|
||||
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
|
||||
//'validateOnReuse' set to false => if validation fails, generate a new token instead of throwing an error
|
||||
@ -34,7 +34,6 @@ function index(req: Request, res: Response) {
|
||||
const theme = options.theme;
|
||||
const themeNote = attributeService.getNoteWithLabel("appTheme", theme);
|
||||
|
||||
const isElectron = utils.isElectron();
|
||||
res.render(view, {
|
||||
device: view,
|
||||
csrfToken: csrfToken,
|
||||
|
@ -1,6 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
import utils from "../services/utils.js";
|
||||
import { isElectron } from "../services/utils.js";
|
||||
import multer from "multer";
|
||||
import log from "../services/log.js";
|
||||
import express from "express";
|
||||
@ -280,7 +280,7 @@ function register(app: express.Application) {
|
||||
apiRoute(DEL, "/api/etapi-tokens/:etapiTokenId", etapiTokensApiRoutes.deleteToken);
|
||||
|
||||
// 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(PST, "/api/clipper/clippings", clipperMiddleware, clipperRoute.addClipping, apiResultHandler);
|
||||
|
@ -2,14 +2,14 @@
|
||||
|
||||
import sqlInit from "../services/sql_init.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 appPath from "../services/app_path.js";
|
||||
import type { Request, Response } from "express";
|
||||
|
||||
function setupPage(req: Request, res: Response) {
|
||||
if (sqlInit.isDbInitialized()) {
|
||||
if (utils.isElectron()) {
|
||||
if (isElectron) {
|
||||
handleElectronRedirect();
|
||||
} else {
|
||||
res.redirect(".");
|
||||
|
@ -23,7 +23,7 @@ Terminal=false
|
||||
* We overwrite this file during every run as it might have been updated.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ const noAuthentication = config.General && config.General.noAuthentication === t
|
||||
function checkAuth(req: Request, res: Response, next: NextFunction) {
|
||||
if (!sqlInit.isDbInitialized()) {
|
||||
res.redirect("setup");
|
||||
} else if (!req.session.loggedIn && !isElectron() && !noAuthentication) {
|
||||
} else if (!req.session.loggedIn && !isElectron && !noAuthentication) {
|
||||
res.redirect("login");
|
||||
} else {
|
||||
next();
|
||||
@ -24,7 +24,7 @@ function checkAuth(req: Request, res: Response, next: NextFunction) {
|
||||
// for electron things which need network stuff
|
||||
// currently, we're doing that for file upload because handling form data seems to be difficult
|
||||
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");
|
||||
} else {
|
||||
next();
|
||||
@ -48,7 +48,7 @@ function checkAppInitialized(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");
|
||||
} else {
|
||||
next();
|
||||
@ -56,7 +56,7 @@ function checkPasswordSet(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");
|
||||
} else {
|
||||
next();
|
||||
|
@ -2,12 +2,10 @@
|
||||
|
||||
import optionService from "./options.js";
|
||||
import log from "./log.js";
|
||||
import { isElectron as getIsElectron, isMac } from "./utils.js";
|
||||
import { isElectron, isMac } from "./utils.js";
|
||||
import type { KeyboardShortcut } from "./keyboard_actions_interface.js";
|
||||
import { t } from "i18next";
|
||||
|
||||
const isElectron = getIsElectron();
|
||||
|
||||
function getDefaultKeyboardActions() {
|
||||
if (!t("keyboard_actions.note-navigation")) {
|
||||
throw new Error("Keyboard actions loaded before translations.");
|
||||
|
@ -18,7 +18,7 @@ let port: number;
|
||||
|
||||
if (process.env.TRILIUM_PORT) {
|
||||
port = parseAndValidate(process.env.TRILIUM_PORT, "environment variable TRILIUM_PORT");
|
||||
} else if (isElectron()) {
|
||||
} else if (isElectron) {
|
||||
port = env.isDev() ? 37740 : 37840;
|
||||
} else {
|
||||
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> {
|
||||
// it's not clear how to explicitly configure proxy (as opposed to system proxy),
|
||||
// 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;
|
||||
} else {
|
||||
const { protocol } = url.parse(opts.url);
|
||||
|
@ -37,7 +37,7 @@ function isDbInitialized() {
|
||||
|
||||
async function initDbConnection() {
|
||||
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;
|
||||
}
|
||||
|
@ -13,6 +13,12 @@ import { dirname, join } from "path";
|
||||
|
||||
const randtoken = generator({ source: "crypto" });
|
||||
|
||||
export const isMac = process.platform === "darwin";
|
||||
|
||||
export const isWindows = process.platform === "win32";
|
||||
|
||||
export const isElectron = !!process.versions["electron"];
|
||||
|
||||
export function newEntityId() {
|
||||
return randomString(12);
|
||||
}
|
||||
@ -58,10 +64,6 @@ export function hmac(secret: any, value: any) {
|
||||
return hmac.digest("base64");
|
||||
}
|
||||
|
||||
export function isElectron() {
|
||||
return !!process.versions["electron"];
|
||||
}
|
||||
|
||||
export function hash(text: string) {
|
||||
text = text.normalize();
|
||||
|
||||
@ -128,7 +130,7 @@ export function escapeRegExp(str: string) {
|
||||
}
|
||||
|
||||
export async function crash() {
|
||||
if (isElectron()) {
|
||||
if (isElectron) {
|
||||
(await import("electron")).app.exit(1);
|
||||
} else {
|
||||
process.exit(1);
|
||||
@ -314,19 +316,13 @@ export function envToBoolean(val: string | undefined) {
|
||||
* @returns the resource dir.
|
||||
*/
|
||||
export function getResourceDir() {
|
||||
if (isElectron() && !env.isDev()) {
|
||||
if (isElectron && !env.isDev()) {
|
||||
return process.resourcesPath;
|
||||
} else {
|
||||
return join(dirname(fileURLToPath(import.meta.url)), "..", "..");
|
||||
}
|
||||
}
|
||||
|
||||
export const isMac = process.platform === "darwin";
|
||||
|
||||
export const isWindows = process.platform === "win32";
|
||||
return process.platform === "win32";
|
||||
}
|
||||
|
||||
export default {
|
||||
randomSecureToken,
|
||||
randomString,
|
||||
|
@ -18,7 +18,7 @@ if (env.isDev()) {
|
||||
const debounce = (await import("debounce")).default;
|
||||
const debouncedReloadFrontend = debounce(() => reloadFrontend("source code change"), 200);
|
||||
chokidar
|
||||
.watch(isElectron() ? "dist/src/public" : "src/public")
|
||||
.watch(isElectron ? "dist/src/public" : "src/public")
|
||||
.on("add", debouncedReloadFrontend)
|
||||
.on("change", debouncedReloadFrontend)
|
||||
.on("unlink", debouncedReloadFrontend);
|
||||
@ -62,7 +62,7 @@ function init(httpServer: HttpServer, sessionParser: SessionParser) {
|
||||
webSocketServer = new WebSocketServer({
|
||||
verifyClient: (info, done) => {
|
||||
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) {
|
||||
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
|
||||
* to do a complex evaluation.
|
||||
*/
|
||||
if (utils.isElectron()) {
|
||||
if (utils.isElectron) {
|
||||
(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.
|
||||
|
||||
if (utils.isElectron()) {
|
||||
if (utils.isElectron) {
|
||||
const electronRouting = await import("./routes/electron.js");
|
||||
electronRouting.default(app);
|
||||
}
|
||||
@ -146,7 +146,7 @@ function startHttpServer() {
|
||||
}
|
||||
}
|
||||
|
||||
if (utils.isElectron()) {
|
||||
if (utils.isElectron) {
|
||||
import("electron").then(({ app, dialog }) => {
|
||||
// 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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user