refactor(bridge): extract BridgeInterface as the OneBot-facing seam

OneBot used to depend on the concrete Bridge class — 778 lines, ~99
public methods including PID management, raw packet dispatch, and cmd
handler registration that no external consumer should touch. Add an
explicit BridgeInterface (bridge/bridge-interface.ts) enumerating only
the surface OneBot actually reads (verified by grepping every bridge.*
access in onebot/), and have Bridge `implements BridgeInterface` so the
compiler enforces alignment.

Net effect at runtime: zero — Bridge's methods, fields, and bodies are
unchanged; the interface is a TypeScript-only declaration. Net effect
for typing: the OneBot package (instance, instance-context,
instance-rkey, manager, media-url-resolver, and the modules under
onebot/modules/) now imports `BridgeInterface` instead of `Bridge`, so
tests / fakes can satisfy the interface without subclassing or pulling
in the SQLite/native bits.

In-tree bridge actions (actions/*, bridge-contacts, highway) keep their
concrete `Bridge` typing — they legitimately need sendRawPacket and
other low-level entry points that intentionally stay off the
interface.
This commit is contained in:
DrSmoothl committed 2026-05-15 01:12:40 +08:00
1 parent 59cdfa98a2
commit e6b609dd4e
11 files changed
+167 -35

No files matched your search

+2 -2
View File
@@ -1,5 +1,5 @@
import type { WebHonorType } from '@/bridge/web/group-honor';
import type { Bridge } from '../bridge/bridge';
import type { BridgeInterface } from '../bridge/bridge-interface';
import type { ApiActionContext } from './api-handler';
import type { ConverterContext } from './event-converter';
import type { MediaStore } from './media-store';
@@ -55,7 +55,7 @@ export interface OneBotInstanceContext {
/** Self UIN parsed once, used in event payloads. */
selfId: number;
bridge: Bridge;
bridge: BridgeInterface;
messageStore: MessageStore;
mediaStore: MediaStore;
+7 -6
View File
@@ -1,5 +1,6 @@
import type { MessageElement } from '../bridge/events';
import type { Bridge, DownloadRKeyInfo } from '../bridge/bridge';
import type { DownloadRKeyInfo } from '../bridge/bridge';
import type { BridgeInterface } from '../bridge/bridge-interface';
import { createLogger } from '../utils/logger';
const log = createLogger('OneBot');
@@ -26,7 +27,7 @@ export class RKeyCache {
private cache = new Map<number, CachedRKey>();
private lastRefreshAttempt = 0;
warmUp(bridge: Bridge, uin: string): void {
warmUp(bridge: BridgeInterface, uin: string): void {
bridge.fetchDownloadRKeys().then(
(rkeys) => {
this.updateCache(rkeys);
@@ -38,7 +39,7 @@ export class RKeyCache {
);
}
resolveImageUrl(bridge: Bridge, element: MessageElement, isGroup: boolean): string {
resolveImageUrl(bridge: BridgeInterface, element: MessageElement, isGroup: boolean): string {
const url = element.imageUrl ?? '';
if (!urlNeedsRKey(url)) return url;
@@ -54,7 +55,7 @@ export class RKeyCache {
* Resolve a URL for video/record/file media elements.
* Appends the correct RKey type based on media kind and group/private context.
*/
resolveMediaUrl(bridge: Bridge, element: MessageElement, isGroup: boolean): string {
resolveMediaUrl(bridge: BridgeInterface, element: MessageElement, isGroup: boolean): string {
const url = element.url ?? '';
if (!url || !urlNeedsRKey(url)) return url;
@@ -93,12 +94,12 @@ export class RKeyCache {
}
}
private findRKey(bridge: Bridge, isGroup: boolean): string | null {
private findRKey(bridge: BridgeInterface, isGroup: boolean): string | null {
const primaryType = isGroup ? GROUP_IMAGE_RKEY_TYPE : PRIVATE_IMAGE_RKEY_TYPE;
return this.findRKeyForType(bridge, primaryType);
}
private findRKeyForType(bridge: Bridge, primaryType: number): string | null {
private findRKeyForType(bridge: BridgeInterface, primaryType: number): string | null {
const now = Math.floor(Date.now() / 1000);
const tryFind = (type: number): string | null => {
+3 -3
View File
@@ -1,5 +1,5 @@
import path from 'path';
import type { Bridge } from '../bridge/bridge';
import type { BridgeInterface } from '../bridge/bridge-interface';
import { ApiHandler } from './api-handler';
import type { ConverterContext } from './event-converter';
import { MediaIndexer } from './media-indexer';
@@ -26,7 +26,7 @@ const log = createLogger('Event');
export class OneBotInstance {
readonly uin: string;
private readonly bridge: Bridge;
private readonly bridge: BridgeInterface;
private readonly apiHandler: ApiHandler;
private readonly converterCtx: ConverterContext;
private readonly messageStore: MessageStore;
@@ -43,7 +43,7 @@ export class OneBotInstance {
get nickname(): string { return this.bridge.identity.nickname; }
constructor(uin: string, bridge: Bridge, config: OneBotConfig) {
constructor(uin: string, bridge: BridgeInterface, config: OneBotConfig) {
this.uin = uin;
this.bridge = bridge;
+3 -3
View File
@@ -1,4 +1,4 @@
import type { Bridge } from '../bridge/bridge';
import type { BridgeInterface } from '../bridge/bridge-interface';
import type { BridgeManager } from '../bridge/manager';
import { loadOneBotConfig } from './config';
import { OneBotInstance } from './instance';
@@ -48,7 +48,7 @@ export class OneBotManager {
this.instances.clear();
}
private onSessionStarted(uin: string, bridge: Bridge): void {
private onSessionStarted(uin: string, bridge: BridgeInterface): void {
if (this.instances.has(uin)) return;
const config = loadOneBotConfig(uin);
@@ -83,7 +83,7 @@ export class OneBotManager {
}
async function warmUpBridgeState(uin: string, bridge: Bridge): Promise<void> {
async function warmUpBridgeState(uin: string, bridge: BridgeInterface): Promise<void> {
const selfUin = parseInt(uin, 10) || 0;
let selfResolved = false;
@@ -12,13 +12,13 @@
//
// Used as the `mediaUrlResolver` callback in `ConverterContext`.
import type { Bridge } from '../bridge/bridge';
import type { BridgeInterface } from '../bridge/bridge-interface';
import type { MessageElement } from '../bridge/events';
import type { RKeyCache } from './instance-rkey';
export class MediaUrlResolver {
constructor(
private readonly bridge: Bridge,
private readonly bridge: BridgeInterface,
private readonly rkeyCache: RKeyCache,
) {}
@@ -1,4 +1,4 @@
import type { Bridge } from '../../bridge/bridge';
import type { BridgeInterface } from '../../bridge/bridge-interface';
import type { IdentityService } from '../../bridge/identity-service';
import type { OneBotInstanceContext } from '../instance-context';
import type { JsonObject } from '../types';
@@ -11,7 +11,7 @@ export function getLoginInfo(ref: OneBotInstanceContext): { userId: number; nick
// Keep refreshes scoped. A broad "refresh all members in all groups" call can
// turn one OneBot request into N OIDB calls, which is risky for busy clients.
async function refreshSingleGroupMembers(bridge: Bridge, groupId: number): Promise<void> {
async function refreshSingleGroupMembers(bridge: BridgeInterface, groupId: number): Promise<void> {
try {
await bridge.fetchGroupMemberList(groupId);
} catch {
@@ -19,7 +19,7 @@ async function refreshSingleGroupMembers(bridge: Bridge, groupId: number): Promi
}
}
export async function getFriendList(bridge: Bridge): Promise<JsonObject[]> {
export async function getFriendList(bridge: BridgeInterface): Promise<JsonObject[]> {
try {
const friends = await bridge.fetchFriendList();
return friends.map(f => ({
@@ -37,7 +37,7 @@ export async function getFriendList(bridge: Bridge): Promise<JsonObject[]> {
}
export async function getGroupList(
bridge: Bridge,
bridge: BridgeInterface,
noCache?: boolean,
): Promise<JsonObject[]> {
try {
@@ -56,7 +56,7 @@ export async function getGroupList(
}
export async function getGroupInfo(
bridge: Bridge,
bridge: BridgeInterface,
groupId: number,
noCache?: boolean,
): Promise<JsonObject | null> {
@@ -78,7 +78,7 @@ export async function getGroupInfo(
}
export async function getGroupMemberList(
bridge: Bridge,
bridge: BridgeInterface,
groupId: number,
noCache?: boolean,
): Promise<JsonObject[]> {
@@ -96,7 +96,7 @@ export async function getGroupMemberList(
}
export async function getGroupMemberInfo(
bridge: Bridge,
bridge: BridgeInterface,
groupId: number,
userId: number,
noCache?: boolean,
@@ -110,7 +110,7 @@ export async function getGroupMemberInfo(
}
export async function getGroupFiles(
bridge: Bridge,
bridge: BridgeInterface,
groupId: number,
folderId?: string,
): Promise<JsonObject> {
@@ -142,7 +142,7 @@ export async function getGroupFiles(
}
export async function getStrangerInfo(
bridge: Bridge,
bridge: BridgeInterface,
userId: number,
): Promise<JsonObject | null> {
try {
@@ -165,7 +165,7 @@ export async function getStrangerInfo(
}
}
export async function getGroupSystemMessages(bridge: Bridge): Promise<JsonObject[]> {
export async function getGroupSystemMessages(bridge: BridgeInterface): Promise<JsonObject[]> {
try {
const reqs = await bridge.fetchGroupRequests();
return reqs.map(r => ({
@@ -182,7 +182,7 @@ export async function getGroupSystemMessages(bridge: Bridge): Promise<JsonObject
}
}
export async function getDownloadRKeys(bridge: Bridge): Promise<JsonObject[]> {
export async function getDownloadRKeys(bridge: BridgeInterface): Promise<JsonObject[]> {
try {
const rkeys = await bridge.fetchDownloadRKeys();
return rkeys.map(r => ({
@@ -1,4 +1,4 @@
import type { Bridge } from '../../bridge/bridge';
import type { BridgeInterface } from '../../bridge/bridge-interface';
import { createLogger } from '../../utils/logger';
import type { MediaStore } from '../media-store';
import type { JsonObject } from '../types';
@@ -21,7 +21,7 @@ export async function getImageInfo(
}
export async function getRecordInfo(
bridge: Bridge,
bridge: BridgeInterface,
mediaStore: MediaStore,
file: string,
): Promise<JsonObject | null> {
@@ -1,4 +1,4 @@
import type { Bridge } from '../../bridge/bridge';
import type { BridgeInterface } from '../../bridge/bridge-interface';
import type { ForwardNodePayload, MessageElement } from '../../bridge/events';
import { createLogger } from '../../utils/logger';
import type { MessageSendResult } from '../api-handler';
@@ -63,7 +63,7 @@ export async function getFriendMsgHistory(
.map(sanitizeMessageEventForApi);
}
export async function deleteMessage(bridge: Bridge, meta: MessageMeta): Promise<void> {
export async function deleteMessage(bridge: BridgeInterface, meta: MessageMeta): Promise<void> {
if (meta.isGroup) {
await bridge.recallGroupMessage(meta.targetId, meta.sequence);
} else {
@@ -78,7 +78,7 @@ export async function deleteMessage(bridge: Bridge, meta: MessageMeta): Promise<
}
export async function setEssenceMessage(
bridge: Bridge,
bridge: BridgeInterface,
messageStore: MessageStore,
messageId: number,
enable: boolean,
@@ -1,7 +1,7 @@
import type { Bridge } from '../../bridge/bridge';
import type { BridgeInterface } from '../../bridge/bridge-interface';
export async function handleGroupAddRequest(
bridge: Bridge,
bridge: BridgeInterface,
flag: string,
approve: boolean,
reason: string,