mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-10-14 13:29:19 +00:00
完善注释
This commit is contained in:
parent
7c4b933e9d
commit
08f74445da
@ -26,7 +26,8 @@
|
|||||||
"reflect-metadata": "^0.2.2",
|
"reflect-metadata": "^0.2.2",
|
||||||
"rxjs": "^7.8.1",
|
"rxjs": "^7.8.1",
|
||||||
"simple-git": "^3.28.0",
|
"simple-git": "^3.28.0",
|
||||||
"ssh2": "^1.16.0"
|
"ssh2": "^1.16.0",
|
||||||
|
"ws": "^8.18.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/eslintrc": "^3.2.0",
|
"@eslint/eslintrc": "^3.2.0",
|
||||||
@ -40,6 +41,7 @@
|
|||||||
"@types/jest": "^29.5.14",
|
"@types/jest": "^29.5.14",
|
||||||
"@types/node": "^22.16.4",
|
"@types/node": "^22.16.4",
|
||||||
"@types/supertest": "^6.0.2",
|
"@types/supertest": "^6.0.2",
|
||||||
|
"@types/ws": "^8.18.1",
|
||||||
"eslint": "^9.18.0",
|
"eslint": "^9.18.0",
|
||||||
"eslint-config-prettier": "^10.0.1",
|
"eslint-config-prettier": "^10.0.1",
|
||||||
"eslint-plugin-prettier": "^5.2.2",
|
"eslint-plugin-prettier": "^5.2.2",
|
||||||
|
25
pnpm-lock.yaml
generated
25
pnpm-lock.yaml
generated
@ -41,6 +41,9 @@ dependencies:
|
|||||||
ssh2:
|
ssh2:
|
||||||
specifier: ^1.16.0
|
specifier: ^1.16.0
|
||||||
version: 1.16.0
|
version: 1.16.0
|
||||||
|
ws:
|
||||||
|
specifier: ^8.18.3
|
||||||
|
version: 8.18.3
|
||||||
|
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@eslint/eslintrc':
|
'@eslint/eslintrc':
|
||||||
@ -76,6 +79,9 @@ devDependencies:
|
|||||||
'@types/supertest':
|
'@types/supertest':
|
||||||
specifier: ^6.0.2
|
specifier: ^6.0.2
|
||||||
version: 6.0.3
|
version: 6.0.3
|
||||||
|
'@types/ws':
|
||||||
|
specifier: ^8.18.1
|
||||||
|
version: 8.18.1
|
||||||
eslint:
|
eslint:
|
||||||
specifier: ^9.18.0
|
specifier: ^9.18.0
|
||||||
version: 9.31.0
|
version: 9.31.0
|
||||||
@ -2008,6 +2014,12 @@ packages:
|
|||||||
'@types/superagent': 8.1.9
|
'@types/superagent': 8.1.9
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/@types/ws@8.18.1:
|
||||||
|
resolution: {integrity: sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg==}
|
||||||
|
dependencies:
|
||||||
|
'@types/node': 22.16.5
|
||||||
|
dev: true
|
||||||
|
|
||||||
/@types/yargs-parser@21.0.3:
|
/@types/yargs-parser@21.0.3:
|
||||||
resolution: {integrity: sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ==}
|
resolution: {integrity: sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ==}
|
||||||
dev: true
|
dev: true
|
||||||
@ -6487,6 +6499,19 @@ packages:
|
|||||||
signal-exit: 3.0.7
|
signal-exit: 3.0.7
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/ws@8.18.3:
|
||||||
|
resolution: {integrity: sha512-PEIGCY5tSlUt50cqyMXfCzX+oOPqN0vuGqWzbcJ2xvnkzkq46oOpz7dQaTDBdfICb4N14+GARUDw2XV2N4tvzg==}
|
||||||
|
engines: {node: '>=10.0.0'}
|
||||||
|
peerDependencies:
|
||||||
|
bufferutil: ^4.0.1
|
||||||
|
utf-8-validate: '>=5.0.2'
|
||||||
|
peerDependenciesMeta:
|
||||||
|
bufferutil:
|
||||||
|
optional: true
|
||||||
|
utf-8-validate:
|
||||||
|
optional: true
|
||||||
|
dev: false
|
||||||
|
|
||||||
/xtend@4.0.2:
|
/xtend@4.0.2:
|
||||||
resolution: {integrity: sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==}
|
resolution: {integrity: sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==}
|
||||||
engines: {node: '>=0.4'}
|
engines: {node: '>=0.4'}
|
||||||
|
@ -6,6 +6,9 @@ import {
|
|||||||
HttpStatus,
|
HttpStatus,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 异常类
|
||||||
|
*/
|
||||||
@Catch()
|
@Catch()
|
||||||
export class AllExceptionsFilter implements ExceptionFilter {
|
export class AllExceptionsFilter implements ExceptionFilter {
|
||||||
catch(exception: unknown, host: ArgumentsHost) {
|
catch(exception: unknown, host: ArgumentsHost) {
|
||||||
|
@ -7,6 +7,9 @@ import {
|
|||||||
import { Observable, map } from 'rxjs';
|
import { Observable, map } from 'rxjs';
|
||||||
import { ApiResponse } from '../response-format';
|
import { ApiResponse } from '../response-format';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规范返回格式
|
||||||
|
*/
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ResponseInterceptor<T>
|
export class ResponseInterceptor<T>
|
||||||
implements NestInterceptor<T, ApiResponse<T>>
|
implements NestInterceptor<T, ApiResponse<T>>
|
||||||
|
@ -14,6 +14,11 @@ export class AppConfigService implements OnModuleInit {
|
|||||||
this.checkRequiredVariables();
|
this.checkRequiredVariables();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取环境变量
|
||||||
|
* @param key 键值
|
||||||
|
* @param defaultValue 默认
|
||||||
|
*/
|
||||||
get<T = string>(key: string, defaultValue?: T): T | undefined {
|
get<T = string>(key: string, defaultValue?: T): T | undefined {
|
||||||
const value = this.nestConfigService.get<T>(key);
|
const value = this.nestConfigService.get<T>(key);
|
||||||
if (value === undefined || value === null) {
|
if (value === undefined || value === null) {
|
||||||
|
@ -22,7 +22,7 @@ export class AutoUpdateService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查是否有远程更新
|
* 检查远程更新
|
||||||
*/
|
*/
|
||||||
async checkForUpdates(): Promise<boolean> {
|
async checkForUpdates(): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
|
@ -28,6 +28,12 @@ export class PersistenceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将数据保存到本地 带格式化
|
||||||
|
* @param dataName 数据名 -> 文件夹名
|
||||||
|
* @param data 内容
|
||||||
|
* @param fileName 文件名
|
||||||
|
*/
|
||||||
public async writeDataLocal<T>(
|
public async writeDataLocal<T>(
|
||||||
dataName: string,
|
dataName: string,
|
||||||
data: T,
|
data: T,
|
||||||
@ -44,6 +50,11 @@ export class PersistenceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从本地读取数据 带解析
|
||||||
|
* @param dataName 数据名 -> 文件夹名
|
||||||
|
* @param fileName 文件名
|
||||||
|
*/
|
||||||
public async readDataLocal<T>(
|
public async readDataLocal<T>(
|
||||||
dataName: string,
|
dataName: string,
|
||||||
fileName: string,
|
fileName: string,
|
||||||
|
@ -63,7 +63,7 @@ export class RedisService implements OnModuleInit {
|
|||||||
|
|
||||||
this.client.on('ready', () => {
|
this.client.on('ready', () => {
|
||||||
this.isConnected = true;
|
this.isConnected = true;
|
||||||
this.logger.debug('Redis连接就绪!');
|
this.logger.log('Redis连接就绪!');
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.on('reconnecting', () => {
|
this.client.on('reconnecting', () => {
|
||||||
@ -71,6 +71,9 @@ export class RedisService implements OnModuleInit {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 等待redis就绪
|
||||||
|
*/
|
||||||
public async waitUntilReady(): Promise<void> {
|
public async waitUntilReady(): Promise<void> {
|
||||||
if (this.isConnected) return;
|
if (this.isConnected) return;
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
@ -80,6 +83,9 @@ export class RedisService implements OnModuleInit {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取单一redis实例
|
||||||
|
*/
|
||||||
public getClient(): Redis {
|
public getClient(): Redis {
|
||||||
if (!this.isConnected) {
|
if (!this.isConnected) {
|
||||||
this.logger.error('Redis未连接');
|
this.logger.error('Redis未连接');
|
||||||
@ -87,11 +93,20 @@ export class RedisService implements OnModuleInit {
|
|||||||
return this.client;
|
return this.client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 断开连接
|
||||||
|
*/
|
||||||
public async disconnect(): Promise<void> {
|
public async disconnect(): Promise<void> {
|
||||||
await this.client.quit();
|
await this.client.quit();
|
||||||
this.isConnected = false;
|
this.isConnected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 储存对象
|
||||||
|
* @param key 键
|
||||||
|
* @param value 键值
|
||||||
|
* @param ttl 缓存时间
|
||||||
|
*/
|
||||||
public async setObject<T>(
|
public async setObject<T>(
|
||||||
key: string,
|
key: string,
|
||||||
value: T,
|
value: T,
|
||||||
@ -104,6 +119,10 @@ export class RedisService implements OnModuleInit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从redis中获取对象
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
public async getObject<T>(key: string): Promise<T | undefined> {
|
public async getObject<T>(key: string): Promise<T | undefined> {
|
||||||
const serialized = await this.client.get(key);
|
const serialized = await this.client.get(key);
|
||||||
if (!serialized) return undefined;
|
if (!serialized) return undefined;
|
||||||
@ -111,6 +130,11 @@ export class RedisService implements OnModuleInit {
|
|||||||
return RedisUtils.reviveDates(deserialized);
|
return RedisUtils.reviveDates(deserialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新redis中的呃对象
|
||||||
|
* @param key
|
||||||
|
* @param updates
|
||||||
|
*/
|
||||||
public async update<T>(key: string, updates: T): Promise<T> {
|
public async update<T>(key: string, updates: T): Promise<T> {
|
||||||
const existing = await this.getObject<T>(key);
|
const existing = await this.getObject<T>(key);
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
@ -121,6 +145,11 @@ export class RedisService implements OnModuleInit {
|
|||||||
return updated;
|
return updated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从本地或redis获取对象
|
||||||
|
* @param key 键 / 文件夹名
|
||||||
|
* @param fileName 文件名
|
||||||
|
*/
|
||||||
public async fetch<T>(key: string, fileName: string): Promise<T | undefined> {
|
public async fetch<T>(key: string, fileName: string): Promise<T | undefined> {
|
||||||
const data = await this.getObject<T>(key);
|
const data = await this.getObject<T>(key);
|
||||||
if (data) return data;
|
if (data) return data;
|
||||||
@ -134,6 +163,12 @@ export class RedisService implements OnModuleInit {
|
|||||||
this.logger.error(`数据${key}不存在`);
|
this.logger.error(`数据${key}不存在`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存对象
|
||||||
|
* @param key 键
|
||||||
|
* @param data 内容
|
||||||
|
* @param fileName 文件名
|
||||||
|
*/
|
||||||
public async persistData<T>(
|
public async persistData<T>(
|
||||||
key: string,
|
key: string,
|
||||||
data: T,
|
data: T,
|
||||||
|
@ -56,6 +56,9 @@ export class SystemService {
|
|||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查更新
|
||||||
|
*/
|
||||||
async checkUpdate(): Promise<void> {
|
async checkUpdate(): Promise<void> {
|
||||||
const updated = await this.autoUpdateService.checkForUpdates();
|
const updated = await this.autoUpdateService.checkForUpdates();
|
||||||
if (updated) {
|
if (updated) {
|
||||||
|
18
src/types/ws.ts
Normal file
18
src/types/ws.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import WebSocket from 'ws';
|
||||||
|
|
||||||
|
export interface AuthenticatedSocket extends WebSocket {
|
||||||
|
isAuthed?: boolean;
|
||||||
|
clientId?: string;
|
||||||
|
heartbeat?: NodeJS.Timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WSMessage {
|
||||||
|
type: string;
|
||||||
|
[key: string]: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AuthMessage extends WSMessage {
|
||||||
|
type: 'auth';
|
||||||
|
secret: string;
|
||||||
|
clientId: string;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user