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

Commit 6312bd8

Browse files
authored
Merge pull request #1384 from TriliumNext/chore_lint-share
chore: fix lint issues in `src/share`
2 parents 8f25d4c + d52ca62 commit 6312bd8

File tree

6 files changed

+31
-22
lines changed

6 files changed

+31
-22
lines changed

src/services/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export function processStringOrBuffer(data: string | Buffer | null) {
365365
}
366366
}
367367

368-
export function safeExtractMessageAndStackFromError(err: unknown) {
368+
export function safeExtractMessageAndStackFromError(err: unknown): [errMessage: string, errStack: string | undefined] {
369369
return (err instanceof Error) ? [err.message, err.stack] as const : ["Unknown Error", undefined] as const;
370370
}
371371

src/share/routes.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { beforeAll, beforeEach, describe, expect, it } from "vitest";
22
import supertest from "supertest";
33
import { initializeTranslations } from "../services/i18n.js";
44
import type { Application, Request, Response, NextFunction } from "express";
5+
import { safeExtractMessageAndStackFromError } from "../services/utils.js";
56

67
let app: Application;
78

@@ -11,8 +12,9 @@ describe("Share API test", () => {
1112
beforeAll(async () => {
1213
initializeTranslations();
1314
app = (await import("../app.js")).default;
14-
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
15-
if (err.message.includes("Cannot set headers after they are sent to the client")) {
15+
app.use((err: unknown, req: Request, res: Response, next: NextFunction) => {
16+
const [errMessage] = safeExtractMessageAndStackFromError(err)
17+
if (errMessage.includes("Cannot set headers after they are sent to the client")) {
1618
cannotSetHeadersCount++;
1719
}
1820

src/share/routes.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import log from "../services/log.js";
1515
import type SNote from "./shaca/entities/snote.js";
1616
import type SBranch from "./shaca/entities/sbranch.js";
1717
import type SAttachment from "./shaca/entities/sattachment.js";
18-
import utils from "../services/utils.js";
18+
import utils, { safeExtractMessageAndStackFromError } from "../services/utils.js";
1919
import options from "../services/options.js";
2020

2121
function getSharedSubTreeRoot(note: SNote): { note?: SNote; branch?: SBranch } {
@@ -115,7 +115,14 @@ function renderImageAttachment(image: SNote, res: Response, attachmentName: stri
115115
svgString = content;
116116
} else {
117117
// backwards compatibility, before attachments, the SVG was stored in the main note content as a separate key
118-
const contentSvg = image.getJsonContentSafely()?.svg;
118+
const possibleSvgContent = image.getJsonContentSafely();
119+
120+
const contentSvg = (typeof possibleSvgContent === "object"
121+
&& possibleSvgContent !== null
122+
&& "svg" in possibleSvgContent
123+
&& typeof possibleSvgContent.svg === "string")
124+
? possibleSvgContent.svg
125+
: null;
119126

120127
if (contentSvg) {
121128
svgString = contentSvg;
@@ -184,8 +191,9 @@ function register(router: Router) {
184191
res.send(ejsResult);
185192
useDefaultView = false; // Rendering went okay, don't use default view
186193
}
187-
} catch (e: any) {
188-
log.error(`Rendering user provided share template (${templateId}) threw exception ${e.message} with stacktrace: ${e.stack}`);
194+
} catch (e: unknown) {
195+
const [errMessage, errStack] = safeExtractMessageAndStackFromError(e);
196+
log.error(`Rendering user provided share template (${templateId}) threw exception ${errMessage} with stacktrace: ${errStack}`);
189197
}
190198
}
191199
}
@@ -195,7 +203,7 @@ function register(router: Router) {
195203
}
196204
}
197205

198-
router.get("/share/", (req, res, next) => {
206+
router.get("/share/", (req, res) => {
199207
if (req.path.substr(-1) !== "/") {
200208
res.redirect("../share/");
201209
return;
@@ -211,7 +219,7 @@ function register(router: Router) {
211219
renderNote(shaca.shareRootNote, req, res);
212220
});
213221

214-
router.get("/share/:shareId", (req, res, next) => {
222+
router.get("/share/:shareId", (req, res) => {
215223
shacaLoader.ensureLoad();
216224

217225
const { shareId } = req.params;
@@ -221,7 +229,7 @@ function register(router: Router) {
221229
renderNote(note, req, res);
222230
});
223231

224-
router.get("/share/api/notes/:noteId", (req, res, next) => {
232+
router.get("/share/api/notes/:noteId", (req, res) => {
225233
shacaLoader.ensureLoad();
226234
let note: SNote | boolean;
227235

@@ -234,7 +242,7 @@ function register(router: Router) {
234242
res.json(note.getPojo());
235243
});
236244

237-
router.get("/share/api/notes/:noteId/download", (req, res, next) => {
245+
router.get("/share/api/notes/:noteId/download", (req, res) => {
238246
shacaLoader.ensureLoad();
239247

240248
let note: SNote | boolean;
@@ -256,7 +264,7 @@ function register(router: Router) {
256264
});
257265

258266
// :filename is not used by trilium, but instead used for "save as" to assign a human-readable filename
259-
router.get("/share/api/images/:noteId/:filename", (req, res, next) => {
267+
router.get("/share/api/images/:noteId/:filename", (req, res) => {
260268
shacaLoader.ensureLoad();
261269

262270
let image: SNote | boolean;
@@ -282,7 +290,7 @@ function register(router: Router) {
282290
});
283291

284292
// :filename is not used by trilium, but instead used for "save as" to assign a human-readable filename
285-
router.get("/share/api/attachments/:attachmentId/image/:filename", (req, res, next) => {
293+
router.get("/share/api/attachments/:attachmentId/image/:filename", (req, res) => {
286294
shacaLoader.ensureLoad();
287295

288296
let attachment: SAttachment | boolean;
@@ -300,7 +308,7 @@ function register(router: Router) {
300308
}
301309
});
302310

303-
router.get("/share/api/attachments/:attachmentId/download", (req, res, next) => {
311+
router.get("/share/api/attachments/:attachmentId/download", (req, res) => {
304312
shacaLoader.ensureLoad();
305313

306314
let attachment: SAttachment | boolean;
@@ -322,7 +330,7 @@ function register(router: Router) {
322330
});
323331

324332
// used for PDF viewing
325-
router.get("/share/api/notes/:noteId/view", (req, res, next) => {
333+
router.get("/share/api/notes/:noteId/view", (req, res) => {
326334
shacaLoader.ensureLoad();
327335

328336
let note: SNote | boolean;
@@ -340,19 +348,18 @@ function register(router: Router) {
340348
});
341349

342350
// Used for searching, require noteId so we know the subTreeRoot
343-
router.get("/share/api/notes", (req, res, next) => {
351+
router.get("/share/api/notes", (req, res) => {
344352
shacaLoader.ensureLoad();
345353

346354
const ancestorNoteId = req.query.ancestorNoteId ?? "_share";
347-
let note;
348355

349356
if (typeof ancestorNoteId !== "string") {
350357
res.status(400).json({ message: "'ancestorNoteId' parameter is mandatory." });
351358
return;
352359
}
353360

354361
// This will automatically return if no ancestorNoteId is provided and there is no shareIndex
355-
if (!(note = checkNoteAccess(ancestorNoteId, req, res))) {
362+
if (!checkNoteAccess(ancestorNoteId, req, res)) {
356363
return;
357364
}
358365

src/share/shaca/entities/sattachment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class SAttachment extends AbstractShacaEntity {
4747
}
4848
}
4949

50-
let content = row.content;
50+
const content = row.content;
5151

5252
if (this.hasStringContent()) {
5353
return content === null ? "" : content.toString("utf-8");

src/share/shaca/entities/snote.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class SNote extends AbstractShacaEntity {
105105
}
106106
}
107107

108-
let content = row.content;
108+
const content = row.content;
109109

110110
if (this.hasStringContent()) {
111111
return content === null ? "" : content.toString("utf-8");
@@ -212,7 +212,7 @@ class SNote extends AbstractShacaEntity {
212212
/**
213213
* @throws Error in case of invalid JSON
214214
*/
215-
getJsonContent(): any | null {
215+
getJsonContent(): unknown | null {
216216
const content = this.getContent();
217217

218218
if (typeof content !== "string" || !content || !content.trim()) {

src/share/shaca/shaca_loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function ensureLoad() {
9393

9494
eventService.subscribe(
9595
[eventService.ENTITY_CREATED, eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED, eventService.ENTITY_CHANGE_SYNCED, eventService.ENTITY_DELETE_SYNCED],
96-
({ entityName, entity }) => {
96+
() => {
9797
shaca.reset();
9898
}
9999
);

0 commit comments

Comments
 (0)