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
Merge remote-tracking branch 'origin/main' into standalone
Elian Dorancccb2e9408fb
diff --cc apps/desktop/src/main.tsindex c08e314544,ec900be24e..5498c92eb7--- a/apps/desktop/src/main.ts+++ b/apps/desktop/src/main.ts@@@ -1,20 -1,9 +1,19 @@@- import { getLog, initializeCore, sql_init } from "@triliumnext/core";-import { initializeTranslations } from "@triliumnext/server/src/services/i18n.js";-import options from "@triliumnext/server/src/services/options.js";++import { getLog, initializeCore, options, sql_init } from "@triliumnext/core";++import ServerBackupService from "@triliumnext/server/src/backup_provider.js";+import ClsHookedExecutionContext from "@triliumnext/server/src/cls_provider.js";- import NodejsCryptoProvider from "@triliumnext/server/src/crypto_provider.js";+import { loadCoreSchema } from "@triliumnext/server/src/core_assets.js";++import NodejsCryptoProvider from "@triliumnext/server/src/crypto_provider.js";+import NodejsInAppHelpProvider from "@triliumnext/server/src/in_app_help_provider.js";++import ServerLogService from "@triliumnext/server/src/log_provider.js";+import dataDirs from "@triliumnext/server/src/services/data_dir.js";- import { options } from "@triliumnext/core";import port from "@triliumnext/server/src/services/port.js";-import sqlInit from "@triliumnext/server/src/services/sql_init.js";+import NodeRequestProvider from "@triliumnext/server/src/services/request.js";+import { RESOURCE_DIR } from "@triliumnext/server/src/services/resource_dir.js";import tray from "@triliumnext/server/src/services/tray.js";import windowService from "@triliumnext/server/src/services/window.js";+import WebSocketMessagingProvider from "@triliumnext/server/src/services/ws_messaging_provider.js";- import ServerBackupService from "@triliumnext/server/src/backup_provider.js";- import ServerLogService from "@triliumnext/server/src/log_provider.js";+import BetterSqlite3Provider from "@triliumnext/server/src/sql_provider.js";+import NodejsZipProvider from "@triliumnext/server/src/zip_provider.js";import { app, BrowserWindow,globalShortcut } from "electron";import electronDebug from "electron-debug";import electronDl from "electron-dl";@@@ -24,9 -12,18 +23,19 @@@ import path, { join, resolve } from "paimport { deferred, LOCALES } from "../../../packages/commons/src";import { PRODUCT_NAME } from "./app-info";+import DesktopPlatformProvider from "./platform_provider";async function main() {+ // Ignore EPIPE errors on stdout/stderr — these occur when the parent process+ // pipe breaks (e.g. after system suspend with Snap packaging).+ for (const stream of [process.stdout, process.stderr]) {+ stream?.on("error", (err: NodeJS.ErrnoException) => {+ if (err.code !== "EPIPE") {+ throw err;+ }+ });+ }+const userDataPath = getUserData();app.setPath("userData", userDataPath);diff --cc packages/trilium-core/src/services/script.spec.tsindex 4e4942d7d8,5031840bb0..2685f3b6bb--- a/packages/trilium-core/src/services/script.spec.ts+++ b/packages/trilium-core/src/services/script.spec.ts@@@ -1,13 -1,17 +1,17 @@@import { trimIndentation } from "@triliumnext/commons";-import { beforeEach, describe, expect, it, vi } from "vitest";+import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";import becca from "../becca/becca.js";- import BBranch from "../becca/entities/bbranch.js";- import BNote from "../becca/entities/bnote.js";import { buildNote } from "../test/becca_easy_mocking.js";- import { NoteBuilder } from "../test/becca_mocking.js";-import cls from "./cls.js";+import { getContext } from "./context.js";- import { buildJsx, executeBundle, getScriptBundle } from "./script.js";+ import scriptService, { buildJsx, executeBundle, getScriptBundle } from "./script.js";+ import ws from "./ws.js";++ vi.mock("./ws.js", () => ({+ default: {+ sendMessageToAllClients: vi.fn()+ }+ }));describe("Script", () => {beforeEach(() => {@@@ -60,19 -52,37 +52,45 @@@});});+ it("executes runOnFrontend from a backend script", () => {+ const note = buildNote({+ type: "code",+ mime: "application/javascript;env=backend",+ content: ""+ });+- cls.init(() => {++ getContext().init(() => {+ const bundle = getScriptBundle(note, true, "backend", [], `+ api.runOnFrontend(() => {+ api.showMessage("Hello frontend");+ });+ `);+ expect(bundle).toBeDefined();+ executeBundle(bundle!);++ expect(ws.sendMessageToAllClients).toHaveBeenCalledOnce();+ expect(ws.sendMessageToAllClients).toHaveBeenCalledWith(+ expect.objectContaining({+ type: "execute-script",+ script: expect.stringContaining("Hello frontend")+ })+ );+ });+ });+describe("dayjs in backend scripts", () => {- const scriptNote = buildNote({- type: "code",- mime: "application/javascript;env=backend",- content: ""+ // buildNote() is called inside beforeAll (not at describe-collection+ // time) so that the test setup's initializeCore() has run first.+ // buildNote generates random IDs via getCrypto(), which crashes with+ // "Crypto not initialized" if invoked during describe collection.+ let scriptNote: ReturnType<typeof buildNote>;++ beforeAll(() => {+ scriptNote = buildNote({+ type: "code",+ mime: "application/javascript;env=backend",+ content: ""+ });});it("dayjs is available", () => {diff --cc packages/trilium-core/src/services/script.tsindex d4fbb73df5,64774afaa3..5e0392446b--- a/packages/trilium-core/src/services/script.ts+++ b/packages/trilium-core/src/services/script.ts@@@ -137,12 -137,14 +137,14 @@@ function getParams(params?: ScriptParam}function getScriptBundleForFrontend(note: BNote, script?: string, params?: ScriptParams) {- // Warn the user if they're trying to run a backend script in the frontend- if (note.isJavaScript() && note.getScriptEnv() === "backend") {+ // Warn the user if they're trying to run a backend script in the frontend.+ // Skip this check when an override script is provided (e.g. from runOnFrontend),+ // since the override is already meant for the frontend.+ if (!script && note.isJavaScript() && note.getScriptEnv() === "backend") {- log.info(`Cannot execute note ${note.noteId} "${note.title}" in frontend, note is of type "Code: JS backend"`);+ getLog().info(`Cannot execute note ${note.noteId} "${note.getTitleOrProtected()}" in frontend, note is of type "Code: JS backend"`);const message = t("script.wrong-environment", {- noteTitle: note.title,+ noteTitle: note.getTitleOrProtected(),noteId: note.noteId,actualEnv: "backend",expectedEnv: "frontend"diff --cc pnpm-lock.yamlindex f559076568,423c0f6b4d..b166c36c44--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@@ -6121,11 -5772,8 +6121,11 @@@ packages'@types/[email protected]':resolution: {integrity: sha512-uh2Lv57xvggst6lCqNdFAmDSvoMG7M/HDtX4iUCquxQ5EGPtaPM5PL5Hmi7LCvOG8db7YaCPNJEeoI8s/WzIQw==}+ '@types/[email protected]':+ resolution: {integrity: sha512-qL05wGXVy0yfWcF8LCE9+9uSeUIpeKdgpm8YmOAPTjLd3FaoZziPOhVxIiLzEhLTFfOvbuwnaWDm4v4i87diRQ==}+- '@types/[email protected]':- resolution: {integrity: sha512-ox1MLWti8gNnWMNYd0Kob2nRNxttvjB0FwtZkWC3MkDtO9SdOe785dfEN+8Bjmrn9x5RzGR6Lj837Icr0seOeg==}+ '@types/[email protected]':+ resolution: {integrity: sha512-7HaugASx842qF+uN7Gwz/23ssmBGIrEhqlQDV99/pTTOu202Jje0A+Z6N3kyJZ5zpdFfAABI2Z+qpNoa4q0RLg==}'@types/[email protected]':resolution: {integrity: sha512-chhaNf2oKHlRkDGt+tiKE2Z5aJ6qalm7Z9rlLdBwmOiAAf09YQvvoLXjWK4HWPF1xU/fqvMgfNfpVoBscA/tKA==}@@@ -20190,9 -19553,7 +20191,9 @@@ snapshots'@types/methods': 1.1.4'@types/superagent': 8.1.9+ '@types/[email protected]': {}+- '@types/[email protected]': {}+ '@types/[email protected]': {}'@types/[email protected]': {}