Skip to content

Commit

Permalink
refa: separate @koishijs/plugin-server
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 8, 2023
1 parent 9b910f1 commit 9bfabd9
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 33 deletions.
29 changes: 1 addition & 28 deletions packages/koishi/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
// This file is only intended for users who do not use CLI.

import { Context, defineProperty, Schema } from '@koishijs/core'
import { Router, WebSocketLayer } from '@satorijs/router'
import Loader from '@koishijs/loader'

import '@satorijs/satori'

export { Loader, Router, WebSocketLayer }
export { Loader }

export * from '@koishijs/core'
export * from '@koishijs/loader'
export * from '@koishijs/utils'

Context.service('router', Router)

declare module '@koishijs/core' {
namespace Context {
interface Config extends Config.Network {}

namespace Config {
interface Network {
host?: string
port?: number
maxPort?: number
selfUrl?: string
}

interface Static extends Schema<Config> {
Network: Schema<Network>
}
}
}
}

defineProperty(Context.Config, 'Network', Router.Config.description('网络设置'))

Context.Config.list.unshift(Context.Config.Network)
26 changes: 26 additions & 0 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Logger } from '@koishijs/core'
import { Loader } from './shared'
import { promises as fs } from 'fs'
// @ts-ignore
import { dependencies } from '../package.json'
import * as dotenv from 'dotenv'
import ns from 'ns-require'

Expand Down Expand Up @@ -29,6 +31,30 @@ export default class NodeLoader extends Loader {
})
}

async migrate() {
if (this.config['port']) {
const { port, host, maxPort, selfUrl } = this.config as any
delete this.config['port']
delete this.config['host']
delete this.config['maxPort']
delete this.config['selfUrl']
this.config.plugins = {
server: { port, host, maxPort, selfUrl },
...this.config.plugins,
}
try {
const version = dependencies['@koishijs/plugin-server']
const data = JSON.parse(await fs.readFile('package.json', 'utf8'))
data.dependencies['@koishijs/plugin-server'] = version
data.dependencies = Object.fromEntries(Object.entries(data.dependencies).sort(([a], [b]) => a.localeCompare(b)))
await fs.writeFile('package.json', JSON.stringify(data, null, 2) + '\n')
} catch {
logger.warn('cannot find package.json, please install @koishijs/plugin-server manually')
}
}
await super.migrate()
}

async readConfig() {
// remove local env variables
for (const key of this.localKeys) {
Expand Down
10 changes: 7 additions & 3 deletions packages/loader/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export abstract class Loader {
throw new Error('config file not found')
}

migrate(plugins: Dict) {
private migrateGroup(plugins: Dict) {
const backup = { ...plugins }
for (const key in backup) delete plugins[key]
for (const key in backup) {
Expand All @@ -196,7 +196,7 @@ export abstract class Loader {
}
const [name] = key.split(':', 1)
const isGroup = name === 'group' || name === '~group'
if (isGroup) this.migrate(backup[key])
if (isGroup) this.migrateGroup(backup[key])
let ident = key.slice(name.length + 1)
if (ident && !this.names.has(ident)) {
this.names.add(ident)
Expand All @@ -208,6 +208,10 @@ export abstract class Loader {
}
}

async migrate() {
this.migrateGroup(this.config.plugins)
}

async readConfig() {
if (this.mime === 'application/yaml') {
this.config = yaml.load(await fs.readFile(this.filename, 'utf8')) as any
Expand All @@ -219,7 +223,7 @@ export abstract class Loader {
this.config = module.default || module
}

this.migrate(this.config.plugins)
await this.migrate()
if (this.writable) await this.writeConfig(true)
return new Context.Config(this.interpolate(this.config))
}
Expand Down
1 change: 1 addition & 0 deletions plugins/mock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"koishi": "^4.15.7"
},
"devDependencies": {
"@koishijs/plugin-server": "^0.1.2",
"koishi": "^4.15.7"
}
}
5 changes: 3 additions & 2 deletions plugins/mock/src/webhook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Context, Dict } from 'koishi'
import {} from '@koishijs/plugin-server'
import { Socket } from 'net'
import { IncomingMessage, ServerResponse } from 'http'

Expand All @@ -11,7 +12,7 @@ export namespace Webhook {
}

export class Webhook {
static inject = ['router']
static inject = ['server']

constructor(public ctx: Context) {}

Expand Down Expand Up @@ -73,7 +74,7 @@ export class Webhook {
if (typeof callback === 'function') callback()
return res
}
this.ctx.router._http.emit('request', req, res)
this.ctx.server._http.emit('request', req, res)
req.emit('data', body)
req.emit('end')
})
Expand Down
2 changes: 2 additions & 0 deletions plugins/server/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
tsconfig.tsbuildinfo
52 changes: 52 additions & 0 deletions plugins/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@koishijs/plugin-server",
"description": "Server service for Koishi",
"version": "0.1.2",
"main": "lib/index.js",
"module": "lib/index.mjs",
"typings": "lib/index.d.ts",
"files": [
"lib",
"src"
],
"author": "Shigma <[email protected]>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/cordisjs/server.git",
"directory": "packages/core"
},
"bugs": {
"url": "https://github.com/satorijs/satori/issues"
},
"homepage": "https://koishi.chat/plugins/develop/server.html",
"keywords": [
"bot",
"adapter",
"protocol",
"chatbot",
"koishi",
"router",
"server"
],
"koishi": {
"service": {
"implements": [
"server"
]
},
"description": {
"en": "Service & Router Service",
"zh": "服务器与路由服务"
}
},
"peerDependencies": {
"koishi": "^4.15.7"
},
"devDependencies": {
"koishi": "^4.15.7"
},
"dependencies": {
"@cordisjs/server": "^0.1.2"
}
}
5 changes: 5 additions & 0 deletions plugins/server/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# [@koishijs/plugin-server](https://koishi.chat/plugins/develop/server.html)

Server service for [Koishi](https://koishi.chat).

- [Documentation](https://koishi.chat/plugins/develop/server.html)
4 changes: 4 additions & 0 deletions plugins/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Server from '@cordisjs/server'

export default Server
export * from '@cordisjs/server'
10 changes: 10 additions & 0 deletions plugins/server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src",
},
"include": [
"src",
],
}

0 comments on commit 9bfabd9

Please sign in to comment.