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

+8 -16
View File
@@ -76,7 +76,7 @@ export class MessageChain<THasReply extends boolean = false> implements MessageC
}
reply(this: MessageChain<false>, id: number | string): MessageChain<true> {
return this.addSegment(segments.reply(id), 'reply') as MessageChain<true>;
return this.addSegment<true>(segments.reply(id));
}
image(file: string, options: Omit<ImageSegment['data'], 'file'> = {}): MessageChain<THasReply> {
@@ -134,7 +134,7 @@ export class MessageChain<THasReply extends boolean = false> implements MessageC
type: TType,
data: TData,
): MessageChain<THasReply> {
return this.addSegment(segments.raw(type, data), type);
return this.addSegment(segments.raw(type, data));
}
append(messageInput: OutgoingMessage): MessageChain<THasReply> {
@@ -146,7 +146,7 @@ export class MessageChain<THasReply extends boolean = false> implements MessageC
}
for (const segment of normalized) {
next = next.addFromSegment(segment);
next = next.addSegment(segment);
}
return next;
@@ -192,24 +192,16 @@ export class MessageChain<THasReply extends boolean = false> implements MessageC
return this.items[Symbol.iterator]();
}
private addFromSegment(segment: AnyMessageSegment): MessageChain<THasReply> {
return this.addSegment(segment, segment.type);
}
private addSegment<TNextReply extends boolean = THasReply>(
segment: AnyMessageSegment,
singleUseType?: string,
): MessageChain<TNextReply> {
if (singleUseType && SINGLE_USE_TYPES.has(singleUseType) && this.used.has(singleUseType)) {
throw new Error(`Message segment "${singleUseType}" can only appear once in a chain`);
}
const nextUsed = new Set(this.used);
if (singleUseType && SINGLE_USE_TYPES.has(singleUseType)) {
nextUsed.add(singleUseType);
const singleUse = SINGLE_USE_TYPES.has(segment.type);
if (singleUse && this.used.has(segment.type)) {
throw new Error(`Message segment "${segment.type}" can only appear once in a chain`);
}
const nextUsed = singleUse ? new Set(this.used).add(segment.type) : this.used;
return new MessageChain<TNextReply>([...this.items, segment], nextUsed);
}
}
+11 -3
View File
@@ -1,4 +1,4 @@
import type { KnownMessageSegment, MessageSegment } from '../types/index';
import type { AnyMessageSegment } from '../types/index';
export function escapeCqText(text: string): string {
return text.replace(/&/g, '&amp;').replace(/\[/g, '&#91;').replace(/\]/g, '&#93;');
@@ -8,15 +8,23 @@ export function escapeCqParam(text: string): string {
return escapeCqText(text).replace(/,/g, '&#44;');
}
export function segmentsToCQString(segments: Array<KnownMessageSegment | MessageSegment>): string {
export function segmentsToCQString(segments: AnyMessageSegment[]): string {
return segments.map(segmentToCq).join('');
}
function segmentToCq(segment: KnownMessageSegment | MessageSegment): string {
function segmentToCq(segment: AnyMessageSegment): string {
if (segment.type === 'text') {
return escapeCqText(String(segment.data.text ?? ''));
}
if (
segment.type === 'reply' &&
segment.data.message !== undefined &&
typeof segment.data.message !== 'string'
) {
throw new TypeError('reply.message containing message segments must be sent as JSON');
}
const params = Object.entries(segment.data)
.filter(([, value]) => value !== undefined && value !== null)
.map(([key, value]) => `${key}=${escapeCqParam(String(value))}`)
+16 -24
View File
@@ -6,10 +6,6 @@ import type { AnyMessageSegment, FaceSegment, OutgoingMessage } from '../types/i
const CQ_REGEX = /\[CQ:([a-zA-Z0-9_.-]+)(?:,([^\]]*))?\]/g;
export function toCQString(input: OutgoingMessage): string {
if (typeof input === 'string') {
return escapeCqText(input);
}
const normalized = normalizeMessage(input);
return typeof normalized === 'string'
? escapeCqText(normalized)
@@ -21,31 +17,18 @@ export function fromCQString(input: string) {
let lastIndex = 0;
for (const match of input.matchAll(CQ_REGEX)) {
const index = match.index ?? 0;
const index = match.index;
if (index > lastIndex) {
const textValue = cqUnescape(input.slice(lastIndex, index));
if (textValue) {
current = current.text(textValue);
}
}
const segment = segmentFromCq(match[1] ?? '', parseCQParams(match[2] ?? ''));
if (segment) {
current = current.append(segment);
current = current.text(cqUnescape(input.slice(lastIndex, index)));
}
current = current.append(segmentFromCq(match[1]!, parseCQParams(match[2] ?? '')));
lastIndex = index + match[0].length;
}
if (lastIndex < input.length) {
const textValue = cqUnescape(input.slice(lastIndex));
if (textValue) {
current = current.text(textValue);
}
current = current.text(cqUnescape(input.slice(lastIndex)));
}
return current;
@@ -80,10 +63,11 @@ function cqUnescape(text: string): string {
}
function cqUnescapeParam(text: string): string {
return cqUnescape(text).replace(/&#44;/g, ',');
// Decode ampersands last so literal entity text is not decoded twice.
return cqUnescape(text.replace(/&#44;/g, ','));
}
function segmentFromCq(type: string, data: Record<string, string>): AnyMessageSegment | null {
function segmentFromCq(type: string, data: Record<string, string>): AnyMessageSegment {
switch (type) {
case 'text':
return segments.text(data.text ?? '');
@@ -120,8 +104,16 @@ function segmentFromCq(type: string, data: Record<string, string>): AnyMessageSe
: segments.raw(type, data);
}
case 'reply':
case 'reply': {
const { message, ...metadata } = data;
if (message !== undefined) {
return segments.customReply(message, metadata);
}
return segments.reply(data.id ?? '0');
}
case 'image':
return segments.image(data.file ?? data.url ?? '', {
url: data.url,
+9
View File
@@ -1,6 +1,7 @@
import type {
AtSegment,
ContactSegment,
CustomReplySegment,
FaceSegment,
ForwardSegment,
ImageSegment,
@@ -54,6 +55,14 @@ export const segments = {
return seg('reply', { id: String(id) });
},
/** An inline quote preview for use inside a merged-forward node. */
customReply(
message: CustomReplySegment['data']['message'],
options: Omit<CustomReplySegment['data'], 'message'> = {},
): CustomReplySegment {
return seg('reply', { ...options, message });
},
image(file: string, options: Omit<ImageSegment['data'], 'file'> = {}): ImageSegment {
return seg('image', { file, ...options });
},
+10 -2
View File
@@ -14,7 +14,15 @@ export type FaceSegment = MessageSegment<
{ id: string; large?: boolean; resultId?: string }
>;
export type AtSegment = MessageSegment<'at', { qq: string; name?: string; uid?: string }>;
export type ReplySegment = MessageSegment<'reply', { id: string }>;
type ReplyContent = {
/** Inline quote preview; supported only inside merged-forward nodes. */
message: string | AnyMessageSegment | AnyMessageSegment[];
qq?: string | number;
time?: string | number;
seq?: string | number;
};
export type ReplySegment = MessageSegment<'reply', { id: string } & Partial<ReplyContent>>;
export type CustomReplySegment = MessageSegment<'reply', { id?: string } & ReplyContent>;
export type ImageSegment = MessageSegment<
'image',
{
@@ -98,7 +106,7 @@ export type KnownMessageSegment =
| LocationSegment
| ContactSegment;
export type AnyMessageSegment = KnownMessageSegment | MessageSegment;
export type AnyMessageSegment = KnownMessageSegment | CustomReplySegment | MessageSegment;
export interface MessageChainLike {
readonly length: number;