mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: upgrade to context tracing
This commit is contained in:
9 files changed
+30
-26
No files matched your search
@@ -338,7 +338,6 @@ export class WebService extends Service {
|
|||||||
HandlerCommon = HandlerCommon;
|
HandlerCommon = HandlerCommon;
|
||||||
Handler = Handler;
|
Handler = Handler;
|
||||||
ConnectionHandler = ConnectionHandler;
|
ConnectionHandler = ConnectionHandler;
|
||||||
handlerCtxBase = this.ctx;
|
|
||||||
|
|
||||||
constructor(ctx: Context, public config: WebServiceConfig) {
|
constructor(ctx: Context, public config: WebServiceConfig) {
|
||||||
super(ctx, 'server', true);
|
super(ctx, 'server', true);
|
||||||
@@ -439,9 +438,6 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`);
|
|||||||
}
|
}
|
||||||
socket.close();
|
socket.close();
|
||||||
});
|
});
|
||||||
this.ctx.inject(['server'], (c) => {
|
|
||||||
this.handlerCtxBase = c;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async listen() {
|
async listen() {
|
||||||
@@ -460,7 +456,7 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`);
|
|||||||
private async handleHttp(ctx: KoaContext, HandlerClass, checker) {
|
private async handleHttp(ctx: KoaContext, HandlerClass, checker) {
|
||||||
const { args } = ctx.HydroContext;
|
const { args } = ctx.HydroContext;
|
||||||
Object.assign(args, ctx.params);
|
Object.assign(args, ctx.params);
|
||||||
const h = new HandlerClass(ctx, this.handlerCtxBase);
|
const h = new HandlerClass(ctx, this.ctx);
|
||||||
ctx.handler = h;
|
ctx.handler = h;
|
||||||
const method = ctx.method.toLowerCase();
|
const method = ctx.method.toLowerCase();
|
||||||
try {
|
try {
|
||||||
@@ -534,7 +530,7 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`);
|
|||||||
|
|
||||||
private async handleWS(ctx: KoaContext, HandlerClass, checker, conn, layer) {
|
private async handleWS(ctx: KoaContext, HandlerClass, checker, conn, layer) {
|
||||||
const { args } = ctx.HydroContext;
|
const { args } = ctx.HydroContext;
|
||||||
const h = new HandlerClass(ctx, this.handlerCtxBase);
|
const h = new HandlerClass(ctx, this.ctx);
|
||||||
await this.ctx.parallel('connection/create', h);
|
await this.ctx.parallel('connection/create', h);
|
||||||
ctx.handler = h;
|
ctx.handler = h;
|
||||||
h.conn = conn;
|
h.conn = conn;
|
||||||
@@ -599,8 +595,8 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`);
|
|||||||
}
|
}
|
||||||
|
|
||||||
private register(type: 'route' | 'conn', routeName: string, path: string, HandlerClass: any, ...permPrivChecker) {
|
private register(type: 'route' | 'conn', routeName: string, path: string, HandlerClass: any, ...permPrivChecker) {
|
||||||
const name = HandlerClass.name;
|
|
||||||
if (!HandlerClass?.[kHandler] || !isClass(HandlerClass)) throw new Error('Invalid registration.');
|
if (!HandlerClass?.[kHandler] || !isClass(HandlerClass)) throw new Error('Invalid registration.');
|
||||||
|
const name = HandlerClass.name;
|
||||||
if (this.registrationCount[name] && this.registry[name] !== HandlerClass) {
|
if (this.registrationCount[name] && this.registry[name] !== HandlerClass) {
|
||||||
logger.warn('Route with name %s already exists.', name);
|
logger.warn('Route with name %s already exists.', name);
|
||||||
}
|
}
|
||||||
@@ -617,7 +613,7 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`);
|
|||||||
const dispose = router.disposeLastOp;
|
const dispose = router.disposeLastOp;
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
this.ctx.parallel(`handler/register/${name}`, HandlerClass);
|
this.ctx.parallel(`handler/register/${name}`, HandlerClass);
|
||||||
this[Context.current]?.on('dispose', () => {
|
this.ctx.on('dispose', () => {
|
||||||
this.registrationCount[name]--;
|
this.registrationCount[name]--;
|
||||||
if (!this.registrationCount[name]) delete this.registry[name];
|
if (!this.registrationCount[name]) delete this.registry[name];
|
||||||
dispose();
|
dispose();
|
||||||
@@ -650,7 +646,7 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`);
|
|||||||
const dispose = () => {
|
const dispose = () => {
|
||||||
this[name] = this[name].filter((i) => i !== layer);
|
this[name] = this[name].filter((i) => i !== layer);
|
||||||
};
|
};
|
||||||
this[Context.current]?.on('dispose', dispose);
|
this[Context.origin]?.on('dispose', dispose);
|
||||||
return dispose;
|
return dispose;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -683,9 +679,11 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`);
|
|||||||
|
|
||||||
public registerRenderer(name: string, func: RendererFunction) {
|
public registerRenderer(name: string, func: RendererFunction) {
|
||||||
if (this.renderers[name]) logger.warn('Renderer %s already exists.', name);
|
if (this.renderers[name]) logger.warn('Renderer %s already exists.', name);
|
||||||
|
this.ctx.effect(() => {
|
||||||
this.renderers[name] = func;
|
this.renderers[name] = func;
|
||||||
this[Context.current]?.on('dispose', () => {
|
return () => {
|
||||||
delete this.renderers[name];
|
delete this.renderers[name];
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,8 +50,10 @@ export abstract class Service<T = any, C extends Context = Context> extends cord
|
|||||||
|
|
||||||
const T = <F extends (...args: any[]) => any>(origFunc: F, disposeFunc?) =>
|
const T = <F extends (...args: any[]) => any>(origFunc: F, disposeFunc?) =>
|
||||||
function method(this: cordis.Service, ...args: Parameters<F>) {
|
function method(this: cordis.Service, ...args: Parameters<F>) {
|
||||||
|
this.ctx.effect(() => {
|
||||||
const res = origFunc(...args);
|
const res = origFunc(...args);
|
||||||
this[Context.current]?.on('dispose', () => (disposeFunc ? disposeFunc(res) : res()));
|
return () => (disposeFunc ? disposeFunc(res) : res());
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export class ApiMixin extends Service {
|
export class ApiMixin extends Service {
|
||||||
|
|||||||
@@ -179,7 +179,6 @@ export class HomeHandler extends Handler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let geoip: Context['geoip'] = null;
|
|
||||||
class HomeSecurityHandler extends Handler {
|
class HomeSecurityHandler extends Handler {
|
||||||
@requireSudo
|
@requireSudo
|
||||||
async get() {
|
async get() {
|
||||||
@@ -189,7 +188,7 @@ class HomeSecurityHandler extends Handler {
|
|||||||
session.isCurrent = session._id === this.session._id;
|
session.isCurrent = session._id === this.session._id;
|
||||||
session._id = md5(session._id);
|
session._id = md5(session._id);
|
||||||
session.updateUa = useragent.parse(session.updateUa || session.createUa || '');
|
session.updateUa = useragent.parse(session.updateUa || session.createUa || '');
|
||||||
session.updateGeoip = geoip?.lookup?.(
|
session.updateGeoip = this.ctx.geoip?.lookup?.(
|
||||||
session.updateIp || session.createIp,
|
session.updateIp || session.createIp,
|
||||||
this.translate('geoip_locale'),
|
this.translate('geoip_locale'),
|
||||||
);
|
);
|
||||||
@@ -202,7 +201,7 @@ class HomeSecurityHandler extends Handler {
|
|||||||
'credentialID', 'name', 'credentialType', 'credentialDeviceType',
|
'credentialID', 'name', 'credentialType', 'credentialDeviceType',
|
||||||
'authenticatorAttachment', 'regat', 'fmt',
|
'authenticatorAttachment', 'regat', 'fmt',
|
||||||
])),
|
])),
|
||||||
geoipProvider: geoip?.provider,
|
geoipProvider: this.ctx?.geoip?.provider,
|
||||||
icon: (str = '') => str.split(' ')[0].toLowerCase(),
|
icon: (str = '') => str.split(' ')[0].toLowerCase(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -591,10 +590,8 @@ class HomeMessagesConnectionHandler extends ConnectionHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function apply(ctx: Context) {
|
export const inject = { geoip: { required: false } };
|
||||||
ctx.inject(['geoip'], (g) => {
|
export function apply(ctx: Context) {
|
||||||
geoip = g.geoip;
|
|
||||||
});
|
|
||||||
ctx.Route('homepage', '/', HomeHandler);
|
ctx.Route('homepage', '/', HomeHandler);
|
||||||
ctx.Route('home_security', '/home/security', HomeSecurityHandler, PRIV.PRIV_USER_PROFILE);
|
ctx.Route('home_security', '/home/security', HomeSecurityHandler, PRIV.PRIV_USER_PROFILE);
|
||||||
ctx.Route('user_changemail_with_code', '/home/changeMail/:code', UserChangemailWithCodeHandler, PRIV.PRIV_USER_PROFILE);
|
ctx.Route('user_changemail_with_code', '/home/changeMail/:code', UserChangemailWithCodeHandler, PRIV.PRIV_USER_PROFILE);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
import { statSync } from 'fs';
|
import { statSync } from 'fs';
|
||||||
import { pick } from 'lodash';
|
import { pick } from 'lodash';
|
||||||
import { lookup } from 'mime-types';
|
import { lookup } from 'mime-types';
|
||||||
|
import { Context } from '../context';
|
||||||
import {
|
import {
|
||||||
AccessDeniedError, FileExistsError, FileLimitExceededError, FileUploadError, NotFoundError,
|
AccessDeniedError, FileExistsError, FileLimitExceededError, FileUploadError, NotFoundError,
|
||||||
ValidationError,
|
ValidationError,
|
||||||
@@ -132,7 +133,7 @@ export class SwitchAccountHandler extends Handler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function apply(ctx) {
|
export async function apply(ctx: Context) {
|
||||||
ctx.Route('switch_language', '/language/:lang', SwitchLanguageHandler);
|
ctx.Route('switch_language', '/language/:lang', SwitchLanguageHandler);
|
||||||
ctx.Route('home_files', '/file', FilesHandler);
|
ctx.Route('home_files', '/file', FilesHandler);
|
||||||
ctx.Route('fs_download', '/file/:uid/:filename', FSDownloadHandler);
|
ctx.Route('fs_download', '/file/:uid/:filename', FSDownloadHandler);
|
||||||
|
|||||||
@@ -19,10 +19,12 @@ class I18nService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
load(lang: string, content: Record<string, string>) {
|
load(lang: string, content: Record<string, string>) {
|
||||||
|
this.ctx.effect(() => {
|
||||||
translations[lang] ||= [];
|
translations[lang] ||= [];
|
||||||
translations[lang].unshift(content);
|
translations[lang].unshift(content);
|
||||||
this[Context.current]?.on('dispose', () => {
|
return () => {
|
||||||
translations[lang] = translations[lang].filter((i) => i !== content);
|
translations[lang] = translations[lang].filter((i) => i !== content);
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -301,8 +301,10 @@ declare module 'cordis' {
|
|||||||
|
|
||||||
const T = <F extends (...args: any[]) => any>(origFunc: F, disposeFunc?) =>
|
const T = <F extends (...args: any[]) => any>(origFunc: F, disposeFunc?) =>
|
||||||
function method(this: cordis.Service, ...args: Parameters<F>) {
|
function method(this: cordis.Service, ...args: Parameters<F>) {
|
||||||
|
this.ctx.effect(() => {
|
||||||
const res = origFunc(...args);
|
const res = origFunc(...args);
|
||||||
this[Context.current]?.on('dispose', () => (disposeFunc ? disposeFunc(res) : res()));
|
return () => (disposeFunc ? disposeFunc(res) : res());
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export class SettingService extends Service {
|
export class SettingService extends Service {
|
||||||
@@ -316,7 +318,7 @@ export class SettingService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get(key: string) {
|
get(key: string) {
|
||||||
return (this[Context.current] ? this[Context.current].domain?.config?.[key.replace(/\./g, '$')] : null) ?? global.Hydro.model.system.get(key);
|
return (this.ctx ? this.ctx.domain?.config?.[key.replace(/\./g, '$')] : null) ?? global.Hydro.model.system.get(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,9 +13,11 @@ export class CheckService extends Service {
|
|||||||
checkers: Dictionary<CheckItem> = {};
|
checkers: Dictionary<CheckItem> = {};
|
||||||
c = {};
|
c = {};
|
||||||
addChecker(type: string, checkFunc: CheckItem) {
|
addChecker(type: string, checkFunc: CheckItem) {
|
||||||
|
this.ctx.effect(() => {
|
||||||
this.checkers[`check${type}`] = checkFunc;
|
this.checkers[`check${type}`] = checkFunc;
|
||||||
this[Context.current]?.on('dispose', () => {
|
return () => {
|
||||||
delete this.checkers[`check${type}`];
|
delete this.checkers[`check${type}`];
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -72,9 +72,11 @@ export class WorkerService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public addHandler(type: string, handler: Function) {
|
public addHandler(type: string, handler: Function) {
|
||||||
|
this.ctx.effect(() => {
|
||||||
this.handlers[type] = handler;
|
this.handlers[type] = handler;
|
||||||
this[Context.current]?.on('dispose', () => {
|
return () => {
|
||||||
delete this.handlers[type];
|
delete this.handlers[type];
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -194,9 +194,7 @@ class VJudgeService extends Service {
|
|||||||
}
|
}
|
||||||
// FIXME: potential race condition
|
// FIXME: potential race condition
|
||||||
if (provider.Langs) this.updateLangs(type, provider.Langs);
|
if (provider.Langs) this.updateLangs(type, provider.Langs);
|
||||||
this[Context.current]?.on('dispose', () => {
|
|
||||||
// TODO dispose session
|
// TODO dispose session
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateLangs(provider: string, mapping: Record<string, Partial<LangConfig>>) {
|
async updateLangs(provider: string, mapping: Record<string, Partial<LangConfig>>) {
|
||||||
|
|||||||
Reference in new issue
Block a user