feat(proton): add @snowluma/proton — compile-time protobuf DSL

Vendors SnowLuma/protobuf-dsl into the monorepo as @snowluma/proton, with
edge-case fixes applied on top of the upstream source. Reference copy of
the unmodified upstream is kept under dev/protobuf-dsl/.

Edge cases fixed (each with a regression test):
  - Aliased pb / pb_repeated marker imports (`import { pb as P }`)
    — collector now resolves the marker name through the import resolver
  - Concrete interface with a generic-instantiated message field
    (`wrapped: pb<5, Wrapper<uint_32>>`) — field typeName now resolves
    to the mangled form, and the inner instantiation is queued for
    monomorphization
  - Generic template with a generic-instantiated field
    (`interface Outer<U> { x: pb<5, Wrapper<U>> }`) — the inner
    instantiation is now re-substituted and re-monomorphized at outer
    instantiation time
  - Wrapper-binding synthetic SourceFile was empty-text — getText()
    returned garbage, breaking mangled name computation downstream
  - Cross-file forwarded wrappers (chain → imported base) were not
    detected — parseFileForDefinitions now resolves imports recursively
    (value imports only, with cycle protection)
  - matchForwardedKnownWrapper did not propagate typePattern — chain
    treated <X> as the encoded type instead of Wrapper<X>
  - Synthetic-SF WeakMap was private to analyzer.ts — replacer could
    not see it. Moved to utils.ts so typeNodeToMangledName picks it up
    automatically.

Defensive guard: any field whose typeName resolves to neither a primitive
nor a known message now throws at analyze() time, instead of silently
leaking the WireType.Varint placeholder into codegen.

