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:
@@ -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 {
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ describe('makeDefaultOneBotConfig', () => {
|
||||
expect(config.networks.httpClients).toEqual([]);
|
||||
expect(config.networks.wsServers).toHaveLength(1);
|
||||
expect(config.networks.wsServers[0].port).toBe(3001);
|
||||
expect(config.networks.wsServers[0].role).toBe('universal');
|
||||
expect(config.networks.wsServers[0].role).toBe('Universal');
|
||||
expect(config.networks.wsServers[0].accessToken).toMatch(TOKEN_PATTERN);
|
||||
expect(config.networks.wsServers[0].messageFormat).toBe('array');
|
||||
expect(config.networks.wsServers[0].reportSelfMessage).toBe(false);
|
||||
@@ -128,6 +128,30 @@ describe('loadOneBotConfig', () => {
|
||||
expect(reloaded.networks.httpClients[0].reportSelfMessage).toBe(true);
|
||||
});
|
||||
|
||||
it('migrates legacy lowercase WsRole values into canonical uppercase form', () => {
|
||||
const uin = '10005';
|
||||
const dir = path.join(tempDir, 'config');
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
fs.writeFileSync(path.join(dir, `onebot_${uin}.json`), JSON.stringify({
|
||||
networks: {
|
||||
httpServers: [],
|
||||
httpClients: [],
|
||||
wsServers: [
|
||||
{ name: 'ws-legacy', host: '0.0.0.0', port: 3201, path: '/', role: 'universal', messageFormat: 'array', reportSelfMessage: false },
|
||||
],
|
||||
wsClients: [
|
||||
{ name: 'wsc-legacy', url: 'ws://127.0.0.1:8080', role: 'event', messageFormat: 'array', reportSelfMessage: false },
|
||||
],
|
||||
},
|
||||
}), 'utf8');
|
||||
|
||||
const config = loadOneBotConfig(uin);
|
||||
expect(config.networks.wsServers).toHaveLength(1);
|
||||
expect(config.networks.wsServers[0].role).toBe('Universal');
|
||||
expect(config.networks.wsClients).toHaveLength(1);
|
||||
expect(config.networks.wsClients[0].role).toBe('Event');
|
||||
});
|
||||
|
||||
it('auto-generates names for legacy entries that lack one', () => {
|
||||
const uin = '10004';
|
||||
const dir = path.join(tempDir, 'config');
|
||||
|
||||
@@ -336,7 +336,7 @@ export function ConfigPage({
|
||||
/>
|
||||
<SelectField
|
||||
label="角色"
|
||||
value={item.role ?? 'universal'}
|
||||
value={item.role ?? 'Universal'}
|
||||
options={WS_ROLE_OPTIONS}
|
||||
onChange={(v) => patch({ role: v })}
|
||||
className="sm:w-32"
|
||||
@@ -355,7 +355,7 @@ export function ConfigPage({
|
||||
host: '0.0.0.0',
|
||||
port: 3001,
|
||||
path: '/',
|
||||
role: 'universal' as WsRole,
|
||||
role: 'Universal' as WsRole,
|
||||
accessToken: generateAccessToken(),
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
@@ -381,7 +381,7 @@ export function ConfigPage({
|
||||
/>
|
||||
<SelectField
|
||||
label="角色"
|
||||
value={item.role ?? 'universal'}
|
||||
value={item.role ?? 'Universal'}
|
||||
options={WS_ROLE_OPTIONS}
|
||||
onChange={(v) => patch({ role: v })}
|
||||
className="sm:w-32"
|
||||
@@ -405,7 +405,7 @@ export function ConfigPage({
|
||||
defaultEntry={(suffix) => ({
|
||||
name: `wsclient-${suffix}`,
|
||||
url: 'ws://127.0.0.1:8080/ws',
|
||||
role: 'universal' as WsRole,
|
||||
role: 'Universal' as WsRole,
|
||||
reconnectIntervalMs: 5000,
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
@@ -559,9 +559,9 @@ const ADAPTER_REPORT_OPTIONS: ReadonlyArray<SegmentedOption<'on' | 'off'>> = [
|
||||
];
|
||||
|
||||
const WS_ROLE_OPTIONS: ReadonlyArray<SegmentedOption<WsRole>> = [
|
||||
{ value: 'universal', label: 'universal' },
|
||||
{ value: 'event', label: 'event' },
|
||||
{ value: 'api', label: 'api' },
|
||||
{ value: 'Universal', label: 'Universal' },
|
||||
{ value: 'Event', label: 'Event' },
|
||||
{ value: 'Api', label: 'Api' },
|
||||
];
|
||||
|
||||
interface BaseAdapter {
|
||||
|
||||
@@ -20,7 +20,7 @@ export interface HookProcessInfo {
|
||||
}
|
||||
|
||||
export type MessageFormat = 'array' | 'string';
|
||||
export type WsRole = 'api' | 'event' | 'universal';
|
||||
export type WsRole = 'Api' | 'Event' | 'Universal';
|
||||
|
||||
interface NetworkBase {
|
||||
name: string;
|
||||
|
||||
Reference in New Issue
Block a user