Replaces the experimental `node:sqlite` builtin with the stable
`better-sqlite3` wrapper across the two hot SQLite users — the
OneBot message-store and the bridge identity-service. While the API
surface is essentially identical (sync, prepared-statement-driven,
`Database`/`Statement` shape), better-sqlite3 (a) doesn't carry the
"ExperimentalWarning: SQLite is an experimental feature" baggage
that ships with `node:sqlite`, (b) has a more mature C++ binding
that consistently outperforms the builtin by ~30-50%, and (c)
encourages reusing `Statement` objects by design — exactly the win
the second half of this commit is after.
Why both at once
----------------
Caching prepared statements (was P1 in the RoadMap-#5 analysis) is
the largest single optimization opportunity on the SQLite path. The
old shape was the textbook SQLite anti-pattern:
// every call re-parsed the SQL
this.db.prepare(BIG_SQL_STRING).run(...);
Each `prepare(sql)` runs the full SQL parser + planner — for simple
inserts/updates/selects that dominates wall-clock cost. Doing both
the driver swap AND the statement caching in one commit lets the
benchmark numbers attribute the combined effect honestly; splitting
them would have double-charged the same disruption budget.
What changed code-wise
----------------------
**messageStore** — 8 distinct SQL templates, all now `readonly`
fields prepared once in the constructor:
- stmtStoreEvent — INSERT/UPDATE event row + JSON payload
- stmtStoreMeta — INSERT/UPDATE meta-only row
- stmtFindEvent — single-row SELECT by message_hash
- stmtFindMeta — single-row SELECT (full meta columns)
- stmtResolveReplyGroup, stmtResolveReplyPrivate
— 2 variants for the reply-id lookup
- stmtListEventsAnchored, stmtListEventsLatest
— 2 variants for /get_history_msg
**identity-service** — 25+ distinct statements. Used a Map-based
`pstmt(sql)` memoization helper rather than enumerating all 25 as
named fields (call sites stay readable; Map lookup is O(1) hash +
string-eq, negligible next to the SQL parse it replaces). The
helper short-circuits to the cached `Statement` on second-and-later
calls.
API differences from node:sqlite (handled in this commit):
- `import { DatabaseSync } from 'node:sqlite'`
→ `import Database from 'better-sqlite3'`
- `new DatabaseSync(path)` → `new Database(path)`
- `db.exec('PRAGMA journal_mode = WAL')`
→ `db.pragma('journal_mode = WAL')`
- `db.exec('BEGIN' / 'COMMIT' / 'ROLLBACK')` — kept as-is; both
drivers accept raw `exec()` for transaction control. The
`db.transaction()` higher-order wrapper is more idiomatic
but the existing manual control already works and changing
it would invalidate the `transaction()` helper's matched
try/catch shape.
vite build wiring:
- `node:sqlite` removed from the externals list
- `better-sqlite3` added — must stay external because it has a
native binding (`require('./build/Release/better_sqlite3.node')`)
that Vite can't bundle. Vendoring the prebuilt `.node` into
`packages/runtime/native/` is a follow-up packaging task; the
workspace `pnpm install` already places the binding under
`node_modules` so dev + tests work today.
Bench results (RoadMap #5 baseline → after)
-------------------------------------------
@snowluma/bridge — recv pipeline (with SQLite identity writes):
group_member_join 17.5 μs/op → 4.7 μs/op (~3.7x faster)
group_message text 0.42→0.42 μs (no path change)
group_message mixed 0.67→0.67 μs (no path change)
@snowluma/onebot — send pipeline:
messageStore.storeMeta 17.0 μs/op → 10.9 μs/op (~1.6x)
messageStore.storeEvent 23.2 μs/op → 18.2 μs/op (~1.3x)
messageStore.findMeta hit 5.4 μs/op → 1.3 μs/op (~4x)
messageStore.findMeta miss 3.7 μs/op → 0.7 μs/op (~5x)
messageStore.findEvent 3.8 μs/op → 1.7 μs/op (~2.3x)
Read paths gained the most because they're parse-bound (small SQL
+ short execution → the parser was most of the cost). Writes gained
less because the fsync/journal overhead on each commit still
dominates the parse savings. Combined send hot path (storeMeta +
storeEvent, called on every outbound message + the /get_msg-fix
self-event store) drops from ~40 μs to ~29 μs — about a 27%
reduction per message sent.
Verification
------------
- pnpm -r typecheck: all 11 projects clean
- @snowluma/onebot test suite: 250/250
- @snowluma/core test suite: 302/302
- Combined: 552/552 (unchanged baseline)
- Vite build (with stubbed native binary): clean, 2.84 MB / 223 KB
gzip (matches pre-Phase-2 envelope after removing the better-
sqlite3 inline-bundle attempt — externalizing it shaves the
~30 KB regression we saw mid-flight)
Follow-up (next commits in this series)
---------------------------------------
- Skip rememberGroupMemberIdentity DB write when the (uid, uin,
nickname, card) tuple is unchanged in the in-memory cache.
- Drop the `Buffer.from(pkt.body)` allocation in msg-push context.
- Synchronous fast-path for elementsToJson when no async resolver
is wired.
- Vendor `better-sqlite3` prebuilt `.node` into the runtime/native
layout for the production package step.
133 lines
4.4 KiB
JSON
133 lines
4.4 KiB
JSON
{
|
|
"name": "@snowluma/bridge",
|
|
"version": "1.9.0",
|
|
"private": true,
|
|
"type": "module",
|
|
"description": "SnowLuma bridge — the protocol-layer between QQ wire bytes and typed events. Owns: identity service (uid↔uin cache with SQLite persistence), event bus, packet pipeline (incoming SSO packet → QQEventVariant), msg-push decoders, element-builder (MessageElement → wire elems[]), highway upload protocol (file/image/video/ptt), OIDB envelope helper, and the event/notice/request type vocabulary. Consumers (@snowluma/core, @snowluma/onebot) talk to this package through `BridgeContext` for runtime calls and the standalone classes (IdentityService, BridgeEventBus, IncomingPacketPipeline) for composition. Per-file subpath exports mirror the legacy `core/src/bridge/*` layout 1:1 so the previous `import { …} from '../bridge/<x>'` collapses to `import { … } from '@snowluma/bridge/<x>'`.",
|
|
"main": "./src/index.ts",
|
|
"types": "./src/index.ts",
|
|
"exports": {
|
|
".": {
|
|
"types": "./src/index.ts",
|
|
"default": "./src/index.ts"
|
|
},
|
|
"./events": {
|
|
"types": "./src/events.ts",
|
|
"default": "./src/events.ts"
|
|
},
|
|
"./event-bus": {
|
|
"types": "./src/event-bus.ts",
|
|
"default": "./src/event-bus.ts"
|
|
},
|
|
"./identity-service": {
|
|
"types": "./src/identity-service.ts",
|
|
"default": "./src/identity-service.ts"
|
|
},
|
|
"./packet-pipeline": {
|
|
"types": "./src/packet-pipeline.ts",
|
|
"default": "./src/packet-pipeline.ts"
|
|
},
|
|
"./element-builder": {
|
|
"types": "./src/element-builder.ts",
|
|
"default": "./src/element-builder.ts"
|
|
},
|
|
"./bridge-oidb": {
|
|
"types": "./src/bridge-oidb.ts",
|
|
"default": "./src/bridge-oidb.ts"
|
|
},
|
|
"./bridge-context": {
|
|
"types": "./src/bridge-context.ts",
|
|
"default": "./src/bridge-context.ts"
|
|
},
|
|
"./qq-info": {
|
|
"types": "./src/qq-info.ts",
|
|
"default": "./src/qq-info.ts"
|
|
},
|
|
"./format": {
|
|
"types": "./src/format.ts",
|
|
"default": "./src/format.ts"
|
|
},
|
|
"./msg-push": {
|
|
"types": "./src/msg-push/index.ts",
|
|
"default": "./src/msg-push/index.ts"
|
|
},
|
|
"./msg-push/registry": {
|
|
"types": "./src/msg-push/registry.ts",
|
|
"default": "./src/msg-push/registry.ts"
|
|
},
|
|
"./msg-push/context": {
|
|
"types": "./src/msg-push/context.ts",
|
|
"default": "./src/msg-push/context.ts"
|
|
},
|
|
"./msg-push/decoders/event-0x2dc": {
|
|
"types": "./src/msg-push/decoders/event-0x2dc.ts",
|
|
"default": "./src/msg-push/decoders/event-0x2dc.ts"
|
|
},
|
|
"./highway": {
|
|
"types": "./src/highway/highway-client.ts",
|
|
"default": "./src/highway/highway-client.ts"
|
|
},
|
|
"./highway/utils": {
|
|
"types": "./src/highway/utils.ts",
|
|
"default": "./src/highway/utils.ts"
|
|
},
|
|
"./highway/pipeline": {
|
|
"types": "./src/highway/pipeline.ts",
|
|
"default": "./src/highway/pipeline.ts"
|
|
},
|
|
"./highway/image-upload": {
|
|
"types": "./src/highway/image-upload.ts",
|
|
"default": "./src/highway/image-upload.ts"
|
|
},
|
|
"./highway/ptt-upload": {
|
|
"types": "./src/highway/ptt-upload.ts",
|
|
"default": "./src/highway/ptt-upload.ts"
|
|
},
|
|
"./highway/video-upload": {
|
|
"types": "./src/highway/video-upload.ts",
|
|
"default": "./src/highway/video-upload.ts"
|
|
},
|
|
"./highway/ffmpeg-addon": {
|
|
"types": "./src/highway/ffmpeg-addon.ts",
|
|
"default": "./src/highway/ffmpeg-addon.ts"
|
|
},
|
|
"./web/group-album": {
|
|
"types": "./src/web/group-album.ts",
|
|
"default": "./src/web/group-album.ts"
|
|
},
|
|
"./web/group-essence": {
|
|
"types": "./src/web/group-essence.ts",
|
|
"default": "./src/web/group-essence.ts"
|
|
},
|
|
"./web/group-honor": {
|
|
"types": "./src/web/group-honor.ts",
|
|
"default": "./src/web/group-honor.ts"
|
|
},
|
|
"./web/group-notice": {
|
|
"types": "./src/web/group-notice.ts",
|
|
"default": "./src/web/group-notice.ts"
|
|
},
|
|
"./web/request-util": {
|
|
"types": "./src/web/request-util.ts",
|
|
"default": "./src/web/request-util.ts"
|
|
}
|
|
},
|
|
"scripts": {
|
|
"bench": "vitest bench --run",
|
|
"typecheck": "tsc --noEmit"
|
|
},
|
|
"dependencies": {
|
|
"@snowluma/common": "workspace:*",
|
|
"@snowluma/proto-defs": "workspace:*",
|
|
"@snowluma/proton": "workspace:*",
|
|
"better-sqlite3": "^11.7.0"
|
|
},
|
|
"devDependencies": {
|
|
"@types/better-sqlite3": "^7.6.13",
|
|
"@types/node": "^24.12.2",
|
|
"typescript": "catalog:",
|
|
"vite": "catalog:",
|
|
"vitest": "^4.1.4"
|
|
}
|
|
}
|