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

@@ -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);
}