framework: upgrade cordis

This commit is contained in:
undefined committed 2026-04-17 18:03:41 +08:00
1 parent 772cb7837a
commit 6bdac9fe25
24 files changed
+108 -128

No files matched your search

+1 -1
View File
@@ -28,7 +28,7 @@
"@zip.js/zip.js": "^2.8.26",
"adm-zip": "0.5.16",
"cac": "^7.0.0",
"cordis": "4.0.0-rc.0",
"cordis": "4.0.0-rc.2",
"csv-stringify": "^6.7.0",
"emoji-regex": "^10.6.0",
"emojis-list": "2.1.0",
+20 -46
View File
@@ -1,7 +1,6 @@
/* eslint-disable ts/no-unsafe-declaration-merging */
import LoggerService from '@cordisjs/plugin-logger';
import { TimerService } from '@cordisjs/plugin-timer';
import * as cordis from 'cordis';
import type { } from '@cordisjs/plugin-logger';
import type { } from '@cordisjs/plugin-timer';
import { Service } from 'cordis';
import Schema from 'schemastery';
import type { DomainDoc, GeoIP, ModuleInterfaces } from './interface';
import { inject } from './lib/ui';
@@ -12,6 +11,21 @@ import type { } from './service/migration';
export { EventMap as Events };
declare module 'cordis' {
export interface Events extends EventMap { }
interface Context {
domain?: DomainDoc;
loader: Loader;
check: CheckService;
setImmediate: typeof setImmediate;
addScript: typeof addScript;
provideModule: typeof provideModule;
injectUI: typeof inject;
broadcast: Context['emit'];
geoip?: GeoIP;
}
}
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.`);
global.Hydro.script[name] = { description, validate, run };
@@ -24,28 +38,10 @@ function provideModule<T extends keyof ModuleInterfaces>(type: T, id: string, mo
return () => delete global.Hydro.module[type][id];
}
export type Fiber = cordis.Fiber<Context>;
export const Fiber = cordis.Fiber;
export { Disposable, FiberState, Plugin } from 'cordis';
export interface Context extends cordis.Context {
[Context.events]: EventMap & cordis.Events<Context>;
loader: Loader;
check: CheckService;
setImmediate: typeof setImmediate;
addScript: typeof addScript;
provideModule: typeof provideModule;
injectUI: typeof inject;
broadcast: Context['emit'];
geoip?: GeoIP;
}
export abstract class Service<T = never> extends cordis.Service<T, Context> {
}
export { Context, Disposable, Fiber, FiberState, Plugin, Service } from 'cordis';
const T = <F extends (...args: any[]) => any>(origFunc: F, disposeFunc?) =>
function method(this: cordis.Service, ...args: Parameters<F>) {
function method(this: Service, ...args: Parameters<F>) {
this.ctx.effect(() => {
const res = origFunc(...args);
return () => (disposeFunc ? disposeFunc(res) : res());
@@ -65,25 +61,3 @@ export class ApiMixin extends Service {
ctx.mixin('$api', ['addScript', 'setImmediate', 'provideModule', 'injectUI', 'broadcast']);
}
}
export class Context extends cordis.Context {
domain?: DomainDoc;
constructor() {
super();
this.plugin(ApiMixin);
this.plugin(TimerService);
this.plugin(LoggerService, {
console: {
showDiff: false,
showTime: 'dd hh:mm:ss',
label: {
align: 'right',
width: 9,
margin: 1,
},
levels: { default: process.env.DEV ? 3 : 2 },
},
});
}
}
+1 -1
View File
@@ -876,7 +876,7 @@ class ScoreboardService extends Service {
}
}
declare module '../context' {
declare module 'cordis' {
interface Context {
scoreboard: ScoreboardService;
}
+1 -1
View File
@@ -2,7 +2,7 @@ import { Context, Service } from '../context';
const translations: Record<string, Record<string, string>[]> = {};
declare module '../context' {
declare module 'cordis' {
interface Context {
i18n: I18nService;
}
+17 -1
View File
@@ -12,12 +12,14 @@ import { I18nService } from './lib/i18n';
import { Logger } from './logger';
import {
Context, Service, FiberState, Fiber,
Context, Service, FiberState, Fiber, ApiMixin,
} from './context';
// eslint-disable-next-line import/no-duplicates
import { sleep, unwrapExports } from './utils';
import { PRIV } from './model/builtin';
import { getAddons } from './options';
import { TimerService } from '@cordisjs/plugin-timer';
import LoggerService from '@cordisjs/plugin-logger';
import Schema from 'schemastery';
import { isEqual } from 'lodash';
@@ -162,6 +164,20 @@ export class Loader extends Service {
}
}
app.plugin(ApiMixin);
app.plugin(TimerService);
app.plugin(LoggerService, {
console: {
showDiff: false,
showTime: 'dd hh:mm:ss',
label: {
align: 'right',
width: 9,
margin: 1,
},
levels: { default: process.env.DEV ? 3 : 2 },
},
});
app.plugin(I18nService);
app.plugin(Loader);
+1 -1
View File
@@ -10,7 +10,7 @@ export interface OauthMap {
uid: number;
}
declare module '../context' {
declare module 'cordis' {
interface Context {
oauth: OauthModel;
}
+3 -3
View File
@@ -22,10 +22,10 @@ export interface EventMap {
'app/before-reload': (entries: Set<string>) => VoidReturn;
'app/reload': (entries: Set<string>) => VoidReturn;
'subscription/init': (h: ConnectionHandler<Context>, privileged: boolean) => VoidReturn;
'subscription/init': (h: ConnectionHandler, privileged: boolean) => VoidReturn;
'subscription/subscribe': (channel: string, user: User, metadata: Record<string, string>) => VoidReturn;
'subscription/enable': (
channel: string, h: ConnectionHandler<Context>, privileged: boolean, onDispose: (disposable: () => void) => void,
channel: string, h: ConnectionHandler, privileged: boolean, onDispose: (disposable: () => void) => void,
) => VoidReturn;
'app/watch/change': (path: string) => VoidReturn;
@@ -91,7 +91,7 @@ export interface EventMap {
'contest/balloon': (domainId: string, tid: ObjectId, bdoc: ContestBalloonDoc) => VoidReturn;
'contest/del': (domainId: string, tid: ObjectId) => VoidReturn;
'oplog/log': (type: string, handler: Handler<Context> | ConnectionHandler<Context>, args: any, data: any) => VoidReturn;
'oplog/log': (type: string, handler: Handler | ConnectionHandler, args: any, data: any) => VoidReturn;
'training/list': (query: Filter<TrainingDoc>, handler: any) => VoidReturn;
'training/get': (tdoc: TrainingDoc, handler: any) => VoidReturn;
+1 -1
View File
@@ -26,7 +26,7 @@ interface MongoConfig {
collectionMap?: Record<string, string>;
}
declare module '../context' {
declare module 'cordis' {
interface Context {
db: MongoService;
}
+3 -3
View File
@@ -9,9 +9,9 @@ import { Context, Plugin, Service } from '../context';
import { Logger } from '../logger';
import { unwrapExports } from '../utils';
declare module '../context' {
declare module 'cordis' {
interface Events {
'hmr/reload': (reloads: Map<Plugin, { filename: string, runtime: Plugin.Runtime<Context> }>) => void;
'hmr/reload': (reloads: Map<Plugin, { filename: string, runtime: Plugin.Runtime }>) => void;
}
interface Context {
hmr: HMR;
@@ -150,7 +150,7 @@ export default class HMR extends Service {
const pending = new Map<string, Plugin>();
/** plugins that should be reloaded */
const reloads = new Map<Plugin, { filename: string, runtime: Plugin.Runtime<Context> }>();
const reloads = new Map<Plugin, { filename: string, runtime: Plugin.Runtime }>();
// we assume that plugin entry files are "atomic"
// that is, reloading them will not cause any other reloads
+1 -1
View File
@@ -9,7 +9,7 @@ const argv = cac().parse();
export type MigrationScript = null | ((ctx: Context) => Promise<boolean | void>);
const logger = new Logger('migration');
declare module '../context' {
declare module 'cordis' {
interface Context {
migration: MigrationService;
}
+3 -3
View File
@@ -28,7 +28,7 @@ const ignoredLimit = `,${argv.options.ignoredLimit},`;
const logger = new Logger('server');
declare module '@hydrooj/framework' {
export interface HandlerCommon<C> { // eslint-disable-line ts/no-unused-vars
export interface HandlerCommon {
domain: DomainDoc;
paginate<T>(cursor: FindCursor<T>, page: number, key: string): Promise<[docs: T[], numPages: number, count: number]>;
@@ -72,10 +72,10 @@ export function requireSudo(target: any, funcName: string, obj: any) {
return obj;
}
export class Handler extends HandlerOriginal<Context> {
export class Handler extends HandlerOriginal {
domain: DomainDoc;
}
export class ConnectionHandler extends ConnectionHandlerOriginal<Context> {
export class ConnectionHandler extends ConnectionHandlerOriginal {
domain: DomainDoc;
}
+1 -1
View File
@@ -423,7 +423,7 @@ export async function apply(ctx: Context, config: ReturnType<typeof FileSetting>
ctx.provide('storage', service);
}
declare module '../context' {
declare module 'cordis' {
interface Context {
storage: RemoteStorageService | LocalStorageService;
}
+1 -1
View File
@@ -4,7 +4,7 @@ import { sleep } from '@hydrooj/utils';
import { Context, Service } from '../context';
import db from './db';
declare module '../context' {
declare module 'cordis' {
interface Context {
worker: WorkerService;
}