core&framework: pass type checks on cordis 4.0.0-alpha.1

This commit is contained in:
undefined committed 2025-03-04 09:49:54 +08:00
1 parent 2c8c03153e
commit bd63331137
13 files changed
+100 -64

No files matched your search

+3 -16
View File
@@ -2,11 +2,9 @@
import {
BSON, Db, Filter, ObjectId, OnlyFieldsOfType,
} from 'mongodb';
import type { Handler, ServerEvents } from '@hydrooj/framework';
import type { Handler } from '@hydrooj/framework';
import pm2 from '@hydrooj/utils/lib/locate-pm2';
import { Context } from '../context';
import type { ProblemSolutionHandler } from '../handler/problem';
import type { UserRegisterHandler } from '../handler/user';
import type {
BaseUserDict, ContestBalloonDoc, DiscussionDoc, DomainDoc, FileInfo,
MessageDoc, ProblemDict, ProblemDoc, RecordDoc,
@@ -16,14 +14,8 @@ import type { DocType } from '../model/document';
export type Disposable = () => void;
export type VoidReturn = Promise<any> | any;
type HookType = 'before-prepare' | 'before' | 'before-operation' | 'after' | 'finish';
type MapHandlerEvents<N extends string, H extends Handler<Context>> = Record<`handler/${HookType}/${N}`, (thisArg: H) => VoidReturn>;
type KnownHandlerEvents =
MapHandlerEvents<'UserRegister', UserRegisterHandler>
& MapHandlerEvents<'ProblemSolution', ProblemSolutionHandler>;
/* eslint-disable @typescript-eslint/naming-convention */
export interface EventMap extends KnownHandlerEvents {
export interface EventMap {
'app/listen': () => void
'app/started': () => void
'app/ready': () => VoidReturn
@@ -38,7 +30,7 @@ export interface EventMap extends KnownHandlerEvents {
'database/config': () => VoidReturn
'system/setting': (args: Record<string, any>) => VoidReturn
'bus/broadcast': (event: keyof EventMap | keyof ServerEvents<Context>, payload: any, trace?: string) => VoidReturn
'bus/broadcast': (event: keyof EventMap, payload: any, trace?: string) => VoidReturn
'monitor/update': (type: 'server' | 'judge', $set: any) => VoidReturn
'monitor/collect': (info: any) => VoidReturn
'api/update': () => void;
@@ -101,11 +93,6 @@ export interface EventMap extends KnownHandlerEvents {
'record/change': (rdoc: RecordDoc, $set?: any, $push?: any, body?: any) => void
'record/judge': (rdoc: RecordDoc, updated: boolean, pdoc?: ProblemDoc) => VoidReturn
}
/* eslint-enable @typescript-eslint/naming-convention */
declare module '@cordisjs/core' {
interface Events extends EventMap { }
}
export function apply(ctx: Context) {
try {
+6 -5
View File
@@ -148,7 +148,7 @@ export default class HMR extends Service {
this.analyzeChanges();
/** plugins pending classification */
const pending = new Map<string, Plugin.Runtime<Context>>();
const pending = new Map<string, Plugin>();
/** plugins that should be reloaded */
const reloads = new Map<Plugin, { filename: string, runtime: Plugin.Runtime<Context> }>();
@@ -161,15 +161,16 @@ export default class HMR extends Service {
if (typeof plugin !== 'object' || !plugin || !('apply' in plugin)) continue;
const runtime = this.ctx.registry.get(plugin);
if (!runtime || this.declined.has(filename)) continue;
pending.set(filename, runtime);
pending.set(filename, plugin);
if (!plugin || !('sideEffect' in plugin) || !plugin['sideEffect']) this.declined.add(filename);
}
for (const [filename, runtime] of pending) {
for (const [filename, plugin] of pending) {
// check if it is a dependent of the changed file
this.declined.delete(filename);
const dependencies = [...loadDependencies(filename, this.declined)];
if (!runtime.plugin['sideEffect']) this.declined.add(filename);
const runtime = this.ctx.registry.get(plugin);
if (!plugin['sideEffect']) this.declined.add(filename);
// we only detect reloads at plugin level
// a plugin will be reloaded if any of its dependencies are accepted
@@ -177,7 +178,7 @@ export default class HMR extends Service {
for (const dep of dependencies) this.accepted.add(dep);
// prepare for reload
reloads.set(runtime.plugin, {
reloads.set(plugin, {
filename,
runtime,
});
+6 -1
View File
@@ -28,7 +28,7 @@ const ignoredLimit = `,${argv.options.ignoredLimit},`;
const logger = new Logger('server');
declare module '@hydrooj/framework' {
export interface HandlerCommon<C> {
export interface HandlerCommon<C> { // eslint-disable-line @typescript-eslint/no-unused-vars
domain: DomainDoc;
paginate<T>(cursor: FindCursor<T>, page: number, key: string): Promise<[docs: T[], numPages: number, count: number]>;
@@ -277,6 +277,8 @@ export async function apply(ctx: Context) {
h.ctx = h.ctx.extend({
domain: h.domain,
});
});
on('handler/create/http', async (h) => {
if (!argv.options.benchmark && !h.notUsage) await h.limitRate('global', 5, 100);
h.loginMethods = Object.keys(global.Hydro.module.oauth)
.map((key) => ({
@@ -287,6 +289,9 @@ export async function apply(ctx: Context) {
if (!h.noCheckPermView && !h.user.hasPriv(PRIV.PRIV_VIEW_ALL_DOMAIN)) h.checkPerm(PERM.PERM_VIEW);
if (h.context.pendingError) throw h.context.pendingError;
});
on('handler/create/ws', async (h) => {
if (h.context.pendingError) throw h.context.pendingError;
});
on('app/listen', () => {
server.listen();