framework&ui: add serialize guard [skip-cache]

This commit is contained in:
undefined committed 2026-04-17 04:13:18 +08:00
1 parent 3d70cf3352
commit 0890c75c17
5 files changed
+24 -14

No files matched your search

+4 -3
View File
@@ -18,9 +18,10 @@ let { template } = argv.options;
if (!template || typeof template !== 'string') template = findFileSync('@hydrooj/ui-default/templates');
else template = findFileSync(template);
const replacer = (k, v) => {
const replacer = (s) => (k, v) => {
if (k.startsWith('_') && k !== '_id') return undefined;
if (typeof v === 'bigint') return `BigInt::${v.toString()}`;
if (v && typeof v === 'object' && 'serialize' in v && typeof v.serialize === 'function') return v.serialize(s);
return v;
};
@@ -68,7 +69,7 @@ class Nunjucks extends nunjucks.Environment {
callback(error);
}
}, true);
this.addFilter('json', (self) => (self ? JSON.stringify(self, replacer) : ''));
this.addFilter('json', (self, s) => (self ? JSON.stringify(self, replacer(s)) : ''));
this.addFilter('parseYaml', (self) => yaml.load(self));
this.addFilter('dumpYaml', (self) => yaml.dump(self));
this.addFilter('assign', (self, data) => Object.assign(self, data));
@@ -79,7 +80,7 @@ class Nunjucks extends nunjucks.Environment {
this.addFilter('base64_decode', (s) => Buffer.from(s, 'base64').toString());
this.addFilter('jsesc', (self) => jsesc(self, { isScriptContext: true }));
this.addFilter('bitand', (self, val) => self & val);
this.addFilter('toString', (self) => (typeof self === 'string' ? self : JSON.stringify(self, replacer)));
this.addFilter('toString', (self, s) => (typeof self === 'string' ? self : JSON.stringify(self, replacer(s))));
this.addFilter('content', (content, language, html) => {
let s: any = '';
try {