95/95 tests pass; tsc --noEmit clean.
This commit is contained in:
DrSmoothl committed 2026-05-19 08:19:13 +08:00
1 parent 02d1848d39
commit 194165f49d
62 files changed
+4714

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
// Aliased import of the pb / pb_repeated markers.
//
// `parsePbTypeRef` compared the literal type-ref identifier text against
// the constants `'pb'` / `'pb_repeated'`. With `import { pb as P }` the
// identifier text becomes `'P'`, so the field was silently dropped from
// the message — interfaces ended up looking empty.
//
// Fix: resolve the marker name through the imported-name resolver before
// comparing.
import type { pb as P, pb_repeated as PR, uint_32 } from '@snowluma/proton';
interface Aliased {
id: P<1, uint_32>;
tags: PR<2, string>;
}
const data = protobuf_encode<Aliased>({ id: 1, tags: ['a', 'b'] });
const decoded = protobuf_decode<Aliased>(data);
export { };
+3
View File
@@ -0,0 +1,3 @@
interface BoolMsg {
active: pb<1, bool>;
}
@@ -0,0 +1,7 @@
import { protobuf_encode, protobuf_decode } from '@snowluma/proton';
import type { UserMsg as AliasMsg } from './types';
const buf = protobuf_encode<AliasMsg>({ id: 42, name: 'alice' });
const decoded = protobuf_decode<AliasMsg>(buf);
export { };
+6
View File
@@ -0,0 +1,6 @@
import type { UserMsg } from './types';
const buf = protobuf_encode<UserMsg>({ id: 42, name: 'alice' });
const decoded = protobuf_decode<UserMsg>(buf);
export { };
+5
View File
@@ -0,0 +1,5 @@
interface Deep {
val: pb<1, uint_32>;
}
export type { Deep };
@@ -0,0 +1,6 @@
import type { Wrapper } from './generic-types';
const buf = protobuf_encode<Wrapper<string>>({ value: 'hello' });
const decoded = protobuf_decode<Wrapper<string>>(buf);
export { };
@@ -0,0 +1,5 @@
interface Wrapper<T> {
value: pb<1, T>;
}
export type { Wrapper };
+5
View File
@@ -0,0 +1,5 @@
interface Inner {
value: pb<1, uint_32>;
}
export type { Inner };
@@ -0,0 +1,11 @@
import type { Wrapper } from './generic-types';
import { protobuf_encode, protobuf_decode } from '@snowluma/proton';
function localEncode<T>(value: T): Uint8Array {
return protobuf_encode<T>(value);
}
const buf = localEncode<Wrapper<string>>({ value: 'local' });
const decoded = protobuf_decode<Wrapper<string>>(buf);
export { };
+7
View File
@@ -0,0 +1,7 @@
import type { Deep } from './deep';
interface Mid {
inner: pb<1, Deep>;
}
export type { Mid };
+10
View File
@@ -0,0 +1,10 @@
import type { Inner } from './inner';
interface Outer {
inner: pb<1, Inner>;
}
const buf = protobuf_encode<Outer>({ inner: { value: 42 } });
const decoded = protobuf_decode<Outer>(buf);
export { };
+5
View File
@@ -0,0 +1,5 @@
interface SimpleMessage {
id: pb<1, uint_32>;
}
export { SimpleMessage };
+10
View File
@@ -0,0 +1,10 @@
import type { Mid } from './mid';
interface Top {
nested: pb<1, Mid>;
}
const buf = protobuf_encode<Top>({ nested: { inner: { val: 99 } } });
const decoded = protobuf_decode<Top>(buf);
export { };
+6
View File
@@ -0,0 +1,6 @@
interface UserMsg {
id: pb<1, uint_32>;
name: pb<2, string>;
}
export type { UserMsg };
@@ -0,0 +1,7 @@
import { SimpleMessage } from './protobuf';
import { protobuf_encode, protobuf_decode } from '@snowluma/proton';
const bytes = protobuf_encode<SimpleMessage>({ id: 42 });
const decoded = protobuf_decode<SimpleMessage>(bytes);
export { };
+49
View File
@@ -0,0 +1,49 @@
import { protobuf_encode, protobuf_decode } from '@snowluma/proton';
export function encodeViaWrapper<T>(value: T): Uint8Array {
return protobuf_encode<T>(value);
}
export function decodeViaWrapper<T>(data: Uint8Array): T {
return protobuf_decode<T>(data);
}
export const encodeArrowWrapper = <T>(value: T): Uint8Array => protobuf_encode<T>(value);
export const decodeArrowWrapper = <T>(data: Uint8Array): T => protobuf_decode<T>(data);
export function encodeSecondGeneric<Ignored, T>(_ignored: Ignored, value: T): Uint8Array {
return protobuf_encode<T>(value);
}
export function passthroughOnly<T>(value: T): T {
return value;
}
export const wrapperObject = {
encode<T>(value: T): Uint8Array {
return protobuf_encode<T>(value);
},
decode<T>(data: Uint8Array): T {
return protobuf_decode<T>(data);
},
encodeArrow: <T>(value: T): Uint8Array => protobuf_encode<T>(value),
};
export function encodeWithBranch<T>(value: T, enabled: boolean): Uint8Array {
if (enabled) {
return protobuf_encode<T>(value);
}
return protobuf_encode<T>(value);
}
export function encodeWithNestedHelper<T>(value: T): Uint8Array {
function inner(payload: T): Uint8Array {
return protobuf_encode<T>(payload);
}
return inner(value);
}
export function encodeChained<T>(value: T): Uint8Array {
return encodeViaWrapper<T>(value);
}
@@ -0,0 +1,21 @@
import type { Wrapper } from './generic-types';
import {
wrapperObject,
encodeWithBranch,
encodeWithNestedHelper,
encodeChained,
} from './wrapper-api';
const objectBuf = wrapperObject.encode<Wrapper<string>>({ value: 'object' });
const objectDecoded = wrapperObject.decode<Wrapper<string>>(objectBuf);
const arrowMemberBuf = wrapperObject.encodeArrow<Wrapper<Wrapper<string>>>({ value: { value: 'member' } });
const { encodeArrow } = wrapperObject;
const destructuredBuf = encodeArrow<Wrapper<string>>({ value: 'destructured' });
const branchBuf = encodeWithBranch<Wrapper<string>>({ value: 'branch' }, true);
const nestedHelperBuf = encodeWithNestedHelper<Wrapper<string>>({ value: 'nested-helper' });
const chainedBuf = encodeChained<Wrapper<string>>({ value: 'chained' });
export { };
@@ -0,0 +1,7 @@
import type { Wrapper } from './generic-types';
import { encodeViaWrapper, decodeViaWrapper } from './wrapper-api';
const buf = encodeViaWrapper<Wrapper<string>>({ value: 'hello' });
const decoded = decodeViaWrapper<Wrapper<string>>(buf);
export { };
@@ -0,0 +1,21 @@
import type { Wrapper } from './generic-types';
import {
encodeViaWrapper as encAlias,
decodeViaWrapper as decAlias,
encodeArrowWrapper,
decodeArrowWrapper,
encodeSecondGeneric,
passthroughOnly,
} from './wrapper-api';
const aliasBuf = encAlias<Wrapper<string>>({ value: 'alias' });
const aliasDecoded = decAlias<Wrapper<string>>(aliasBuf);
const arrowBuf = encodeArrowWrapper<Wrapper<Wrapper<string>>>({ value: { value: 'nested' } });
const arrowDecoded = decodeArrowWrapper<Wrapper<Wrapper<string>>>(arrowBuf);
const secondBuf = encodeSecondGeneric<number, Wrapper<string>>(1, { value: 'second' });
const passthrough = passthroughOnly<Wrapper<string>>({ value: 'ignored' });
export { };
@@ -0,0 +1,6 @@
import type { Wrapper } from './generic-types';
import { protobuf_encode } from '@snowluma/proton';
export function encodeBoxed<T>(value: Wrapper<T>): Uint8Array {
return protobuf_encode<Wrapper<T>>(value);
}
@@ -0,0 +1,15 @@
import {
encodeBoxed,
decodeBoxed,
encodeAliasBoxed,
encodeNestedBoxed,
encodeBoxedChain,
} from './wrapper-type-source';
const boxedBuf = encodeBoxed<string>({ value: 'boxed' });
const boxedDecoded = decodeBoxed<string>(boxedBuf);
const aliasBuf = encodeAliasBoxed<string>({ value: 'alias' });
const nestedBuf = encodeNestedBoxed<string>({ value: { value: 'nested' } });
const chainedBuf = encodeBoxedChain<string>({ value: 'chain' });
export { };
@@ -0,0 +1,25 @@
import type { Wrapper } from './generic-types';
import { protobuf_encode, protobuf_decode } from '@snowluma/proton';
import { encodeBoxed as encodeBoxedBase } from './wrapper-type-source-base';
export type AliasWrapper<T> = Wrapper<T>;
export function encodeBoxed<T>(value: Wrapper<T>): Uint8Array {
return protobuf_encode<Wrapper<T>>(value);
}
export function decodeBoxed<T>(data: Uint8Array): Wrapper<T> {
return protobuf_decode<Wrapper<T>>(data);
}
export function encodeAliasBoxed<T>(value: AliasWrapper<T>): Uint8Array {
return protobuf_encode<Wrapper<T>>(value);
}
export function encodeNestedBoxed<T>(value: Wrapper<Wrapper<T>>): Uint8Array {
return protobuf_encode<Wrapper<Wrapper<T>>>(value);
}
export function encodeBoxedChain<T>(value: Wrapper<T>): Uint8Array {
return encodeBoxedBase<T>(value);
}
+3
View File
@@ -0,0 +1,3 @@
interface TestProtobufAny<T> {
name?: pb<1, T>;
}
+8
View File
@@ -0,0 +1,8 @@
interface TestProtobufAny<T> {
name?: pb<1, T>;
}
const dataT = protobuf_encode<TestProtobufAny<TestProtobufAny<string>>>({ name: { name: "test" } });
const decodedT = protobuf_decode<TestProtobufAny<TestProtobufAny<string>>>(dataT);
export { };
@@ -0,0 +1,24 @@
// Generic template with a generic-instantiated message field.
//
// The outer template's type param (`U`) shows up *inside* an inner generic
// instantiation (`Wrapper<U>`). When the outer is monomorphized at the call
// site (`Outer<uint_32>`), the inner instantiation must be re-instantiated
// with the substitution (becoming `Wrapper<uint_32>`) and put into the
// registry under its mangled name `Wrapper__uint_32`.
//
// Without the fix, the field's `rawTypeName` was `'Wrapper__U'` and the
// substituted-mono never produced `Wrapper__uint_32`, leaving the field's
// wire-type unresolved.
interface Wrapper<T> {
value: pb<1, T>;
}
interface Outer<U> {
wrapped: pb<5, Wrapper<U>>;
}
const data = protobuf_encode<Outer<uint_32>>({ wrapped: { value: 42 } });
const decoded = protobuf_decode<Outer<uint_32>>(data);
export { };
+5
View File
@@ -0,0 +1,5 @@
interface UserProfile {
id: pb<1, uint_32>;
username: pb<2, string>;
active: pb<3, bool>;
}
+24
View File
@@ -0,0 +1,24 @@
// Concrete interface with a generic-instantiated message field.
//
// The call site only references the concrete outer type (`Container`), so
// the analyzer must independently:
// 1) resolve the field's typeName to the *mangled* form (`Wrapper__uint_32`)
// so it matches what monomorphization produces,
// 2) enqueue `Wrapper<uint_32>` for monomorphization itself, since the
// call-site-driven queue only sees `Container`.
//
// Without both, wire-type resolution silently leaves the field at the
// `WireType.Varint` placeholder and codegen emits a wrong wire format.
interface Wrapper<T> {
value: pb<1, T>;
}
interface Container {
wrapped: pb<5, Wrapper<uint_32>>;
}
const data = protobuf_encode<Container>({ wrapped: { value: 42 } });
const decoded = protobuf_decode<Container>(data);
export { };
+7
View File
@@ -0,0 +1,7 @@
interface Inner {
value: pb<1, uint_32>;
}
interface Outer {
inner: pb<1, Inner>;
}
+3
View File
@@ -0,0 +1,3 @@
interface Foo {
bar: string;
}
+12
View File
@@ -0,0 +1,12 @@
interface TestProtobuf {
name: pb<1, uint_32>;
}
interface TestProtobufOutput {
name: pb<1, TestProtobuf>;
}
const data = protobuf_encode<TestProtobuf>({ name: 123 });
const decoded = protobuf_decode<TestProtobufOutput>(data);
export { };
+9
View File
@@ -0,0 +1,9 @@
interface RepeatedMsg {
ids: pb_repeated<1, uint_32>;
names: pb_repeated<2, string>;
}
const data = protobuf_encode<RepeatedMsg>({ ids: [1, 2, 3], names: ["a", "b"] });
const decoded = protobuf_decode<RepeatedMsg>(data);
export { };
+3
View File
@@ -0,0 +1,3 @@
interface SimpleMsg {
value: pb<1, uint_32>;
}
+3
View File
@@ -0,0 +1,3 @@
interface StringMsg {
text: pb<1, string>;
}