fixed leaking entities into the responses and thus increased performance of e.g. create/duplicate note

This commit is contained in:
zadam 2021-10-06 20:52:23 +02:00
parent df8706026e
commit 711af02928
3 changed files with 49 additions and 13 deletions

View file

@ -156,6 +156,10 @@ if (utils.isElectron()) {
} }
if (arg.statusCode >= 200 && arg.statusCode < 300) { if (arg.statusCode >= 200 && arg.statusCode < 300) {
if (arg.headers['Content-Type'] === 'application/json') {
arg.body = JSON.parse(arg.body);
}
reqResolves[arg.requestId]({ reqResolves[arg.requestId]({
body: arg.body, body: arg.body,
headers: arg.headers headers: arg.headers

View file

@ -57,9 +57,8 @@ const csrfMiddleware = csurf({
path: '' // nothing so cookie is valid only for current path path: '' // nothing so cookie is valid only for current path
}); });
function apiResultHandler(req, res, result) { /** Handling common patterns. If entity is not caught, serialization to JSON will fail */
res.setHeader('trilium-max-entity-change-id', entityChangesService.getMaxEntityChangeId()); function convertEntitiesToPojo(result) {
if (result instanceof AbstractEntity) { if (result instanceof AbstractEntity) {
result = result.getPojo(); result = result.getPojo();
} }
@ -70,22 +69,55 @@ function apiResultHandler(req, res, result) {
} }
} }
} }
else {
if (result && result.note instanceof AbstractEntity) {
result.note = result.note.getPojo();
}
if (result && result.branch instanceof AbstractEntity) {
result.branch = result.branch.getPojo();
}
}
return result;
}
function apiResultHandler(req, res, result) {
res.setHeader('trilium-max-entity-change-id', entityChangesService.getMaxEntityChangeId());
result = convertEntitiesToPojo(result);
// if it's an array and first element is integer then we consider this to be [statusCode, response] format // if it's an array and first element is integer then we consider this to be [statusCode, response] format
if (Array.isArray(result) && result.length > 0 && Number.isInteger(result[0])) { if (Array.isArray(result) && result.length > 0 && Number.isInteger(result[0])) {
const [statusCode, response] = result; const [statusCode, response] = result;
res.status(statusCode).send(response);
if (statusCode !== 200 && statusCode !== 201 && statusCode !== 204) { if (statusCode !== 200 && statusCode !== 201 && statusCode !== 204) {
log.info(`${req.method} ${req.originalUrl} returned ${statusCode} with response ${JSON.stringify(response)}`); log.info(`${req.method} ${req.originalUrl} returned ${statusCode} with response ${JSON.stringify(response)}`);
} }
return send(res, statusCode, response);
} }
else if (result === undefined) { else if (result === undefined) {
res.status(204).send(); return send(res, 204, "");
} }
else { else {
res.send(result); return send(res, 200, result);
}
}
function send(res, statusCode, response) {
if (typeof response === 'string') {
res.status(statusCode, response);
return response.length;
}
else {
const json = JSON.stringify(response);
res.setHeader("Content-Type", "application/json");
res.status(statusCode).send(json);
return json.length;
} }
} }
@ -115,9 +147,9 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio
if (result && result.then) { if (result && result.then) {
result result
.then(actualResult => { .then(actualResult => {
resultHandler(req, res, actualResult); const responseLength = resultHandler(req, res, actualResult);
log.request(req, res, Date.now() - start); log.request(req, res, Date.now() - start, responseLength);
}) })
.catch(e => { .catch(e => {
log.error(`${method} ${path} threw exception: ` + e.stack); log.error(`${method} ${path} threw exception: ` + e.stack);
@ -126,9 +158,9 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio
}); });
} }
else { else {
resultHandler(req, res, result); const responseLength = resultHandler(req, res, result);
log.request(req, res, Date.now() - start); log.request(req, res, Date.now() - start, responseLength);
} }
} }
} }

View file

@ -68,7 +68,7 @@ function error(message) {
const requestBlacklist = [ "/libraries", "/app", "/images", "/stylesheets" ]; const requestBlacklist = [ "/libraries", "/app", "/images", "/stylesheets" ];
function request(req, res, timeMs) { function request(req, res, timeMs, responseLength = "?") {
for (const bl of requestBlacklist) { for (const bl of requestBlacklist) {
if (req.url.startsWith(bl)) { if (req.url.startsWith(bl)) {
return; return;
@ -80,7 +80,7 @@ function request(req, res, timeMs) {
} }
info((timeMs >= 10 ? "Slow " : "") + info((timeMs >= 10 ? "Slow " : "") +
res.statusCode + " " + req.method + " " + req.url + " took " + timeMs + "ms"); `${res.statusCode} ${req.method} ${req.url} with ${responseLength} bytes took ${timeMs}ms`);
} }
function pad(num) { function pad(num) {