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

Commit bf29b30

Browse files
committed
server-esm: Fix some more service imports
1 parent c556153 commit bf29b30

File tree

9 files changed

+26
-24
lines changed

9 files changed

+26
-24
lines changed

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() {

src/routes/api/image.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import imageService from "../../services/image.js";
44
import becca from "../../becca/becca.js";
5-
const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
65
import fs from "fs";
76
import { Request, Response } from 'express';
87
import BNote from "../../becca/entities/bnote.js";
98
import BRevision from "../../becca/entities/brevision.js";
109
import { AppRequest } from '../route-interface';
10+
import resource_dir from "../../services/resource_dir.js";
1111

1212
function returnImageFromNote(req: Request, res: Response) {
1313
const image = becca.getNote(req.params.noteId);
@@ -24,7 +24,7 @@ function returnImageFromRevision(req: Request, res: Response) {
2424
function returnImageInt(image: BNote | BRevision | null, res: Response) {
2525
if (!image) {
2626
res.set('Content-Type', 'image/png');
27-
return res.send(fs.readFileSync(`${RESOURCE_DIR}/db/image-deleted.png`));
27+
return res.send(fs.readFileSync(`${resource_dir.RESOURCE_DIR}/db/image-deleted.png`));
2828
} else if (!["image", "canvas", "mermaid"].includes(image.type)) {
2929
return res.sendStatus(400);
3030
}
@@ -68,7 +68,7 @@ function returnAttachedImage(req: Request, res: Response) {
6868

6969
if (!attachment) {
7070
res.set('Content-Type', 'image/png');
71-
return res.send(fs.readFileSync(`${RESOURCE_DIR}/db/image-deleted.png`));
71+
return res.send(fs.readFileSync(`${resource_dir.RESOURCE_DIR}/db/image-deleted.png`));
7272
}
7373

7474
if (!["image"].includes(attachment.role)) {

src/routes/api/sync.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import ws from "../../services/ws.js";
1414
import { Request } from 'express';
1515
import { EntityChange, EntityChangeRecord } from '../../services/entity_changes_interface';
1616
import ValidationError from "../../errors/validation_error.js";
17+
import consistencyChecksService from "../../services/consistency_checks.js";
1718

1819
async function testSync() {
1920
try {
@@ -206,7 +207,7 @@ function queueSector(req: Request) {
206207
}
207208

208209
function checkEntityChanges() {
209-
require('../../services/consistency_checks').runEntityChangesChecks();
210+
consistencyChecksService.runEntityChangesChecks();
210211
}
211212

212213
export default {

src/services/entity_changes.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import becca from "../becca/becca.js";
88
import blobService from "../services/blob.js";
99
import { EntityChange } from './entity_changes_interface';
1010
import type { Blob } from "./blob-interface";
11+
import eventService from "./events.js";
1112

1213
let maxEntityChangeId = 0;
1314

@@ -57,8 +58,6 @@ function putNoteReorderingEntityChange(parentNoteId: string, componentId?: strin
5758
instanceId
5859
});
5960

60-
const eventService = require('./events');
61-
6261
eventService.emit(eventService.ENTITY_CHANGED, {
6362
entityName: 'note_reordering',
6463
entity: sql.getMap(`SELECT branchId, notePosition FROM branches WHERE isDeleted = 0 AND parentNoteId = ?`, [parentNoteId])

src/services/export/zip.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import protectedSessionService from "../protected_session.js";
1111
import sanitize from "sanitize-filename";
1212
import fs from "fs";
1313
import becca from "../../becca/becca.js";
14-
const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
1514
import archiver from "archiver";
1615
import log from "../log.js";
1716
import TaskContext from "../task_context.js";
@@ -21,6 +20,7 @@ import AttachmentMeta from "../meta/attachment_meta.js";
2120
import AttributeMeta from "../meta/attribute_meta.js";
2221
import BBranch from "../../becca/entities/bbranch.js";
2322
import { Response } from 'express';
23+
import resource_dir from "../resource_dir.js";
2424

2525
async function exportToZip(taskContext: TaskContext, branch: BBranch, format: "html" | "markdown", res: Response | fs.WriteStream, setHeaders = true) {
2626
if (!['html', 'markdown'].includes(format)) {
@@ -473,7 +473,7 @@ ${markdownContent}`;
473473
}
474474

475475
function saveCss(rootMeta: NoteMeta, cssMeta: NoteMeta) {
476-
const cssContent = fs.readFileSync(`${RESOURCE_DIR}/libraries/ckeditor/ckeditor-content.css`);
476+
const cssContent = fs.readFileSync(`${resource_dir.RESOURCE_DIR}/libraries/ckeditor/ckeditor-content.css`);
477477

478478
archive.append(cssContent, { name: cssMeta.dataFileName });
479479
}

src/services/image.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ function saveImageToAttachment(noteId: string, uploadBuffer: Buffer, originalNam
156156
setTimeout(() => {
157157
sql.transactional(() => {
158158
const note = becca.getNoteOrThrow(noteId);
159-
const noteService = require('../services/notes');
160159
noteService.asyncPostProcessContent(note, note.getContent()); // to mark an unused attachment for deletion
161160
});
162161
}, 5000);

src/services/notes.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import html2plaintext from "html2plaintext";
2626
import { AttachmentRow, AttributeRow, BranchRow, NoteRow, NoteType } from '../becca/entities/rows';
2727
import TaskContext from "./task_context.js";
2828
import { NoteParams } from './note-interface';
29+
import imageService from "./image.js";
2930

3031
interface FoundLink {
3132
name: "imageLink" | "internalLink" | "includeNoteLink" | "relationMapLink",
@@ -466,7 +467,7 @@ async function downloadImage(noteId: string, imageUrl: string) {
466467
const unescapedUrl = utils.unescapeHtml(imageUrl);
467468

468469
try {
469-
let imageBuffer;
470+
let imageBuffer: Buffer;
470471

471472
if (imageUrl.toLowerCase().startsWith("file://")) {
472473
imageBuffer = await new Promise((res, rej) => {
@@ -487,10 +488,13 @@ async function downloadImage(noteId: string, imageUrl: string) {
487488
const parsedUrl = url.parse(unescapedUrl);
488489
const title = path.basename(parsedUrl.pathname || "");
489490

490-
const imageService = require('../services/image');
491491
const attachment = imageService.saveImageToAttachment(noteId, imageBuffer, title, true, true);
492-
493-
imageUrlToAttachmentIdMapping[imageUrl] = attachment.attachmentId;
492+
493+
if (attachment.attachmentId) {
494+
imageUrlToAttachmentIdMapping[imageUrl] = attachment.attachmentId;
495+
} else {
496+
log.error(`Download of '${imageUrl}' due to no attachment ID.`);
497+
}
494498

495499
log.info(`Download of '${imageUrl}' succeeded and was saved as image attachment '${attachment.attachmentId}' of note '${noteId}'`);
496500
}
@@ -520,7 +524,6 @@ function downloadImages(noteId: string, content: string) {
520524
const imageBase64 = url.substr(inlineImageMatch[0].length);
521525
const imageBuffer = Buffer.from(imageBase64, 'base64');
522526

523-
const imageService = require('../services/image');
524527
const attachment = imageService.saveImageToAttachment(noteId, imageBuffer, "inline image", true, true);
525528

526529
const encodedTitle = encodeURIComponent(attachment.title);

src/services/request.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function exec<T>(opts: ExecOpts): Promise<T> {
137137
});
138138
}
139139

140-
function getImage(imageUrl: string) {
140+
function getImage(imageUrl: string): Promise<Buffer> {
141141
const proxyConf = syncOptions.getSyncProxy();
142142
const opts: ClientOpts = {
143143
method: 'GET',
@@ -149,7 +149,7 @@ function getImage(imageUrl: string) {
149149
const proxyAgent = getProxyAgent(opts);
150150
const parsedTargetUrl = url.parse(opts.url);
151151

152-
return new Promise((resolve, reject) => {
152+
return new Promise<Buffer>((resolve, reject) => {
153153
try {
154154
const request = client.request({
155155
method: opts.method,
@@ -181,8 +181,7 @@ function getImage(imageUrl: string) {
181181
});
182182

183183
request.end(undefined);
184-
}
185-
catch (e: any) {
184+
} catch (e: any) {
186185
reject(generateError(opts, e.message));
187186
}
188187
});

src/services/sql_init.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import migrationService from "./migration.js";
1111
import cls from "./cls.js";
1212
import config from "./config.js";
1313
import { OptionRow } from '../becca/entities/rows';
14+
import optionsInitService from "./options_init.js";
15+
import BNote from "../becca/entities/bnote.js";
16+
import BBranch from "../becca/entities/bbranch.js";
1417

1518
const dbReady = utils.deferred<void>();
1619

@@ -63,9 +66,6 @@ async function createInitialDatabase() {
6366

6467
require('../becca/becca_loader').load();
6568

66-
const BNote = require('../becca/entities/bnote');
67-
const BBranch = require('../becca/entities/bbranch');
68-
6969
log.info("Creating root note ...");
7070

7171
rootNote = new BNote({
@@ -84,8 +84,6 @@ async function createInitialDatabase() {
8484
notePosition: 10
8585
}).save();
8686

87-
const optionsInitService = require('./options_init');
88-
8987
optionsInitService.initDocumentOptions();
9088
optionsInitService.initNotSyncedOptions(true, {});
9189
optionsInitService.initStartupOptions();

0 commit comments

Comments
 (0)