webpack hot reload support

This commit is contained in:
undefined committed 2020-07-17 19:20:42 +08:00
1 parent d8e1a63f99
commit 0ee306d0cf
15 files changed
+89 -63

No files matched your search

-2
View File
@@ -220,11 +220,9 @@ export async function wiki(pending: string[], fail: string[]) {
try {
const categories = fs.readdirSync(p);
for (const category of categories) {
console.log('category', category);
if (!fs.statSync(p).isDirectory()) return;
const pages = fs.readdirSync(path.join(p, category));
if (!global.Hydro.wiki[category]) global.Hydro.wiki[category] = {};
console.log(pages);
for (const page of pages) {
const c = fs.readFileSync(path.join(p, category, page)).toString().split('\n');
const pagename = page.split('.')[0];
+9 -9
View File
@@ -57,9 +57,9 @@ class SystemCheckConnHandler extends ConnectionHandler {
}
async check() {
const log = (payload) => this.send({ type: 'log', payload });
const warn = (payload) => this.send({ type: 'warn', payload });
const error = (payload) => this.send({ type: 'error', payload });
const log = (payload: any) => this.send({ type: 'log', payload });
const warn = (payload: any) => this.send({ type: 'warn', payload });
const error = (payload: any) => this.send({ type: 'error', payload });
await check.start(log, warn, error, (id) => { this.id = id; });
}
@@ -108,10 +108,10 @@ class SystemScriptHandler extends SystemHandler {
args = JSON.parse(args);
validate(global.Hydro.script[id].validate, args);
const rid = await record.add(domainId, {
pid: 1,
pid: -1,
uid: this.user._id,
lang: null,
code: null,
lang: '-',
code: id,
status: STATUS.STATUS_JUDGING,
hidden: true,
}, false);
@@ -122,7 +122,7 @@ class SystemScriptHandler extends SystemHandler {
setTimeout(() => {
const start = new Date().getTime();
global.Hydro.script[id].run(args, report)
.then((ret) => {
.then((ret: any) => {
const time = new Date().getTime() - start;
judge.end({
domainId,
@@ -146,7 +146,7 @@ class SystemScriptHandler extends SystemHandler {
memory_kb: 0,
});
});
}, 500);
}, 300);
this.response.body = { rid };
this.response.redirect = this.url('record_detail', { rid });
}
@@ -192,4 +192,4 @@ async function apply() {
Connection('manage_check', '/manage/check-conn', SystemCheckConnHandler);
}
global.Hydro.handler.manage = module.exports = apply;
global.Hydro.handler.manage = apply;
+7 -7
View File
@@ -201,15 +201,15 @@ class UserSearchHandler extends Handler {
@param('q', Types.String)
@param('exectMatch', Types.Boolean, true)
async get(domainId: string, q: string, exactMatch = false) {
let udocs;
if (exactMatch) udocs = [];
else udocs = await user.getPrefixList(q, 20);
const udoc = await user.getById(domainId, parseInt(q, 10));
let udoc = await user.getById(domainId, parseInt(q, 10));
const udocs = udoc ? [udoc] : [];
udoc = await user.getByUname(domainId, q);
if (udoc) udocs.push(udoc);
udoc = await user.getByEmail(domainId, q);
if (udoc) udocs.push(udoc);
if (!exactMatch) udocs.push(...await user.getPrefixList(q, 20));
for (const i in udocs) {
if (udocs[i].gravatar) {
udocs[i].gravatar = misc.gravatar(udocs[i].gravatar);
}
udocs[i].gravatar = misc.gravatar(udocs[i].gravatar || '');
}
this.response.body = udocs;
}
+12 -1
View File
@@ -90,7 +90,18 @@ class RatingCalculator {
}
}
function calculate(users: any[]) {
interface RatingInputUser {
old: number,
uid: number,
rank: number,
}
interface RatingOutputUser {
new: number,
uid: number,
}
function calculate(users: RatingInputUser[]): RatingOutputUser[] {
const calculator = new RatingCalculator(users);
return calculator.calculate();
}
+5 -18
View File
@@ -23,36 +23,23 @@ function calc(udict: ND, rankedDocs: [number, number][]) {
}
}
async function runProblem(pdoc: Pdoc, udict: ND, report: Function): Promise<void>
async function runProblem(
domainId: string, pid: number, udict: ND, report: Function
): Promise<void>
async function runProblem(pdoc: Pdoc, udict: ND): Promise<void>
async function runProblem(domainId: string, pid: number, udict: ND): Promise<void>
async function runProblem(...arg: Array<any>) {
const start = new Date().getTime();
const pdoc: Pdoc = (typeof arg[0] === 'string')
? await contest.get(arg[0], arg[1], -1)
: arg[0];
const udict: ND = (typeof arg[0] === 'string') ? arg[2] : arg[1];
const report = (typeof arg[0] === 'string') ? arg[3] : arg[2];
// TODO maybe some other rules?
// TODO pagination
const psdocs = await problem.getMultiStatus(
pdoc.domainId, { docId: pdoc.docId, status: STATUS.STATUS_ACCEPTED },
).sort('rid', 1).toArray();
const ranked = [];
for (const index of psdocs) {
for (const index in psdocs) {
ranked.push([index + 1, psdocs[index].uid]);
}
calc(udict, ranked);
await report({
case: {
status: STATUS.STATUS_ACCEPTED,
judgeText: `Problem ${pdoc.title} finished`,
time_ms: new Date().getTime() - start,
memory_kb: 0,
score: 0,
},
});
}
async function runContest(tdoc: Tdoc, udict: ND, report: Function): Promise<void>
@@ -93,7 +80,7 @@ async function runInDomain(domainId: string, isSub: boolean, report: Function) {
await report({ message: `Found ${problems.length} problems in ${domainId}` });
for (const i in problems) {
const pdoc = problems[i];
runProblem(pdoc, udict, report);
runProblem(pdoc, udict);
if (!isSub) {
await report({
progress: Math.floor(((parseInt(i, 10) + 1) / problems.length) * 100),
@@ -147,4 +134,4 @@ export const validate = {
],
};
global.Hydro.script.recalcRating = { run, description, validate };
global.Hydro.script.rating = { run, description, validate };
+19 -7
View File
@@ -20,7 +20,7 @@ import {
UserNotFoundError, BlacklistedError, PermissionError,
UserFacingError, ValidationError, PrivilegeError,
CsrfTokenError, InvalidOperationError, MethodNotAllowedError,
NotFoundError,
NotFoundError, HydroError,
} from '../error';
import { render } from '../lib/template';
import hash from '../lib/hash.hydro';
@@ -169,9 +169,15 @@ export function requireCsrfToken(target: any, funcName: string, obj: any) {
export async function prepare() {
server = http.createServer(app.callback());
app.keys = await system.get('session.keys');
app.use(cache(path.join(os.tmpdir(), 'hydro', 'public'), {
maxAge: 365 * 24 * 60 * 60,
}));
if (process.env.debug) {
app.use(cache(path.join(process.cwd(), 'public'), {
maxAge: 0,
}));
} else {
app.use(cache(path.join(os.tmpdir(), 'hydro', 'public'), {
maxAge: 365 * 24 * 60 * 60,
}));
}
app.use(Body({
multipart: true,
formidable: {
@@ -553,8 +559,8 @@ async function handle(ctx, HandlerClass, checker) {
}
const Checker = (permPrivChecker) => {
let perm;
let priv;
let perm: bigint;
let priv: number;
let checker = () => { };
for (const item of permPrivChecker) {
if (typeof item === 'object') {
@@ -694,8 +700,14 @@ export class ConnectionHandler {
this.conn.close(code.toString(), reason);
}
onerror(err: Error) {
onerror(err: HydroError) {
console.error(err);
this.send({
error: {
name: err.name,
params: err.params || [],
},
});
this.close(1001, err.toString());
}