From 3697423fda7dfb91e035194915df3a8d7d6b8856 Mon Sep 17 00:00:00 2001 From: Isaac Date: Sun, 4 Sep 2022 19:55:32 +0100 Subject: [PATCH] Fix sqlite middleware --- src/http.js | 13 ++++++------- src/index.js | 3 +-- src/lib/middleware/prisma-sqlite.js | 12 ++++++++++-- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/http.js b/src/http.js index 4c71c2f..8880f06 100644 --- a/src/http.js +++ b/src/http.js @@ -6,8 +6,7 @@ const { join } = require('path'); const { files } = require('node-dir'); module.exports = client => { - - // cors plugins + // cors plugin fastify.register(require('@fastify/cors'), { credentials: true, methods: ['DELETE', 'GET', 'PATCH', 'PUT', 'POST'], @@ -112,13 +111,13 @@ module.exports = client => { : res.statusCode >= 200 ? '&2' : '&f') + res.statusCode; - let response_time = res.getResponseTime().toFixed(2); - response_time = (response_time >= 20 + let responseTime = res.getResponseTime().toFixed(2); + responseTime = (responseTime >= 20 ? '&c' - : response_time >= 5 + : responseTime >= 5 ? '&e' - : '&a') + response_time + 'ms'; - client.log.info.http(short(`${req.ip} ${req.method} ${req.routerPath ?? '*'} &m-+>&r ${status}&b in ${response_time}`)); + : '&a') + responseTime + 'ms'; + client.log.info.http(short(`${req.ip} ${req.method} ${req.routerPath ?? '*'} &m-+>&r ${status}&b in ${responseTime}`)); done(); }); diff --git a/src/index.js b/src/index.js index 8019b3b..35b5791 100644 --- a/src/index.js +++ b/src/index.js @@ -21,7 +21,6 @@ * @license GNU-GPLv3 */ - const pkg = require('../package.json'); const fs = require('fs'); const semver = require('semver'); @@ -42,7 +41,7 @@ if (!semver.satisfies(process.versions.node, pkg.engines.node)) { } if (process.env.ENCRYPTION_KEY === undefined) { - console.log('\x07' + colours.redBright('Error: The "ENCRYPTION_KEY" environment variable is not set.\nRun "npm run keygen" to generate a key, or set it to "false" to disable encryption (not recommended).')); + console.log('\x07' + colours.redBright('Error: The "ENCRYPTION_KEY" environment variable is not set.\nRun "npm run keygen" to generate a key.')); process.exit(1); } diff --git a/src/lib/middleware/prisma-sqlite.js b/src/lib/middleware/prisma-sqlite.js index 7606ee4..776b647 100644 --- a/src/lib/middleware/prisma-sqlite.js +++ b/src/lib/middleware/prisma-sqlite.js @@ -17,8 +17,16 @@ const traverse = (obj, action) => { prop = 'create'; traverse(obj[prop], action); } else if (jsonFields.includes(prop) && obj[prop] !== null && obj[prop] !== undefined) { - if (action === 'SERIALISE' && typeof obj[prop] !== 'string') { - obj[prop] = JSON.stringify(obj[prop]); + if (action === 'SERIALISE') { + if (typeof obj[prop] === 'string') { + try { + JSON.parse(obj[prop]); + } catch { + obj[prop] = JSON.stringify(obj[prop]); + } + } else { + obj[prop] = JSON.stringify(obj[prop]); + } } else if (action === 'PARSE' && typeof obj[prop] === 'string') { obj[prop] = JSON.parse(obj[prop]); }