diff --git a/build/register.js b/build/register.js index a74813f041..39e94721cc 100644 --- a/build/register.js +++ b/build/register.js @@ -68,7 +68,7 @@ require.extensions['.ts'] = (module, filename) => { format: 'cjs', loader: 'ts', charset: 'utf8', - target: 'es2020', + target: 'es2019', define, }) cache[filename] = code diff --git a/packages/cli/src/worker/index.ts b/packages/cli/src/worker/index.ts index d972e7cc8f..5dfe84b8a4 100644 --- a/packages/cli/src/worker/index.ts +++ b/packages/cli/src/worker/index.ts @@ -76,4 +76,4 @@ app.start().then(() => { for (const name in loader.cache) { loader.diagnose(name) } -}) +}, handleException) diff --git a/packages/koishi/package.json b/packages/koishi/package.json index 702bba20ec..676dad8bcd 100644 --- a/packages/koishi/package.json +++ b/packages/koishi/package.json @@ -49,6 +49,7 @@ "ns-require": "^1.1.2", "parseurl": "^1.3.3", "path-to-regexp": "^6.2.0", + "portfinder": "^1.0.28", "proxy-agent": "^5.0.0", "ws": "^8.6.0" } diff --git a/packages/koishi/src/patch.ts b/packages/koishi/src/patch.ts index 453fd53dc4..e18486de76 100644 --- a/packages/koishi/src/patch.ts +++ b/packages/koishi/src/patch.ts @@ -1,5 +1,6 @@ import { App, Context } from '@koishijs/core' import { trimSlash } from '@koishijs/utils' +import { getPortPromise } from 'portfinder' import ns from 'ns-require' declare module '@koishijs/core' { @@ -43,9 +44,16 @@ Context.prototype.plugin = function (this: Context, entry: any, config?: any) { const start = App.prototype.start App.prototype.start = async function (this: App, ...args) { - const { host, port, selfUrl } = this.options - if (selfUrl) this.options.selfUrl = trimSlash(selfUrl) - if (port) { + if (this.options.selfUrl) { + this.options.selfUrl = trimSlash(this.options.selfUrl) + } + + if (this.options.port) { + this.options.port = await getPortPromise({ + port: this.options.port, + stopPort: this.options.maxPort || this.options.port, + }) + const { host, port } = this.options await new Promise(resolve => this._httpServer.listen(port, host, resolve)) this.logger('app').info('server listening at %c', `http://${host}:${port}`) this.on('dispose', () => { @@ -54,5 +62,6 @@ App.prototype.start = async function (this: App, ...args) { this._httpServer?.close() }) } + return start.call(this, ...args) } diff --git a/packages/koishi/src/router.ts b/packages/koishi/src/router.ts index 743092ca97..31ec2105cb 100644 --- a/packages/koishi/src/router.ts +++ b/packages/koishi/src/router.ts @@ -32,6 +32,7 @@ declare module '@koishijs/core' { interface Network { host?: string port?: number + maxPort?: number selfUrl?: string } } @@ -41,6 +42,7 @@ declare module '@koishijs/core' { defineProperty(App.Config, 'Network', Schema.object({ host: Schema.string().default('localhost').description('要监听的 IP 地址。如果将此设置为 `0.0.0.0` 将监听所有地址,包括局域网和公网地址。'), port: Schema.natural().max(65535).description('要监听的端口。'), + maxPort: Schema.natural().max(65535).description('允许监听的最大端口号。'), selfUrl: Schema.string().role('url').description('应用暴露在公网的地址。部分插件 (例如 github 和 telegram) 需要用到。'), }).description('网络设置'))