fix(onebot): hot-reload HTTP server accessToken without re-binding
`HttpServerAdapter.startServer` baked `accessToken` into the
`createServer` callback's closure, then relied on
`bindingSignature` including the token to trigger a
`close()`+`open()` round-trip on every change. Two problems:
1. The three sibling adapters (`ws-server`, `http-post`,
`ws-client`) all read `this.config.accessToken` inline at the
point of use, so the HTTP-server's closure-capture pattern was
the odd one out.
2. `server.close()` is asynchronous; the immediately-following
`server.listen()` on the same port can race with the OS
releasing it. When it loses, the new listener fails with
EADDRINUSE (logged via `server.on('error', …)` but not bubbled
up), the old server is gone, and the operator's `reload` looks
successful while the endpoint is actually broken — which lines
up with the "token didn't hot-reload" reports.
Drop the closure capture: `handleRequest` reads
`this.config.path` and `this.config.accessToken` fresh per request
(`reload()` already replaces `this.config` in place via
`structuredClone(next)`). Remove `accessToken` from
`bindingSignature` so a token change no longer triggers a port
re-bind. `host`/`port`/`path` stay in the signature because Node's
`Server.listen()` binds the socket on those.
Four hot-reload tests pin the new contract: token swap, clearing
to empty, setting from empty, and "token-only change returns
`NetworkReloadType.Normal`" (no Reopened/Closed). All four hit a
single OS-assigned port and verify the port is unchanged after
reload — a re-bind would either land on a different port or fail.
core: 613/613 tests pass (+4), typecheck clean.
This commit is contained in:
2 files changed
+162
-7
No files matched your search
@@ -79,11 +79,8 @@ export class HttpServerAdapter extends IOneBotNetworkAdapter<HttpServerNetwork>
|
||||
onEvent(_event: JsonObject, _payload: DispatchPayload): void { /* no-op */ }
|
||||
|
||||
private startServer(): void {
|
||||
const expectedPath = normalizePath(this.config.path ?? '/');
|
||||
const accessToken = this.config.accessToken ?? '';
|
||||
|
||||
const server = createServer((req, res) => {
|
||||
void this.handleRequest(expectedPath, accessToken, req, res);
|
||||
void this.handleRequest(req, res);
|
||||
});
|
||||
this.server = server;
|
||||
|
||||
@@ -93,7 +90,7 @@ export class HttpServerAdapter extends IOneBotNetworkAdapter<HttpServerNetwork>
|
||||
this.name,
|
||||
this.config.host ?? '0.0.0.0',
|
||||
this.config.port,
|
||||
expectedPath,
|
||||
normalizePath(this.config.path ?? '/'),
|
||||
);
|
||||
});
|
||||
server.on('error', (err) => {
|
||||
@@ -103,7 +100,15 @@ export class HttpServerAdapter extends IOneBotNetworkAdapter<HttpServerNetwork>
|
||||
server.listen(this.config.port, this.config.host ?? '0.0.0.0');
|
||||
}
|
||||
|
||||
private async handleRequest(expectedPath: string, accessToken: string, req: IncomingMessage, res: ServerResponse): Promise<void> {
|
||||
private async handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
|
||||
// Read `path` + `accessToken` fresh from `this.config` on every
|
||||
// request so config hot-reloads (`reload()` overwrites `this.config`
|
||||
// in place) take effect on the very next request without
|
||||
// re-binding the listener. Matches the ws-server / http-post /
|
||||
// ws-client adapters, which all read `this.config.accessToken`
|
||||
// inline at the point of use.
|
||||
const expectedPath = normalizePath(this.config.path ?? '/');
|
||||
const accessToken = this.config.accessToken ?? '';
|
||||
const parsedUrl = new URL(req.url ?? '/', 'http://127.0.0.1');
|
||||
const incomingPath = parsedUrl.pathname;
|
||||
|
||||
@@ -224,7 +229,16 @@ export class HttpServerAdapter extends IOneBotNetworkAdapter<HttpServerNetwork>
|
||||
}
|
||||
|
||||
function bindingSignature(net: HttpServerNetwork): string {
|
||||
return `${net.host ?? '0.0.0.0'}:${net.port}${normalizePath(net.path ?? '/')}#${net.accessToken ?? ''}`;
|
||||
// `accessToken` deliberately omitted: it's read per-request from
|
||||
// `this.config`, so a token change doesn't need to re-bind the
|
||||
// listener. Including it here would cause a `close()` + `listen()`
|
||||
// round trip on every token edit — a needless port-rebind that, on
|
||||
// some hosts, races with the OS releasing the port and lands the
|
||||
// server in a half-broken state where the old token still
|
||||
// authenticates because the EADDRINUSE-failing new server never
|
||||
// replaced the old one. `host`/`port`/`path` still gate a restart
|
||||
// because Node's HTTP `Server.listen()` binds the socket on those.
|
||||
return `${net.host ?? '0.0.0.0'}:${net.port}${normalizePath(net.path ?? '/')}`;
|
||||
}
|
||||
|
||||
function readRequestBody(req: IncomingMessage, maxBytes = 2 * 1024 * 1024): Promise<string> {
|
||||
|
||||
Reference in new issue
Block a user