Skip to content

Build your personal knowledge base with Trilium Notes

fix(server): requests failing due to cls namespacing issue

Elian Doran527f5020835b
diff --git a/apps/server/src/cls_provider.ts b/apps/server/src/cls_provider.ts
index 3bac6352c6..a11f616129 100644
--- a/apps/server/src/cls_provider.ts
+++ b/apps/server/src/cls_provider.ts
@@ -1,7 +1,7 @@
import { ExecutionContext } from "@triliumnext/core";
import clsHooked from "cls-hooked";
-const namespace = clsHooked.createNamespace("trilium");
+export const namespace = clsHooked.createNamespace("trilium");
export default class ClsHookedExecutionContext implements ExecutionContext {
diff --git a/apps/server/src/etapi/etapi_utils.ts b/apps/server/src/etapi/etapi_utils.ts
index 1319162575..f018975ecb 100644
--- a/apps/server/src/etapi/etapi_utils.ts
+++ b/apps/server/src/etapi/etapi_utils.ts
@@ -1,12 +1,14 @@
-import cls from "../services/cls.js";
-import sql from "../services/sql.js";
-import log from "../services/log.js";
+import type { NextFunction, Request, RequestHandler, Response, Router } from "express";
+
import becca from "../becca/becca.js";
-import etapiTokenService from "../services/etapi_tokens.js";
+import { namespace } from "../cls_provider.js";
+import type { ApiRequestHandler, SyncRouteRequestHandler } from "../routes/route_api.js";
+import cls from "../services/cls.js";
import config from "../services/config.js";
-import type { NextFunction, Request, RequestHandler, Response, Router } from "express";
+import etapiTokenService from "../services/etapi_tokens.js";
+import log from "../services/log.js";
+import sql from "../services/sql.js";
import type { ValidatorMap } from "./etapi-interface.js";
-import type { ApiRequestHandler, SyncRouteRequestHandler } from "../routes/route_api.js";
const GENERIC_CODE = "GENERIC";
type HttpMethod = "all" | "get" | "post" | "put" | "delete" | "patch" | "options" | "head";
@@ -35,8 +37,8 @@ function sendError(res: Response, statusCode: number, code: string, message: str
.send(
JSON.stringify({
status: statusCode,
- code: code,
- message: message
+ code,
+ message
})
);
}
@@ -51,8 +53,8 @@ function checkEtapiAuth(req: Request, res: Response, next: NextFunction) {
function processRequest(req: Request, res: Response, routeHandler: ApiRequestHandler, next: NextFunction, method: string, path: string) {
try {
- cls.namespace.bindEmitter(req);
- cls.namespace.bindEmitter(res);
+ namespace.bindEmitter(req);
+ namespace.bindEmitter(res);
cls.init(() => {
cls.set("componentId", "etapi");
@@ -86,9 +88,9 @@ function getAndCheckNote(noteId: string) {
if (note) {
return note;
- } else {
- throw new EtapiError(404, "NOTE_NOT_FOUND", `Note '${noteId}' not found.`);
- }
+ }
+ throw new EtapiError(404, "NOTE_NOT_FOUND", `Note '${noteId}' not found.`);
+
}
function getAndCheckAttachment(attachmentId: string) {
@@ -96,9 +98,9 @@ function getAndCheckAttachment(attachmentId: string) {
if (attachment) {
return attachment;
- } else {
- throw new EtapiError(404, "ATTACHMENT_NOT_FOUND", `Attachment '${attachmentId}' not found.`);
- }
+ }
+ throw new EtapiError(404, "ATTACHMENT_NOT_FOUND", `Attachment '${attachmentId}' not found.`);
+
}
function getAndCheckBranch(branchId: string) {
@@ -106,9 +108,9 @@ function getAndCheckBranch(branchId: string) {
if (branch) {
return branch;
- } else {
- throw new EtapiError(404, "BRANCH_NOT_FOUND", `Branch '${branchId}' not found.`);
- }
+ }
+ throw new EtapiError(404, "BRANCH_NOT_FOUND", `Branch '${branchId}' not found.`);
+
}
function getAndCheckAttribute(attributeId: string) {
@@ -116,9 +118,9 @@ function getAndCheckAttribute(attributeId: string) {
if (attribute) {
return attribute;
- } else {
- throw new EtapiError(404, "ATTRIBUTE_NOT_FOUND", `Attribute '${attributeId}' not found.`);
- }
+ }
+ throw new EtapiError(404, "ATTRIBUTE_NOT_FOUND", `Attribute '${attributeId}' not found.`);
+
}
function validateAndPatch(target: any, source: any, allowedProperties: ValidatorMap) {
diff --git a/apps/server/src/routes/custom.ts b/apps/server/src/routes/custom.ts
index b27a710f2e..5498ef6d06 100644
--- a/apps/server/src/routes/custom.ts
+++ b/apps/server/src/routes/custom.ts
@@ -1,11 +1,13 @@
+import type { Request, Response, Router } from "express";
+
+import becca from "../becca/becca.js";
+import { namespace } from "../cls_provider.js";
+import cls from "../services/cls.js";
import log from "../services/log.js";
-import fileService from "./api/files.js";
import scriptService from "../services/script.js";
-import cls from "../services/cls.js";
import sql from "../services/sql.js";
-import becca from "../becca/becca.js";
-import type { Request, Response, Router } from "express";
-import { safeExtractMessageAndStackFromError, normalizeCustomHandlerPattern } from "../services/utils.js";
+import { normalizeCustomHandlerPattern,safeExtractMessageAndStackFromError } from "../services/utils.js";
+import fileService from "./api/files.js";
function handleRequest(req: Request, res: Response) {
@@ -27,7 +29,7 @@ function handleRequest(req: Request, res: Response) {
// splitPath.map(segment => encodeURIComponent(segment)).join("/")
// might be safer
- const path = splitPath.join("/")
+ const path = splitPath.join("/");
const attributeIds = sql.getColumn<string>("SELECT attributeId FROM attributes WHERE isDeleted = 0 AND type = 'label' AND name IN ('customRequestHandler', 'customResourceProvider')");
@@ -96,8 +98,8 @@ function register(router: Router) {
// explicitly no CSRF middleware since it's meant to allow integration from external services
router.all("/custom/*path", (req: Request, res: Response, _next) => {
- cls.namespace.bindEmitter(req);
- cls.namespace.bindEmitter(res);
+ namespace.bindEmitter(req);
+ namespace.bindEmitter(res);
cls.init(() => handleRequest(req, res));
});
diff --git a/apps/server/src/routes/route_api.ts b/apps/server/src/routes/route_api.ts
index 1b4ea48f24..6f33260740 100644
--- a/apps/server/src/routes/route_api.ts
+++ b/apps/server/src/routes/route_api.ts
@@ -1,15 +1,17 @@
import express, { type RequestHandler } from "express";
import multer from "multer";
-import log from "../services/log.js";
-import cls from "../services/cls.js";
-import sql from "../services/sql.js";
-import entityChangesService from "../services/entity_changes.js";
+
import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.js";
+import { namespace } from "../cls_provider.js";
import NotFoundError from "../errors/not_found_error.js";
import ValidationError from "../errors/validation_error.js";
import auth from "../services/auth.js";
-import { doubleCsrfProtection as csrfMiddleware } from "./csrf_protection.js";
+import cls from "../services/cls.js";
+import entityChangesService from "../services/entity_changes.js";
+import log from "../services/log.js";
+import sql from "../services/sql.js";
import { safeExtractMessageAndStackFromError } from "../services/utils.js";
+import { doubleCsrfProtection as csrfMiddleware } from "./csrf_protection.js";
const MAX_ALLOWED_FILE_SIZE_MB = 250;
export const router = express.Router();
@@ -67,9 +69,9 @@ export function apiResultHandler(req: express.Request, res: express.Response, re
return send(res, statusCode, response);
} else if (result === undefined) {
return send(res, 204, "");
- } else {
- return send(res, 200, result);
}
+ return send(res, 200, result);
+
}
function send(res: express.Response, statusCode: number, response: unknown) {
@@ -81,14 +83,14 @@ function send(res: express.Response, statusCode: number, response: unknown) {
res.status(statusCode).send(response);
return response.length;
- } else {
- const json = JSON.stringify(response);
+ }
+ const json = JSON.stringify(response);
- res.setHeader("Content-Type", "application/json");
- res.status(statusCode).send(json);
+ res.setHeader("Content-Type", "application/json");
+ res.status(statusCode).send(json);
+
+ return json.length;
- return json.length;
- }
}
export function apiRoute(method: HttpMethod, path: string, routeHandler: SyncRouteRequestHandler) {
@@ -112,8 +114,8 @@ function internalRoute(method: HttpMethod, path: string, middleware: express.Han
const start = Date.now();
try {
- cls.namespace.bindEmitter(req);
- cls.namespace.bindEmitter(res);
+ namespace.bindEmitter(req);
+ namespace.bindEmitter(res);
const result = cls.init(() => {
cls.set("componentId", req.headers["trilium-component-id"]);
@@ -193,7 +195,7 @@ export function createUploadMiddleware(): RequestHandler {
const uploadMiddleware = createUploadMiddleware();
export const uploadMiddlewareWithErrorHandling = function (req: express.Request, res: express.Response, next: express.NextFunction) {
- uploadMiddleware(req, res, function (err) {
+ uploadMiddleware(req, res, (err) => {
if (err?.code === "LIMIT_FILE_SIZE") {
res.setHeader("Content-Type", "text/plain").status(400).send(`Cannot upload file because it excceeded max allowed file size of ${MAX_ALLOWED_FILE_SIZE_MB} MiB`);
} else {