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

+1 -2
View File
@@ -4,8 +4,7 @@ Compile-time protobuf encoder/decoder generator for SnowLuma. TypeScript
interfaces in, fully-inlined zero-runtime codec functions out.
> Vendored from [SnowLuma/protobuf-dsl](https://github.com/SnowLuma/protobuf-dsl)
> with edge-case fixes applied. The unmodified upstream is kept under
> `dev/protobuf-dsl/` as a reference copy.
> with edge-case fixes applied.
## What it does
+6 -3
View File
@@ -4,7 +4,7 @@
"version": "1.14.22",
"private": true,
"type": "module",
"description": "Compile-time protobuf encoder/decoder generator for SnowLuma. TypeScript interfaces → fully-inlined, zero-runtime protobuf codec functions. Vendored from SnowLuma/protobuf-dsl (reference copy under dev/protobuf-dsl/) with edge-case fixes applied.",
"description": "Compile-time protobuf encoder/decoder generator for SnowLuma. TypeScript interfaces → fully-inlined, zero-runtime protobuf codec functions. Vendored from SnowLuma/protobuf-dsl with edge-case fixes applied.",
"main": "./src/runtime.ts",
"types": "./protobuf.d.ts",
"exports": {
@@ -18,11 +18,11 @@
}
},
"scripts": {
"build": "tsup src/index.ts --format esm --out-dir dist --target node20 --external typescript --external vite",
"build": "tsup src/index.ts --format esm --out-dir dist --target node24 --external typescript --external vite",
"test": "vitest run",
"test:watch": "vitest",
"typecheck": "tsc --noEmit",
"prepare": "test -f dist/index.js || pnpm build"
"prepare": "pnpm run build"
},
"dependencies": {
"typescript": "catalog:"
@@ -40,5 +40,8 @@
"tsup": "^8.5.0",
"vite": "catalog:",
"vitest": "^4.1.4"
},
"engines": {
"node": ">=24"
}
}
+1 -12
View File
@@ -14,6 +14,7 @@ import {
import { monomorphizeTypeNode } from './monomorphizer.js';
import {
createImportedTypeNameResolver,
getCallableName,
typeNodeToMangledName,
registerSyntheticTypeSourceFile,
resolveSourceFileForTypeNode,
@@ -28,18 +29,6 @@ import { buildDependencyRegistry } from './dependency-graph.js';
export { typeNodeToMangledName } from './utils.js';
function getCallableName(expr: ts.Expression): string | null {
if (ts.isIdentifier(expr)) {
return expr.text;
}
if (ts.isPropertyAccessExpression(expr)) {
return expr.name.text;
}
return null;
}
function instantiateWrapperTypePattern(
binding: WrapperBinding,
callerTypeArgs: ts.NodeArray<ts.TypeNode>,
+1 -12
View File
@@ -5,6 +5,7 @@ import { collectInterface, collectGenericInterface } from './collector.js';
import { PRIMITIVE_TYPE_MAP, type ProtobufMessage, type GenericProtobufTemplate } from './types.js';
import {
createImportedTypeNameResolver,
getCallableName,
isKeywordTypeNode,
type ImportedTypeNameResolver,
} from './utils.js';
@@ -44,18 +45,6 @@ interface ImportClause {
specifier: string;
}
function getCallableName(expr: ts.Expression): string | null {
if (ts.isIdentifier(expr)) {
return expr.text;
}
if (ts.isPropertyAccessExpression(expr)) {
return expr.name.text;
}
return null;
}
/** Extract all named imports from the source file (both type and value imports). */
function extractImports(sf: ts.SourceFile): ImportClause[] {
const result: ImportClause[] = [];
+12
View File
@@ -2,6 +2,18 @@ import ts from 'typescript';
export type ImportedTypeNameResolver = (name: string) => string;
export function getCallableName(expr: ts.Expression): string | null {
if (ts.isIdentifier(expr)) {
return expr.text;
}
if (ts.isPropertyAccessExpression(expr)) {
return expr.name.text;
}
return null;
}
function identityImportedTypeName(name: string): string {
return name;
}
-75
View File
@@ -1,75 +0,0 @@
// Protobuf wire format helpers — used directly by wire.test.ts
// The generated code no longer depends on these (everything is inlined).
export function encodeVarint(value: number, buf: number[]): void {
value = value >>> 0;
while (value > 0x7f) {
buf.push((value & 0x7f) | 0x80);
value = value >>> 7;
}
buf.push(value & 0x7f);
}
export function decodeVarint(data: Uint8Array, offset: number): [number, number] {
let result = 0;
let shift = 0;
let byte: number;
do {
byte = data[offset++];
result |= (byte & 0x7f) << shift;
shift += 7;
} while (byte & 0x80);
return [result >>> 0, offset];
}
export function writeTag(fieldNumber: number, wireType: number, buf: number[]): void {
encodeVarint((fieldNumber << 3) | wireType, buf);
}
export function writeString(value: string, buf: number[]): void {
const encoded = new TextEncoder().encode(value);
encodeVarint(encoded.length, buf);
for (let i = 0; i < encoded.length; i++) {
buf.push(encoded[i]);
}
}
export function readString(data: Uint8Array, offset: number, length: number): string {
return new TextDecoder().decode(data.subarray(offset, offset + length));
}
export function writeBytes(value: Uint8Array, buf: number[]): void {
encodeVarint(value.length, buf);
for (let i = 0; i < value.length; i++) {
buf.push(value[i]);
}
}
export function skipField(data: Uint8Array, offset: number, wireType: number): number {
switch (wireType) {
case 0:
while (data[offset] & 0x80) {
offset++;
}
return offset + 1;
case 1:
return offset + 8;
case 2: {
const [len, o] = decodeVarint(data, offset);
return o + len;
}
case 5:
return offset + 4;
default:
throw new Error(`Unknown wire type: ${wireType}`);
}
}
@@ -6,29 +6,17 @@ import { loadFixture } from '../helpers';
/** Analyze fixture → generate → eval → return encode/decode pair */
function makeRoundTrip(fixtureName: string, msgName: string) {
const gen = generateCode(analyzeSource(loadFixture(fixtureName), fixtureName));
new Function(
gen +
`\nglobalThis.__enc = protobuf_encode_${msgName}; globalThis.__dec = protobuf_decode_${msgName};`,
)();
const enc = (globalThis as any).__enc as (obj: any) => Uint8Array;
const dec = (globalThis as any).__dec as (data: Uint8Array) => any;
delete (globalThis as any).__enc;
delete (globalThis as any).__dec;
return { enc, dec };
return makeRoundTripFromSchema(loadFixture(fixtureName), msgName);
}
function makeRoundTripFromSchema(schema: string, msgName: string) {
const gen = generateCode(analyzeSource(schema, `${msgName}.ts`));
new Function(
gen +
`\nglobalThis.__enc = protobuf_encode_${msgName}; globalThis.__dec = protobuf_decode_${msgName};`,
)();
const enc = (globalThis as any).__enc as (obj: any) => Uint8Array;
const dec = (globalThis as any).__dec as (data: Uint8Array) => any;
delete (globalThis as any).__enc;
delete (globalThis as any).__dec;
return { enc, dec };
return new Function(
gen + `\nreturn { enc: protobuf_encode_${msgName}, dec: protobuf_decode_${msgName} };`,
)() as {
enc: (obj: unknown) => Uint8Array;
dec: (data: Uint8Array) => any;
};
}
function encodeVarint32(value: number): number[] {
@@ -50,6 +38,30 @@ function encodeVarint32(value: number): number[] {
}
describe('code generator', () => {
it.each([
[0, []],
[1, [0x08, 0x01]],
[127, [0x08, 0x7f]],
[128, [0x08, 0x80, 0x01]],
[300, [0x08, 0xac, 0x02]],
[16384, [0x08, 0x80, 0x80, 0x01]],
[0xffffffff, [0x08, 0xff, 0xff, 0xff, 0xff, 0x0f]],
] as const)('encodes and decodes uint32 wire fixture %i', (value, bytes) => {
const { enc, dec } = makeRoundTrip('simple.ts', 'SimpleMsg');
expect([...enc({ value })]).toEqual(bytes);
expect(dec(Uint8Array.from(bytes)).value).toBe(value);
});
it.each([
[0x10, 0xac, 0x02],
[0x12, 0x03, 0x41, 0x42, 0x43],
[0x15, 0, 0, 0, 0],
[0x11, 0, 0, 0, 0, 0, 0, 0, 0],
])('skips an unknown wire field before a known field (%j)', (...bytes) => {
const { dec } = makeRoundTrip('simple.ts', 'SimpleMsg');
expect(dec(Uint8Array.from([...bytes, 0x08, 42])).value).toBe(42);
});
it('generates encode/decode for simple uint_32', () => {
const gen = generateCode(analyzeSource(loadFixture('simple.ts'), 't.ts'));
expect(gen).toContain('function protobuf_encode_SimpleMsg(obj)');
-117
View File
@@ -1,117 +0,0 @@
import { describe, it, expect } from 'vitest';
import {
encodeVarint,
decodeVarint,
writeTag,
writeString,
readString,
skipField,
} from '../../src/codegen/wire';
describe('wire format primitives', () => {
describe('varint', () => {
const cases: [number, number[]][] = [
[0, [0x00]],
[1, [0x01]],
[127, [0x7f]],
[128, [0x80, 0x01]],
[300, [0xac, 0x02]],
[16384, [0x80, 0x80, 0x01]],
];
it.each(cases)('encodeVarint(%i) produces correct bytes', (value, expected) => {
const buf: number[] = [];
encodeVarint(value, buf);
expect(buf).toEqual(expected);
});
it.each(cases)('decodeVarint round-trip for %i', (value) => {
const buf: number[] = [];
encodeVarint(value, buf);
const data = new Uint8Array(buf);
const [decoded, offset] = decodeVarint(data, 0);
expect(decoded).toBe(value);
expect(offset).toBe(buf.length);
});
it('handles max uint32', () => {
const value = 0xffffffff;
const buf: number[] = [];
encodeVarint(value, buf);
const [decoded] = decodeVarint(new Uint8Array(buf), 0);
expect(decoded).toBe(value);
});
});
describe('tag encoding', () => {
it('field 1, varint (wire type 0) produces 0x08', () => {
const buf: number[] = [];
writeTag(1, 0, buf);
expect(buf).toEqual([0x08]);
});
it('field 1, length-delimited (wire type 2) produces 0x0A', () => {
const buf: number[] = [];
writeTag(1, 2, buf);
expect(buf).toEqual([0x0a]);
});
it('field 2, varint produces 0x10', () => {
const buf: number[] = [];
writeTag(2, 0, buf);
expect(buf).toEqual([0x10]);
});
});
describe('string encoding', () => {
it('encodes and decodes "hello"', () => {
const buf: number[] = [];
writeString('hello', buf);
const data = new Uint8Array(buf);
// First byte is length (5), followed by UTF-8 bytes
expect(data[0]).toBe(5);
const decoded = readString(data, 1, 5);
expect(decoded).toBe('hello');
});
it('handles empty string', () => {
const buf: number[] = [];
writeString('', buf);
expect(buf).toEqual([0]); // length 0
});
it('handles unicode', () => {
const buf: number[] = [];
writeString('你好', buf);
const data = new Uint8Array(buf);
const [len, offset] = [data[0], 1];
const decoded = readString(data, offset, len);
expect(decoded).toBe('你好');
});
});
describe('skipField', () => {
it('skips varint field', () => {
const buf: number[] = [];
encodeVarint(300, buf); // multi-byte varint
const data = new Uint8Array(buf);
const newOffset = skipField(data, 0, 0);
expect(newOffset).toBe(buf.length);
});
it('skips length-delimited field', () => {
// length=3 followed by 3 bytes
const data = new Uint8Array([3, 0x41, 0x42, 0x43]);
const newOffset = skipField(data, 0, 2);
expect(newOffset).toBe(4);
});
it('skips 32-bit field', () => {
expect(skipField(new Uint8Array(4), 0, 5)).toBe(4);
});
it('skips 64-bit field', () => {
expect(skipField(new Uint8Array(8), 0, 1)).toBe(8);
});
});
});