remove ensureindex api (use postInit instead)

This commit is contained in:
undefined committed 2020-07-25 12:18:54 +08:00
1 parent be9d2bffb9
commit e62e7b49a3
13 files changed
+54 -105

No files matched your search

-1
View File
@@ -24,7 +24,6 @@ export async function load(call: Entry) {
await fs.writeFile(lockfile, JSON.stringify(context)); await fs.writeFile(lockfile, JSON.stringify(context));
global.onDestory.push(() => { global.onDestory.push(() => {
fs.removeSync(lockfile); fs.removeSync(lockfile);
fs.emptyDirSync(tmpdir);
}); });
require('../lib/i18n'); require('../lib/i18n');
require('../utils'); require('../utils');
+1 -17
View File
@@ -37,11 +37,6 @@ export async function load() {
for (const i of builtinModel) require(`../model/${i}`); for (const i of builtinModel) require(`../model/${i}`);
for (const i of builtinHandler) require(`../handler/${i}`); for (const i of builtinHandler) require(`../handler/${i}`);
await model(pending, fail); await model(pending, fail);
for (const m in global.Hydro.model) {
if (global.Hydro.model[m].ensureIndexes) {
await global.Hydro.model[m].ensureIndexes();
}
}
const modelSetting = require('../model/setting'); const modelSetting = require('../model/setting');
await setting(pending, fail, modelSetting); await setting(pending, fail, modelSetting);
await handler(pending, fail); await handler(pending, fail);
@@ -50,20 +45,9 @@ export async function load() {
} }
const notfound = require('../handler/notfound'); const notfound = require('../handler/notfound');
await notfound.apply(); await notfound.apply();
for (const i in global.Hydro.service) {
if (global.Hydro.service[i].postInit) {
try {
await global.Hydro.service[i].postInit();
} catch (e) {
console.error(e);
}
}
}
for (const i of builtinScript) require(`../script/${i}`); for (const i of builtinScript) require(`../script/${i}`);
await script(pending, fail, active); await script(pending, fail, active);
for (const postInit of global.Hydro.postInit) { for (const postInit of global.Hydro.postInit) await postInit();
await postInit();
}
pending = []; pending = [];
await server.start(); await server.start();
setInterval(() => { setInterval(() => {
+5 -5
View File
@@ -83,7 +83,8 @@ class UserRegisterHandler extends Handler {
} }
class UserRegisterWithCodeHandler extends Handler { class UserRegisterWithCodeHandler extends Handler {
async get({ code }) { @param('code', Types.String)
async get(domainId: string, code: string) {
this.response.template = 'user_register_with_code.html'; this.response.template = 'user_register_with_code.html';
const { mail } = await token.get(code, token.TYPE_REGISTRATION); const { mail } = await token.get(code, token.TYPE_REGISTRATION);
if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code); if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code);
@@ -95,14 +96,13 @@ class UserRegisterWithCodeHandler extends Handler {
@param('uname', Types.String, isUname) @param('uname', Types.String, isUname)
@param('code', Types.String) @param('code', Types.String)
async post( async post(
domainId: string, password: string, verifyPassword: string, domainId: string, password: string, verify: string,
uname: string, code: string, uname: string, code: string,
) { ) {
const { mail } = await token.get(code, token.TYPE_REGISTRATION); const { mail } = await token.get(code, token.TYPE_REGISTRATION);
if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code); if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code);
if (password !== verifyPassword) throw new VerifyPasswordError(); if (password !== verify) throw new VerifyPasswordError();
const uid = await system.inc('user'); const uid = await user.create(mail, uname, password, undefined, this.request.ip);
await user.create(mail, uname, password, uid, this.request.ip);
await token.del(code, token.TYPE_REGISTRATION); await token.del(code, token.TYPE_REGISTRATION);
this.session.uid = uid; this.session.uid = uid;
this.response.redirect = this.url('homepage'); this.response.redirect = this.url('homepage');
+3 -4
View File
@@ -21,10 +21,9 @@ export function del(ip: string) {
return coll.deleteOne({ _id: ip }); return coll.deleteOne({ _id: ip });
} }
export function ensureIndexes() { function ensureIndexes() {
return coll.createIndex('expireAt', { expireAfterSeconds: 0 }); return coll.createIndex('expireAt', { expireAfterSeconds: 0 });
} }
global.Hydro.model.blacklist = { global.Hydro.postInit.push(ensureIndexes);
add, get, del, ensureIndexes, global.Hydro.model.blacklist = { add, get, del };
};
+2 -2
View File
@@ -328,7 +328,7 @@ export async function revSetStatus(
return await collStatus.updateOne(filter, update); return await collStatus.updateOne(filter, update);
} }
export async function ensureIndexes() { async function ensureIndexes() {
await coll.createIndex({ domainId: 1, docType: 1, docId: 1 }, { unique: true }); await coll.createIndex({ domainId: 1, docType: 1, docId: 1 }, { unique: true });
await coll.createIndex({ await coll.createIndex({
domainId: 1, docType: 1, owner: 1, docId: -1, domainId: 1, docType: 1, owner: 1, docId: -1,
@@ -390,6 +390,7 @@ export async function ensureIndexes() {
}, { sparse: true }); }, { sparse: true });
} }
global.Hydro.postInit.push(ensureIndexes);
global.Hydro.model.document = { global.Hydro.model.document = {
add, add,
addToSet, addToSet,
@@ -399,7 +400,6 @@ global.Hydro.model.document = {
deleteMultiStatus, deleteMultiStatus,
deleteOne, deleteOne,
deleteSub, deleteSub,
ensureIndexes,
get, get,
getMulti, getMulti,
getMultiStatus, getMultiStatus,
+3 -2
View File
@@ -135,13 +135,14 @@ export function url(arg0: any, name: any) {
return __url(arg0, name); return __url(arg0, name);
} }
export function ensureIndexes() { function ensureIndexes() {
return Promise.all([ return Promise.all([
collChunk.createIndex({ files_id: 1, n: 1 }, { unique: true }), collChunk.createIndex({ files_id: 1, n: 1 }, { unique: true }),
collFile.createIndex({ md5: 1 }), collFile.createIndex({ md5: 1 }),
]); ]);
} }
global.Hydro.postInit.push(ensureIndexes);
global.Hydro.model.file = { global.Hydro.model.file = {
add, get, del, inc, dec, getMeta, getWithSecret, getMulti, getMetaDict, url, ensureIndexes, add, get, del, inc, dec, getMeta, getWithSecret, getMulti, getMetaDict, url,
}; };
+2 -2
View File
@@ -62,13 +62,14 @@ export function getMulti(uid: number) {
return coll.find({ $or: [{ from: uid }, { to: uid }] }); return coll.find({ $or: [{ from: uid }, { to: uid }] });
} }
export function ensureIndexes() { function ensureIndexes() {
return Promise.all([ return Promise.all([
coll.createIndex({ to: 1, _id: -1 }), coll.createIndex({ to: 1, _id: -1 }),
coll.createIndex({ from: 1, _id: -1 }), coll.createIndex({ from: 1, _id: -1 }),
]); ]);
} }
global.Hydro.postInit.push(ensureIndexes);
global.Hydro.model.message = { global.Hydro.model.message = {
FLAG_UNREAD, FLAG_UNREAD,
FLAG_ALERT, FLAG_ALERT,
@@ -81,5 +82,4 @@ global.Hydro.model.message = {
getMany, getMany,
getMulti, getMulti,
send, send,
ensureIndexes,
}; };
+3 -2
View File
@@ -19,8 +19,9 @@ export async function inc(op: string, ident: string, periodSecs: number, maxOper
} }
} }
export function ensureIndexes() { function ensureIndexes() {
return coll.createIndex('expireAt', { expireAfterSeconds: 0 }); return coll.createIndex('expireAt', { expireAfterSeconds: 0 });
} }
global.Hydro.model.opcount = { inc, ensureIndexes }; global.Hydro.postInit.push(ensureIndexes);
global.Hydro.model.opcount = { inc };
+3 -17
View File
@@ -80,31 +80,17 @@ export async function add(
rejudged: false, rejudged: false,
}; };
if ((contestOrConfig as ContestInfo).type) { if ((contestOrConfig as ContestInfo).type) {
// is contest
data.contest = contestOrConfig as ContestInfo; data.contest = contestOrConfig as ContestInfo;
} else { } else {
data.config = contestOrConfig as PretestConfig; data.config = contestOrConfig as PretestConfig;
} }
const [pdoc, res] = await Promise.all([ const res = await coll.insertOne(data);
problem.get(domainId, pid, null),
coll.insertOne(data),
]);
if (addTask) { if (addTask) {
const t: any = { await task.add({
type: 'judge', type: 'judge',
rid: res.insertedId, rid: res.insertedId,
domainId, domainId,
pid: data.pid, });
lang: data.lang,
code: data.code,
};
if (t.type === 'judge') {
t.data = pdoc.data;
t.config = pdoc.config;
} else {
t.config = yaml.safeDump(contestOrConfig);
}
await task.add(t);
} }
return res.insertedId; return res.insertedId;
} }
+3 -4
View File
@@ -10,7 +10,7 @@ export const TYPE_CHANGEMAIL = 3;
export const TYPE_OAUTH = 4; export const TYPE_OAUTH = 4;
export const TYPE_LOSTPASS = 5; export const TYPE_LOSTPASS = 5;
export function ensureIndexes() { function ensureIndexes() {
return Promise.all([ return Promise.all([
coll.createIndex([{ uid: 1 }, { tokenType: 1 }, { updateAt: -1 }], { sparse: true }), coll.createIndex([{ uid: 1 }, { tokenType: 1 }, { updateAt: -1 }], { sparse: true }),
coll.createIndex('expireAt', { expireAfterSeconds: 0 }), coll.createIndex('expireAt', { expireAfterSeconds: 0 }),
@@ -31,9 +31,8 @@ export async function add(tokenType: number, expireSeconds: number, data: any) {
return [str, res.ops[0]]; return [str, res.ops[0]];
} }
export async function get(tokenId: string, tokenType: number, doThrow = true) { export async function get(tokenId: string, tokenType: number) {
const res = await coll.findOne({ _id: tokenId, tokenType }); const res = await coll.findOne({ _id: tokenId, tokenType });
if (!res && doThrow) throw new ValidationError('token');
return res; return res;
} }
@@ -86,6 +85,7 @@ export function delByUid(uid: number) {
return coll.deleteMany({ uid }); return coll.deleteMany({ uid });
} }
global.Hydro.postInit.push(ensureIndexes);
global.Hydro.model.token = { global.Hydro.model.token = {
TYPE_SESSION, TYPE_SESSION,
TYPE_CHANGEMAIL, TYPE_CHANGEMAIL,
@@ -94,7 +94,6 @@ global.Hydro.model.token = {
TYPE_REGISTRATION, TYPE_REGISTRATION,
TYPE_LOSTPASS, TYPE_LOSTPASS,
ensureIndexes,
add, add,
createOrUpdate, createOrUpdate,
get, get,
+12 -6
View File
@@ -85,12 +85,18 @@ export class User implements _User {
} }
} }
hasPerm(p: bigint) { hasPerm(...perm: bigint[]) {
return (this.perm() & p) === p; for (const i in perm) {
if ((this.perm() & perm[i]) === perm[i]) return true;
}
return false;
} }
hasPriv(p: number) { hasPriv(...priv: number[]) {
return !!(this.priv & p); for (const i in priv) {
if ((this.priv & priv[i]) === priv[i]) return true;
}
return false;
} }
checkPassword(password: string) { checkPassword(password: string) {
@@ -215,13 +221,14 @@ export function ban(uid: number) {
]); ]);
} }
export function ensureIndexes() { function ensureIndexes() {
return Promise.all([ return Promise.all([
coll.createIndex('unameLower', { unique: true }), coll.createIndex('unameLower', { unique: true }),
coll.createIndex('mailLower', { sparse: true }), coll.createIndex('mailLower', { sparse: true }),
]); ]);
} }
global.Hydro.postInit.push(ensureIndexes);
global.Hydro.model.user = { global.Hydro.model.user = {
User, User,
create, create,
@@ -237,5 +244,4 @@ global.Hydro.model.user = {
setPriv, setPriv,
getList, getList,
ban, ban,
ensureIndexes,
}; };
+16 -42
View File
@@ -210,7 +210,7 @@ export class Handler {
query: any, query: any,
path: string, path: string,
params: any, params: any,
referer: any, referer: string,
json: boolean, json: boolean,
}; };
@@ -218,9 +218,9 @@ export class Handler {
body: any, body: any,
type: string, type: string,
status: number, status: number,
template: string | undefined, template?: string,
redirect: string | undefined, redirect?: string,
disposition: string | undefined, disposition?: string,
attachment: (name: string) => void, attachment: (name: string) => void,
}; };
@@ -286,7 +286,7 @@ export class Handler {
return res; return res;
} }
async limitRate(op, periodSecs, maxOperations) { async limitRate(op: string, periodSecs: number, maxOperations: number) {
await opcount.inc(op, this.request.ip, periodSecs, maxOperations); await opcount.inc(op, this.request.ip, periodSecs, maxOperations);
} }
@@ -299,40 +299,14 @@ export class Handler {
return `${this.translate(str)} - Hydro`; return `${this.translate(str)} - Hydro`;
} }
checkPerm(...args: Array<bigint[] | bigint>) { checkPerm(...args: bigint[]) {
for (const i in args) {
if (args[i] instanceof Array) {
let p = false;
for (const j in args) {
if (this.user.hasPerm(args[i][j])) {
p = true;
break;
}
}
if (!p) throw new PermissionError([args[i]]);
// @ts-ignore // @ts-ignore
} else if (!this.user.hasPerm(args[i])) { if (!this.user.hasPerm(...args)) throw new PermissionError(...args);
throw new PermissionError([[args[i]]]);
}
}
} }
checkPriv(...args: Array<number[] | number>) { checkPriv(...args: number[]) {
for (const i in args) {
if (args[i] instanceof Array) {
let p = false;
for (const j in args) {
if (this.user.hasPriv(args[i][j])) {
p = true;
break;
}
}
if (!p) throw new PrivilegeError([args[i]]);
// @ts-ignore // @ts-ignore
} else if (!this.user.hasPriv(args[i])) { if (!this.user.hasPriv(...args)) throw new PrivilegeError(...args);
throw new PrivilegeError([[args[i]]]);
}
}
} }
url(name: string, kwargs = {}) { url(name: string, kwargs = {}) {
@@ -373,7 +347,7 @@ export class Handler {
async getSession() { async getSession() {
const sid = this.request.cookies.get('sid'); const sid = this.request.cookies.get('sid');
this.session = await token.get(sid, token.TYPE_SESSION, false); this.session = await token.get(sid, token.TYPE_SESSION);
if (!this.session) this.session = { uid: 0 }; if (!this.session) this.session = { uid: 0 };
} }
@@ -416,7 +390,10 @@ export class Handler {
} catch (error) { } catch (error) {
this.response.status = error instanceof UserFacingError ? error.code : 500; this.response.status = error instanceof UserFacingError ? error.code : 500;
if (this.request.json) this.response.body = { error }; if (this.request.json) this.response.body = { error };
else await this.render(error instanceof UserFacingError ? 'error.html' : 'bsod.html', { error }); else {
await this.render(error instanceof UserFacingError ? 'error.html' : 'bsod.html', { error })
.catch(() => { });
}
} }
await this.putResponse(); await this.putResponse();
await this.saveCookie(); await this.saveCookie();
@@ -720,11 +697,8 @@ export class ConnectionHandler {
} }
async init({ domainId }) { async init({ domainId }) {
try { this.session = await token.get(this.request.params.token, token.TYPE_TOKEN);
this.session = await token.get(this.request.params.token, token.TYPE_TOKEN, true); this.session = this.session || { uid: 0, domainId: 'system' };
} catch (e) {
this.session = { uid: 0, domainId: 'system' };
}
this.args.domainId = this.session.domainId; this.args.domainId = this.session.domainId;
const bdoc = await blacklist.get(this.request.ip); const bdoc = await blacklist.get(this.request.ip);
if (bdoc) throw new BlacklistedError(this.request.ip); if (bdoc) throw new BlacklistedError(this.request.ip);
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.10.6", "version": "2.10.7",
"main": "dist/loader.js", "main": "dist/loader.js",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"repository": "https://github.com/hydro-dev/Hydro.git", "repository": "https://github.com/hydro-dev/Hydro.git",