chore(core): integrate scripting services

This commit is contained in:
Elian Doran
2026-03-28 12:54:44 +02:00
parent 3a053d3104
commit b02e9ba52b
10 changed files with 285 additions and 288 deletions

View File

@@ -1,262 +0,0 @@
import { ScriptParams } from "@triliumnext/commons";
import { binary_utils } from "@triliumnext/core";
import { transform } from "sucrase";
import becca from "../becca/becca.js";
import type BNote from "../becca/entities/bnote.js";
import type { ApiParams } from "./backend_script_api_interface.js";
import cls from "./cls.js";
import log from "./log.js";
import ScriptContext from "./script_context.js";
export interface Bundle {
note?: BNote;
noteId?: string;
script: string;
html: string;
allNotes?: BNote[];
allNoteIds?: string[];
}
function executeNote(note: BNote, apiParams: ApiParams) {
if (!note.isJavaScript() || note.getScriptEnv() !== "backend" || !note.isContentAvailable()) {
log.info(`Cannot execute note ${note.noteId} "${note.title}", note must be of type "Code: JS backend"`);
return;
}
const bundle = getScriptBundle(note, true, "backend");
if (!bundle) {
throw new Error("Unable to determine bundle.");
}
return executeBundle(bundle, apiParams);
}
function executeNoteNoException(note: BNote, apiParams: ApiParams) {
try {
executeNote(note, apiParams);
} catch (e) {
// just swallow, exception is logged already in executeNote
}
}
export function executeBundle(bundle: Bundle, apiParams: ApiParams = {}) {
if (!apiParams.startNote) {
// this is the default case, the only exception is when we want to preserve frontend startNote
apiParams.startNote = bundle.note;
}
const originalComponentId = cls.get("componentId");
cls.set("componentId", "script");
cls.set("bundleNoteId", bundle.note?.noteId);
// last \r\n is necessary if the script contains line comment on its last line
const script = `function() {\r
${bundle.script}\r
}`;
const ctx = new ScriptContext(bundle.allNotes || [], apiParams);
try {
return execute(ctx, script);
} catch (e: any) {
log.error(`Execution of script "${bundle.note?.title}" (${bundle.note?.noteId}) failed with error: ${e.message}`);
throw e;
} finally {
cls.set("componentId", originalComponentId);
}
}
/**
* THIS METHOD CAN'T BE ASYNC, OTHERWISE TRANSACTION WRAPPER WON'T BE EFFECTIVE AND WE WILL BE LOSING THE
* ENTITY CHANGES IN CLS.
*
* This method preserves frontend startNode - that's why we start execution from currentNote and override
* bundle's startNote.
*/
function executeScript(script: string, params: ScriptParams, startNoteId: string, currentNoteId: string, originEntityName: string, originEntityId: string) {
const startNote = becca.getNote(startNoteId);
const currentNote = becca.getNote(currentNoteId);
const originEntity = becca.getEntity(originEntityName, originEntityId);
if (!currentNote) {
throw new Error("Cannot find note.");
}
// we're just executing an excerpt of the original frontend script in the backend context, so we must
// override normal note's content, and it's mime type / script environment
const overrideContent = `return (${script}\r\n)(${getParams(params)})`;
const bundle = getScriptBundle(currentNote, true, "backend", [], overrideContent);
if (!bundle) {
throw new Error("Unable to determine script bundle.");
}
return executeBundle(bundle, { startNote, originEntity });
}
function execute(ctx: ScriptContext, script: string) {
return function () {
return eval(`const apiContext = this;\r\n(${script}\r\n)()`);
}.call(ctx);
}
function getParams(params?: ScriptParams) {
if (!params) {
return params;
}
return params
.map((p) => {
if (typeof p === "string" && p.startsWith("!@#Function: ")) {
return p.substr(13);
}
return JSON.stringify(p);
})
.join(",");
}
function getScriptBundleForFrontend(note: BNote, script?: string, params?: ScriptParams) {
let overrideContent: string | null = null;
if (script) {
overrideContent = `return (${script}\r\n)(${getParams(params)})`;
}
const bundle = getScriptBundle(note, true, "frontend", [], overrideContent);
if (!bundle) {
return;
}
// for frontend, we return just noteIds because frontend needs to use its own entity instances
bundle.noteId = bundle.note?.noteId;
delete bundle.note;
bundle.allNoteIds = bundle.allNotes?.map((note) => note.noteId);
delete bundle.allNotes;
return bundle;
}
export function getScriptBundle(note: BNote, root: boolean = true, scriptEnv: string | null = null, includedNoteIds: string[] = [], overrideContent: string | null = null): Bundle | undefined {
if (!note.isContentAvailable()) {
return;
}
if (!(note.isJavaScript() || note.isHtml() || note.isJsx())) {
return;
}
if (!root && note.hasOwnedLabel("disableInclusion")) {
return;
}
if (note.type !== "file" && !root && scriptEnv !== note.getScriptEnv()) {
return;
}
const bundle: Bundle = {
note,
script: "",
html: "",
allNotes: [note]
};
if (includedNoteIds.includes(note.noteId)) {
return bundle;
}
includedNoteIds.push(note.noteId);
const modules: BNote[] = [];
for (const child of note.getChildNotes()) {
const childBundle = getScriptBundle(child, false, scriptEnv, includedNoteIds);
if (childBundle) {
if (childBundle.note) {
modules.push(childBundle.note);
}
bundle.script += childBundle.script;
bundle.html += childBundle.html;
if (bundle.allNotes && childBundle.allNotes) {
bundle.allNotes = bundle.allNotes.concat(childBundle.allNotes);
}
}
}
const moduleNoteIds = modules.map((mod) => mod.noteId);
// only frontend scripts are async. Backend cannot be async because of transaction management.
const isFrontend = scriptEnv === "frontend";
if (note.isJsx() || note.isJavaScript()) {
let scriptContent = note.getContent();
if (note.isJsx()) {
scriptContent = buildJsx(scriptContent).code;
}
bundle.script += `
apiContext.modules['${note.noteId}'] = { exports: {} };
${root ? "return " : ""}${isFrontend ? "await" : ""} ((${isFrontend ? "async" : ""} function(exports, module, require, api${modules.length > 0 ? ", " : ""}${modules.map((child) => sanitizeVariableName(child.title)).join(", ")}) {
try {
${overrideContent || scriptContent};
} catch (e) { throw new Error("Load of script note \\"${note.title}\\" (${note.noteId}) failed with: " + e.message); }
for (const exportKey in exports) module.exports[exportKey] = exports[exportKey];
return module.exports;
}).call({}, {}, apiContext.modules['${note.noteId}'], apiContext.require(${JSON.stringify(moduleNoteIds)}), apiContext.apis['${note.noteId}']${modules.length > 0 ? ", " : ""}${modules.map((mod) => `apiContext.modules['${mod.noteId}'].exports`).join(", ")}));
`;
} else if (note.isHtml()) {
bundle.html += note.getContent();
}
return bundle;
}
export function buildJsx(contentRaw: string | Uint8Array) {
const content = binary_utils.unwrapStringOrBuffer(contentRaw);
const output = transform(content, {
transforms: ["jsx", "imports"],
jsxPragma: "api.preact.h",
jsxFragmentPragma: "api.preact.Fragment",
production: true
});
let code = output.code;
// Rewrite ESM-like exports to `module.exports =`.
code = code.replaceAll(
/\bexports\s*\.\s*default\s*=\s*/g,
'module.exports = '
);
// Rewrite ESM-like imports to Preact, to `const { foo } = api.preact`
code = code.replaceAll(
/(?:var|let|const)\s+(\w+)\s*=\s*require\(['"]trilium:preact['"]\);?/g,
'const $1 = api.preact;'
);
// Rewrite ESM-like imports to internal API, to `const { foo } = api`
code = code.replaceAll(
/(?:var|let|const)\s+(\w+)\s*=\s*require\(['"]trilium:api['"]\);?/g,
'const $1 = api;'
);
output.code = code;
return output;
}
function sanitizeVariableName(str: string) {
return str.replace(/[^a-z0-9_]/gim, "");
}
export default {
executeNote,
executeNoteNoException,
executeScript,
getScriptBundleForFrontend
};

View File

@@ -1,9 +1,14 @@
import { type AttributeRow, dayjs, formatLogMessage } from "@triliumnext/commons";
import { type AbstractBeccaEntity, Becca, branches as branchService, NoteParams, SearchContext, sync_mutex as syncMutex, zipExportService } from "@triliumnext/core";
import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity";
import Becca from "../becca/becca-interface";
import axios from "axios";
import * as cheerio from "cheerio";
import xml2js from "xml2js";
import branchService from "./branches";
import { NoteParams } from "./notes.js";
import SearchContext from "./search/search_context";
import syncMutex from "./sync_mutex";
import zipExportService from "./export/zip";
import becca from "../becca/becca.js";
import type BAttachment from "../becca/entities/battachment.js";
import type BAttribute from "../becca/entities/battribute.js";
@@ -19,15 +24,15 @@ import backupService from "./backup.js";
import cloningService from "./cloning.js";
import config from "./config.js";
import dateNoteService from "./date_notes.js";
import log from "./log.js";
import log, { getLog } from "./log.js";
import noteService from "./notes.js";
import optionsService from "./options.js";
import searchService from "./search/services/search.js";
import SpacedUpdate from "./spaced_update.js";
import specialNotesService from "./special_notes.js";
import sql from "./sql.js";
import { getSql } from "./sql/index";
import treeService from "./tree.js";
import { escapeHtml, randomString, unescapeHtml } from "./utils.js";
import { escapeHtml, randomString, unescapeHtml } from "./utils/index";
import ws from "./ws.js";
/**
@@ -519,7 +524,7 @@ function BackendScriptApi(this: Api, currentNote: BNote, apiParams: ApiParams) {
extraOptions.content = content;
}
return sql.transactional(() => {
return getSql().transactional(() => {
const { note, branch } = noteService.createNewNote(extraOptions);
for (const attr of _extraOptions.attributes || []) {
@@ -539,9 +544,11 @@ function BackendScriptApi(this: Api, currentNote: BNote, apiParams: ApiParams) {
this.logMessages = {};
this.logSpacedUpdates = {};
const logInstance = getLog();
const sql = getSql();
this.log = (rawMessage) => {
const message = formatLogMessage(rawMessage);
log.info(message);
logInstance.info(message);
if (!this.startNote) {
return;

View File

@@ -1,7 +1,7 @@
import type { AbstractBeccaEntity } from "@triliumnext/core";
import type { Request, Response } from "express";
import type BNote from "../becca/entities/bnote.js";
import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.js";
export interface ApiParams {
startNote?: BNote | null;

View File

@@ -1,5 +1,6 @@
export default {
backupNow(name: string) {
async backupNow(name: string) {
console.warn("Backup not yet available.");
return "backup-" + name + "-" + new Date().toISOString() + ".zip";
}
}

View File

@@ -1,7 +1,8 @@
// TODO: Real implementation.
export default {
General: {
readOnly: false
readOnly: false,
instanceName: "",
},
Sync: {
syncServerHost: "",

View File

@@ -6,7 +6,7 @@ import type BNote from "../becca/entities/bnote.js";
import hiddenSubtreeService from "./hidden_subtree.js";
import noteService from "./notes.js";
import oneTimeTimer from "./one_time_timer.js";
import * as scriptService from "./script.js";
import scriptService from "./script.js";
import treeService from "./tree.js";
import eventService from "./events.js";
import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.js";

View File

@@ -6,7 +6,7 @@ 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";
describe("Script", () => {
@@ -51,7 +51,7 @@ describe("Script", () => {
});
it("returns result from script", () => {
cls.init(() => {
getContext().init(() => {
const result = executeBundle({
script: `return "world";`,
html: "",
@@ -68,7 +68,7 @@ describe("Script", () => {
});
it("dayjs is available", () => {
cls.init(() => {
getContext().init(() => {
const bundle = getScriptBundle(scriptNote, true, "backend", [], `return api.dayjs().format("YYYY-MM-DD");`);
expect(bundle).toBeDefined();
const result = executeBundle(bundle!);
@@ -77,7 +77,7 @@ describe("Script", () => {
});
it("dayjs is-same-or-before plugin exists", () => {
cls.init(() => {
getContext().init(() => {
const bundle = getScriptBundle(scriptNote, true, "backend", [], `return api.dayjs("2023-10-01").isSameOrBefore(api.dayjs("2023-10-02"));`);
expect(bundle).toBeDefined();
const result = executeBundle(bundle!);

View File

@@ -1,11 +1,262 @@
import type BNote from "../becca/entities/bnote";
import { ScriptParams } from "@triliumnext/commons";
import { transform } from "sucrase";
export function executeNoteNoException(script: unknown, { originEntity: unknown }) {
console.warn("Skipped script execution");
import becca from "../becca/becca.js";
import type BNote from "../becca/entities/bnote.js";
import type { ApiParams } from "./backend_script_api_interface.js";
import { getLog } from "./log.js";
import ScriptContext from "./script_context.js";
import { getContext } from "./context.js";
import { unwrapStringOrBuffer } from "./utils/binary.js";
export interface Bundle {
note?: BNote;
noteId?: string;
script: string;
html: string;
allNotes?: BNote[];
allNoteIds?: string[];
}
function executeNote(note: BNote, apiParams: ApiParams) {
if (!note.isJavaScript() || note.getScriptEnv() !== "backend" || !note.isContentAvailable()) {
getLog().info(`Cannot execute note ${note.noteId} "${note.title}", note must be of type "Code: JS backend"`);
return;
}
const bundle = getScriptBundle(note, true, "backend");
if (!bundle) {
throw new Error("Unable to determine bundle.");
}
return executeBundle(bundle, apiParams);
}
function executeNoteNoException(note: BNote, apiParams: ApiParams) {
try {
executeNote(note, apiParams);
} catch (e) {
// just swallow, exception is logged already in executeNote
}
}
export function executeBundle(bundle: Bundle, apiParams: ApiParams = {}) {
if (!apiParams.startNote) {
// this is the default case, the only exception is when we want to preserve frontend startNote
apiParams.startNote = bundle.note;
}
const originalComponentId = getContext().get("componentId");
getContext().set("componentId", "script");
getContext().set("bundleNoteId", bundle.note?.noteId);
// last \r\n is necessary if the script contains line comment on its last line
const script = `function() {\r
${bundle.script}\r
}`;
const ctx = new ScriptContext(bundle.allNotes || [], apiParams);
try {
return execute(ctx, script);
} catch (e: any) {
getLog().error(`Execution of script "${bundle.note?.title}" (${bundle.note?.noteId}) failed with error: ${e.message}`);
throw e;
} finally {
getContext().set("componentId", originalComponentId);
}
}
/**
* THIS METHOD CAN'T BE ASYNC, OTHERWISE TRANSACTION WRAPPER WON'T BE EFFECTIVE AND WE WILL BE LOSING THE
* ENTITY CHANGES IN CLS.
*
* This method preserves frontend startNode - that's why we start execution from currentNote and override
* bundle's startNote.
*/
function executeScript(script: string, params: ScriptParams, startNoteId: string, currentNoteId: string, originEntityName: string, originEntityId: string) {
const startNote = becca.getNote(startNoteId);
const currentNote = becca.getNote(currentNoteId);
const originEntity = becca.getEntity(originEntityName, originEntityId);
if (!currentNote) {
throw new Error("Cannot find note.");
}
// we're just executing an excerpt of the original frontend script in the backend context, so we must
// override normal note's content, and it's mime type / script environment
const overrideContent = `return (${script}\r\n)(${getParams(params)})`;
const bundle = getScriptBundle(currentNote, true, "backend", [], overrideContent);
if (!bundle) {
throw new Error("Unable to determine script bundle.");
}
return executeBundle(bundle, { startNote, originEntity });
}
function execute(ctx: ScriptContext, script: string) {
return function () {
return eval(`const apiContext = this;\r\n(${script}\r\n)()`);
}.call(ctx);
}
function getParams(params?: ScriptParams) {
if (!params) {
return params;
}
return params
.map((p) => {
if (typeof p === "string" && p.startsWith("!@#Function: ")) {
return p.substr(13);
}
return JSON.stringify(p);
})
.join(",");
}
function getScriptBundleForFrontend(note: BNote, script?: string, params?: ScriptParams) {
let overrideContent: string | null = null;
if (script) {
overrideContent = `return (${script}\r\n)(${getParams(params)})`;
}
const bundle = getScriptBundle(note, true, "frontend", [], overrideContent);
if (!bundle) {
return;
}
// for frontend, we return just noteIds because frontend needs to use its own entity instances
bundle.noteId = bundle.note?.noteId;
delete bundle.note;
bundle.allNoteIds = bundle.allNotes?.map((note) => note.noteId);
delete bundle.allNotes;
return bundle;
}
export function getScriptBundle(note: BNote, root: boolean = true, scriptEnv: string | null = null, includedNoteIds: string[] = [], overrideContent: string | null = null): Bundle | undefined {
if (!note.isContentAvailable()) {
return;
}
if (!(note.isJavaScript() || note.isHtml() || note.isJsx())) {
return;
}
if (!root && note.hasOwnedLabel("disableInclusion")) {
return;
}
if (note.type !== "file" && !root && scriptEnv !== note.getScriptEnv()) {
return;
}
const bundle: Bundle = {
note,
script: "",
html: "",
allNotes: [note]
};
if (includedNoteIds.includes(note.noteId)) {
return bundle;
}
includedNoteIds.push(note.noteId);
const modules: BNote[] = [];
for (const child of note.getChildNotes()) {
const childBundle = getScriptBundle(child, false, scriptEnv, includedNoteIds);
if (childBundle) {
if (childBundle.note) {
modules.push(childBundle.note);
}
bundle.script += childBundle.script;
bundle.html += childBundle.html;
if (bundle.allNotes && childBundle.allNotes) {
bundle.allNotes = bundle.allNotes.concat(childBundle.allNotes);
}
}
}
const moduleNoteIds = modules.map((mod) => mod.noteId);
// only frontend scripts are async. Backend cannot be async because of transaction management.
const isFrontend = scriptEnv === "frontend";
if (note.isJsx() || note.isJavaScript()) {
let scriptContent = note.getContent();
if (note.isJsx()) {
scriptContent = buildJsx(scriptContent).code;
}
bundle.script += `
apiContext.modules['${note.noteId}'] = { exports: {} };
${root ? "return " : ""}${isFrontend ? "await" : ""} ((${isFrontend ? "async" : ""} function(exports, module, require, api${modules.length > 0 ? ", " : ""}${modules.map((child) => sanitizeVariableName(child.title)).join(", ")}) {
try {
${overrideContent || scriptContent};
} catch (e) { throw new Error("Load of script note \\"${note.title}\\" (${note.noteId}) failed with: " + e.message); }
for (const exportKey in exports) module.exports[exportKey] = exports[exportKey];
return module.exports;
}).call({}, {}, apiContext.modules['${note.noteId}'], apiContext.require(${JSON.stringify(moduleNoteIds)}), apiContext.apis['${note.noteId}']${modules.length > 0 ? ", " : ""}${modules.map((mod) => `apiContext.modules['${mod.noteId}'].exports`).join(", ")}));
`;
} else if (note.isHtml()) {
bundle.html += note.getContent();
}
return bundle;
}
export function buildJsx(contentRaw: string | Uint8Array) {
const content = unwrapStringOrBuffer(contentRaw);
const output = transform(content, {
transforms: ["jsx", "imports"],
jsxPragma: "api.preact.h",
jsxFragmentPragma: "api.preact.Fragment",
production: true
});
let code = output.code;
// Rewrite ESM-like exports to `module.exports =`.
code = code.replaceAll(
/\bexports\s*\.\s*default\s*=\s*/g,
'module.exports = '
);
// Rewrite ESM-like imports to Preact, to `const { foo } = api.preact`
code = code.replaceAll(
/(?:var|let|const)\s+(\w+)\s*=\s*require\(['"]trilium:preact['"]\);?/g,
'const $1 = api.preact;'
);
// Rewrite ESM-like imports to internal API, to `const { foo } = api`
code = code.replaceAll(
/(?:var|let|const)\s+(\w+)\s*=\s*require\(['"]trilium:api['"]\);?/g,
'const $1 = api;'
);
output.code = code;
return output;
}
function sanitizeVariableName(str: string) {
return str.replace(/[^a-z0-9_]/gim, "");
}
export default {
executeNote(scriptNote: BNote, args: {}) {
console.warn("Note not executed");
}
}
executeNote,
executeNoteNoException,
executeScript,
getScriptBundleForFrontend
};

View File

@@ -1,8 +1,7 @@
import { utils } from "@triliumnext/core";
import type BNote from "../becca/entities/bnote.js";
import BackendScriptApi from "./backend_script_api.js";
import type { ApiParams } from "./backend_script_api_interface.js";
import { toObject } from "./utils/index.js";
type Module = {
exports: any[];
@@ -17,8 +16,8 @@ class ScriptContext {
constructor(allNotes: BNote[], apiParams: ApiParams) {
this.allNotes = allNotes;
this.modules = {};
this.notes = utils.toObject(allNotes, (note) => [note.noteId, note]);
this.apis = utils.toObject(allNotes, (note) => [note.noteId, new BackendScriptApi(note, apiParams)]);
this.notes = toObject(allNotes, (note) => [note.noteId, note]);
this.apis = toObject(allNotes, (note) => [note.noteId, new BackendScriptApi(note, apiParams)]);
}
require(moduleNoteIds: string[]) {