From dd02535a73ea73e8ad536eff1165911e9d1c3164 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Tue, 12 May 2026 20:59:25 +0800 Subject: [PATCH] chore(core): flatten single-file dirs + drop dead writeFailed surface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two small cleanups that came out of the round-2 architecture review and are sub-five-minutes each — bundled because each one is too small to justify its own commit. #5 — flatten single-file directories packages/core/src/bridge/builders/element-builder.ts -> packages/core/src/bridge/element-builder.ts packages/core/src/bridge/handlers/msg-push-handler.ts -> packages/core/src/bridge/msg-push-handler.ts Both dirs had exactly one file in them — a smell that says "planned a sibling that never landed". Moving up one level removes the wrapper directory. Touch list: - bridge.ts: 2 import paths - bridge/actions/forward.ts: 2 import paths - msg-push-handler.ts: internal ../X -> ./X for events, proto/element, proto/message (the dynamic-import-typeof references too) - element-builder.ts: internal ../X -> ./X for bridge, events, highway/* - 4 test files: import paths (2 regular, 2 vi.mock) #6 — drop IdentityService.hasWriteFailure identity-service.ts had a `private writeFailed = false` field set by the runWrite catch block and exposed via `get hasWriteFailure()`, but a full-repo grep showed zero callers of the getter. It was a public API stub for a feature that hadn't been wired (a webui health indicator, presumably). Removed: - the field - the getter - the assignment in runWrite's catch block (the error log on the next line is the actual observable behavior; nothing else reads the flag) If we want a "did any write fail this session" surface later, it's trivial to add back with an explicit consumer in mind. Behavior preserved end-to-end. No public API change outside the moved files. 369 tests pass (unchanged). 3 stability runs. --- packages/core/src/bridge/actions/forward.ts | 4 ++-- packages/core/src/bridge/bridge.ts | 4 ++-- .../bridge/{builders => }/element-builder.ts | 18 +++++++------- packages/core/src/bridge/identity-service.ts | 6 ----- .../bridge/{handlers => }/msg-push-handler.ts | 24 +++++++++---------- packages/core/tests/actions/forward.test.ts | 2 +- .../core/tests/bridge-private-routing.test.ts | 2 +- packages/core/tests/message-parser.test.ts | 2 +- packages/core/tests/msg-push-handler.test.ts | 2 +- 9 files changed, 29 insertions(+), 35 deletions(-) rename packages/core/src/bridge/{builders => }/element-builder.ts (94%) rename packages/core/src/bridge/{handlers => }/msg-push-handler.ts (98%) diff --git a/packages/core/src/bridge/actions/forward.ts b/packages/core/src/bridge/actions/forward.ts index ff99745d..6b89d87a 100644 --- a/packages/core/src/bridge/actions/forward.ts +++ b/packages/core/src/bridge/actions/forward.ts @@ -6,8 +6,8 @@ import type { Bridge } from '../bridge'; import { gunzipSync, gzipSync } from 'zlib'; import { protoDecode, protoEncode } from '../../protobuf/decode'; -import { buildSendElems } from '../builders/element-builder'; -import { parseMsgPush } from '../handlers/msg-push-handler'; +import { buildSendElems } from '../element-builder'; +import { parseMsgPush } from '../msg-push-handler'; import type { ForwardNodePayload } from '../events'; import type { PacketInfo } from '../../protocol/types'; import { diff --git a/packages/core/src/bridge/bridge.ts b/packages/core/src/bridge/bridge.ts index fe748ffa..67e22ca0 100644 --- a/packages/core/src/bridge/bridge.ts +++ b/packages/core/src/bridge/bridge.ts @@ -6,10 +6,10 @@ import type { PacketInfo } from '../protocol/types'; import type { ForwardNodePayload, QQEventVariant, MessageElement } from './events'; import type { FriendInfo, QQGroupInfo, GroupMemberInfo, UserProfileInfo, GroupRequestInfo } from './qq-info'; import { QQInfo } from './qq-info'; -import { MSG_PUSH_CMD, parseMsgPush } from './handlers/msg-push-handler'; +import { MSG_PUSH_CMD, parseMsgPush } from './msg-push-handler'; import type { PacketSender, SendPacketResult } from '../protocol/packet-sender'; import { protoEncode, protoDecode } from '../protobuf/decode'; -import { buildSendElems } from './builders/element-builder'; +import { buildSendElems } from './element-builder'; import { IdentityService } from './identity-service'; import { createLogger } from '../utils/logger'; import { diff --git a/packages/core/src/bridge/builders/element-builder.ts b/packages/core/src/bridge/element-builder.ts similarity index 94% rename from packages/core/src/bridge/builders/element-builder.ts rename to packages/core/src/bridge/element-builder.ts index 2dddd450..61755c86 100644 --- a/packages/core/src/bridge/builders/element-builder.ts +++ b/packages/core/src/bridge/element-builder.ts @@ -2,20 +2,20 @@ // for encoding with protoEncode(SendMessageRequestSchema). // Port of src/bridge/src/bridge_messages.cpp build_send_elems() -import type { Bridge } from '../bridge'; -import type { MessageElement } from '../events'; -import type { ProtoDecoded } from '../../protobuf/decode'; -import { protoEncode } from '../../protobuf/decode'; +import type { Bridge } from './bridge'; +import type { MessageElement } from './events'; +import type { ProtoDecoded } from '../protobuf/decode'; +import { protoEncode } from '../protobuf/decode'; import { ElemSchema, -} from '../proto/element'; +} from './proto/element'; import { MentionExtraSendSchema, MarkdownDataSchema, -} from '../proto/action'; -import { uploadImageMsgInfo } from '../highway/image-upload'; -import { uploadPttMsgInfo } from '../highway/ptt-upload'; -import { uploadVideoMsgInfo } from '../highway/video-upload'; +} from './proto/action'; +import { uploadImageMsgInfo } from './highway/image-upload'; +import { uploadPttMsgInfo } from './highway/ptt-upload'; +import { uploadVideoMsgInfo } from './highway/video-upload'; type ProtoElem = Partial>; diff --git a/packages/core/src/bridge/identity-service.ts b/packages/core/src/bridge/identity-service.ts index 2f62a519..a7ce76cf 100644 --- a/packages/core/src/bridge/identity-service.ts +++ b/packages/core/src/bridge/identity-service.ts @@ -71,7 +71,6 @@ export class IdentityService { private readonly uinByUid = new Map(); private readonly uidByUin = new Map(); private inTransaction = false; - private writeFailed = false; constructor( private readonly qqInfo: QQInfo, @@ -106,10 +105,6 @@ export class IdentityService { return this.db !== null; } - get hasWriteFailure(): boolean { - return this.writeFailed; - } - rememberFriends(friends: FriendInfo[]): void { this.qqInfo.setFriends(friends); for (const friend of friends) this.rememberUidUin(friend.uid, friend.uin); @@ -693,7 +688,6 @@ export class IdentityService { try { fn(); } catch (err) { - this.writeFailed = true; log.error('identity db write failed [%s]: %s', label, err instanceof Error ? (err.stack ?? err.message) : String(err)); } } diff --git a/packages/core/src/bridge/handlers/msg-push-handler.ts b/packages/core/src/bridge/msg-push-handler.ts similarity index 98% rename from packages/core/src/bridge/handlers/msg-push-handler.ts rename to packages/core/src/bridge/msg-push-handler.ts index 1d07bd1f..78086d5b 100644 --- a/packages/core/src/bridge/handlers/msg-push-handler.ts +++ b/packages/core/src/bridge/msg-push-handler.ts @@ -2,31 +2,31 @@ // into bridge events. Port of src/bridge/src/handlers/msg_push_handler.h/.cpp import { inflateSync } from 'zlib'; -import { protoDecode } from '../../protobuf/decode'; -import { WireMessage } from '../../protobuf/wire'; -import { PushMsgSchema } from '../proto/message'; +import { protoDecode } from '../protobuf/decode'; +import { WireMessage } from '../protobuf/wire'; +import { PushMsgSchema } from './proto/message'; import { MentionExtraSchema, QFaceExtraSchema, QSmallFaceExtraSchema, MsgInfoSchema, GroupFileExtraSchema, NotOnlineImageSchema, -} from '../proto/element'; -import { FileExtraSchema } from '../proto/message'; +} from './proto/element'; +import { FileExtraSchema } from './proto/message'; import { GroupChangeSchema, GroupAdminSchema, GroupJoinSchema, GroupInvitationSchema, GroupInviteSchema, FriendRequestSchema, FriendRecallSchema, GroupMuteSchema, NotifyMessageBodySchema, GeneralGrayTipInfoSchema, OperatorInfoSchema, -} from '../proto/notify'; -import type { QQInfo } from '../qq-info'; -import type { PacketInfo } from '../../protocol/types'; +} from './proto/notify'; +import type { QQInfo } from './qq-info'; +import type { PacketInfo } from '../protocol/types'; import type { QQEventVariant, MessageElement, FriendMessage, GroupMessage, TempMessage, GroupMemberJoin, GroupMemberLeave, GroupMuteEvent, GroupAdminEvent, FriendRecall, GroupRecallEvent, FriendRequestEvent, GroupInviteEvent, FriendPokeEvent, GroupPokeEvent, GroupEssenceEvent, -} from '../events'; -import type { ProtoDecoded, ProtoSchema } from '../../protobuf/decode'; +} from './events'; +import type { ProtoDecoded, ProtoSchema } from '../protobuf/decode'; // --- PkgType enum --- const enum PkgType { @@ -147,7 +147,7 @@ function unwrapGroupNotifyPayload(content: Uint8Array): Uint8Array | null { // --- Element conversion --- -type ElemDecoded = ProtoDecoded; +type ElemDecoded = ProtoDecoded; function convertElements(elems: ElemDecoded[]): MessageElement[] { const result: MessageElement[] = []; @@ -497,7 +497,7 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] { } function extractRichtextExtras( - rt: ProtoDecoded, + rt: ProtoDecoded, elements: MessageElement[], isGroup = false ): void { diff --git a/packages/core/tests/actions/forward.test.ts b/packages/core/tests/actions/forward.test.ts index 0b2c4a5c..df4acdb1 100644 --- a/packages/core/tests/actions/forward.test.ts +++ b/packages/core/tests/actions/forward.test.ts @@ -10,7 +10,7 @@ vi.mock('../../src/bridge/bridge-oidb', () => ({ // element-builder reaches into protoEncode with element-specific schemas // that we don't want to construct manually in tests; stub it to return // a benign placeholder. -vi.mock('../../src/bridge/builders/element-builder', () => ({ +vi.mock('../../src/bridge/element-builder', () => ({ buildSendElems: vi.fn(async () => []), })); diff --git a/packages/core/tests/bridge-private-routing.test.ts b/packages/core/tests/bridge-private-routing.test.ts index a729a17b..078ec6ef 100644 --- a/packages/core/tests/bridge-private-routing.test.ts +++ b/packages/core/tests/bridge-private-routing.test.ts @@ -3,7 +3,7 @@ import type { SendPacketResult } from '../src/protocol/packet-sender'; import { protoDecode, protoEncode } from '../src/protobuf/decode'; import { SendMessageRequestSchema, SendMessageResponseSchema } from '../src/bridge/proto/action'; -vi.mock('../src/bridge/builders/element-builder', () => ({ +vi.mock('../src/bridge/element-builder', () => ({ buildSendElems: vi.fn(async () => [{ text: { str: 'stub media elem' } }]), })); diff --git a/packages/core/tests/message-parser.test.ts b/packages/core/tests/message-parser.test.ts index d71c1303..fe2af804 100644 --- a/packages/core/tests/message-parser.test.ts +++ b/packages/core/tests/message-parser.test.ts @@ -3,7 +3,7 @@ import os from 'os'; import path from 'path'; import { describe, it, expect, vi } from 'vitest'; import { parseMessage } from '../src/onebot/message-parser'; -import { buildSendElems } from '../src/bridge/builders/element-builder'; +import { buildSendElems } from '../src/bridge/element-builder'; import { MentionExtraSendSchema } from '../src/bridge/proto/action'; import { makeOidbBaseSchema } from '../src/bridge/proto/oidb'; import { NTV2UploadRichMediaRespSchema } from '../src/bridge/proto/highway'; diff --git a/packages/core/tests/msg-push-handler.test.ts b/packages/core/tests/msg-push-handler.test.ts index 24af2d2c..11877b49 100644 --- a/packages/core/tests/msg-push-handler.test.ts +++ b/packages/core/tests/msg-push-handler.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { parseMsgPush, MSG_PUSH_CMD } from '../src/bridge/handlers/msg-push-handler'; +import { parseMsgPush, MSG_PUSH_CMD } from '../src/bridge/msg-push-handler'; import { QQInfo, type GroupMemberInfo, type QQGroupInfo } from '../src/bridge/qq-info'; import { PushMsgSchema } from '../src/bridge/proto/message'; import { GroupChangeSchema, OperatorInfoSchema } from '../src/bridge/proto/notify';