workspace: upgrade to cordis@4.0.0-beta.1, fix #975

This commit is contained in:
undefined committed 2025-04-11 21:21:47 +08:00
1 parent 5d5f6c69d7
commit bf51bd30c7
34 files changed
+87 -88

No files matched your search

+3 -13
View File
@@ -1,4 +1,4 @@
import * as cordis from '@cordisjs/core';
import * as cordis from 'cordis';
import LoggerService from '@cordisjs/logger';
import { TimerService } from '@cordisjs/plugin-timer';
import Schema from 'schemastery';
@@ -23,9 +23,9 @@ function provideModule<T extends keyof ModuleInterfaces>(type: T, id: string, mo
return () => delete global.Hydro.module[type][id];
}
export type EffectScope = cordis.EffectScope<Context>;
export type Fiber = cordis.Fiber<Context>;
export { Disposable, Plugin, ScopeStatus } from '@cordisjs/core';
export { Disposable, Plugin, FiberState } from 'cordis';
export interface Context extends cordis.Context {
[Context.events]: EventMap & cordis.Events<Context>;
@@ -74,13 +74,3 @@ export class Context extends cordis.Context {
this.plugin(LoggerService);
}
}
const old = cordis.Registry.prototype.inject;
// cordis.Registry.prototype.using = old;
cordis.Registry.prototype.inject = function wrapper(...args) {
if (typeof args[0] === 'string') {
console.warn('old functionality of ctx.inject is deprecated. please use ctx.injectUI instead.');
return T(inject).call(this, ...args as any) as any;
}
return old.call(this, ...args);
};
+1 -1
View File
@@ -142,7 +142,7 @@ class DomainUserHandler extends ManageHandler {
user: 1,
role: 1,
join: 1,
displayName: this.user.hasPerm(PERM.PERM_VIEW_DISPLAYNAME),
...(this.user.hasPerm(PERM.PERM_VIEW_DISPLAYNAME) ? { displayName: 1 } : {}),
},
},
]).toArray(),
+1 -5
View File
@@ -13,7 +13,7 @@ declare module '../service/bus' {
}
}
class I18nService extends Service {
export class I18nService extends Service {
constructor(ctx: Context) {
super(ctx, 'i18n');
this.translate = this.translate.bind(this);
@@ -55,10 +55,6 @@ class I18nService extends Service {
}
export const name = 'i18n';
export function apply(ctx: Context) {
ctx.provide('i18n', undefined, true);
ctx.i18n = new I18nService(ctx);
}
function collect(lang: string) {
const s = translations[lang];
+1 -3
View File
@@ -1,4 +1,3 @@
import { ZipFS, ZipOpenFS } from '@yarnpkg/libzip';
import AdmZip from 'adm-zip';
import _ from 'lodash';
import { ObjectId as ObjectID } from 'mongodb';
@@ -10,9 +9,8 @@ export { WebSocket, WebSocketServer } from '@hydrooj/framework';
export * from './utils';
export * from './interface';
export * from './typeutils';
export * as LibZip from '@yarnpkg/libzip';
export {
Schema, AdmZip, superagent, _, ObjectID, ZipFS, ZipOpenFS,
Schema, AdmZip, superagent, _, ObjectID,
};
export const definePlugin = <T = never>(args: {
using?: keyof Context[];
+8 -8
View File
@@ -9,11 +9,11 @@ import child from 'child_process';
import './utils';
import cac from 'cac';
import './ui';
import * as I18n from './lib/i18n';
import { I18nService } from './lib/i18n';
import { Logger } from './logger';
import {
Context, Service, ScopeStatus, EffectScope,
Context, Service, FiberState, Fiber,
} from './context';
// eslint-disable-next-line import/no-duplicates
import { sleep, unwrapExports } from './utils';
@@ -45,7 +45,7 @@ if (process.env.NIX_PROFILES) {
}
export class Loader extends Service {
public state: Record<string, EffectScope> = Object.create(null);
public state: Record<string, Fiber> = Object.create(null);
public suspend = false;
public cache: Record<string, string> = Object.create(null);
// public warnings: Record<string, string> = Object.create(null);
@@ -56,8 +56,8 @@ export class Loader extends Service {
super(ctx, 'loader');
ctx.on('app/started', () => {
ctx.setInterval(async () => {
const pending = Object.entries(this.state).filter((v) => v[1].status === ScopeStatus.PENDING);
ctx.interval(async () => {
const pending = Object.entries(this.state).filter((v) => v[1].state === FiberState.PENDING);
if (pending.length) {
logger.warn('Plugins are still pending: %s', pending.map((v) => v[0]).join(', '));
for (const [key, value] of pending) {
@@ -65,7 +65,7 @@ export class Loader extends Service {
console.log(value);
}
}
const loading = Object.entries(this.state).filter((v) => v[1].status === ScopeStatus.LOADING);
const loading = Object.entries(this.state).filter((v) => v[1].state === FiberState.LOADING);
if (loading.length) {
logger.warn('Plugins are still loading: %s', loading.map((v) => v[0]).join(', '));
for (const [key, value] of loading) {
@@ -73,7 +73,7 @@ export class Loader extends Service {
console.log(value);
}
}
const failed = Object.entries(this.state).filter((v) => v[1].status === ScopeStatus.FAILED);
const failed = Object.entries(this.state).filter((v) => v[1].state === FiberState.FAILED);
if (failed.length) {
logger.warn('Plugins failed to load: %s', failed.map((v) => v[0]).join(', '));
for (const [key, value] of failed) {
@@ -143,7 +143,7 @@ export class Loader extends Service {
}
}
app.plugin(I18n);
app.plugin(I18nService);
app.plugin(Loader);
async function preload() {
+2 -2
View File
@@ -1,6 +1,6 @@
/* eslint-disable max-len */
/* eslint-disable no-await-in-loop */
import * as cordis from '@cordisjs/core';
import * as cordis from 'cordis';
import yaml from 'js-yaml';
import { Dictionary } from 'lodash';
import moment from 'moment-timezone';
@@ -356,7 +356,7 @@ SystemSetting(
export const langs: Record<string, LangConfig> = {};
declare module '@cordisjs/core' {
declare module 'cordis' {
interface Context {
setting: SettingService;
}
+1 -1
View File
@@ -1,7 +1,7 @@
// TODO check more
import os from 'os';
import { DisposableList } from '@cordisjs/core';
import { DisposableList } from 'cordis';
import { Context, Service } from '../context';
import * as system from '../model/system';
import db from './db';
+1 -1
View File
@@ -62,7 +62,7 @@ export class MongoService extends Service {
yield () => { this.client.close(); };
this.db = this.client.db(url.database || 'hydro');
await bus.parallel('database/connect', this.db);
yield this.ctx.setInterval(() => this.fixExpireAfter(), Time.hour);
yield this.ctx.interval(() => this.fixExpireAfter(), Time.hour);
}
public collection<K extends keyof Collections>(c: K) {
+1 -1
View File
@@ -209,7 +209,7 @@ export default class HMR extends Service {
const reload = async (plugin: any, runtime?: Plugin.Runtime) => {
if (!runtime) return;
for (const oldFiber of runtime.scopes) {
for (const oldFiber of runtime.fibers) {
// eslint-disable-next-line no-await-in-loop
await oldFiber.parent.plugin(plugin, oldFiber.config);
}
+17 -1
View File
@@ -1,8 +1,11 @@
/* eslint-disable no-await-in-loop */
import cac from 'cac';
import { Logger } from '@hydrooj/utils';
import { Context, Service } from '../context';
import * as system from '../model/system';
const argv = cac().parse();
export type MigrationScript = null | ((ctx: Context) => Promise<boolean | void>);
const logger = new Logger('migration');
@@ -43,6 +46,18 @@ export default class MigrationService extends Service {
await system.set(name, expected);
continue;
}
let upgraded = false;
if (dbVer > expected) {
logger.warn('Current database version for [%s] is %d, expected %d.', channel, dbVer, expected);
logger.warn('You are likely trying to apply a downgrade.');
logger.warn('This version of Hydro is not compatible with newer data version.');
if (!argv.options.ignoreVersion) {
logger.warn('To prevent data corruption, the startup has been aborted.');
logger.warn('If you want to continue, use --ignore-version on startup.');
logger.warn('Do it at your own risk.');
await new Promise(() => { });
}
}
while (dbVer < expected) {
const func = scripts[dbVer];
if (typeof func !== 'function') {
@@ -62,8 +77,9 @@ export default class MigrationService extends Service {
}
dbVer++;
await system.set(name, dbVer);
upgraded = true;
}
logger.success('Database upgraded [%s]: current version %d', channel, dbVer);
if (upgraded) logger.success('Database upgraded [%s]: current version %d', channel, dbVer);
}
}
}
+1 -1
View File
@@ -107,5 +107,5 @@ export async function apply(ctx: Context) {
{ upsert: true },
);
feedback();
return ctx.setInterval(update, 1800 * 1000); // eslint-disable-line
return ctx.interval(update, 1800 * 1000); // eslint-disable-line
}
+1 -1
View File
@@ -425,7 +425,7 @@ export async function apply(ctx: Context, config: ReturnType<typeof FileSetting>
return await next();
});
});
ctx.set('storage', service);
ctx.provide('storage', service);
}
declare module '../context' {
+1 -1
View File
@@ -11,7 +11,7 @@ async function terminate() {
}, 1000);
try {
await bus.parallel('app/exit');
await app.scope.dispose();
await app.fiber.dispose();
} catch (e) {
hasError = true;
}