Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
Publish MCP to npm / Build, test, and publish MCP (push) Waiting to run
Publish SDK to npm / Build, test, and publish SDK (push) Waiting to run
Sync action catalog to docs / sync (push) Waiting to run
Publish UI to npm / Build and publish UI (push) Waiting to run
Build and Release / preflight (push) Waiting to run
41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
import ts from 'typescript';
|
|
import { SourceContext } from '../../src/source/context';
|
|
import { SchemaResolver } from '../../src/schema/resolver';
|
|
import { generateCode } from '../../src/codegen/generator';
|
|
|
|
export function schemaCodecs<T>(code: string, name = 'Msg') {
|
|
const generated = generateCode(schemaRegistry(code));
|
|
return new Function(
|
|
`${generated}\nreturn { encode: protobuf_encode_${name}, decode: protobuf_decode_${name} };`,
|
|
)() as {
|
|
encode(value: T): Uint8Array;
|
|
decode(bytes: Uint8Array): T;
|
|
};
|
|
}
|
|
|
|
export function schemaRegistry(code: string, file = 'schema.ts') {
|
|
const context = new SourceContext();
|
|
const source = context.source(file, code);
|
|
const resolver = new SchemaResolver(context);
|
|
const roots: string[] = [];
|
|
|
|
for (const statement of source.statements) {
|
|
if (ts.isInterfaceDeclaration(statement) && !statement.typeParameters?.length) {
|
|
roots.push(statement.name.text);
|
|
}
|
|
}
|
|
|
|
const expanded = context.source(
|
|
file,
|
|
code + roots.map((name, i) => `\ntype __TestRoot${i} = ${name};`).join(''),
|
|
);
|
|
|
|
for (const statement of expanded.statements) {
|
|
if (ts.isTypeAliasDeclaration(statement) && statement.name.text.startsWith('__TestRoot')) {
|
|
resolver.message(resolver.type(statement.type), statement);
|
|
}
|
|
}
|
|
|
|
return resolver.registry;
|
|
}
|