Skip to content

Commit

Permalink
feat(core): support config.maxPort, fix #675
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 21, 2022
1 parent 56af3d6 commit bc402b0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ require.extensions['.ts'] = (module, filename) => {
format: 'cjs',
loader: 'ts',
charset: 'utf8',
target: 'es2020',
target: 'es2019',
define,
})
cache[filename] = code
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ app.start().then(() => {
for (const name in loader.cache) {
loader.diagnose(name)
}
})
}, handleException)
1 change: 1 addition & 0 deletions packages/koishi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
15 changes: 12 additions & 3 deletions packages/koishi/src/patch.ts
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down Expand Up @@ -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<void>(resolve => this._httpServer.listen(port, host, resolve))
this.logger('app').info('server listening at %c', `http://${host}:${port}`)
this.on('dispose', () => {
Expand All @@ -54,5 +62,6 @@ App.prototype.start = async function (this: App, ...args) {
this._httpServer?.close()
})
}

return start.call(this, ...args)
}
2 changes: 2 additions & 0 deletions packages/koishi/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare module '@koishijs/core' {
interface Network {
host?: string
port?: number
maxPort?: number
selfUrl?: string
}
}
Expand All @@ -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('网络设置'))

Expand Down

0 comments on commit bc402b0

Please sign in to comment.