Canonicalize WsRole strings and migrate legacy values

Normalize OneBot WS role values to canonical PascalCase ('Api'|'Event'|'Universal') across core and web UI, and support auto-migration from legacy lowercase on-disk values. Updated types, defaults, serializers/parsers, and runtime checks in ws-transport, adjusted WebUI select options and default entries, and updated the client User-Agent string. Added a test to verify that legacy lowercase role values are migrated to the new canonical form on load.
This commit is contained in:
手瓜一十雪 committed 2026-05-09 10:21:47 +08:00
1 parent 4d97f02fe9
commit b62a6edd37
6 files changed
+57 -28

No files matched your search

+12 -7
View File
@@ -35,7 +35,7 @@ export function makeDefaultOneBotConfig(): OneBotConfig {
host: '0.0.0.0',
port: 3001,
path: '/',
role: 'universal',
role: 'Universal',
accessToken: generateAccessToken(),
messageFormat: 'array',
reportSelfMessage: false,
@@ -132,7 +132,7 @@ function wsServerToJson(n: WsServerNetwork): JsonObject {
out.host = n.host ?? '0.0.0.0';
out.port = n.port;
out.path = n.path ?? '/';
out.role = n.role ?? 'universal';
out.role = n.role ?? 'Universal';
return out;
}
@@ -140,7 +140,7 @@ function wsClientToJson(n: WsClientNetwork): JsonObject {
const out: JsonObject = {};
applyBase(out, n);
out.url = n.url;
out.role = n.role ?? 'universal';
out.role = n.role ?? 'Universal';
out.reconnectIntervalMs =
typeof n.reconnectIntervalMs === 'number' && Number.isFinite(n.reconnectIntervalMs)
? Math.max(1000, Math.trunc(n.reconnectIntervalMs))
@@ -284,7 +284,7 @@ function parseWsServer(value: JsonObject, defaults: AdapterDefaults): WsServerNe
host: asString(value.host, '0.0.0.0'),
port,
path: asString(value.path, '/'),
role: asRole(value.role, 'universal'),
role: asRole(value.role, 'Universal'),
});
}
@@ -295,7 +295,7 @@ function parseWsClient(value: JsonObject, defaults: AdapterDefaults): WsClientNe
return clean({
...parseBase(value, defaults),
url,
role: asRole(value.role, 'universal'),
role: asRole(value.role, 'Universal'),
reconnectIntervalMs: Math.max(1000, reconnectIntervalMs),
});
}
@@ -324,8 +324,13 @@ function clean<T extends Record<string, unknown>>(obj: T): T {
}
function asRole(value: unknown, fallback: WsRole): WsRole {
const text = asString(value, fallback);
return text === 'api' || text === 'event' || text === 'universal' ? text : fallback;
// Accept both uppercase (canonical) and lowercase (legacy on-disk) forms.
// Lowercase values are auto-migrated to uppercase on the next save.
const text = asString(value, fallback).toLowerCase();
if (text === 'api') return 'Api';
if (text === 'event') return 'Event';
if (text === 'universal') return 'Universal';
return fallback;
}
function asString(value: unknown, fallback = ''): string {
+1 -1
View File
@@ -19,7 +19,7 @@ export interface OneBotRequest {
echo?: JsonValue;
}
export type WsRole = 'api' | 'event' | 'universal';
export type WsRole = 'Api' | 'Event' | 'Universal';
export type MessageFormat = 'array' | 'string';
+11 -11
View File
@@ -193,14 +193,14 @@ export class WsTransport {
const dispatch = payload ?? buildDispatchPayload(event);
for (const conn of this.forwardConnections.values()) {
if (conn.role !== 'event' && conn.role !== 'universal') continue;
if (conn.role !== 'Event' && conn.role !== 'Universal') continue;
const json = pickDispatchJson(dispatch, conn.options);
if (json === null) continue;
safeSend(conn.socket, json);
}
for (const conn of this.reverseConnections.values()) {
if (conn.role !== 'event' && conn.role !== 'universal') continue;
if (conn.role !== 'Event' && conn.role !== 'Universal') continue;
const json = pickDispatchJson(dispatch, conn.options);
if (json === null) continue;
safeSend(conn.socket, json);
@@ -267,9 +267,9 @@ export class WsTransport {
if (this.reverseConnections.has(name) || this.reconnectTimers.has(name)) return;
const network = state.network;
const role = network.role ?? 'universal';
const role = network.role ?? 'Universal';
const headers: Record<string, string> = {
'User-Agent': 'OneBot',
'User-Agent': 'OneBot/11',
'X-Self-ID': this.context.uin,
'X-Client-Role': role,
};
@@ -316,7 +316,7 @@ export class WsTransport {
}
private async handleApiMessage(socket: WebSocket, role: WsRole, raw: Buffer | string): Promise<void> {
if (role !== 'api' && role !== 'universal') return;
if (role !== 'Api' && role !== 'Universal') return;
const text = rawDataToString(raw);
if (!text) return;
@@ -326,7 +326,7 @@ export class WsTransport {
}
private sendBootstrapMetaEvents(socket: WebSocket, role: WsRole, options: EventReportOptions): void {
if (role !== 'event' && role !== 'universal') return;
if (role !== 'Event' && role !== 'Universal') return;
const events = [
this.context.buildLifecycleEvent('connect'),
@@ -343,9 +343,9 @@ export class WsTransport {
function classifyForwardRole(request: IncomingMessage): WsRole {
const path = parseRequestPath(request.url ?? '/');
if (path.endsWith('/api')) return 'api';
if (path.endsWith('/event')) return 'event';
return 'universal';
if (path.endsWith('/api')) return 'Api';
if (path.endsWith('/event')) return 'Event';
return 'Universal';
}
function parseRequestPath(urlValue: string): string {
@@ -375,7 +375,7 @@ function wsServerSignature(network: WsServerNetwork): string {
}
function wsClientSignature(network: WsClientNetwork): string {
const role = network.role ?? 'universal';
const role = network.role ?? 'Universal';
const reconnectIntervalMs = Math.max(1000, network.reconnectIntervalMs ?? 5000);
return `${network.url}#${role}#${reconnectIntervalMs}#${network.accessToken ?? ''}`;
}
@@ -393,7 +393,7 @@ function isAuthorized(request: IncomingMessage, token: string): boolean {
if (url.searchParams.get('access_token') === token) {
return true;
}
} catch {}
} catch { }
return false;
}