core: fix bigint serializer

This commit is contained in:
undefined committed 2021-04-22 22:56:54 +08:00
1 parent f42393f7af
commit b6a6f46d20
5 files changed
+16 -25

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.23.20", "version": "2.23.21",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "dist/loader.js", "main": "dist/loader.js",
"typings": "dist/loader.d.ts", "typings": "dist/loader.d.ts",
+1 -5
View File
@@ -22,11 +22,7 @@ class MessageModel {
const mdoc = { const mdoc = {
from, to, content, _id: res.insertedId, flag, from, to, content, _id: res.insertedId, flag,
}; };
if (from !== to) { if (from !== to) bus.boardcast('user/message', to, mdoc);
// ENHANCE domainId?
const udoc = await user.getById('system', to);
bus.boardcast('user/message', to, mdoc, udoc);
}
await user.inc(to, 'unreadMsg', 1); await user.inc(to, 'unreadMsg', 1);
return mdoc; return mdoc;
} }
+5 -6
View File
@@ -5,26 +5,25 @@ import db from '../service/db';
const coll = db.collection('opcount'); const coll = db.collection('opcount');
export async function inc(op: string, ident: string, periodSecs: number, maxOperations: number) { export async function inc(op: string, ident: string, periodSecs: number, maxOperations: number) {
const curTime = new Date().getTime(); const now = new Date().getTime();
const beginAt = new Date(curTime - (curTime % (periodSecs * 1000))); const expireAt = new Date(now - (now % (periodSecs * 1000)) + periodSecs * 1000);
const expireAt = new Date(beginAt.getTime() + periodSecs * 1000);
try { try {
await coll.findOneAndUpdate({ await coll.findOneAndUpdate({
op, op,
ident, ident,
beginAt,
expireAt, expireAt,
opcount: { $lt: maxOperations }, opcount: { $lt: maxOperations },
}, { $inc: { opcount: 1 } }, { upsert: true }); }, { $inc: { opcount: 1 } }, { upsert: true });
} catch (e) { } catch (e) {
throw new OpcountExceededError(op, periodSecs, maxOperations); if (e.message.includes('duplicate')) throw new OpcountExceededError(op, periodSecs, maxOperations);
throw e;
} }
} }
function ensureIndexes() { function ensureIndexes() {
return Promise.all([ return Promise.all([
coll.createIndex('expireAt', { expireAfterSeconds: 0 }), coll.createIndex('expireAt', { expireAfterSeconds: 0 }),
coll.createIndex({ op: 1, ident: 1 }, { unique: true }), coll.createIndex({ op: 1, ident: 1, expireAt: 1 }, { unique: true }),
]); ]);
} }
+1 -1
View File
@@ -45,7 +45,7 @@ export interface EventMap extends Record<string, any> {
'monitor/update': (type: 'server' | 'judge', $set: any) => VoidReturn 'monitor/update': (type: 'server' | 'judge', $set: any) => VoidReturn
'user/message': (uid: number, mdoc: Mdoc, udoc: User) => void 'user/message': (uid: number, mdoc: Mdoc) => void
'user/get': (udoc: User) => void 'user/get': (udoc: User) => void
'domain/create': (ddoc: DomainDoc) => VoidReturn 'domain/create': (ddoc: DomainDoc) => VoidReturn
+8 -12
View File
@@ -446,7 +446,7 @@ export class Handler extends HandlerCommon {
const [xff, xhost] = system.getMany(['server.xff', 'server.xhost']); const [xff, xhost] = system.getMany(['server.xff', 'server.xhost']);
if (xff) this.request.ip = this.request.headers[xff.toLowerCase()] || this.request.ip; if (xff) this.request.ip = this.request.headers[xff.toLowerCase()] || this.request.ip;
if (xhost) this.request.host = this.request.headers[xhost.toLowerCase()] || this.request.host; if (xhost) this.request.host = this.request.headers[xhost.toLowerCase()] || this.request.host;
this.noCheckPermView = true; this.noCheckPermView = false;
} }
// eslint-disable-next-line class-methods-use-this // eslint-disable-next-line class-methods-use-this
@@ -468,7 +468,7 @@ export class Handler extends HandlerCommon {
this.response.body = data; this.response.body = data;
this.response.template = null; this.response.template = null;
this.response.type = 'application/octet-stream'; this.response.type = 'application/octet-stream';
this.response.disposition = `attachment; filename="${name}"`; if (name) this.response.disposition = `attachment; filename="${name}"`;
} }
async getSession() { async getSession() {
@@ -477,19 +477,15 @@ export class Handler extends HandlerCommon {
if (!this.session) this.session = { uid: 0 }; if (!this.session) this.session = { uid: 0 };
} }
async getBdoc() {
const bdoc = await blacklist.get(`ip::${this.request.ip}`);
if (bdoc) throw new BlacklistedError(this.request.ip);
}
async init({ domainId }) { async init({ domainId }) {
if (!argv.benchmark) await this.limitRate('global', 10, 1); if (!argv.benchmark) await this.limitRate('global', 10, 88);
const [absoluteDomain, inferDomain] = await Promise.all([ const [absoluteDomain, inferDomain, bdoc] = await Promise.all([
domain.get(domainId), domain.get(domainId),
domain.getByHost(this.request.host), domain.getByHost(this.request.host),
blacklist.get(`ip::${this.request.ip}`),
this.getSession(), this.getSession(),
this.getBdoc(),
]); ]);
if (bdoc) throw new BlacklistedError(this.request.ip);
if (inferDomain && !this.request.path.startsWith('/d/')) { if (inferDomain && !this.request.path.startsWith('/d/')) {
this.domainId = inferDomain._id; this.domainId = inferDomain._id;
this.args.domainId = inferDomain._id; this.args.domainId = inferDomain._id;
@@ -553,7 +549,7 @@ export class Handler extends HandlerCommon {
this.request.json || this.response.redirect this.request.json || this.response.redirect
|| this.request.query.noTemplate || !this.response.template) { || this.request.query.noTemplate || !this.response.template) {
try { try {
this.response.body = JSON.stringify(this.response.body, serializer, 2); this.response.body = JSON.stringify(this.response.body, serializer);
} catch (e) { } catch (e) {
this.response.body = new SystemError('Serialize failure', e.message); this.response.body = new SystemError('Serialize failure', e.message);
} }
@@ -738,7 +734,7 @@ export class ConnectionHandler extends HandlerCommon {
} }
send(data: any) { send(data: any) {
this.conn.write(JSON.stringify(data)); this.conn.write(JSON.stringify(data, serializer));
} }
close(code: number, reason: string) { close(code: number, reason: string) {