Migrate project to pnpm monorepo

Restructure the repository into a pnpm workspace under packages/ (core, webui, runtime). Move source files for core and webui into packages/*, add package manifests for @snowluma/core and @snowluma/runtime, and remove the old launcher package.json. Update root package.json to be a monorepo controller (scripts delegate to pnpm filters, set packageManager and Node engine). Configure build outputs to emit into the repo-root dist/ (vite config changes) so the existing release pipeline continues to work. Add .npmrc with mirrors, expand .gitignore for monorepo layout, add tsconfig base and package-level tsconfigs, and add vitest config for the core package. Adjust injector native search paths and update tests import paths to the new package layout. Small CI/workflow comment added to clarify build behavior and README updated with monorepo structure and common commands.
This commit is contained in:
手瓜一十雪 committed 2026-05-03 14:02:29 +08:00
1 parent 6bba36bba6
commit 39f1672b29
122 files changed
+279 -85

No files matched your search

+241
View File
@@ -0,0 +1,241 @@
// Runtime schema-based protobuf decode/encode.
// TypeScript equivalent of the C++ proto_field<N,T> DSL using plain schema objects.
//
// Usage:
// const MyMessageSchema = {
// fromUin: { field: 1, type: 'uint32' as const },
// fromUid: { field: 2, type: 'string' as const },
// grp: { field: 8, type: 'message' as const, schema: GrpSchema },
// elements: { field: 2, type: 'repeated_message' as const, schema: ElemSchema },
// };
// type MyMessage = ProtoDecoded<typeof MyMessageSchema>;
// const msg = protoDecode(rawBytes, MyMessageSchema);
import { WireMessage, WireType, encodeVarint, encodeKey, decodeVarint } from './wire';
// --- Schema types ---
export type FieldType =
| 'bool' | 'int32' | 'uint32' | 'int64' | 'uint64'
| 'string' | 'bytes' | 'message'
| 'repeated_varint' | 'repeated_uint32' | 'repeated_string'
| 'repeated_bytes' | 'repeated_message';
export interface FieldDef {
field: number;
type: FieldType;
schema?: ProtoSchema; // required for 'message' / 'repeated_message'
}
export type ProtoSchema = { [key: string]: FieldDef };
// --- Decoded type inference ---
type FieldTypeToTS<T extends FieldType, S extends ProtoSchema | undefined> =
T extends 'bool' ? boolean :
T extends 'int32' | 'uint32' ? number :
T extends 'int64' | 'uint64' ? bigint :
T extends 'string' ? string :
T extends 'bytes' ? Uint8Array :
T extends 'message' ? (S extends ProtoSchema ? ProtoDecoded<S> : Record<string, unknown>) :
T extends 'repeated_varint' | 'repeated_uint32' ? number[] :
T extends 'repeated_string' ? string[] :
T extends 'repeated_bytes' ? Uint8Array[] :
T extends 'repeated_message' ? (S extends ProtoSchema ? ProtoDecoded<S>[] : Record<string, unknown>[]) :
never;
type IsRepeated<T extends FieldType> = T extends `repeated_${string}` ? true : false;
type FieldValue<F extends FieldDef> =
IsRepeated<F['type']> extends true
? FieldTypeToTS<F['type'], F['schema']>
: FieldTypeToTS<F['type'], F['schema']> | undefined;
export type ProtoDecoded<S extends ProtoSchema> = {
[K in keyof S]: FieldValue<S[K]>;
};
// --- Decode ---
export function protoDecode<S extends ProtoSchema>(
data: Uint8Array | WireMessage,
schema: S
): ProtoDecoded<S> | null {
const wire = data instanceof WireMessage ? data : WireMessage.decode(data);
if (!wire) return null;
const result: Record<string, unknown> = {};
for (const [key, def] of Object.entries(schema)) {
switch (def.type) {
case 'bool': {
const v = wire.getVarint(def.field);
if (v !== null) result[key] = v !== 0n;
break;
}
case 'int32': {
const v = wire.getInt32(def.field);
if (v !== null) result[key] = v;
break;
}
case 'uint32': {
const v = wire.getUint32(def.field);
if (v !== null) result[key] = v;
break;
}
case 'int64': {
const v = wire.getInt64(def.field);
if (v !== null) result[key] = v;
break;
}
case 'uint64': {
const v = wire.getUint64(def.field);
if (v !== null) result[key] = v;
break;
}
case 'string': {
const v = wire.getString(def.field);
if (v !== null) result[key] = v;
break;
}
case 'bytes': {
const v = wire.getBytes(def.field);
if (v !== null) result[key] = v;
break;
}
case 'message': {
const sub = wire.getMessage(def.field);
if (sub && def.schema) {
result[key] = protoDecode(sub, def.schema);
}
break;
}
case 'repeated_varint': {
const vals = wire.getRepeatedVarint(def.field);
result[key] = vals.map(v => Number(BigInt.asIntN(32, v)));
break;
}
case 'repeated_uint32': {
const vals = wire.getRepeatedVarint(def.field);
result[key] = vals.map(v => Number(BigInt.asUintN(32, v)));
break;
}
case 'repeated_string': {
result[key] = wire.getRepeatedString(def.field);
break;
}
case 'repeated_bytes': {
const flds = wire.fields(def.field);
result[key] = flds
.filter(f => f.wireType === WireType.LengthDelimited)
.map(f => f.bytes);
break;
}
case 'repeated_message': {
if (!def.schema) { result[key] = []; break; }
const msgs = wire.getRepeatedMessage(def.field);
result[key] = msgs
.map(m => protoDecode(m, def.schema!))
.filter((m): m is NonNullable<typeof m> => m !== null);
break;
}
}
}
return result as ProtoDecoded<S>;
}
// --- Encode ---
export function protoEncode<S extends ProtoSchema>(
obj: Partial<ProtoDecoded<S>>,
schema: S
): Uint8Array {
const out: number[] = [];
const record = obj as Record<string, any>;
for (const [key, def] of Object.entries(schema)) {
const val = record[key];
if (val === undefined || val === null) continue;
switch (def.type) {
case 'bool': {
encodeKey(def.field, WireType.Varint, out);
encodeVarint(val ? 1 : 0, out);
break;
}
case 'int32':
case 'uint32': {
encodeKey(def.field, WireType.Varint, out);
encodeVarint(val as number, out);
break;
}
case 'int64':
case 'uint64': {
encodeKey(def.field, WireType.Varint, out);
encodeVarint(val as bigint, out);
break;
}
case 'string': {
const encoded = new TextEncoder().encode(val as string);
encodeKey(def.field, WireType.LengthDelimited, out);
encodeVarint(encoded.length, out);
for (const b of encoded) out.push(b);
break;
}
case 'bytes': {
const buf = val as Uint8Array;
encodeKey(def.field, WireType.LengthDelimited, out);
encodeVarint(buf.length, out);
for (const b of buf) out.push(b);
break;
}
case 'message': {
if (!def.schema) break;
const nested = protoEncode(val as any, def.schema);
encodeKey(def.field, WireType.LengthDelimited, out);
encodeVarint(nested.length, out);
for (const b of nested) out.push(b);
break;
}
case 'repeated_varint':
case 'repeated_uint32': {
for (const v of val as number[]) {
encodeKey(def.field, WireType.Varint, out);
encodeVarint(v, out);
}
break;
}
case 'repeated_string': {
const encoder = new TextEncoder();
for (const s of val as string[]) {
const encoded = encoder.encode(s);
encodeKey(def.field, WireType.LengthDelimited, out);
encodeVarint(encoded.length, out);
for (const b of encoded) out.push(b);
}
break;
}
case 'repeated_bytes': {
for (const buf of val as Uint8Array[]) {
encodeKey(def.field, WireType.LengthDelimited, out);
encodeVarint(buf.length, out);
for (const b of buf) out.push(b);
}
break;
}
case 'repeated_message': {
if (!def.schema) break;
for (const item of val as any[]) {
const nested = protoEncode(item, def.schema);
encodeKey(def.field, WireType.LengthDelimited, out);
encodeVarint(nested.length, out);
for (const b of nested) out.push(b);
}
break;
}
}
}
return new Uint8Array(out);
}
+242
View File
@@ -0,0 +1,242 @@
// Protobuf wire-format decoder/encoder.
// Generic decoder that works without .proto schemas — decodes any protobuf
// binary into a field_number → value tree structure.
// Port of src/protobuf/wire.h + wire.cpp
export enum WireType {
Varint = 0,
Fixed64 = 1,
LengthDelimited = 2,
Fixed32 = 5,
}
export interface WireField {
fieldNumber: number;
wireType: WireType;
varint: bigint; // Varint, Fixed64, Fixed32
bytes: Uint8Array; // LengthDelimited
}
// --- Varint decode ---
export function decodeVarint(data: Uint8Array, offset: number): [bigint, number] | null {
let result = 0n;
for (let i = 0; i < 10 && offset + i < data.length; i++) {
result |= BigInt(data[offset + i] & 0x7f) << BigInt(7 * i);
if ((data[offset + i] & 0x80) === 0) {
return [result, i + 1];
}
}
return null;
}
// --- Varint encode ---
export function encodeVarint(value: bigint | number, out: number[]): void {
let v = typeof value === 'number' ? BigInt(value >>> 0) : value;
if (v < 0n) v = v + (1n << 64n); // treat as unsigned 64-bit
while (v >= 0x80n) {
out.push(Number(v & 0x7Fn) | 0x80);
v >>= 7n;
}
out.push(Number(v));
}
// --- Key encode ---
export function encodeKey(fieldNumber: number, wireType: WireType, out: number[]): void {
encodeVarint(BigInt(fieldNumber) << 3n | BigInt(wireType), out);
}
// --- Field value helpers ---
export function fieldAsInt32(f: WireField): number { return Number(BigInt.asIntN(32, f.varint)); }
export function fieldAsUint32(f: WireField): number { return Number(BigInt.asUintN(32, f.varint)); }
export function fieldAsInt64(f: WireField): bigint { return BigInt.asIntN(64, f.varint); }
export function fieldAsUint64(f: WireField): bigint { return f.varint; }
export function fieldAsBool(f: WireField): boolean { return f.varint !== 0n; }
export function fieldAsString(f: WireField): string {
return new TextDecoder().decode(f.bytes);
}
export function fieldAsBytes(f: WireField): Uint8Array {
return f.bytes;
}
export class WireMessage {
private fields_: WireField[];
private constructor(fields: WireField[]) {
this.fields_ = fields;
}
static decode(data: Uint8Array): WireMessage | null {
if (!data || data.length === 0) return null;
const fields: WireField[] = [];
let pos = 0;
while (pos < data.length) {
const tagResult = decodeVarint(data, pos);
if (!tagResult) break;
const [tag, tagLen] = tagResult;
pos += tagLen;
const fieldNum = Number(tag >> 3n);
const wireType = Number(tag & 0x07n) as WireType;
if (fieldNum === 0) break;
const field: WireField = {
fieldNumber: fieldNum,
wireType,
varint: 0n,
bytes: new Uint8Array(0),
};
switch (wireType) {
case WireType.Varint: {
const valResult = decodeVarint(data, pos);
if (!valResult) return null;
field.varint = valResult[0];
pos += valResult[1];
break;
}
case WireType.Fixed64: {
if (pos + 8 > data.length) return null;
const dv = new DataView(data.buffer, data.byteOffset + pos, 8);
field.varint = dv.getBigUint64(0, true);
pos += 8;
break;
}
case WireType.Fixed32: {
if (pos + 4 > data.length) return null;
const dv = new DataView(data.buffer, data.byteOffset + pos, 4);
field.varint = BigInt(dv.getUint32(0, true));
pos += 4;
break;
}
case WireType.LengthDelimited: {
const lenResult = decodeVarint(data, pos);
if (!lenResult) return null;
const fieldLen = Number(lenResult[0]);
pos += lenResult[1];
if (pos + fieldLen > data.length) return null;
field.bytes = data.subarray(pos, pos + fieldLen);
pos += fieldLen;
break;
}
default:
return null;
}
fields.push(field);
}
return new WireMessage(fields);
}
// --- Single field access ---
field(num: number): WireField | null {
for (const f of this.fields_) {
if (f.fieldNumber === num) return f;
}
return null;
}
has(num: number): boolean {
return this.field(num) !== null;
}
getVarint(num: number): bigint | null {
const f = this.field(num);
if (!f || f.wireType !== WireType.Varint) return null;
return f.varint;
}
getInt32(num: number): number | null {
const v = this.getVarint(num);
return v !== null ? Number(BigInt.asIntN(32, v)) : null;
}
getUint32(num: number): number | null {
const v = this.getVarint(num);
return v !== null ? Number(BigInt.asUintN(32, v)) : null;
}
getInt64(num: number): bigint | null {
const v = this.getVarint(num);
return v !== null ? BigInt.asIntN(64, v) : null;
}
getUint64(num: number): bigint | null {
return this.getVarint(num);
}
getString(num: number): string | null {
const f = this.field(num);
if (!f || f.wireType !== WireType.LengthDelimited) return null;
return fieldAsString(f);
}
getBytes(num: number): Uint8Array | null {
const f = this.field(num);
if (!f || f.wireType !== WireType.LengthDelimited) return null;
return f.bytes;
}
getMessage(num: number): WireMessage | null {
const f = this.field(num);
if (!f || f.wireType !== WireType.LengthDelimited) return null;
return WireMessage.decode(f.bytes);
}
// --- Repeated field access ---
fields(num: number): WireField[] {
return this.fields_.filter(f => f.fieldNumber === num);
}
getRepeatedVarint(num: number): bigint[] {
const result: bigint[] = [];
for (const f of this.fields_) {
if (f.fieldNumber !== num) continue;
if (f.wireType === WireType.Varint) {
result.push(f.varint);
} else if (f.wireType === WireType.LengthDelimited) {
// Packed repeated varint
let pos = 0;
while (pos < f.bytes.length) {
const r = decodeVarint(f.bytes, pos);
if (!r) break;
result.push(r[0]);
pos += r[1];
}
}
}
return result;
}
getRepeatedString(num: number): string[] {
const result: string[] = [];
const decoder = new TextDecoder();
for (const f of this.fields_) {
if (f.fieldNumber === num && f.wireType === WireType.LengthDelimited) {
result.push(decoder.decode(f.bytes));
}
}
return result;
}
getRepeatedMessage(num: number): WireMessage[] {
const result: WireMessage[] = [];
for (const f of this.fields_) {
if (f.fieldNumber === num && f.wireType === WireType.LengthDelimited) {
const sub = WireMessage.decode(f.bytes);
if (sub) result.push(sub);
}
}
return result;
}
// --- Raw iteration ---
get allFields(): WireField[] { return this.fields_; }
get empty(): boolean { return this.fields_.length === 0; }
}