@alepha/protobuf 0.10.7 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
- import { Alepha, Static, TObject, TSchema } from "@alepha/core";
1
+ import * as _alepha_core0 from "@alepha/core";
2
+ import { Alepha, SchemaCodec, StaticDecode, TObject, TSchema } from "@alepha/core";
2
3
  import protobufjs, { Type } from "protobufjs";
4
+ import "@alepha/datetime";
3
5
 
4
6
  //#region src/providers/ProtobufProvider.d.ts
5
7
  declare class ProtobufProvider {
@@ -10,15 +12,15 @@ declare class ProtobufProvider {
10
12
  /**
11
13
  * Encode an object to a Uint8Array.
12
14
  */
13
- encode(schema: TObject, data: any): Uint8Array;
15
+ encode(schema: ProtobufSchema, message: any): Uint8Array;
14
16
  /**
15
17
  * Decode a Uint8Array to an object.
16
18
  */
17
- decode<T extends TObject>(schema: T, data: Uint8Array): Static<T>;
19
+ decode<T = any>(schema: ProtobufSchema, data: Uint8Array): T;
18
20
  /**
19
21
  * Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.
20
22
  */
21
- parse(schema: ProtobufSchema | TObject, typeName?: string): Type;
23
+ parse(schema: ProtobufSchema, typeName?: string): Type;
22
24
  /**
23
25
  * Convert a TypeBox schema to a Protobuf schema as a string.
24
26
  */
@@ -59,5 +61,49 @@ interface CreateProtobufSchemaOptions {
59
61
  mainMessageName?: string;
60
62
  }
61
63
  //#endregion
62
- export { CreateProtobufSchemaOptions, ProtobufProvider, ProtobufSchema };
64
+ //#region src/providers/ProtobufSchemaCodec.d.ts
65
+ /**
66
+ * ProtobufSchemaCodec handles encoding/decoding for Protobuf format.
67
+ *
68
+ * Key differences from JSON codec:
69
+ * - BigInt values are kept as BigInt (not converted to string)
70
+ * - Date values are converted to ISO strings for protobuf compatibility
71
+ * - Binary data (Uint8Array) is kept as-is
72
+ * - Proto3 default values are applied when decoding (to handle omitted fields)
73
+ */
74
+ declare class ProtobufSchemaCodec extends SchemaCodec {
75
+ protected protobufProvider: ProtobufProvider;
76
+ protected decoder: TextDecoder;
77
+ encodeToString(schema: TSchema, value: any): string;
78
+ encodeToBinary(schema: TSchema, value: any): Uint8Array;
79
+ decode<T extends TSchema>(schema: T, value: any): StaticDecode<T>;
80
+ /**
81
+ * Apply proto3 default values for fields that were omitted during encoding.
82
+ * Proto3 omits fields with default values, so we need to restore them.
83
+ * Also converts enum integers back to their string values.
84
+ */
85
+ protected applyProto3Defaults(schema: TSchema, value: any): any;
86
+ /**
87
+ * Check if a schema is an enum type.
88
+ */
89
+ protected isEnum(schema: TSchema): boolean;
90
+ /**
91
+ * Convert an enum value from protobuf integer to TypeBox string.
92
+ */
93
+ protected convertEnumValue(schema: TSchema, value: any): any;
94
+ /**
95
+ * Get the proto3 default value for a schema type.
96
+ */
97
+ protected getProto3Default(schema: TSchema): any;
98
+ /**
99
+ * Transform types for Protobuf compatibility.
100
+ * This method is called for each type in the schema tree.
101
+ */
102
+ protected transformType(schema: TSchema): TSchema | undefined;
103
+ }
104
+ //#endregion
105
+ //#region src/index.d.ts
106
+ declare const AlephaProtobuf: _alepha_core0.Service<_alepha_core0.Module<{}>>;
107
+ //#endregion
108
+ export { AlephaProtobuf, CreateProtobufSchemaOptions, ProtobufProvider, ProtobufSchema, ProtobufSchemaCodec };
63
109
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/providers/ProtobufProvider.ts"],"sourcesContent":[],"mappings":";;;;cAWa,gBAAA;EAAA,mBAAgB,MAAA,EACF,MADE;EACF,mBAAA,OAAA,EACG,GADH,CAAA,MAAA,GACgB,OADhB,EACyB,IADzB,CAAA;EACgB,mBAAA,QAAA,EAAA,OACL,UADK;EAAS,mBAAA,eAAA,EAEd,GAFc,CAAA,MAAA,EAAA,MAAA,EAAA,CAAA;EAAtB;;;EAON,MAAA,CAAA,MAAA,EAAA,OAAA,EAAA,IAAA,EAAA,GAAA,CAAA,EAAqB,UAArB;EAAqB;;;EAOO,MAAA,CAAA,UAA1B,OAA0B,CAAA,CAAA,MAAA,EAAT,CAAS,EAAA,IAAA,EAAA,UAAA,CAAA,EAAa,MAAb,CAAoB,CAApB,CAAA;EAAoB;;;EAQ3C,KAAA,CAAA,MAAA,EAAjB,cAAiB,GAAA,OAAA,EAAA,QAAA,CAAA,EAAA,MAAA,CAAA,EAExB,IAFwB;EAExB;;;EAoDI,oBAAA,CAAA,MAAA,EApCG,OAoCH,EAAA,OAAA,CAAA,EAnCI,2BAmCJ,CAAA,EAAA,MAAA;EAsIuB;;;EA4CS,UAAA,2BAAA,CAAA,GAAA,EAlLhC,OAkLgC,EAAA,UAAA,EAAA,MAAA,CAAA,EAAA;IA0C7B,OAAA,EAAA,MAAc;IAET,WAAA,EAAA,MAAA,EAAA;;;;;gCAxFe;;;;;2BAqCL;;;;kCAOO;;;;;;;;;;;KA0CtB,cAAA;UAEK,2BAAA"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/providers/ProtobufProvider.ts","../src/providers/ProtobufSchemaCodec.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;cAIa,gBAAA;6BACc;8BACG,aAAa,SAAS;EAFvC,mBAAgB,QAAA,EAAA,OAGS,UAHT;EACF,mBAAA,eAAA,EAGW,GAHX,CAAA,MAAA,EAAA,MAAA,EAAA,CAAA;EACgB;;;EACL,MAAA,CAAA,MAAA,EAMd,cANc,EAAA,OAAA,EAAA,GAAA,CAAA,EAMiB,UANjB;EACA;;;EAYL,MAAA,CAAA,IAAA,GAAA,CAAA,CAAA,MAAA,EAAA,cAAA,EAAA,IAAA,EAAsB,UAAtB,CAAA,EAAmC,CAAnC;EAAsB;;;EAOW,KAAA,CAAA,MAAA,EAA3C,cAA2C,EAAA,QAAA,CAAA,EAAA,MAAA,CAAA,EAAA,IAAA;EAgBtD;;;EA0KoB,oBAAA,CAAA,MAAA,EA1KpB,OA0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A;EAKA;;;;EAKwC,UAAA,aAAA,CAAA,MAAA,EA8GtC,OA9GsC,CAAA,EA8G5B,OA9G4B,GAAA,SAAA;;;;cC1B3D,gBAAc,aAAA,CAAA,QAOzB,aAAA,CAPyB"}
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
- import { $inject, Alepha, t } from "@alepha/core";
1
+ import { $inject, $module, Alepha, SchemaCodec, t } from "@alepha/core";
2
2
  import protobufjs from "protobufjs";
3
+ import "@alepha/datetime";
3
4
 
4
5
  //#region src/providers/ProtobufProvider.ts
5
6
  var ProtobufProvider = class {
@@ -10,14 +11,14 @@ var ProtobufProvider = class {
10
11
  /**
11
12
  * Encode an object to a Uint8Array.
12
13
  */
13
- encode(schema, data) {
14
- return this.parse(schema).encode(this.alepha.parse(schema, data)).finish();
14
+ encode(schema, message) {
15
+ return this.parse(schema).encode(message).finish();
15
16
  }
16
17
  /**
17
18
  * Decode a Uint8Array to an object.
18
19
  */
19
20
  decode(schema, data) {
20
- return this.alepha.parse(schema, this.parse(schema).decode(data));
21
+ return this.parse(schema).decode(data);
21
22
  }
22
23
  /**
23
24
  * Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.
@@ -25,8 +26,7 @@ var ProtobufProvider = class {
25
26
  parse(schema, typeName = "root.Target") {
26
27
  const exists = this.schemas.get(schema);
27
28
  if (exists) return exists;
28
- const pbSchema = typeof schema === "string" ? schema : this.createProtobufSchema(schema);
29
- const type = this.protobuf.parse(pbSchema).root.lookupType(typeName);
29
+ const type = this.protobuf.parse(schema).root.lookupType(typeName);
30
30
  this.schemas.set(schema, type);
31
31
  return type;
32
32
  }
@@ -189,5 +189,97 @@ var ProtobufProvider = class {
189
189
  };
190
190
 
191
191
  //#endregion
192
- export { ProtobufProvider };
192
+ //#region src/providers/ProtobufSchemaCodec.ts
193
+ /**
194
+ * ProtobufSchemaCodec handles encoding/decoding for Protobuf format.
195
+ *
196
+ * Key differences from JSON codec:
197
+ * - BigInt values are kept as BigInt (not converted to string)
198
+ * - Date values are converted to ISO strings for protobuf compatibility
199
+ * - Binary data (Uint8Array) is kept as-is
200
+ * - Proto3 default values are applied when decoding (to handle omitted fields)
201
+ */
202
+ var ProtobufSchemaCodec = class extends SchemaCodec {
203
+ protobufProvider = $inject(ProtobufProvider);
204
+ decoder = new TextDecoder();
205
+ encodeToString(schema, value) {
206
+ const binary = this.encodeToBinary(schema, value);
207
+ return this.decoder.decode(binary);
208
+ }
209
+ encodeToBinary(schema, value) {
210
+ const proto = this.protobufProvider.createProtobufSchema(schema);
211
+ return this.protobufProvider.encode(proto, this.encode(schema, value));
212
+ }
213
+ decode(schema, value) {
214
+ const proto = this.protobufProvider.createProtobufSchema(schema);
215
+ const decoded = this.protobufProvider.decode(proto, value);
216
+ const withDefaults = this.applyProto3Defaults(schema, decoded);
217
+ return super.decode(schema, withDefaults);
218
+ }
219
+ /**
220
+ * Apply proto3 default values for fields that were omitted during encoding.
221
+ * Proto3 omits fields with default values, so we need to restore them.
222
+ * Also converts enum integers back to their string values.
223
+ */
224
+ applyProto3Defaults(schema, value) {
225
+ if (!value || typeof value !== "object") return value;
226
+ if (t.schema.isObject(schema)) {
227
+ const result = { ...value };
228
+ for (const [key, propSchema] of Object.entries(schema.properties)) if (!(key in result) || result[key] === void 0) result[key] = this.getProto3Default(propSchema);
229
+ else if (this.isEnum(propSchema)) result[key] = this.convertEnumValue(propSchema, result[key]);
230
+ else if (typeof result[key] === "object" && result[key] !== null) result[key] = this.applyProto3Defaults(propSchema, result[key]);
231
+ return result;
232
+ }
233
+ if (t.schema.isArray(schema) && Array.isArray(value)) return value.map((item) => this.applyProto3Defaults(schema.items, item));
234
+ return value;
235
+ }
236
+ /**
237
+ * Check if a schema is an enum type.
238
+ */
239
+ isEnum(schema) {
240
+ return "enum" in schema && Array.isArray(schema.enum);
241
+ }
242
+ /**
243
+ * Convert an enum value from protobuf integer to TypeBox string.
244
+ */
245
+ convertEnumValue(schema, value) {
246
+ if (typeof value === "number" && "enum" in schema && Array.isArray(schema.enum)) return schema.enum[value];
247
+ return value;
248
+ }
249
+ /**
250
+ * Get the proto3 default value for a schema type.
251
+ */
252
+ getProto3Default(schema) {
253
+ if (t.schema.isOptional(schema) || t.schema.isUnion(schema)) return;
254
+ if (t.schema.isArray(schema)) return [];
255
+ if (t.schema.isRecord(schema)) return {};
256
+ if (t.schema.isString(schema)) return "";
257
+ if (t.schema.isNumber(schema)) return 0;
258
+ if (t.schema.isInteger(schema)) return 0;
259
+ if (t.schema.isBigInt(schema)) return BigInt(0);
260
+ if (t.schema.isBoolean(schema)) return false;
261
+ if (t.schema.isObject(schema)) return {};
262
+ }
263
+ /**
264
+ * Transform types for Protobuf compatibility.
265
+ * This method is called for each type in the schema tree.
266
+ */
267
+ transformType(schema) {
268
+ if (t.schema.isBigInt(schema)) return t.bigint();
269
+ }
270
+ };
271
+
272
+ //#endregion
273
+ //#region src/index.ts
274
+ const AlephaProtobuf = $module({
275
+ name: "alepha.protobuf",
276
+ services: [ProtobufProvider, ProtobufSchemaCodec],
277
+ register: (alepha) => {
278
+ alepha.with(ProtobufProvider);
279
+ alepha.codec.register("protobuf", alepha.inject(ProtobufSchemaCodec));
280
+ }
281
+ });
282
+
283
+ //#endregion
284
+ export { AlephaProtobuf, ProtobufProvider, ProtobufSchemaCodec };
193
285
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["fields: string[]","subMessages: string[]","fieldType","valueSchema: TSchema | undefined"],"sources":["../src/providers/ProtobufProvider.ts"],"sourcesContent":["import {\n $inject,\n Alepha,\n type Static,\n type TObject,\n type TSchema,\n t,\n} from \"@alepha/core\";\nimport type { Type } from \"protobufjs\";\nimport protobufjs from \"protobufjs\";\n\nexport class ProtobufProvider {\n protected readonly alepha = $inject(Alepha);\n protected readonly schemas: Map<string | TObject, Type> = new Map();\n protected readonly protobuf: typeof protobufjs = protobufjs;\n protected readonly enumDefinitions: Map<string, string[]> = new Map();\n\n /**\n * Encode an object to a Uint8Array.\n */\n public encode(schema: TObject, data: any): Uint8Array {\n return this.parse(schema).encode(this.alepha.parse(schema, data)).finish();\n }\n\n /**\n * Decode a Uint8Array to an object.\n */\n public decode<T extends TObject>(schema: T, data: Uint8Array): Static<T> {\n return this.alepha.parse(schema, this.parse(schema).decode(data));\n }\n\n /**\n * Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.\n */\n public parse(\n schema: ProtobufSchema | TObject,\n typeName = \"root.Target\",\n ): Type {\n const exists = this.schemas.get(schema);\n if (exists) return exists;\n\n const pbSchema =\n typeof schema === \"string\" ? schema : this.createProtobufSchema(schema);\n const result = this.protobuf.parse(pbSchema);\n const type = result.root.lookupType(typeName);\n this.schemas.set(schema, type);\n return type;\n }\n\n /**\n * Convert a TypeBox schema to a Protobuf schema as a string.\n */\n public createProtobufSchema(\n schema: TSchema,\n options: CreateProtobufSchemaOptions = {},\n ): string {\n const { rootName = \"root\", mainMessageName = \"Target\" } = options;\n // Clear enum definitions for this schema generation\n this.enumDefinitions.clear();\n\n const context = {\n proto: `package ${rootName};\\nsyntax = \"proto3\";\\n\\n`,\n fieldIndex: 1,\n };\n\n if (t.schema.isObject(schema)) {\n const { message, subMessages } = this.parseObjectWithDependencies(\n schema,\n mainMessageName,\n );\n\n // Add all enum definitions first\n for (const [enumName, values] of this.enumDefinitions) {\n context.proto += this.generateEnumDefinition(enumName, values);\n }\n\n // Add all sub-messages\n context.proto += subMessages.join(\"\");\n // Then add the main message\n context.proto += message;\n }\n\n return context.proto;\n }\n\n /**\n * Parse an object schema with dependencies (sub-messages).\n */\n protected parseObjectWithDependencies(\n obj: TSchema,\n parentName: string,\n ): { message: string; subMessages: string[] } {\n if (!t.schema.isObject(obj)) {\n return { message: \"\", subMessages: [] };\n }\n\n const fields: string[] = [];\n const subMessages: string[] = [];\n let fieldIndex = 1;\n\n for (const [key, value] of Object.entries(obj.properties)) {\n // Handle arrays\n if (t.schema.isArray(value)) {\n // Check if array items are enums\n if (this.isEnum(value.items)) {\n const enumValues = this.getEnumValues(value.items);\n const enumName = this.registerEnum(key, enumValues);\n fields.push(` repeated ${enumName} ${key} = ${fieldIndex++};`);\n continue;\n }\n\n if (t.schema.isObject(value.items)) {\n const subMessageName =\n \"title\" in value.items && typeof value.items.title === \"string\"\n ? value.items.title\n : `${parentName}_${key}`;\n const { message: subMessage, subMessages: nestedSubMessages } =\n this.parseObjectWithDependencies(value.items, subMessageName);\n subMessages.push(...nestedSubMessages);\n subMessages.push(subMessage);\n fields.push(` repeated ${subMessageName} ${key} = ${fieldIndex++};`);\n continue;\n }\n\n const itemType = this.convertType(value.items);\n fields.push(` repeated ${itemType} ${key} = ${fieldIndex++};`);\n continue;\n }\n\n // Handle nested objects\n if (t.schema.isObject(value)) {\n const subMessageName =\n \"title\" in value && typeof value.title === \"string\"\n ? value.title\n : `${parentName}_${key}`;\n const { message: subMessage, subMessages: nestedSubMessages } =\n this.parseObjectWithDependencies(value, subMessageName);\n subMessages.push(...nestedSubMessages);\n subMessages.push(subMessage);\n fields.push(` ${subMessageName} ${key} = ${fieldIndex++};`);\n continue;\n }\n\n // Handle union types (nullable fields)\n if (t.schema.isUnion(value)) {\n const nonNullType = value.anyOf.find(\n (type: TSchema) => !t.schema.isNull(type),\n );\n if (nonNullType) {\n // Check if it's an enum\n if (this.isEnum(nonNullType)) {\n const enumValues = this.getEnumValues(nonNullType);\n const enumName = this.registerEnum(key, enumValues);\n fields.push(` ${enumName} ${key} = ${fieldIndex++};`);\n continue;\n }\n\n if (t.schema.isObject(nonNullType)) {\n const subMessageName =\n \"title\" in nonNullType && typeof nonNullType.title === \"string\"\n ? nonNullType.title\n : `${parentName}_${key}`;\n const { message: subMessage, subMessages: nestedSubMessages } =\n this.parseObjectWithDependencies(nonNullType, subMessageName);\n subMessages.push(...nestedSubMessages);\n subMessages.push(subMessage);\n fields.push(` ${subMessageName} ${key} = ${fieldIndex++};`);\n continue;\n }\n const fieldType = this.convertType(nonNullType);\n fields.push(` ${fieldType} ${key} = ${fieldIndex++};`);\n continue;\n }\n }\n\n // Handle records (maps)\n if (t.schema.isRecord(value)) {\n // TypeBox records use additionalProperties or patternProperties for the value type\n let valueSchema: TSchema | undefined;\n if (\n \"additionalProperties\" in value &&\n value.additionalProperties &&\n typeof value.additionalProperties === \"object\"\n ) {\n valueSchema = value.additionalProperties;\n } else if (\n value.patternProperties &&\n typeof value.patternProperties === \"object\"\n ) {\n // Get the first pattern property (usually \"^(.*)$\" or similar)\n const patterns = Object.values(value.patternProperties);\n if (patterns.length > 0 && typeof patterns[0] === \"object\") {\n valueSchema = patterns[0] as TSchema;\n }\n }\n\n if (valueSchema) {\n const valueType = this.convertType(valueSchema);\n fields.push(` map<string, ${valueType}> ${key} = ${fieldIndex++};`);\n continue;\n }\n }\n\n // Handle enum fields\n if (this.isEnum(value)) {\n const enumValues = this.getEnumValues(value);\n const enumName = this.registerEnum(key, enumValues);\n fields.push(` ${enumName} ${key} = ${fieldIndex++};`);\n continue;\n }\n\n // Handle regular fields\n const fieldType = this.convertType(value);\n fields.push(` ${fieldType} ${key} = ${fieldIndex++};`);\n }\n\n const message = `message ${parentName} {\\n${fields.join(\"\\n\")}\\n}\\n`;\n return { message, subMessages };\n }\n\n /**\n * Convert a primitive TypeBox schema type to a Protobuf spec type.\n */\n protected convertType(schema: TSchema): string {\n if (t.schema.isBoolean(schema)) return \"bool\";\n if (t.schema.isNumber(schema) && schema.format === \"int64\") return \"int64\";\n if (t.schema.isNumber(schema)) return \"double\";\n if (t.schema.isInteger(schema)) return \"int32\";\n if (t.schema.isBigInt(schema)) return \"int64\";\n if (t.schema.isString(schema)) return \"string\";\n\n // Handle union types (nullable)\n if (t.schema.isUnion(schema)) {\n // Find the non-null type in the union\n const nonNullType = schema.anyOf.find(\n (type: TSchema) => !t.schema.isNull(type),\n );\n if (nonNullType) {\n return this.convertType(nonNullType);\n }\n }\n\n // Handle optional types\n if (t.schema.isOptional(schema)) {\n return this.convertType(schema);\n }\n\n // Handle unsafe types (like enums)\n if (t.schema.isUnsafe(schema)) {\n // if it's an enum or other unsafe types, default to string\n return \"string\";\n }\n\n throw new Error(`Unsupported type: ${JSON.stringify(schema)}`);\n }\n\n /**\n * Check if a schema is an enum type.\n * TypeBox enums have an \"enum\" property with an array of values.\n */\n protected isEnum(schema: TSchema): boolean {\n return \"enum\" in schema && Array.isArray(schema.enum);\n }\n\n /**\n * Extract enum values from a TypeBox enum schema.\n */\n protected getEnumValues(schema: TSchema): string[] {\n if (\"enum\" in schema && Array.isArray(schema.enum)) {\n return schema.enum.map(String);\n }\n return [];\n }\n\n /**\n * Register an enum and return its type name.\n * Generates a PascalCase name from the field name.\n */\n protected registerEnum(fieldName: string, values: string[]): string {\n // Capitalize first letter of field name for enum type name\n const enumName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1);\n\n // Check if we already have this exact enum registered\n const valueKey = values.join(\",\");\n const existingEnum = Array.from(this.enumDefinitions.entries()).find(\n ([_, enumValues]) => enumValues.join(\",\") === valueKey,\n );\n\n if (existingEnum) {\n // Reuse existing enum with same values\n return existingEnum[0];\n }\n\n // Register new enum\n this.enumDefinitions.set(enumName, values);\n return enumName;\n }\n\n /**\n * Generate a protobuf enum definition.\n */\n protected generateEnumDefinition(enumName: string, values: string[]): string {\n const enumValues = values\n .map((value, index) => ` ${value} = ${index};`)\n .join(\"\\n\");\n return `enum ${enumName} {\\n${enumValues}\\n}\\n`;\n }\n}\n\nexport type ProtobufSchema = string;\n\nexport interface CreateProtobufSchemaOptions {\n rootName?: string;\n mainMessageName?: string;\n}\n"],"mappings":";;;;AAWA,IAAa,mBAAb,MAA8B;CAC5B,AAAmB,SAAS,QAAQ,OAAO;CAC3C,AAAmB,0BAAuC,IAAI,KAAK;CACnE,AAAmB,WAA8B;CACjD,AAAmB,kCAAyC,IAAI,KAAK;;;;CAKrE,AAAO,OAAO,QAAiB,MAAuB;AACpD,SAAO,KAAK,MAAM,OAAO,CAAC,OAAO,KAAK,OAAO,MAAM,Q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Aa;AAItB,OAAK,gBAAgB,IAAI,UAAU,OAAO;AAC1C,SAAO;;;;;CAMT,AAAU,uBAAuB,UAAkB,QAA0B;AAI3E,SAAO,QAAQ,SAAS,MAHL,OAChB,KAAK,OAAO,UAAU,KAAK,MAAM,KAAK,MAAM,GAAG,CAC/C,KAAK,KAAK,CAC4B"}
1
+ {"version":3,"file":"index.js","names":["fields: string[]","subMessages: string[]","fieldType","valueSchema: TSchema | undefined","result: any"],"sources":["../src/providers/ProtobufProvider.ts","../src/providers/ProtobufSchemaCodec.ts","../src/index.ts"],"sourcesContent":["import { $inject, Alepha, type TObject, type TSchema, t } from \"@alepha/core\";\nimport type { Type } from \"protobufjs\";\nimport protobufjs from \"protobufjs\";\n\nexport class ProtobufProvider {\n protected readonly alepha = $inject(Alepha);\n protected readonly schemas: Map<string | TObject, Type> = new Map();\n protected readonly protobuf: typeof protobufjs = protobufjs;\n protected readonly enumDefinitions: Map<string, string[]> = new Map();\n\n /**\n * Encode an object to a Uint8Array.\n */\n public encode(schema: ProtobufSchema, message: any): Uint8Array {\n return this.parse(schema).encode(message).finish();\n }\n\n /**\n * Decode a Uint8Array to an object.\n */\n public decode<T = any>(schema: ProtobufSchema, data: Uint8Array): T {\n return this.parse(schema).decode(data) as T;\n }\n\n /**\n * Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.\n */\n public parse(schema: ProtobufSchema, typeName = \"root.Target\"): Type {\n const exists = this.schemas.get(schema);\n if (exists) {\n return exists;\n }\n\n const result = this.protobuf.parse(schema);\n const type = result.root.lookupType(typeName);\n this.schemas.set(schema, type);\n return type;\n }\n\n /**\n * Convert a TypeBox schema to a Protobuf schema as a string.\n */\n public createProtobufSchema(\n schema: TSchema,\n options: CreateProtobufSchemaOptions = {},\n ): string {\n const { rootName = \"root\", mainMessageName = \"Target\" } = options;\n // Clear enum definitions for this schema generation\n this.enumDefinitions.clear();\n\n const context = {\n proto: `package ${rootName};\\nsyntax = \"proto3\";\\n\\n`,\n fieldIndex: 1,\n };\n\n if (t.schema.isObject(schema)) {\n const { message, subMessages } = this.parseObjectWithDependencies(\n schema,\n mainMessageName,\n );\n\n // Add all enum definitions first\n for (const [enumName, values] of this.enumDefinitions) {\n context.proto += this.generateEnumDefinition(enumName, values);\n }\n\n // Add all sub-messages\n context.proto += subMessages.join(\"\");\n // Then add the main message\n context.proto += message;\n }\n\n return context.proto;\n }\n\n /**\n * Parse an object schema with dependencies (sub-messages).\n */\n protected parseObjectWithDependencies(\n obj: TSchema,\n parentName: string,\n ): { message: string; subMessages: string[] } {\n if (!t.schema.isObject(obj)) {\n return { message: \"\", subMessages: [] };\n }\n\n const fields: string[] = [];\n const subMessages: string[] = [];\n let fieldIndex = 1;\n\n for (const [key, value] of Object.entries(obj.properties)) {\n // Handle arrays\n if (t.schema.isArray(value)) {\n // Check if array items are enums\n if (this.isEnum(value.items)) {\n const enumValues = this.getEnumValues(value.items);\n const enumName = this.registerEnum(key, enumValues);\n fields.push(` repeated ${enumName} ${key} = ${fieldIndex++};`);\n continue;\n }\n\n if (t.schema.isObject(value.items)) {\n const subMessageName =\n \"title\" in value.items && typeof value.items.title === \"string\"\n ? value.items.title\n : `${parentName}_${key}`;\n const { message: subMessage, subMessages: nestedSubMessages } =\n this.parseObjectWithDependencies(value.items, subMessageName);\n subMessages.push(...nestedSubMessages);\n subMessages.push(subMessage);\n fields.push(` repeated ${subMessageName} ${key} = ${fieldIndex++};`);\n continue;\n }\n\n const itemType = this.convertType(value.items);\n fields.push(` repeated ${itemType} ${key} = ${fieldIndex++};`);\n continue;\n }\n\n // Handle nested objects\n if (t.schema.isObject(value)) {\n const subMessageName =\n \"title\" in value && typeof value.title === \"string\"\n ? value.title\n : `${parentName}_${key}`;\n const { message: subMessage, subMessages: nestedSubMessages } =\n this.parseObjectWithDependencies(value, subMessageName);\n subMessages.push(...nestedSubMessages);\n subMessages.push(subMessage);\n fields.push(` ${subMessageName} ${key} = ${fieldIndex++};`);\n continue;\n }\n\n // Handle union types (nullable fields)\n if (t.schema.isUnion(value)) {\n const nonNullType = value.anyOf.find(\n (type: TSchema) => !t.schema.isNull(type),\n );\n if (nonNullType) {\n // Check if it's an enum\n if (this.isEnum(nonNullType)) {\n const enumValues = this.getEnumValues(nonNullType);\n const enumName = this.registerEnum(key, enumValues);\n fields.push(` ${enumName} ${key} = ${fieldIndex++};`);\n continue;\n }\n\n if (t.schema.isObject(nonNullType)) {\n const subMessageName =\n \"title\" in nonNullType && typeof nonNullType.title === \"string\"\n ? nonNullType.title\n : `${parentName}_${key}`;\n const { message: subMessage, subMessages: nestedSubMessages } =\n this.parseObjectWithDependencies(nonNullType, subMessageName);\n subMessages.push(...nestedSubMessages);\n subMessages.push(subMessage);\n fields.push(` ${subMessageName} ${key} = ${fieldIndex++};`);\n continue;\n }\n const fieldType = this.convertType(nonNullType);\n fields.push(` ${fieldType} ${key} = ${fieldIndex++};`);\n continue;\n }\n }\n\n // Handle records (maps)\n if (t.schema.isRecord(value)) {\n // TypeBox records use additionalProperties or patternProperties for the value type\n let valueSchema: TSchema | undefined;\n if (\n \"additionalProperties\" in value &&\n value.additionalProperties &&\n typeof value.additionalProperties === \"object\"\n ) {\n valueSchema = value.additionalProperties;\n } else if (\n value.patternProperties &&\n typeof value.patternProperties === \"object\"\n ) {\n // Get the first pattern property (usually \"^(.*)$\" or similar)\n const patterns = Object.values(value.patternProperties);\n if (patterns.length > 0 && typeof patterns[0] === \"object\") {\n valueSchema = patterns[0] as TSchema;\n }\n }\n\n if (valueSchema) {\n const valueType = this.convertType(valueSchema);\n fields.push(` map<string, ${valueType}> ${key} = ${fieldIndex++};`);\n continue;\n }\n }\n\n // Handle enum fields\n if (this.isEnum(value)) {\n const enumValues = this.getEnumValues(value);\n const enumName = this.registerEnum(key, enumValues);\n fields.push(` ${enumName} ${key} = ${fieldIndex++};`);\n continue;\n }\n\n // Handle regular fields\n const fieldType = this.convertType(value);\n fields.push(` ${fieldType} ${key} = ${fieldIndex++};`);\n }\n\n const message = `message ${parentName} {\\n${fields.join(\"\\n\")}\\n}\\n`;\n return { message, subMessages };\n }\n\n /**\n * Convert a primitive TypeBox schema type to a Protobuf spec type.\n */\n protected convertType(schema: TSchema): string {\n if (t.schema.isBoolean(schema)) return \"bool\";\n if (t.schema.isNumber(schema) && schema.format === \"int64\") return \"int64\";\n if (t.schema.isNumber(schema)) return \"double\";\n if (t.schema.isInteger(schema)) return \"int32\";\n if (t.schema.isBigInt(schema)) return \"int64\";\n if (t.schema.isString(schema)) return \"string\";\n\n // Handle union types (nullable)\n if (t.schema.isUnion(schema)) {\n // Find the non-null type in the union\n const nonNullType = schema.anyOf.find(\n (type: TSchema) => !t.schema.isNull(type),\n );\n if (nonNullType) {\n return this.convertType(nonNullType);\n }\n }\n\n // Handle optional types\n if (t.schema.isOptional(schema)) {\n return this.convertType(schema);\n }\n\n // Handle unsafe types (like enums)\n if (t.schema.isUnsafe(schema)) {\n // if it's an enum or other unsafe types, default to string\n return \"string\";\n }\n\n throw new Error(`Unsupported type: ${JSON.stringify(schema)}`);\n }\n\n /**\n * Check if a schema is an enum type.\n * TypeBox enums have an \"enum\" property with an array of values.\n */\n protected isEnum(schema: TSchema): boolean {\n return \"enum\" in schema && Array.isArray(schema.enum);\n }\n\n /**\n * Extract enum values from a TypeBox enum schema.\n */\n protected getEnumValues(schema: TSchema): string[] {\n if (\"enum\" in schema && Array.isArray(schema.enum)) {\n return schema.enum.map(String);\n }\n return [];\n }\n\n /**\n * Register an enum and return its type name.\n * Generates a PascalCase name from the field name.\n */\n protected registerEnum(fieldName: string, values: string[]): string {\n // Capitalize first letter of field name for enum type name\n const enumName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1);\n\n // Check if we already have this exact enum registered\n const valueKey = values.join(\",\");\n const existingEnum = Array.from(this.enumDefinitions.entries()).find(\n ([_, enumValues]) => enumValues.join(\",\") === valueKey,\n );\n\n if (existingEnum) {\n // Reuse existing enum with same values\n return existingEnum[0];\n }\n\n // Register new enum\n this.enumDefinitions.set(enumName, values);\n return enumName;\n }\n\n /**\n * Generate a protobuf enum definition.\n */\n protected generateEnumDefinition(enumName: string, values: string[]): string {\n const enumValues = values\n .map((value, index) => ` ${value} = ${index};`)\n .join(\"\\n\");\n return `enum ${enumName} {\\n${enumValues}\\n}\\n`;\n }\n}\n\nexport type ProtobufSchema = string;\n\nexport interface CreateProtobufSchemaOptions {\n rootName?: string;\n mainMessageName?: string;\n}\n","import {\n $inject,\n SchemaCodec,\n type StaticDecode,\n type TSchema,\n t,\n} from \"@alepha/core\";\nimport \"@alepha/datetime\";\nimport { ProtobufProvider } from \"./ProtobufProvider.ts\";\n\n/**\n * ProtobufSchemaCodec handles encoding/decoding for Protobuf format.\n *\n * Key differences from JSON codec:\n * - BigInt values are kept as BigInt (not converted to string)\n * - Date values are converted to ISO strings for protobuf compatibility\n * - Binary data (Uint8Array) is kept as-is\n * - Proto3 default values are applied when decoding (to handle omitted fields)\n */\nexport class ProtobufSchemaCodec extends SchemaCodec {\n protected protobufProvider = $inject(ProtobufProvider);\n protected decoder = new TextDecoder();\n\n public encodeToString(schema: TSchema, value: any): string {\n const binary = this.encodeToBinary(schema, value);\n return this.decoder.decode(binary);\n }\n\n public encodeToBinary(schema: TSchema, value: any): Uint8Array {\n const proto = this.protobufProvider.createProtobufSchema(schema);\n return this.protobufProvider.encode(proto, this.encode(schema, value));\n }\n\n public decode<T extends TSchema>(schema: T, value: any): StaticDecode<T> {\n // First decode from protobuf binary to object\n const proto = this.protobufProvider.createProtobufSchema(schema);\n const decoded = this.protobufProvider.decode(proto, value);\n\n // Apply proto3 default values for missing fields\n const withDefaults = this.applyProto3Defaults(schema, decoded);\n\n // Then use the parent decode to validate and transform\n return super.decode(schema, withDefaults);\n }\n\n /**\n * Apply proto3 default values for fields that were omitted during encoding.\n * Proto3 omits fields with default values, so we need to restore them.\n * Also converts enum integers back to their string values.\n */\n protected applyProto3Defaults(schema: TSchema, value: any): any {\n if (!value || typeof value !== \"object\") {\n return value;\n }\n\n if (t.schema.isObject(schema)) {\n const result: any = { ...value };\n\n for (const [key, propSchema] of Object.entries(schema.properties)) {\n if (!(key in result) || result[key] === undefined) {\n // Apply proto3 default values based on type\n result[key] = this.getProto3Default(propSchema);\n } else {\n // Convert enum integers to strings\n if (this.isEnum(propSchema)) {\n result[key] = this.convertEnumValue(propSchema, result[key]);\n } else if (typeof result[key] === \"object\" && result[key] !== null) {\n // Recursively apply defaults to nested objects\n result[key] = this.applyProto3Defaults(propSchema, result[key]);\n }\n }\n }\n\n return result;\n }\n\n if (t.schema.isArray(schema) && Array.isArray(value)) {\n return value.map((item) => this.applyProto3Defaults(schema.items, item));\n }\n\n return value;\n }\n\n /**\n * Check if a schema is an enum type.\n */\n protected isEnum(schema: TSchema): boolean {\n return \"enum\" in schema && Array.isArray(schema.enum);\n }\n\n /**\n * Convert an enum value from protobuf integer to TypeBox string.\n */\n protected convertEnumValue(schema: TSchema, value: any): any {\n if (\n typeof value === \"number\" &&\n \"enum\" in schema &&\n Array.isArray(schema.enum)\n ) {\n // Protobuf encodes enums as integers, convert back to string\n return schema.enum[value];\n }\n return value;\n }\n\n /**\n * Get the proto3 default value for a schema type.\n */\n protected getProto3Default(schema: TSchema): any {\n // Handle nullable/optional types - they can be undefined\n if (t.schema.isOptional(schema) || t.schema.isUnion(schema)) {\n return undefined;\n }\n\n // Handle arrays - default is empty array\n if (t.schema.isArray(schema)) {\n return [];\n }\n\n // Handle records (maps) - default is empty object\n if (t.schema.isRecord(schema)) {\n return {};\n }\n\n // Handle primitive types\n if (t.schema.isString(schema)) return \"\";\n if (t.schema.isNumber(schema)) return 0;\n if (t.schema.isInteger(schema)) return 0;\n if (t.schema.isBigInt(schema)) return BigInt(0);\n if (t.schema.isBoolean(schema)) return false;\n\n // For objects, return empty object (will be filled in recursively)\n if (t.schema.isObject(schema)) {\n return {};\n }\n\n return undefined;\n }\n\n /**\n * Transform types for Protobuf compatibility.\n * This method is called for each type in the schema tree.\n */\n protected transformType(schema: TSchema): TSchema | undefined {\n // For bigint: keep as-is, don't convert to string\n // The schema is still a string type with format \"int64\", but we override encode/decode\n if (t.schema.isBigInt(schema)) {\n return t.bigint();\n }\n\n // For other types, use default behavior\n return undefined;\n }\n}\n","import { $module } from \"@alepha/core\";\nimport { ProtobufProvider } from \"./providers/ProtobufProvider.ts\";\nimport { ProtobufSchemaCodec } from \"./providers/ProtobufSchemaCodec.ts\";\n\nexport * from \"./providers/ProtobufProvider.ts\";\nexport * from \"./providers/ProtobufSchemaCodec.ts\";\n\nexport const AlephaProtobuf = $module({\n name: \"alepha.protobuf\",\n services: [ProtobufProvider, ProtobufSchemaCodec],\n register: (alepha) => {\n alepha.with(ProtobufProvider);\n alepha.codec.register(\"protobuf\", alepha.inject(ProtobufSchemaCodec));\n },\n});\n"],"mappings":";;;;;AAIA,IAAa,mBAAb,MAA8B;CAC5B,AAAmB,SAAS,QAAQ,OAAO;CAC3C,AAAmB,0BAAuC,IAAI,KAAK;CACnE,AAAmB,WAA8B;CACjD,AAAmB,kCAAyC,IAAI,KAAK;;;;CAKrE,AAAO,OAAO,QAAwB,SAA0B;AAC9D,SAAO,KAAK,MAAM,OAAO,CAAC,OAAO,QAAQ,CAAC,QAAQ;;;;;CAMpD,AAAO,OAAgB,QAAwB,MAAqB;AAClE,SAAO,KAAK,MAAM,OAAO,CAAC,OAAO,KAAK;;;;;CAMxC,AAAO,MAAM,QAAwB,WAAW,eAAqB;EACnE,MAAM,SAAS,KAAK,QAAQ,IAAI,OAAO;AACvC,MAAI,OACF,QAAO;EAIT,MAAM,OADS,KAAK,SAAS,MAAM,OAAO,CACtB,KAAK,WAAW,SAAS;AAC7C,OAAK,QAAQ,IAAI,QAAQ,KAAK;AAC9B,SAAO;;;;;CAMT,AAAO,qBACL,QACA,UAAuC,EAAE,EACjC;EACR,MAAM,EAAE,WAAW,QAAQ,kBAAkB,aAAa;AAE1D,OAAK,gBAAgB,OAAO;EAE5B,MAAM,UAAU;GACd,OAAO,WAAW,SAAS;GAC3B,YAAY;GACb;A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;AAIX,MAAI,EAAE,OAAO,SAAS,OAAO,CAC3B,QAAO,EAAE;AAIX,MAAI,EAAE,OAAO,SAAS,OAAO,CAAE,QAAO;AACtC,MAAI,EAAE,OAAO,SAAS,OAAO,CAAE,QAAO;AACtC,MAAI,EAAE,OAAO,UAAU,OAAO,CAAE,QAAO;AACvC,MAAI,EAAE,OAAO,SAAS,OAAO,CAAE,QAAO,OAAO,EAAE;AAC/C,MAAI,EAAE,OAAO,UAAU,OAAO,CAAE,QAAO;AAGvC,MAAI,EAAE,OAAO,SAAS,OAAO,CAC3B,QAAO,EAAE;;;;;;CAUb,AAAU,cAAc,QAAsC;AAG5D,MAAI,EAAE,OAAO,SAAS,OAAO,CAC3B,QAAO,EAAE,QAAQ;;;;;;AC5IvB,MAAa,iBAAiB,QAAQ;CACpC,MAAM;CACN,UAAU,CAAC,kBAAkB,oBAAoB;CACjD,WAAW,WAAW;AACpB,SAAO,KAAK,iBAAiB;AAC7B,SAAO,MAAM,SAAS,YAAY,OAAO,OAAO,oBAAoB,CAAC;;CAExE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alepha/protobuf",
3
- "version": "0.10.7",
3
+ "version": "0.11.1",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=22.0.0"
@@ -13,20 +13,21 @@
13
13
  "src"
14
14
  ],
15
15
  "dependencies": {
16
- "@alepha/core": "0.10.7",
16
+ "@alepha/core": "0.11.1",
17
+ "@alepha/datetime": "0.11.1",
17
18
  "protobufjs": "^7.5.4"
18
19
  },
19
20
  "devDependencies": {
20
- "@biomejs/biome": "^2.2.6",
21
- "tsdown": "^0.15.9",
21
+ "@biomejs/biome": "^2.3.2",
22
+ "tsdown": "^0.15.11",
22
23
  "typescript": "^5.9.3",
23
24
  "vitest": "^3.2.4"
24
25
  },
25
26
  "scripts": {
26
27
  "test": "vitest run",
27
- "lint": "biome check --write --unsafe",
28
- "build": "tsdown -c ../../tsdown.config.ts",
29
- "check": "tsc"
28
+ "lint": "biome check --fix",
29
+ "build": "tsdown -c=../../tsdown.config.ts",
30
+ "typecheck": "tsc --noEmit"
30
31
  },
31
32
  "homepage": "https://github.com/feunard/alepha",
32
33
  "repository": {
package/src/index.ts CHANGED
@@ -1 +1,15 @@
1
+ import { $module } from "@alepha/core";
2
+ import { ProtobufProvider } from "./providers/ProtobufProvider.ts";
3
+ import { ProtobufSchemaCodec } from "./providers/ProtobufSchemaCodec.ts";
4
+
1
5
  export * from "./providers/ProtobufProvider.ts";
6
+ export * from "./providers/ProtobufSchemaCodec.ts";
7
+
8
+ export const AlephaProtobuf = $module({
9
+ name: "alepha.protobuf",
10
+ services: [ProtobufProvider, ProtobufSchemaCodec],
11
+ register: (alepha) => {
12
+ alepha.with(ProtobufProvider);
13
+ alepha.codec.register("protobuf", alepha.inject(ProtobufSchemaCodec));
14
+ },
15
+ });
@@ -1,11 +1,4 @@
1
- import {
2
- $inject,
3
- Alepha,
4
- type Static,
5
- type TObject,
6
- type TSchema,
7
- t,
8
- } from "@alepha/core";
1
+ import { $inject, Alepha, type TObject, type TSchema, t } from "@alepha/core";
9
2
  import type { Type } from "protobufjs";
10
3
  import protobufjs from "protobufjs";
11
4
 
@@ -18,30 +11,27 @@ export class ProtobufProvider {
18
11
  /**
19
12
  * Encode an object to a Uint8Array.
20
13
  */
21
- public encode(schema: TObject, data: any): Uint8Array {
22
- return this.parse(schema).encode(this.alepha.parse(schema, data)).finish();
14
+ public encode(schema: ProtobufSchema, message: any): Uint8Array {
15
+ return this.parse(schema).encode(message).finish();
23
16
  }
24
17
 
25
18
  /**
26
19
  * Decode a Uint8Array to an object.
27
20
  */
28
- public decode<T extends TObject>(schema: T, data: Uint8Array): Static<T> {
29
- return this.alepha.parse(schema, this.parse(schema).decode(data));
21
+ public decode<T = any>(schema: ProtobufSchema, data: Uint8Array): T {
22
+ return this.parse(schema).decode(data) as T;
30
23
  }
31
24
 
32
25
  /**
33
26
  * Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.
34
27
  */
35
- public parse(
36
- schema: ProtobufSchema | TObject,
37
- typeName = "root.Target",
38
- ): Type {
28
+ public parse(schema: ProtobufSchema, typeName = "root.Target"): Type {
39
29
  const exists = this.schemas.get(schema);
40
- if (exists) return exists;
30
+ if (exists) {
31
+ return exists;
32
+ }
41
33
 
42
- const pbSchema =
43
- typeof schema === "string" ? schema : this.createProtobufSchema(schema);
44
- const result = this.protobuf.parse(pbSchema);
34
+ const result = this.protobuf.parse(schema);
45
35
  const type = result.root.lookupType(typeName);
46
36
  this.schemas.set(schema, type);
47
37
  return type;
@@ -0,0 +1,154 @@
1
+ import {
2
+ $inject,
3
+ SchemaCodec,
4
+ type StaticDecode,
5
+ type TSchema,
6
+ t,
7
+ } from "@alepha/core";
8
+ import "@alepha/datetime";
9
+ import { ProtobufProvider } from "./ProtobufProvider.ts";
10
+
11
+ /**
12
+ * ProtobufSchemaCodec handles encoding/decoding for Protobuf format.
13
+ *
14
+ * Key differences from JSON codec:
15
+ * - BigInt values are kept as BigInt (not converted to string)
16
+ * - Date values are converted to ISO strings for protobuf compatibility
17
+ * - Binary data (Uint8Array) is kept as-is
18
+ * - Proto3 default values are applied when decoding (to handle omitted fields)
19
+ */
20
+ export class ProtobufSchemaCodec extends SchemaCodec {
21
+ protected protobufProvider = $inject(ProtobufProvider);
22
+ protected decoder = new TextDecoder();
23
+
24
+ public encodeToString(schema: TSchema, value: any): string {
25
+ const binary = this.encodeToBinary(schema, value);
26
+ return this.decoder.decode(binary);
27
+ }
28
+
29
+ public encodeToBinary(schema: TSchema, value: any): Uint8Array {
30
+ const proto = this.protobufProvider.createProtobufSchema(schema);
31
+ return this.protobufProvider.encode(proto, this.encode(schema, value));
32
+ }
33
+
34
+ public decode<T extends TSchema>(schema: T, value: any): StaticDecode<T> {
35
+ // First decode from protobuf binary to object
36
+ const proto = this.protobufProvider.createProtobufSchema(schema);
37
+ const decoded = this.protobufProvider.decode(proto, value);
38
+
39
+ // Apply proto3 default values for missing fields
40
+ const withDefaults = this.applyProto3Defaults(schema, decoded);
41
+
42
+ // Then use the parent decode to validate and transform
43
+ return super.decode(schema, withDefaults);
44
+ }
45
+
46
+ /**
47
+ * Apply proto3 default values for fields that were omitted during encoding.
48
+ * Proto3 omits fields with default values, so we need to restore them.
49
+ * Also converts enum integers back to their string values.
50
+ */
51
+ protected applyProto3Defaults(schema: TSchema, value: any): any {
52
+ if (!value || typeof value !== "object") {
53
+ return value;
54
+ }
55
+
56
+ if (t.schema.isObject(schema)) {
57
+ const result: any = { ...value };
58
+
59
+ for (const [key, propSchema] of Object.entries(schema.properties)) {
60
+ if (!(key in result) || result[key] === undefined) {
61
+ // Apply proto3 default values based on type
62
+ result[key] = this.getProto3Default(propSchema);
63
+ } else {
64
+ // Convert enum integers to strings
65
+ if (this.isEnum(propSchema)) {
66
+ result[key] = this.convertEnumValue(propSchema, result[key]);
67
+ } else if (typeof result[key] === "object" && result[key] !== null) {
68
+ // Recursively apply defaults to nested objects
69
+ result[key] = this.applyProto3Defaults(propSchema, result[key]);
70
+ }
71
+ }
72
+ }
73
+
74
+ return result;
75
+ }
76
+
77
+ if (t.schema.isArray(schema) && Array.isArray(value)) {
78
+ return value.map((item) => this.applyProto3Defaults(schema.items, item));
79
+ }
80
+
81
+ return value;
82
+ }
83
+
84
+ /**
85
+ * Check if a schema is an enum type.
86
+ */
87
+ protected isEnum(schema: TSchema): boolean {
88
+ return "enum" in schema && Array.isArray(schema.enum);
89
+ }
90
+
91
+ /**
92
+ * Convert an enum value from protobuf integer to TypeBox string.
93
+ */
94
+ protected convertEnumValue(schema: TSchema, value: any): any {
95
+ if (
96
+ typeof value === "number" &&
97
+ "enum" in schema &&
98
+ Array.isArray(schema.enum)
99
+ ) {
100
+ // Protobuf encodes enums as integers, convert back to string
101
+ return schema.enum[value];
102
+ }
103
+ return value;
104
+ }
105
+
106
+ /**
107
+ * Get the proto3 default value for a schema type.
108
+ */
109
+ protected getProto3Default(schema: TSchema): any {
110
+ // Handle nullable/optional types - they can be undefined
111
+ if (t.schema.isOptional(schema) || t.schema.isUnion(schema)) {
112
+ return undefined;
113
+ }
114
+
115
+ // Handle arrays - default is empty array
116
+ if (t.schema.isArray(schema)) {
117
+ return [];
118
+ }
119
+
120
+ // Handle records (maps) - default is empty object
121
+ if (t.schema.isRecord(schema)) {
122
+ return {};
123
+ }
124
+
125
+ // Handle primitive types
126
+ if (t.schema.isString(schema)) return "";
127
+ if (t.schema.isNumber(schema)) return 0;
128
+ if (t.schema.isInteger(schema)) return 0;
129
+ if (t.schema.isBigInt(schema)) return BigInt(0);
130
+ if (t.schema.isBoolean(schema)) return false;
131
+
132
+ // For objects, return empty object (will be filled in recursively)
133
+ if (t.schema.isObject(schema)) {
134
+ return {};
135
+ }
136
+
137
+ return undefined;
138
+ }
139
+
140
+ /**
141
+ * Transform types for Protobuf compatibility.
142
+ * This method is called for each type in the schema tree.
143
+ */
144
+ protected transformType(schema: TSchema): TSchema | undefined {
145
+ // For bigint: keep as-is, don't convert to string
146
+ // The schema is still a string type with format "int64", but we override encode/decode
147
+ if (t.schema.isBigInt(schema)) {
148
+ return t.bigint();
149
+ }
150
+
151
+ // For other types, use default behavior
152
+ return undefined;
153
+ }
154
+ }