refactor: rename all coreContext -> core

This commit is contained in:
Wesley F. Young
2024-08-26 09:19:50 +08:00
parent a5f9e44ebb
commit 04fb771f42
79 changed files with 386 additions and 389 deletions

View File

@@ -13,10 +13,10 @@ export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
constructor(
public url: string,
public secret: string | undefined,
public coreContext: NapCatCore,
public core: NapCatCore,
public obContext: NapCatOneBot11Adapter
) {
this.logger = coreContext.context.logger;
this.logger = core.context.logger;
}
onEvent<T extends OB11EmitEventContent>(event: T) {
@@ -25,7 +25,7 @@ export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
}
const headers: Record<string, string> = {
'Content-Type': 'application/json',
'x-self-id': this.coreContext.selfInfo.uin,
'x-self-id': this.core.selfInfo.uin,
};
const msgStr = JSON.stringify(event);
if (this.secret && this.secret.length > 0) {
@@ -48,7 +48,7 @@ export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
return;
}
try {
handleQuickOperation(this.coreContext, this.obContext, event as QuickActionEvent, resJson).then().catch(this.logger.logError);
handleQuickOperation(this.core, this.obContext, event as QuickActionEvent, resJson).then().catch(this.logger.logError);
} catch (e: any) {
this.logger.logError('[OneBot] [Http Client] 新消息事件HTTP上报返回快速操作失败', e);
}

View File

@@ -19,10 +19,10 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
public reconnectIntervalInMillis: number,
public heartbeatIntervalInMillis: number,
private token: string,
public coreContext: NapCatCore,
public core: NapCatCore,
public actions: ActionMap,
) {
this.logger = coreContext.context.logger;
this.logger = core.context.logger;
}
onEvent<T extends OB11EmitEventContent>(event: T) {
@@ -37,7 +37,7 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
}
this.heartbeatRef = setInterval(() => {
if (this.connection && this.connection.readyState === WebSocket.OPEN) {
this.connection.send(JSON.stringify(new OB11HeartbeatEvent(this.coreContext, this.heartbeatIntervalInMillis, this.coreContext.selfInfo.online, true)));
this.connection.send(JSON.stringify(new OB11HeartbeatEvent(this.core, this.heartbeatIntervalInMillis, this.core.selfInfo.online, true)));
}
}, this.heartbeatIntervalInMillis);
await this.tryConnect();
@@ -74,7 +74,7 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
handshakeTimeout: 2000,
perMessageDeflate: false,
headers: {
'X-Self-ID': this.coreContext.selfInfo.uin,
'X-Self-ID': this.core.selfInfo.uin,
'Authorization': `Bearer ${this.token}`,
'x-client-role': 'Universal', // koishi-adapter-onebot 需要这个字段
'User-Agent': 'OneBot/11',
@@ -89,7 +89,7 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
});
this.connection.on('open', () => {
try {
this.connectEvent(this.coreContext);
this.connectEvent(this.core);
} catch (e) {
this.logger.logError('[OneBot] [WebSocket Client] 发送连接生命周期失败', e);
}

View File

@@ -14,7 +14,7 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
constructor(
public port: number,
public token: string,
public coreContext: NapCatCore,
public core: NapCatCore,
public actions: ActionMap,
) {
}
@@ -26,7 +26,7 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
open() {
try {
if (this.isOpen) {
this.coreContext.context.logger.logError('Cannot open a closed HTTP server');
this.core.context.logger.logError('Cannot open a closed HTTP server');
return;
}
if (!this.isOpen) {
@@ -34,7 +34,7 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
this.isOpen = true;
}
} catch (e) {
this.coreContext.context.logger.logError(`[OneBot] [HTTP Server Adapter] Boot Error: ${e}`);
this.core.context.logger.logError(`[OneBot] [HTTP Server Adapter] Boot Error: ${e}`);
}
}
@@ -66,7 +66,7 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
this.app.use((req, res) => this.handleRequest(req, res));
this.server.listen(this.port, () => {
this.coreContext.context.logger.log(`[OneBot] [HTTP Server Adapter] Start On Port ${this.port}`);
this.core.context.logger.log(`[OneBot] [HTTP Server Adapter] Start On Port ${this.port}`);
});
}
@@ -84,7 +84,7 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
private async handleRequest(req: Request, res: Response) {
if (!this.isOpen) {
this.coreContext.context.logger.log(`[OneBot] [HTTP Server Adapter] Server is closed`);
this.core.context.logger.log(`[OneBot] [HTTP Server Adapter] Server is closed`);
return res.json(OB11Response.error('Server is closed', 200));
}

View File

@@ -1,12 +1,10 @@
import { IOB11NetworkAdapter, OB11EmitEventContent } from './index';
import urlParse from 'url';
import BaseAction from '@/onebot/action/BaseAction';
import { WebSocket, WebSocketServer } from 'ws';
import { Mutex } from 'async-mutex';
import { OB11Response } from '../action/OB11Response';
import { ActionName } from '../action/types';
import { NapCatCore } from '@/core';
import { NapCatOneBot11Adapter } from '..';
import { LogWrapper } from '@/common/utils/log';
import { OB11HeartbeatEvent } from '../event/meta/OB11HeartbeatEvent';
import { IncomingMessage } from 'http';
@@ -20,7 +18,7 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
isOpen: boolean = false;
hasBeenClosed: boolean = false;
heartbeatInterval: number = 0;
coreContext: NapCatCore;
core: NapCatCore;
logger: LogWrapper;
private heartbeatIntervalId: NodeJS.Timeout | null = null;
@@ -29,11 +27,11 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
port: number,
heartbeatInterval: number,
token: string,
coreContext: NapCatCore,
core: NapCatCore,
public actions: ActionMap
) {
this.coreContext = coreContext;
this.logger = coreContext.context.logger;
this.core = core;
this.logger = core.context.logger;
this.heartbeatInterval = heartbeatInterval;
this.wsServer = new WebSocketServer({
@@ -41,7 +39,6 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
host: ip,
maxPayload: 1024 * 1024 * 1024,
});
const core = coreContext;
this.wsServer.on('connection', async (wsClient, wsReq) => {
if (!this.isOpen) {
wsClient.close();
@@ -120,7 +117,7 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
this.wsClientsMutex.runExclusive(async () => {
this.wsClients.forEach((wsClient) => {
if (wsClient.readyState === WebSocket.OPEN) {
wsClient.send(JSON.stringify(new OB11HeartbeatEvent(this.coreContext, this.heartbeatInterval, this.coreContext.selfInfo.online, true)));
wsClient.send(JSON.stringify(new OB11HeartbeatEvent(this.core, this.heartbeatInterval, this.core.selfInfo.online, true)));
}
});
});