feat: add async SQLite on Node 24 and improve messaging diagnostics (#520)

Move database work off the main thread on Node.js 24 and improve message handling and runtime diagnostics.

Preserve migration, restart, and concurrency coverage with the async SQLite driver.
This commit is contained in:
MliKiowa authored and GitHub committed 2026-10-13 10:12:47 +08:00
1 parent 20f48d11ec
commit c7428363cb
170 files changed
+7748 -3913

No files matched your search

@@ -295,7 +295,7 @@ export const ELEMENT_CODECS = {
const eventName = ctx.isGroup
? GROUP_MESSAGE_EVENT
: privateMessageEventName(sentBySelf, false);
const id = resolveReplyId(
const id = await resolveReplyId(
ctx.isGroup,
ctx.sessionId,
element.replySeq ?? 0,
@@ -328,10 +328,10 @@ export const ELEMENT_CODECS = {
return null;
}
const meta = options?.resolveReplyMeta?.(id) ?? null;
const meta = (await options?.resolveReplyMeta?.(id)) ?? null;
if (options?.resolveReplySequence) {
const resolved = options.resolveReplySequence(id);
const resolved = await options.resolveReplySequence(id);
if (typeof resolved === 'number' && resolved > 0) {
const element: MessageElement = {
@@ -400,7 +400,7 @@ export const ELEMENT_CODECS = {
}
if (ctx.mediaSegmentSink) {
ctx.mediaSegmentSink('image', element, data, ctx.isGroup, ctx.sessionId);
await ctx.mediaSegmentSink('image', element, data, ctx.isGroup, ctx.sessionId);
}
return { type: 'image', data };
@@ -437,7 +437,7 @@ export const ELEMENT_CODECS = {
};
if (ctx.mediaSegmentSink) {
ctx.mediaSegmentSink('record', element, data, ctx.isGroup, ctx.sessionId);
await ctx.mediaSegmentSink('record', element, data, ctx.isGroup, ctx.sessionId);
}
return { type: 'record', data };
@@ -468,7 +468,7 @@ export const ELEMENT_CODECS = {
};
if (ctx.mediaSegmentSink) {
ctx.mediaSegmentSink('video', element, data, ctx.isGroup, ctx.sessionId);
await ctx.mediaSegmentSink('video', element, data, ctx.isGroup, ctx.sessionId);
}
return { type: 'video', data };
+2 -2
View File
@@ -40,7 +40,7 @@ export type MessageIdResolver = (
eventName: string,
timestamp?: number,
replyElements?: readonly MessageElement[],
) => number;
) => number | Promise<number>;
export type MediaSegmentSink = (
mediaType: 'image' | 'record' | 'video',
@@ -48,7 +48,7 @@ export type MediaSegmentSink = (
data: JsonObject,
isGroup: boolean,
sessionId: number,
) => void;
) => void | Promise<void>;
// ─────────────── context ───────────────
@@ -46,7 +46,7 @@ async function convertPrivateMessage(
event.kind === 'friend_message'
? privateMessageEventName(isSelf, hasCanonicalNtSequence)
: PRIVATE_MESSAGE_EVENT;
const messageId = applyMessageIdResolver(
const messageId = await applyMessageIdResolver(
ctx.messageIdResolver,
false,
sessionId,
@@ -104,7 +104,7 @@ export async function convertGroupMessage(
): Promise<JsonObject> {
const isSelf = event.senderUin === ctx.selfId;
const postType = isSelf ? 'message_sent' : 'message';
const messageId = applyMessageIdResolver(
const messageId = await applyMessageIdResolver(
ctx.messageIdResolver,
true,
event.groupId,
@@ -84,8 +84,11 @@ export function convertGroupAdmin(ctx: ConverterContext, event: GroupAdmin): Jso
});
}
export function convertFriendRecall(ctx: ConverterContext, event: FriendRecall): JsonObject {
const messageId = applyMessageIdResolver(
export async function convertFriendRecall(
ctx: ConverterContext,
event: FriendRecall,
): Promise<JsonObject> {
const messageId = await applyMessageIdResolver(
ctx.messageIdResolver,
false,
event.userUin,
@@ -100,8 +103,11 @@ export function convertFriendRecall(ctx: ConverterContext, event: FriendRecall):
});
}
export function convertGroupRecall(ctx: ConverterContext, event: GroupRecall): JsonObject {
const messageId = applyMessageIdResolver(
export async function convertGroupRecall(
ctx: ConverterContext,
event: GroupRecall,
): Promise<JsonObject> {
const messageId = await applyMessageIdResolver(
ctx.messageIdResolver,
true,
event.groupId,
@@ -143,8 +149,11 @@ export function convertGroupPoke(ctx: ConverterContext, event: GroupPoke): JsonO
});
}
export function convertGroupEssence(ctx: ConverterContext, event: GroupEssence): JsonObject {
const messageId = applyMessageIdResolver(
export async function convertGroupEssence(
ctx: ConverterContext,
event: GroupEssence,
): Promise<JsonObject> {
const messageId = await applyMessageIdResolver(
ctx.messageIdResolver,
true,
event.groupId,
@@ -265,11 +274,11 @@ export function convertFriendInputStatus(
});
}
export function convertGroupMsgEmojiLike(
export async function convertGroupMsgEmojiLike(
ctx: ConverterContext,
event: GroupMsgEmojiLike,
): JsonObject {
const messageId = applyMessageIdResolver(
): Promise<JsonObject> {
const messageId = await applyMessageIdResolver(
ctx.messageIdResolver,
true,
event.groupId,
@@ -8,29 +8,14 @@ import { attachMessageMedia } from '../message-media';
const log = createLogger('OneBot');
function toSegmentContext(
ctx: ConverterContext,
isGroup: boolean,
sessionId: number,
): ToSegmentContext {
return {
isGroup,
sessionId,
selfId: ctx.selfId,
imageUrlResolver: ctx.imageUrlResolver,
mediaUrlResolver: ctx.mediaUrlResolver,
messageIdResolver: ctx.messageIdResolver,
mediaSegmentSink: ctx.mediaSegmentSink,
};
}
export async function elementsToOneBotSegments(
ctx: ConverterContext,
elements: MessageElement[],
isGroup: boolean,
sessionId: number,
options: { includeReplyContent?: boolean } = {},
): Promise<JsonArray> {
const segmentCtx = toSegmentContext(ctx, isGroup, sessionId);
const segmentCtx: ToSegmentContext = { ...ctx, isGroup, sessionId, mediaSegmentSink: null };
const result: JsonArray = [];
for (const element of elements) {
@@ -39,7 +24,7 @@ export async function elementsToOneBotSegments(
// One malformed element shouldn't drop the whole message — skip it (with a
// breadcrumb) and keep converting the rest.
try {
segment = await elementToSegment(element, { ...segmentCtx, mediaSegmentSink: null });
segment = await elementToSegment(element, segmentCtx);
} catch (err) {
log.warn(
'segment convert skipped type=%s (%s)',
@@ -49,11 +34,43 @@ export async function elementsToOneBotSegments(
continue;
}
// Forward quotes often refer to messages outside the local account's
// history. Carry their inline content so consumers can resend the node.
if (
options.includeReplyContent &&
element.type === 'reply' &&
element.replyElements?.length
) {
const message = await elementsToOneBotSegments(
ctx,
element.replyElements.filter((quoted) => quoted.type !== 'reply'),
isGroup,
sessionId,
);
if (message.length > 0) {
const data = segment.data as JsonObject;
data.message = message;
if (element.replySenderUin) {
data.qq = String(element.replySenderUin);
}
if (element.replyTime !== undefined) {
data.time = String(element.replyTime);
}
if (element.replySeq) {
data.seq = String(element.replySeq);
}
}
}
// Persistence failures must propagate, not masquerade as bad wire elements.
attachMessageMedia(segment, element);
if (element.type === 'image' || element.type === 'record' || element.type === 'video') {
ctx.mediaSegmentSink?.(
await ctx.mediaSegmentSink?.(
element.type,
element,
segment.data as JsonObject,
+13 -11
View File
@@ -2,11 +2,6 @@ import { GROUP_MESSAGE_EVENT, PRIVATE_MESSAGE_EVENT } from '../message-id';
import type { MessageIdResolver } from './index';
import type { MessageElement } from '@snowluma/protocol/events';
export function parseSelfId(instanceUin: string): number {
const parsed = Number.parseInt(instanceUin, 10);
return Number.isNaN(parsed) ? 0 : parsed;
}
export function isSameActor(
leftUin: number,
leftUid: string | undefined,
@@ -20,16 +15,16 @@ export function isSameActor(
return Boolean(leftUid) && leftUid === rightUid;
}
export function applyMessageIdResolver(
export async function applyMessageIdResolver(
resolver: MessageIdResolver | null,
isGroup: boolean,
sessionId: number,
sequence: number,
eventName: string,
timestamp?: number,
): number {
): Promise<number> {
if (resolver) {
const resolved = resolver(isGroup, sessionId, sequence, eventName, timestamp);
const resolved = await resolver(isGroup, sessionId, sequence, eventName, timestamp);
if (Number.isInteger(resolved) && resolved !== 0) {
return resolved;
@@ -40,7 +35,7 @@ export function applyMessageIdResolver(
return seq === 0 ? 0 : seq;
}
export function resolveReplyId(
export async function resolveReplyId(
isGroup: boolean,
sessionId: number,
sequence: number,
@@ -48,7 +43,7 @@ export function resolveReplyId(
eventName = isGroup ? GROUP_MESSAGE_EVENT : PRIVATE_MESSAGE_EVENT,
timestamp?: number,
replyElements?: readonly MessageElement[],
): number {
): Promise<number> {
const seq = Math.trunc(sequence);
if (seq === 0) {
@@ -56,7 +51,14 @@ export function resolveReplyId(
}
if (resolver) {
const resolved = resolver(isGroup, sessionId, seq, eventName, timestamp, replyElements);
const resolved = await resolver(
isGroup,
sessionId,
seq,
eventName,
timestamp,
replyElements,
);
if (Number.isInteger(resolved) && resolved !== 0) {
return resolved;