Skip to content

Commit

Permalink
feat: 完成编译
Browse files Browse the repository at this point in the history
  • Loading branch information
CakmLexi committed Jun 24, 2024
1 parent ffeb0b6 commit 3b9edeb
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 58 deletions.
4 changes: 2 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ data.forEach(val => {
val.rules['camelcase'] = ['off']
}

// 排除掉plugins
// 排除掉plugins dist
if (Array.isArray(val.ignores)) {
val.ignores = val.ignores.filter((v) => !v.includes('plugins'))
val.ignores = val.ignores.filter((v) => !v.includes('plugins') && !v.includes('dist'))
}
newData.push(val)
})
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from '../src/index'
// export * from '../src/index'
export * from '../dist/index.js'
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
"imports": {
"#Karin": "./lib/index.js"
},
"main": "./lib/index.js",
"main": "./dist/index.js",
"workspaces": [
"plugins/**"
],
"scripts": {
"app": "node .",
"build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
"build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json && npm run fix",
"fix": "npx eslint dist/**/*.js --fix",
"delete": "pm2 delete ./config/config/pm2.yaml",
"dev": "tsx ./src/index.ts",
"init": "node lib/tools/install.js",
Expand Down
24 changes: 12 additions & 12 deletions src/core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import fs from 'fs'
import express, { Express } from 'express'
import { createServer } from 'http'
import { WebSocketServer } from 'ws'
// import Renderer from '../renderer/App.js'
// import connect from '../renderer/Wormhole.js'
// import HttpRenderer from '../renderer/Http.js'
import { render } from '../index'
import connect from '../renderer/wormhole'
import HttpRenderer from '../renderer/http'
import logger from '../utils/logger'
import common from '../utils/common'
import config from '../utils/config'
Expand All @@ -20,7 +20,7 @@ export default new (class Server {
WebSocketServer: WebSocketServer
RegExp: RegExp
constructor() {
this.reg = new RegExp('')
this.reg = /(?:)/
this.list = []
this.app = express()
this.server = createServer(this.app)
Expand Down Expand Up @@ -96,15 +96,15 @@ export default new (class Server {

const { enable, WormholeClient } = config.Server.HttpRender
if (enable) {
// this.static()
// if (WormholeClient) {
// connect(config)
// return this
// }
// const { host, post, token } = config.Server.HttpRender
this.static()
if (WormholeClient) {
connect()
return this
}
const { host, post, token } = config.Server.HttpRender
/** 注册渲染器 */
// const rd = new HttpRenderer(host, post, token)
// Renderer.app({ id: 'puppeteer', type: 'image', render: rd.render.bind(rd) })
const rd = new HttpRenderer(host, post, token)
render.app({ id: 'puppeteer', type: 'image', render: rd.render.bind(rd) })
}

return this
Expand Down
7 changes: 5 additions & 2 deletions src/db/level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = process.cwd() + '/data/db/Level'
/**
* @type {Level}
*/
export default new (class LevelDB extends Level {
class LevelDB extends Level {
id: string
constructor() {
super(path, { valueEncoding: 'json' })
Expand Down Expand Up @@ -35,4 +35,7 @@ export default new (class LevelDB extends Level {
async set(key: string, value: string) {
return await super.put(key, value)
}
})()
}

const level = new LevelDB()
export default level
4 changes: 2 additions & 2 deletions src/db/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ export default class Redis {
* @param {import("redis").RedisClientOptions} options
* @return {Promise<{status: 'ok', data: import("redis").RedisClientType} | {status: 'error', data: Error}>}
*/
async connect(options: redis.RedisClientOptions) {
async connect(options: redis.RedisClientOptions): Promise<{ status: 'ok'; data: redis.RedisClientType } | { status: 'error'; data: string }> {
const client = createClient(options)
try {
await client.connect()
return { status: 'ok', data: client }
return { status: 'ok', data: client as redis.RedisClientType }
} catch (error) {
return { status: 'error', data: error as string }
}
Expand Down
29 changes: 16 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
// 基本模块
import '../src/core/init'
import config from '../src/utils/config'
import logger from '../src/utils/logger'
import './core/init'
import config from './utils/config'
import logger from './utils/logger'

// 核心模块
import listener from '../src/core/listener'
import server from '../src/core/server'
import Plugin from '../src/core/plugin'
import PluginLoader from '../src/core/plugin.loader'
import listener from './core/listener'
import server from './core/server'
import Plugin from './core/plugin'
import PluginLoader from './core/plugin.loader'

// 工具类
import common from '../src/utils/common'
import segment from '../src/utils/segment'
import handler from '../src/utils/handler'
import common from './utils/common'
import segment from './utils/segment'
import handler from './utils/handler'
import ffmpeg from './utils/ffmpeg'
import exec from './utils/exec'
import button from './utils/button'
import update from './utils/update'
import YamlEditor from './utils/YamlEditor'
import render from './renderer/app'
import { KarinMessage } from './event/message'

// 适配器
import Puppeteer from '../src/renderer/server'
import OneBot11 from '../src/adapter/onebot/onebot11'
import Puppeteer from './renderer/server'
import OneBot11 from './adapter/onebot/onebot11'

// 数据库
import level from './db/level'
import Redis from './db/redis'
import { RedisClientType } from 'redis'

// proto
import { kritor } from 'kritor-proto'

// 初始化
server.init()
const redis = await new Redis().start()
const redis: RedisClientType = (await new Redis().start()) as RedisClientType
PluginLoader.load()
listener.emit('adapter', Puppeteer)
listener.emit('adapter', OneBot11)
Expand Down Expand Up @@ -64,4 +66,5 @@ export {
YamlEditor,
render as Renderer,
update as Update,
KarinMessage,
}
28 changes: 15 additions & 13 deletions src/renderer/wormhole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import WebSocket from 'ws'
import fs from 'fs'
import path from 'path'
import { URL } from 'url'
import Renderer from './App'
import HttpRenderer from './Http'
import HttpRenderer from './http'
import config from '../utils/config'
import { render } from '../index'

let ws
let reConnect
let ws: WebSocket
let reConnect: NodeJS.Timeout | undefined
const chunkSize = 1024 * 1024 * 3 // 文件分片大小

export default function connect(Cfg) {
let heartbeat
export default function connect() {
let heartbeat: string | number | NodeJS.Timeout | null | undefined
let index = 0
reConnect = undefined
const wsUrl = Cfg.Server.HttpRender.WormholeClient
const wsUrl = config.Server.HttpRender.WormholeClient
ws = new WebSocket(wsUrl)
ws.on('open', function open() {
logger.info('连接到wormhole服务器' + wsUrl)
Expand All @@ -23,24 +24,25 @@ export default function connect(Cfg) {
}, 30000) // 每30秒发送一次心跳
})

ws.on('message', function incoming(data) {
ws.on('message', msg => {
let data: any = ''
try {
data = JSON.parse(data)
data = JSON.parse(msg.toString())
} catch (error) {
logger.warn(`收到非法消息${data}`)
}
const echo = data.echo
switch (data.type) {
case 'msg': {
const { post, token, WormholeClient } = Cfg.Server.HttpRender
const { post, token, WormholeClient } = config.Server.HttpRender
const parsedUrl = new URL(WormholeClient)
const { hostname, port } = parsedUrl
const ishttps = WormholeClient.includes('wss://')
const host = `${ishttps ? 'https' : 'http'}://${hostname}${port ? `:${port}` : ''}/web/${data.date}`
logger.mark(`web渲染器已连接,地址:${host}`)
/** 注册渲染器 */
const rd = new HttpRenderer(host, post, token)
index = Renderer.app({ id: 'puppeteer', type: 'image', render: rd.render.bind(rd) })
index = render.app({ id: 'puppeteer', type: 'image', render: rd.render.bind(rd) })
break
}
case 'web':
Expand Down Expand Up @@ -142,7 +144,7 @@ export default function connect(Cfg) {

ws.on('close', function close() {
/** 卸载渲染器 */
index && Renderer.unapp(index)
index && render.unapp(index)
index = 0
if (heartbeat) {
clearInterval(heartbeat)
Expand All @@ -156,7 +158,7 @@ export default function connect(Cfg) {

ws.on('error', function error() {
/** 卸载渲染器 */
index && Renderer.unapp(index)
index && render.unapp(index)
index = 0
if (heartbeat) {
clearInterval(heartbeat)
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ export default new (class Common {
case 'forward':
logs.push(`[forward:${val.res_id}]`)
break
break
case 'xml':
logs.push(`[xml:${val.data}]`)
break
Expand Down
1 change: 1 addition & 0 deletions src/utils/kritor-proto.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'kritor-proto'
31 changes: 21 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"rootDir": "./src",
"moduleResolution": "Node",
"resolveJsonModule": true,
"target": "ESNext",
"esModuleInterop": true,
"moduleResolution": "node",
"outDir": "./dist",
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"baseUrl": ".",
"paths": {
"@/*": [
"src/*"
]
},
"rootDir": "./src",
"declaration": true,
"noImplicitAny": true,
"strict": true
},
"tsc-alias": {
"resolveFullPaths": true,
"resolveFullExtension": ".js"
},
"include": [
"src/**/*",
"src/**/*"
],
"exclude": [
"node_modules"
"node_modules",
"**/*.spec.ts"
]
}

0 comments on commit 3b9edeb

Please sign in to comment.