try to fix this spaghetti of a code

This commit is contained in:
Frederik Spirgi
2026-09-09 10:59:30 +02:00
parent 729de5dcc4
commit c43845356a
5 changed files with 23 additions and 9 deletions
+7 -2
View File
@@ -1,4 +1,4 @@
const crypto = require('node:crypto');
const crypto = require('node:crypto');
class AppError extends Error {
constructor(code, message, status = 400) { super(message); this.code = code; this.status = status; }
}
@@ -16,4 +16,9 @@ class KeyedQueue {
async drain() { await Promise.all([...this.tails.values()]); }
}
const hash = value => crypto.createHash('sha256').update(value).digest('hex');
module.exports = {AppError, KeyedQueue, hash};
function canonical(value) {
if (Array.isArray(value)) return value.map(canonical);
if (value && typeof value === 'object') return Object.fromEntries(Object.keys(value).sort().map(key => [key, canonical(value[key])]));
return value;
}
module.exports = {AppError, KeyedQueue, hash, canonical};