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

+2 -2
View File
@@ -16,8 +16,8 @@
"@aws-sdk/lib-storage": "3.726.1",
"@aws-sdk/s3-presigned-post": "3.726.1",
"@aws-sdk/s3-request-presigner": "3.726.1",
"@cordisjs/core": "4.0.0-alpha.0",
"@cordisjs/logger": "1.0.0-alpha.0",
"@cordisjs/core": "4.0.0-alpha.1",
"@cordisjs/logger": "1.0.0-alpha.1",
"@cordisjs/plugin-timer": "1.0.0-alpha.0",
"@graphql-tools/schema": "^10.0.18",
"@hydrooj/common": "workspace:^",
+4 -6
View File
@@ -2,16 +2,15 @@ import * as cordis from '@cordisjs/core';
import { Logger } from '@cordisjs/logger';
import { TimerService } from '@cordisjs/plugin-timer';
import Schema from 'schemastery';
import type { ServerEvents, WebService } from '@hydrooj/framework';
import type { DomainDoc, GeoIP, ModuleInterfaces } from './interface';
import { inject } from './lib/ui';
import { Loader } from './loader';
import type { EventMap } from './service/bus';
import type { CheckService } from './service/check';
import type { } from './service/migration';
import type { ConnectionHandler, Handler } from './service/server';
export interface Events<C extends Context = Context> extends cordis.Events<C>, EventMap { }
// TODO: this is an broken declaration
export { EventMap as Events };
function addScript<K>(name: string, description: string, validate: Schema<K>, run: (args: K, report: any) => boolean | Promise<boolean>) {
if (global.Hydro.script[name]) throw new Error(`duplicate script ${name} registered.`);
@@ -30,8 +29,7 @@ export type EffectScope = cordis.EffectScope<Context>;
export { Disposable, Plugin, ScopeStatus } from '@cordisjs/core';
export interface Context extends cordis.Context {
// @ts-ignore
[Context.events]: Events<this> & ServerEvents<this>;
[Context.events]: EventMap & cordis.Events<Context>;
loader: Loader;
check: CheckService;
setImmediate: typeof setImmediate;
@@ -53,7 +51,7 @@ const T = <F extends (...args: any[]) => any>(origFunc: F, disposeFunc?) =>
});
};
export class ApiMixin extends cordis.Service {
export class ApiMixin extends cordis.Service<Context> {
addScript = T(addScript);
setImmediate = T(setImmediate, clearImmediate);
provideModule = T(provideModule);
+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();
+1
View File
@@ -5,6 +5,7 @@ import {
ForbiddenError, fs, ObjectId, parseTimeMS, PERM, ProblemConfig, ProblemModel,
STATUS, STATUS_SHORT_TEXTS, STATUS_TEXTS, Time, UserModel,
} from 'hydrooj';
import type { } from 'hydrooj/src/handler/contest';
import { ResolverInput } from './interface';
interface IpLoginInfo {
+1
View File
@@ -2,6 +2,7 @@ import path from 'path';
import {
ContestModel, Context, fs, Handler, ObjectId, PERM, Schema, STATUS, Types, UserModel,
} from 'hydrooj';
import type { } from 'hydrooj/src/handler/contest';
const file = fs.readFileSync(path.join(__dirname, 'public/assets/board.html'), 'utf8');
const indexJs = file.match(/index-([A-Za-z0-9_-]+)\.js"/)?.[1];
+1 -1
View File
@@ -15,7 +15,7 @@
},
"devDependencies": {
"@blueprintjs/core": "^5.16.6",
"@cordisjs/core": "4.0.0-alpha.0",
"@cordisjs/core": "4.0.0-alpha.1",
"@fontsource/dm-mono": "^5.1.1",
"@fontsource/fira-code": "^5.1.1",
"@fontsource/inconsolata": "^5.1.1",