Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 0606fe0

Browse files
authored
Merge pull request #242 from TriliumNext/feature/server_esm_part2
Server ESM port: Convert some of the asynchronous imports
2 parents 7f2caa0 + 3aa38b9 commit 0606fe0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+193
-155
lines changed

electron.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22

33
import electron from "electron";
4+
import electronDebug from "electron-debug";
5+
import electronDl from "electron-dl";
46
import sqlInit from "./src/services/sql_init.js";
57
import appIconService from "./src/services/app_icon.js";
68
import windowService from "./src/services/window.js";
@@ -12,11 +14,11 @@ if (require('electron-squirrel-startup')) {
1214
}
1315

1416
// Adds debug features like hotkeys for triggering dev tools and reload
15-
require('electron-debug')();
17+
electronDebug();
1618

1719
appIconService.installLocalAppIcon();
1820

19-
require('electron-dl')({ saveAs: true });
21+
electronDl({ saveAs: true });
2022

2123
// needed for excalidraw export https://github.com/zadam/trilium/issues/4271
2224
electron.app.commandLine.appendSwitch(

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
"@types/sax": "^1.2.7",
145145
"@types/semver": "^7.5.8",
146146
"@types/serve-favicon": "^2.5.7",
147+
"@types/session-file-store": "^1.2.5",
147148
"@types/stream-throttle": "^0.1.4",
148149
"@types/tmp": "^0.2.6",
149150
"@types/turndown": "^5.0.4",

spec/search/parens.spec.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
const handleParens = require('../../src/services/search/services/handle_parens');
1+
import handleParens from "../../src/services/search/services/handle_parens";
2+
import { TokenStructure } from "../../src/services/search/services/types";
23

34
describe("Parens handler", () => {
45
it("handles parens", () => {
56
const input = ["(", "hello", ")", "and", "(", "(", "pick", "one", ")", "and", "another", ")"]
67
.map(token => ({token}));
78

8-
expect(handleParens(input))
9-
.toEqual([
9+
const actual: TokenStructure = [
10+
[
11+
{token: "hello"}
12+
],
13+
{token: "and"},
14+
[
1015
[
11-
{token: "hello"}
16+
{token: "pick"},
17+
{token: "one"}
1218
],
1319
{token: "and"},
14-
[
15-
[
16-
{token: "pick"},
17-
{token: "one"}
18-
],
19-
{token: "and"},
20-
{token: "another"}
21-
]
22-
]);
20+
{token: "another"}
21+
]
22+
];
23+
24+
expect(handleParens(input)).toEqual(actual);
2325
});
2426
});

src/becca/becca-interface.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,7 @@ export default class Becca {
155155
}
156156

157157
getRevision(revisionId: string): BRevision | null {
158-
const row = sql.getRow("SELECT * FROM revisions WHERE revisionId = ?", [revisionId]);
159-
160-
const BRevision = require('./entities/brevision'); // avoiding circular dependency problems
158+
const row = sql.getRow<RevisionRow | null>("SELECT * FROM revisions WHERE revisionId = ?", [revisionId]);
161159
return row ? new BRevision(row) : null;
162160
}
163161

@@ -179,9 +177,7 @@ export default class Becca {
179177
WHERE attachmentId = ? AND isDeleted = 0`
180178
: `SELECT * FROM attachments WHERE attachmentId = ? AND isDeleted = 0`;
181179

182-
const BAttachment = require('./entities/battachment'); // avoiding circular dependency problems
183-
184-
return sql.getRows(query, [attachmentId])
180+
return sql.getRows<AttachmentRow>(query, [attachmentId])
185181
.map(row => new BAttachment(row))[0];
186182
}
187183

@@ -194,7 +190,6 @@ export default class Becca {
194190
}
195191

196192
getAttachments(attachmentIds: string[]): BAttachment[] {
197-
const BAttachment = require('./entities/battachment'); // avoiding circular dependency problems
198193
return sql.getManyRows<AttachmentRow>("SELECT * FROM attachments WHERE attachmentId IN (???) AND isDeleted = 0", attachmentIds)
199194
.map(row => new BAttachment(row));
200195
}
@@ -204,9 +199,7 @@ export default class Becca {
204199
return null;
205200
}
206201

207-
const row = sql.getRow("SELECT *, LENGTH(content) AS contentLength FROM blobs WHERE blobId = ?", [entity.blobId]);
208-
209-
const BBlob = require('./entities/bblob'); // avoiding circular dependency problems
202+
const row = sql.getRow<BBlob | null>("SELECT *, LENGTH(content) AS contentLength FROM blobs WHERE blobId = ?", [entity.blobId]);
210203
return row ? new BBlob(row) : null;
211204
}
212205

@@ -248,16 +241,12 @@ export default class Becca {
248241
}
249242

250243
getRecentNotesFromQuery(query: string, params: string[] = []): BRecentNote[] {
251-
const rows = sql.getRows(query, params);
252-
253-
const BRecentNote = require('./entities/brecent_note'); // avoiding circular dependency problems
244+
const rows = sql.getRows<BRecentNote>(query, params);
254245
return rows.map(row => new BRecentNote(row));
255246
}
256247

257248
getRevisionsFromQuery(query: string, params: string[] = []): BRevision[] {
258249
const rows = sql.getRows<RevisionRow>(query, params);
259-
260-
const BRevision = require('./entities/brevision'); // avoiding circular dependency problems
261250
return rows.map(row => new BRevision(row));
262251
}
263252

src/becca/becca_loader.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import cls from "../services/cls.js";
1414
import entityConstructor from "../becca/entity_constructor.js";
1515
import { AttributeRow, BranchRow, EtapiTokenRow, NoteRow, OptionRow } from './entities/rows';
1616
import AbstractBeccaEntity from "./entities/abstract_becca_entity.js";
17+
import options_init from "../services/options_init.js";
18+
import ws from "../services/ws.js";
1719

1820
const beccaLoaded = new Promise<void>((res, rej) => {
1921
sqlInit.dbReady.then(() => {
2022
cls.init(() => {
2123
load();
2224

23-
require('../services/options_init').initStartupOptions();
25+
options_init.initStartupOptions();
2426

2527
res();
2628
});
@@ -73,7 +75,7 @@ function load() {
7375
function reload(reason: string) {
7476
load();
7577

76-
require('../services/ws').reloadFrontend(reason || "becca reloaded");
78+
ws.reloadFrontend(reason || "becca reloaded");
7779
}
7880

7981
eventService.subscribeBeccaLoader([eventService.ENTITY_CHANGE_SYNCED], ({ entityName, entityRow }) => {

src/becca/entities/battachment.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import log from "../../services/log.js";
99
import { AttachmentRow } from './rows';
1010
import BNote from "./bnote.js";
1111
import BBranch from "./bbranch.js";
12+
import noteService from "../../services/notes.js";
1213

1314
const attachmentRoleToNoteTypeMapping = {
1415
'image': 'image',
@@ -157,8 +158,6 @@ class BAttachment extends AbstractBeccaEntity<BAttachment> {
157158
throw new Error(`Cannot convert protected attachment outside of protected session`);
158159
}
159160

160-
const noteService = require('../../services/notes');
161-
162161
const { note, branch } = noteService.createNewNote({
163162
parentNoteId: this.ownerId,
164163
title: this.title,

src/becca/entities/bbranch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import TaskContext from "../../services/task_context.js";
88
import cls from "../../services/cls.js";
99
import log from "../../services/log.js";
1010
import { BranchRow } from './rows';
11+
import handlers from "../../services/handlers.js";
1112

1213
/**
1314
* Branch represents a relationship between a child note and its parent note. Trilium allows a note to have multiple
@@ -157,7 +158,6 @@ class BBranch extends AbstractBeccaEntity<BBranch> {
157158

158159
if (parentBranches.length === 1 && parentBranches[0] === this) {
159160
// needs to be run before branches and attributes are deleted and thus attached relations disappear
160-
const handlers = require('../../services/handlers');
161161
handlers.runAttachedRelations(note, 'runOnNoteDeletion', note);
162162
}
163163
}

src/becca/entities/bnote.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ import TaskContext from "../../services/task_context.js";
1212
import dayjs from "dayjs";
1313
import utc from "dayjs/plugin/utc";
1414
import eventService from "../../services/events.js";
15-
import { AttachmentRow, NoteRow, NoteType, RevisionRow } from './rows';
15+
import { AttachmentRow, AttributeType, NoteRow, NoteType, RevisionRow } from './rows';
1616
import BBranch from "./bbranch.js";
1717
import BAttribute from "./battribute.js";
1818
import { NotePojo } from '../becca-interface';
19+
import searchService from "../../services/search/services/search.js";
20+
import cloningService, { CloneResponse } from "../../services/cloning.js";
21+
import noteService from "../../services/notes.js";
22+
import handlers from "../../services/handlers.js";
1923
dayjs.extend(utc);
2024

2125
const LABEL = 'label';
@@ -890,11 +894,9 @@ class BNote extends AbstractBeccaEntity<BNote> {
890894
}
891895

892896
try {
893-
const searchService = require('../../services/search/services/search');
894-
const {searchResultNoteIds} = searchService.searchFromNote(this);
895-
897+
const result = searchService.searchFromNote(this);
896898
const becca = this.becca;
897-
return (searchResultNoteIds as string[]) // TODO: remove cast once search is converted
899+
return (result.searchResultNoteIds)
898900
.map(resultNoteId => becca.notes[resultNoteId])
899901
.filter(note => !!note);
900902
}
@@ -1261,7 +1263,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
12611263
* @param name - attribute name
12621264
* @param value - attribute value (optional)
12631265
*/
1264-
setAttribute(type: string, name: string, value?: string) {
1266+
setAttribute(type: AttributeType, name: string, value?: string) {
12651267
const attributes = this.getOwnedAttributes();
12661268
const attr = attributes.find(attr => attr.type === type && attr.name === name);
12671269

@@ -1274,8 +1276,6 @@ class BNote extends AbstractBeccaEntity<BNote> {
12741276
}
12751277
}
12761278
else {
1277-
const BAttribute = require('./battribute');
1278-
12791279
new BAttribute({
12801280
noteId: this.noteId,
12811281
type: type,
@@ -1310,9 +1310,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
13101310
* @param name - name of the attribute, not including the leading ~/#
13111311
* @param value - value of the attribute - text for labels, target note ID for relations; optional.
13121312
*/
1313-
addAttribute(type: string, name: string, value: string = "", isInheritable: boolean = false, position: number | null = null): BAttribute {
1314-
const BAttribute = require('./battribute');
1315-
1313+
addAttribute(type: AttributeType, name: string, value: string = "", isInheritable: boolean = false, position: number | null = null): BAttribute {
13161314
return new BAttribute({
13171315
noteId: this.noteId,
13181316
type: type,
@@ -1351,7 +1349,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
13511349
* @param name - attribute name
13521350
* @param value - attribute value (optional)
13531351
*/
1354-
toggleAttribute(type: string, enabled: boolean, name: string, value?: string) {
1352+
toggleAttribute(type: AttributeType, enabled: boolean, name: string, value?: string) {
13551353
if (enabled) {
13561354
this.setAttribute(type, name, value);
13571355
}
@@ -1423,21 +1421,23 @@ class BNote extends AbstractBeccaEntity<BNote> {
14231421
}
14241422

14251423
searchNotesInSubtree(searchString: string) {
1426-
const searchService = require('../../services/search/services/search');
1427-
14281424
return searchService.searchNotes(searchString) as BNote[];
14291425
}
14301426

14311427
searchNoteInSubtree(searchString: string) {
14321428
return this.searchNotesInSubtree(searchString)[0];
14331429
}
14341430

1435-
cloneTo(parentNoteId: string) {
1436-
const cloningService = require('../../services/cloning');
1437-
1431+
cloneTo(parentNoteId: string): CloneResponse {
14381432
const branch = this.becca.getNote(parentNoteId)?.getParentBranches()[0];
1433+
if (!branch?.branchId) {
1434+
return {
1435+
success: false,
1436+
message: "Unable to find the branch ID to clone."
1437+
};
1438+
}
14391439

1440-
return cloningService.cloneNoteToBranch(this.noteId, branch?.branchId);
1440+
return cloningService.cloneNoteToBranch(this.noteId, branch.branchId);
14411441
}
14421442

14431443
isEligibleForConversionToAttachment(opts: ConvertOpts = { autoConversion: false }) {
@@ -1508,7 +1508,6 @@ class BNote extends AbstractBeccaEntity<BNote> {
15081508

15091509
parentNote.setContent(fixedContent);
15101510

1511-
const noteService = require('../../services/notes');
15121511
noteService.asyncPostProcessContent(parentNote, fixedContent); // to mark an unused attachment for deletion
15131512

15141513
this.deleteNote();
@@ -1535,7 +1534,6 @@ class BNote extends AbstractBeccaEntity<BNote> {
15351534
}
15361535

15371536
// needs to be run before branches and attributes are deleted and thus attached relations disappear
1538-
const handlers = require('../../services/handlers');
15391537
handlers.runAttachedRelations(this, 'runOnNoteDeletion', this);
15401538
taskContext.noteDeletionHandlerTriggered = true;
15411539

src/becca/entities/brevision.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import AbstractBeccaEntity from "./abstract_becca_entity.js";
88
import sql from "../../services/sql.js";
99
import BAttachment from "./battachment.js";
1010
import { AttachmentRow, RevisionRow } from './rows';
11+
import eraseService from "../../services/erase.js";
1112

1213
interface ContentOpts {
1314
/** will also save this BRevision entity */
@@ -164,7 +165,9 @@ class BRevision extends AbstractBeccaEntity<BRevision> {
164165
* Revisions are not soft-deletable, they are immediately hard-deleted (erased).
165166
*/
166167
eraseRevision() {
167-
require('../../services/erase.js').eraseRevisions([this.revisionId]);
168+
if (this.revisionId) {
169+
eraseService.eraseRevisions([this.revisionId]);
170+
}
168171
}
169172

170173
beforeSaving() {

0 commit comments

Comments
 (0)