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:
1 parent
4d97f02fe9
commit
b62a6edd37
6 files changed
+57
-28
No files matched your search
@@ -35,7 +35,7 @@ export function makeDefaultOneBotConfig(): OneBotConfig {
|
|||||||
host: '0.0.0.0',
|
host: '0.0.0.0',
|
||||||
port: 3001,
|
port: 3001,
|
||||||
path: '/',
|
path: '/',
|
||||||
role: 'universal',
|
role: 'Universal',
|
||||||
accessToken: generateAccessToken(),
|
accessToken: generateAccessToken(),
|
||||||
messageFormat: 'array',
|
messageFormat: 'array',
|
||||||
reportSelfMessage: false,
|
reportSelfMessage: false,
|
||||||
@@ -132,7 +132,7 @@ function wsServerToJson(n: WsServerNetwork): JsonObject {
|
|||||||
out.host = n.host ?? '0.0.0.0';
|
out.host = n.host ?? '0.0.0.0';
|
||||||
out.port = n.port;
|
out.port = n.port;
|
||||||
out.path = n.path ?? '/';
|
out.path = n.path ?? '/';
|
||||||
out.role = n.role ?? 'universal';
|
out.role = n.role ?? 'Universal';
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,7 +140,7 @@ function wsClientToJson(n: WsClientNetwork): JsonObject {
|
|||||||
const out: JsonObject = {};
|
const out: JsonObject = {};
|
||||||
applyBase(out, n);
|
applyBase(out, n);
|
||||||
out.url = n.url;
|
out.url = n.url;
|
||||||
out.role = n.role ?? 'universal';
|
out.role = n.role ?? 'Universal';
|
||||||
out.reconnectIntervalMs =
|
out.reconnectIntervalMs =
|
||||||
typeof n.reconnectIntervalMs === 'number' && Number.isFinite(n.reconnectIntervalMs)
|
typeof n.reconnectIntervalMs === 'number' && Number.isFinite(n.reconnectIntervalMs)
|
||||||
? Math.max(1000, Math.trunc(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'),
|
host: asString(value.host, '0.0.0.0'),
|
||||||
port,
|
port,
|
||||||
path: asString(value.path, '/'),
|
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({
|
return clean({
|
||||||
...parseBase(value, defaults),
|
...parseBase(value, defaults),
|
||||||
url,
|
url,
|
||||||
role: asRole(value.role, 'universal'),
|
role: asRole(value.role, 'Universal'),
|
||||||
reconnectIntervalMs: Math.max(1000, reconnectIntervalMs),
|
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 {
|
function asRole(value: unknown, fallback: WsRole): WsRole {
|
||||||
const text = asString(value, fallback);
|
// Accept both uppercase (canonical) and lowercase (legacy on-disk) forms.
|
||||||
return text === 'api' || text === 'event' || text === 'universal' ? text : fallback;
|
// 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 {
|
function asString(value: unknown, fallback = ''): string {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export interface OneBotRequest {
|
|||||||
echo?: JsonValue;
|
echo?: JsonValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type WsRole = 'api' | 'event' | 'universal';
|
export type WsRole = 'Api' | 'Event' | 'Universal';
|
||||||
|
|
||||||
export type MessageFormat = 'array' | 'string';
|
export type MessageFormat = 'array' | 'string';
|
||||||
|
|
||||||
|
|||||||
@@ -193,14 +193,14 @@ export class WsTransport {
|
|||||||
const dispatch = payload ?? buildDispatchPayload(event);
|
const dispatch = payload ?? buildDispatchPayload(event);
|
||||||
|
|
||||||
for (const conn of this.forwardConnections.values()) {
|
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);
|
const json = pickDispatchJson(dispatch, conn.options);
|
||||||
if (json === null) continue;
|
if (json === null) continue;
|
||||||
safeSend(conn.socket, json);
|
safeSend(conn.socket, json);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const conn of this.reverseConnections.values()) {
|
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);
|
const json = pickDispatchJson(dispatch, conn.options);
|
||||||
if (json === null) continue;
|
if (json === null) continue;
|
||||||
safeSend(conn.socket, json);
|
safeSend(conn.socket, json);
|
||||||
@@ -267,9 +267,9 @@ export class WsTransport {
|
|||||||
if (this.reverseConnections.has(name) || this.reconnectTimers.has(name)) return;
|
if (this.reverseConnections.has(name) || this.reconnectTimers.has(name)) return;
|
||||||
|
|
||||||
const network = state.network;
|
const network = state.network;
|
||||||
const role = network.role ?? 'universal';
|
const role = network.role ?? 'Universal';
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
'User-Agent': 'OneBot',
|
'User-Agent': 'OneBot/11',
|
||||||
'X-Self-ID': this.context.uin,
|
'X-Self-ID': this.context.uin,
|
||||||
'X-Client-Role': role,
|
'X-Client-Role': role,
|
||||||
};
|
};
|
||||||
@@ -316,7 +316,7 @@ export class WsTransport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async handleApiMessage(socket: WebSocket, role: WsRole, raw: Buffer | string): Promise<void> {
|
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);
|
const text = rawDataToString(raw);
|
||||||
if (!text) return;
|
if (!text) return;
|
||||||
@@ -326,7 +326,7 @@ export class WsTransport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private sendBootstrapMetaEvents(socket: WebSocket, role: WsRole, options: EventReportOptions): void {
|
private sendBootstrapMetaEvents(socket: WebSocket, role: WsRole, options: EventReportOptions): void {
|
||||||
if (role !== 'event' && role !== 'universal') return;
|
if (role !== 'Event' && role !== 'Universal') return;
|
||||||
|
|
||||||
const events = [
|
const events = [
|
||||||
this.context.buildLifecycleEvent('connect'),
|
this.context.buildLifecycleEvent('connect'),
|
||||||
@@ -343,9 +343,9 @@ export class WsTransport {
|
|||||||
|
|
||||||
function classifyForwardRole(request: IncomingMessage): WsRole {
|
function classifyForwardRole(request: IncomingMessage): WsRole {
|
||||||
const path = parseRequestPath(request.url ?? '/');
|
const path = parseRequestPath(request.url ?? '/');
|
||||||
if (path.endsWith('/api')) return 'api';
|
if (path.endsWith('/api')) return 'Api';
|
||||||
if (path.endsWith('/event')) return 'event';
|
if (path.endsWith('/event')) return 'Event';
|
||||||
return 'universal';
|
return 'Universal';
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseRequestPath(urlValue: string): string {
|
function parseRequestPath(urlValue: string): string {
|
||||||
@@ -375,7 +375,7 @@ function wsServerSignature(network: WsServerNetwork): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function wsClientSignature(network: WsClientNetwork): string {
|
function wsClientSignature(network: WsClientNetwork): string {
|
||||||
const role = network.role ?? 'universal';
|
const role = network.role ?? 'Universal';
|
||||||
const reconnectIntervalMs = Math.max(1000, network.reconnectIntervalMs ?? 5000);
|
const reconnectIntervalMs = Math.max(1000, network.reconnectIntervalMs ?? 5000);
|
||||||
return `${network.url}#${role}#${reconnectIntervalMs}#${network.accessToken ?? ''}`;
|
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) {
|
if (url.searchParams.get('access_token') === token) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch {}
|
} catch { }
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ describe('makeDefaultOneBotConfig', () => {
|
|||||||
expect(config.networks.httpClients).toEqual([]);
|
expect(config.networks.httpClients).toEqual([]);
|
||||||
expect(config.networks.wsServers).toHaveLength(1);
|
expect(config.networks.wsServers).toHaveLength(1);
|
||||||
expect(config.networks.wsServers[0].port).toBe(3001);
|
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].accessToken).toMatch(TOKEN_PATTERN);
|
||||||
expect(config.networks.wsServers[0].messageFormat).toBe('array');
|
expect(config.networks.wsServers[0].messageFormat).toBe('array');
|
||||||
expect(config.networks.wsServers[0].reportSelfMessage).toBe(false);
|
expect(config.networks.wsServers[0].reportSelfMessage).toBe(false);
|
||||||
@@ -128,6 +128,30 @@ describe('loadOneBotConfig', () => {
|
|||||||
expect(reloaded.networks.httpClients[0].reportSelfMessage).toBe(true);
|
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', () => {
|
it('auto-generates names for legacy entries that lack one', () => {
|
||||||
const uin = '10004';
|
const uin = '10004';
|
||||||
const dir = path.join(tempDir, 'config');
|
const dir = path.join(tempDir, 'config');
|
||||||
|
|||||||
@@ -336,7 +336,7 @@ export function ConfigPage({
|
|||||||
/>
|
/>
|
||||||
<SelectField
|
<SelectField
|
||||||
label="角色"
|
label="角色"
|
||||||
value={item.role ?? 'universal'}
|
value={item.role ?? 'Universal'}
|
||||||
options={WS_ROLE_OPTIONS}
|
options={WS_ROLE_OPTIONS}
|
||||||
onChange={(v) => patch({ role: v })}
|
onChange={(v) => patch({ role: v })}
|
||||||
className="sm:w-32"
|
className="sm:w-32"
|
||||||
@@ -355,7 +355,7 @@ export function ConfigPage({
|
|||||||
host: '0.0.0.0',
|
host: '0.0.0.0',
|
||||||
port: 3001,
|
port: 3001,
|
||||||
path: '/',
|
path: '/',
|
||||||
role: 'universal' as WsRole,
|
role: 'Universal' as WsRole,
|
||||||
accessToken: generateAccessToken(),
|
accessToken: generateAccessToken(),
|
||||||
messageFormat: 'array',
|
messageFormat: 'array',
|
||||||
reportSelfMessage: false,
|
reportSelfMessage: false,
|
||||||
@@ -381,7 +381,7 @@ export function ConfigPage({
|
|||||||
/>
|
/>
|
||||||
<SelectField
|
<SelectField
|
||||||
label="角色"
|
label="角色"
|
||||||
value={item.role ?? 'universal'}
|
value={item.role ?? 'Universal'}
|
||||||
options={WS_ROLE_OPTIONS}
|
options={WS_ROLE_OPTIONS}
|
||||||
onChange={(v) => patch({ role: v })}
|
onChange={(v) => patch({ role: v })}
|
||||||
className="sm:w-32"
|
className="sm:w-32"
|
||||||
@@ -405,7 +405,7 @@ export function ConfigPage({
|
|||||||
defaultEntry={(suffix) => ({
|
defaultEntry={(suffix) => ({
|
||||||
name: `wsclient-${suffix}`,
|
name: `wsclient-${suffix}`,
|
||||||
url: 'ws://127.0.0.1:8080/ws',
|
url: 'ws://127.0.0.1:8080/ws',
|
||||||
role: 'universal' as WsRole,
|
role: 'Universal' as WsRole,
|
||||||
reconnectIntervalMs: 5000,
|
reconnectIntervalMs: 5000,
|
||||||
messageFormat: 'array',
|
messageFormat: 'array',
|
||||||
reportSelfMessage: false,
|
reportSelfMessage: false,
|
||||||
@@ -559,9 +559,9 @@ const ADAPTER_REPORT_OPTIONS: ReadonlyArray<SegmentedOption<'on' | 'off'>> = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
const WS_ROLE_OPTIONS: ReadonlyArray<SegmentedOption<WsRole>> = [
|
const WS_ROLE_OPTIONS: ReadonlyArray<SegmentedOption<WsRole>> = [
|
||||||
{ value: 'universal', label: 'universal' },
|
{ value: 'Universal', label: 'Universal' },
|
||||||
{ value: 'event', label: 'event' },
|
{ value: 'Event', label: 'Event' },
|
||||||
{ value: 'api', label: 'api' },
|
{ value: 'Api', label: 'Api' },
|
||||||
];
|
];
|
||||||
|
|
||||||
interface BaseAdapter {
|
interface BaseAdapter {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export interface HookProcessInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type MessageFormat = 'array' | 'string';
|
export type MessageFormat = 'array' | 'string';
|
||||||
export type WsRole = 'api' | 'event' | 'universal';
|
export type WsRole = 'Api' | 'Event' | 'Universal';
|
||||||
|
|
||||||
interface NetworkBase {
|
interface NetworkBase {
|
||||||
name: string;
|
name: string;
|
||||||
|
|||||||
Reference in new issue
Block a user