mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
framework&ui: add serialize guard [skip-cache]
This commit is contained in:
5 files changed
+24
-14
No files matched your search
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user