@alepha/protobuf 0.11.8 → 0.11.10
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.cjs +305 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +103 -0
- package/dist/index.d.cts.map +1 -0
- package/package.json +6 -5
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
let __alepha_core = require("@alepha/core");
|
|
25
|
+
let protobufjs = require("protobufjs");
|
|
26
|
+
protobufjs = __toESM(protobufjs);
|
|
27
|
+
require("@alepha/datetime");
|
|
28
|
+
|
|
29
|
+
//#region src/providers/ProtobufProvider.ts
|
|
30
|
+
var ProtobufProvider = class {
|
|
31
|
+
alepha = (0, __alepha_core.$inject)(__alepha_core.Alepha);
|
|
32
|
+
schemas = /* @__PURE__ */ new Map();
|
|
33
|
+
protobuf = protobufjs.default;
|
|
34
|
+
enumDefinitions = /* @__PURE__ */ new Map();
|
|
35
|
+
/**
|
|
36
|
+
* Encode an object to a Uint8Array.
|
|
37
|
+
*/
|
|
38
|
+
encode(schema, message) {
|
|
39
|
+
return this.parse(schema).encode(message).finish();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Decode a Uint8Array to an object.
|
|
43
|
+
*/
|
|
44
|
+
decode(schema, data) {
|
|
45
|
+
return this.parse(schema).decode(data);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.
|
|
49
|
+
*/
|
|
50
|
+
parse(schema, typeName = "root.Target") {
|
|
51
|
+
const exists = this.schemas.get(schema);
|
|
52
|
+
if (exists) return exists;
|
|
53
|
+
const type = this.protobuf.parse(schema).root.lookupType(typeName);
|
|
54
|
+
this.schemas.set(schema, type);
|
|
55
|
+
return type;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Convert a TypeBox schema to a Protobuf schema as a string.
|
|
59
|
+
*/
|
|
60
|
+
createProtobufSchema(schema, options = {}) {
|
|
61
|
+
const { rootName = "root", mainMessageName = "Target" } = options;
|
|
62
|
+
this.enumDefinitions.clear();
|
|
63
|
+
const context = {
|
|
64
|
+
proto: `package ${rootName};\nsyntax = "proto3";\n\n`,
|
|
65
|
+
fieldIndex: 1
|
|
66
|
+
};
|
|
67
|
+
if (__alepha_core.t.schema.isObject(schema)) {
|
|
68
|
+
const { message, subMessages } = this.parseObjectWithDependencies(schema, mainMessageName);
|
|
69
|
+
for (const [enumName, values] of this.enumDefinitions) context.proto += this.generateEnumDefinition(enumName, values);
|
|
70
|
+
context.proto += subMessages.join("");
|
|
71
|
+
context.proto += message;
|
|
72
|
+
}
|
|
73
|
+
return context.proto;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Parse an object schema with dependencies (sub-messages).
|
|
77
|
+
*/
|
|
78
|
+
parseObjectWithDependencies(obj, parentName) {
|
|
79
|
+
if (!__alepha_core.t.schema.isObject(obj)) return {
|
|
80
|
+
message: "",
|
|
81
|
+
subMessages: []
|
|
82
|
+
};
|
|
83
|
+
const fields = [];
|
|
84
|
+
const subMessages = [];
|
|
85
|
+
let fieldIndex = 1;
|
|
86
|
+
for (const [key, value] of Object.entries(obj.properties)) {
|
|
87
|
+
if (__alepha_core.t.schema.isArray(value)) {
|
|
88
|
+
if (this.isEnum(value.items)) {
|
|
89
|
+
const enumValues = this.getEnumValues(value.items);
|
|
90
|
+
const enumName = this.registerEnum(key, enumValues);
|
|
91
|
+
fields.push(` repeated ${enumName} ${key} = ${fieldIndex++};`);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (__alepha_core.t.schema.isObject(value.items)) {
|
|
95
|
+
const subMessageName = "title" in value.items && typeof value.items.title === "string" ? value.items.title : `${parentName}_${key}`;
|
|
96
|
+
const { message: subMessage, subMessages: nestedSubMessages } = this.parseObjectWithDependencies(value.items, subMessageName);
|
|
97
|
+
subMessages.push(...nestedSubMessages);
|
|
98
|
+
subMessages.push(subMessage);
|
|
99
|
+
fields.push(` repeated ${subMessageName} ${key} = ${fieldIndex++};`);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const itemType = this.convertType(value.items);
|
|
103
|
+
fields.push(` repeated ${itemType} ${key} = ${fieldIndex++};`);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (__alepha_core.t.schema.isObject(value)) {
|
|
107
|
+
const subMessageName = "title" in value && typeof value.title === "string" ? value.title : `${parentName}_${key}`;
|
|
108
|
+
const { message: subMessage, subMessages: nestedSubMessages } = this.parseObjectWithDependencies(value, subMessageName);
|
|
109
|
+
subMessages.push(...nestedSubMessages);
|
|
110
|
+
subMessages.push(subMessage);
|
|
111
|
+
fields.push(` ${subMessageName} ${key} = ${fieldIndex++};`);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (__alepha_core.t.schema.isUnion(value)) {
|
|
115
|
+
const nonNullType = value.anyOf.find((type) => !__alepha_core.t.schema.isNull(type));
|
|
116
|
+
if (nonNullType) {
|
|
117
|
+
if (this.isEnum(nonNullType)) {
|
|
118
|
+
const enumValues = this.getEnumValues(nonNullType);
|
|
119
|
+
const enumName = this.registerEnum(key, enumValues);
|
|
120
|
+
fields.push(` ${enumName} ${key} = ${fieldIndex++};`);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (__alepha_core.t.schema.isObject(nonNullType)) {
|
|
124
|
+
const subMessageName = "title" in nonNullType && typeof nonNullType.title === "string" ? nonNullType.title : `${parentName}_${key}`;
|
|
125
|
+
const { message: subMessage, subMessages: nestedSubMessages } = this.parseObjectWithDependencies(nonNullType, subMessageName);
|
|
126
|
+
subMessages.push(...nestedSubMessages);
|
|
127
|
+
subMessages.push(subMessage);
|
|
128
|
+
fields.push(` ${subMessageName} ${key} = ${fieldIndex++};`);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const fieldType$1 = this.convertType(nonNullType);
|
|
132
|
+
fields.push(` ${fieldType$1} ${key} = ${fieldIndex++};`);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (__alepha_core.t.schema.isRecord(value)) {
|
|
137
|
+
let valueSchema;
|
|
138
|
+
if ("additionalProperties" in value && value.additionalProperties && typeof value.additionalProperties === "object") valueSchema = value.additionalProperties;
|
|
139
|
+
else if (value.patternProperties && typeof value.patternProperties === "object") {
|
|
140
|
+
const patterns = Object.values(value.patternProperties);
|
|
141
|
+
if (patterns.length > 0 && typeof patterns[0] === "object") valueSchema = patterns[0];
|
|
142
|
+
}
|
|
143
|
+
if (valueSchema) {
|
|
144
|
+
const valueType = this.convertType(valueSchema);
|
|
145
|
+
fields.push(` map<string, ${valueType}> ${key} = ${fieldIndex++};`);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (this.isEnum(value)) {
|
|
150
|
+
const enumValues = this.getEnumValues(value);
|
|
151
|
+
const enumName = this.registerEnum(key, enumValues);
|
|
152
|
+
fields.push(` ${enumName} ${key} = ${fieldIndex++};`);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const fieldType = this.convertType(value);
|
|
156
|
+
fields.push(` ${fieldType} ${key} = ${fieldIndex++};`);
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
message: `message ${parentName} {\n${fields.join("\n")}\n}\n`,
|
|
160
|
+
subMessages
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Convert a primitive TypeBox schema type to a Protobuf spec type.
|
|
165
|
+
*/
|
|
166
|
+
convertType(schema) {
|
|
167
|
+
if (__alepha_core.t.schema.isBoolean(schema)) return "bool";
|
|
168
|
+
if (__alepha_core.t.schema.isNumber(schema) && schema.format === "int64") return "int64";
|
|
169
|
+
if (__alepha_core.t.schema.isNumber(schema)) return "double";
|
|
170
|
+
if (__alepha_core.t.schema.isInteger(schema)) return "int32";
|
|
171
|
+
if (__alepha_core.t.schema.isBigInt(schema)) return "int64";
|
|
172
|
+
if (__alepha_core.t.schema.isString(schema)) return "string";
|
|
173
|
+
if (__alepha_core.t.schema.isUnion(schema)) {
|
|
174
|
+
const nonNullType = schema.anyOf.find((type) => !__alepha_core.t.schema.isNull(type));
|
|
175
|
+
if (nonNullType) return this.convertType(nonNullType);
|
|
176
|
+
}
|
|
177
|
+
if (__alepha_core.t.schema.isOptional(schema)) return this.convertType(schema);
|
|
178
|
+
if (__alepha_core.t.schema.isUnsafe(schema)) return "string";
|
|
179
|
+
throw new Error(`Unsupported type: ${JSON.stringify(schema)}`);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Check if a schema is an enum type.
|
|
183
|
+
* TypeBox enums have an "enum" property with an array of values.
|
|
184
|
+
*/
|
|
185
|
+
isEnum(schema) {
|
|
186
|
+
return "enum" in schema && Array.isArray(schema.enum);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Extract enum values from a TypeBox enum schema.
|
|
190
|
+
*/
|
|
191
|
+
getEnumValues(schema) {
|
|
192
|
+
if ("enum" in schema && Array.isArray(schema.enum)) return schema.enum.map(String);
|
|
193
|
+
return [];
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Register an enum and return its type name.
|
|
197
|
+
* Generates a PascalCase name from the field name.
|
|
198
|
+
*/
|
|
199
|
+
registerEnum(fieldName, values) {
|
|
200
|
+
const enumName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1);
|
|
201
|
+
const valueKey = values.join(",");
|
|
202
|
+
const existingEnum = Array.from(this.enumDefinitions.entries()).find(([_, enumValues]) => enumValues.join(",") === valueKey);
|
|
203
|
+
if (existingEnum) return existingEnum[0];
|
|
204
|
+
this.enumDefinitions.set(enumName, values);
|
|
205
|
+
return enumName;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Generate a protobuf enum definition.
|
|
209
|
+
*/
|
|
210
|
+
generateEnumDefinition(enumName, values) {
|
|
211
|
+
return `enum ${enumName} {\n${values.map((value, index) => ` ${value} = ${index};`).join("\n")}\n}\n`;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
//#endregion
|
|
216
|
+
//#region src/providers/ProtobufSchemaCodec.ts
|
|
217
|
+
/**
|
|
218
|
+
* ProtobufSchemaCodec handles encoding/decoding for Protobuf format.
|
|
219
|
+
*
|
|
220
|
+
* Key differences from JSON codec:
|
|
221
|
+
* - BigInt values are kept as BigInt (not converted to string)
|
|
222
|
+
* - Date values are converted to ISO strings for protobuf compatibility
|
|
223
|
+
* - Binary data (Uint8Array) is kept as-is
|
|
224
|
+
* - Proto3 default values are applied when decoding (to handle omitted fields)
|
|
225
|
+
*/
|
|
226
|
+
var ProtobufSchemaCodec = class extends __alepha_core.SchemaCodec {
|
|
227
|
+
protobufProvider = (0, __alepha_core.$inject)(ProtobufProvider);
|
|
228
|
+
decoder = new TextDecoder();
|
|
229
|
+
encodeToString(schema, value) {
|
|
230
|
+
const binary = this.encodeToBinary(schema, value);
|
|
231
|
+
if (typeof Buffer !== "undefined") return Buffer.from(binary).toString("base64");
|
|
232
|
+
else return btoa(String.fromCharCode(...binary));
|
|
233
|
+
}
|
|
234
|
+
encodeToBinary(schema, value) {
|
|
235
|
+
const proto = this.protobufProvider.createProtobufSchema(schema);
|
|
236
|
+
return this.protobufProvider.encode(proto, value);
|
|
237
|
+
}
|
|
238
|
+
decode(schema, value) {
|
|
239
|
+
const proto = this.protobufProvider.createProtobufSchema(schema);
|
|
240
|
+
if (value instanceof Uint8Array) return this.applyProto3Defaults(schema, this.protobufProvider.decode(proto, value));
|
|
241
|
+
if (typeof value === "string") return this.applyProto3Defaults(schema, this.protobufProvider.decode(proto, typeof Buffer !== "undefined" ? Uint8Array.from(Buffer.from(value, "base64")) : Uint8Array.from(atob(value).split("").map((c) => c.charCodeAt(0)))));
|
|
242
|
+
throw new __alepha_core.AlephaError(`Unsupported value type for Protobuf decoding: ${typeof value}`);
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Apply proto3 default values for fields that were omitted during encoding.
|
|
246
|
+
* Proto3 omits fields with default values, so we need to restore them.
|
|
247
|
+
* Also converts enum integers back to their string values.
|
|
248
|
+
*/
|
|
249
|
+
applyProto3Defaults(schema, value) {
|
|
250
|
+
if (!value || typeof value !== "object") return value;
|
|
251
|
+
if (__alepha_core.t.schema.isObject(schema)) {
|
|
252
|
+
const result = { ...value };
|
|
253
|
+
for (const [key, propSchema] of Object.entries(schema.properties)) if (!(key in result) || result[key] === void 0) result[key] = this.getProto3Default(propSchema);
|
|
254
|
+
else if (this.isEnum(propSchema)) result[key] = this.convertEnumValue(propSchema, result[key]);
|
|
255
|
+
else if (typeof result[key] === "object" && result[key] !== null) result[key] = this.applyProto3Defaults(propSchema, result[key]);
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
if (__alepha_core.t.schema.isArray(schema) && Array.isArray(value)) return value.map((item) => this.applyProto3Defaults(schema.items, item));
|
|
259
|
+
return value;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Check if a schema is an enum type.
|
|
263
|
+
*/
|
|
264
|
+
isEnum(schema) {
|
|
265
|
+
return "enum" in schema && Array.isArray(schema.enum);
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Convert an enum value from protobuf integer to TypeBox string.
|
|
269
|
+
*/
|
|
270
|
+
convertEnumValue(schema, value) {
|
|
271
|
+
if (typeof value === "number" && "enum" in schema && Array.isArray(schema.enum)) return schema.enum[value];
|
|
272
|
+
return value;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Get the proto3 default value for a schema type.
|
|
276
|
+
*/
|
|
277
|
+
getProto3Default(schema) {
|
|
278
|
+
if (__alepha_core.t.schema.isOptional(schema) || __alepha_core.t.schema.isUnion(schema)) return;
|
|
279
|
+
if (__alepha_core.t.schema.isArray(schema)) return [];
|
|
280
|
+
if (__alepha_core.t.schema.isRecord(schema)) return {};
|
|
281
|
+
if (__alepha_core.t.schema.isString(schema)) return "";
|
|
282
|
+
if (__alepha_core.t.schema.isNumber(schema)) return 0;
|
|
283
|
+
if (__alepha_core.t.schema.isInteger(schema)) return 0;
|
|
284
|
+
if (__alepha_core.t.schema.isBigInt(schema)) return BigInt(0);
|
|
285
|
+
if (__alepha_core.t.schema.isBoolean(schema)) return false;
|
|
286
|
+
if (__alepha_core.t.schema.isObject(schema)) return {};
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
//#endregion
|
|
291
|
+
//#region src/index.ts
|
|
292
|
+
const AlephaProtobuf = (0, __alepha_core.$module)({
|
|
293
|
+
name: "alepha.protobuf",
|
|
294
|
+
services: [ProtobufProvider, ProtobufSchemaCodec],
|
|
295
|
+
register: (alepha) => {
|
|
296
|
+
alepha.with(ProtobufProvider);
|
|
297
|
+
alepha.codec.register("protobuf", alepha.inject(ProtobufSchemaCodec));
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
//#endregion
|
|
302
|
+
exports.AlephaProtobuf = AlephaProtobuf;
|
|
303
|
+
exports.ProtobufProvider = ProtobufProvider;
|
|
304
|
+
exports.ProtobufSchemaCodec = ProtobufSchemaCodec;
|
|
305
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["Alepha","t","fields: string[]","subMessages: string[]","fieldType","valueSchema: TSchema | undefined","SchemaCodec","AlephaError","t","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 AlephaError,\n SchemaCodec,\n type Static,\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<T extends TSchema>(\n schema: T,\n value: Static<T>,\n ): string {\n const binary = this.encodeToBinary(schema, value);\n // convert binary to base64 string for text representation\n if (typeof Buffer !== \"undefined\") {\n return Buffer.from(binary).toString(\"base64\");\n } else {\n return btoa(String.fromCharCode(...binary));\n }\n }\n\n public encodeToBinary<T extends TSchema>(\n schema: T,\n value: Static<T>,\n ): Uint8Array {\n const proto = this.protobufProvider.createProtobufSchema(schema);\n return this.protobufProvider.encode(proto, value);\n }\n\n public decode<T>(schema: TSchema, value: unknown): T {\n // First decode from protobuf binary to object\n const proto = this.protobufProvider.createProtobufSchema(schema);\n\n if (value instanceof Uint8Array) {\n return this.applyProto3Defaults(\n schema,\n this.protobufProvider.decode(proto, value),\n );\n }\n\n if (typeof value === \"string\") {\n return this.applyProto3Defaults(\n schema,\n this.protobufProvider.decode(\n proto,\n typeof Buffer !== \"undefined\"\n ? Uint8Array.from(Buffer.from(value, \"base64\"))\n : Uint8Array.from(\n atob(value)\n .split(\"\")\n .map((c) => c.charCodeAt(0)),\n ),\n ),\n );\n }\n\n throw new AlephaError(\n `Unsupported value type for Protobuf decoding: ${typeof value}`,\n );\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","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,oCAAiBA,qBAAO;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,AA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AC7B,SAAO,MAAM,SAAS,YAAY,OAAO,OAAO,oBAAoB,CAAC;;CAExE,CAAC"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import * as _alepha_core0 from "@alepha/core";
|
|
2
|
+
import { Alepha, SchemaCodec, Static, TObject, TSchema } from "@alepha/core";
|
|
3
|
+
import protobufjs, { Type } from "protobufjs";
|
|
4
|
+
|
|
5
|
+
//#region src/providers/ProtobufProvider.d.ts
|
|
6
|
+
declare class ProtobufProvider {
|
|
7
|
+
protected readonly alepha: Alepha;
|
|
8
|
+
protected readonly schemas: Map<string | TObject, Type>;
|
|
9
|
+
protected readonly protobuf: typeof protobufjs;
|
|
10
|
+
protected readonly enumDefinitions: Map<string, string[]>;
|
|
11
|
+
/**
|
|
12
|
+
* Encode an object to a Uint8Array.
|
|
13
|
+
*/
|
|
14
|
+
encode(schema: ProtobufSchema, message: any): Uint8Array;
|
|
15
|
+
/**
|
|
16
|
+
* Decode a Uint8Array to an object.
|
|
17
|
+
*/
|
|
18
|
+
decode<T = any>(schema: ProtobufSchema, data: Uint8Array): T;
|
|
19
|
+
/**
|
|
20
|
+
* Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.
|
|
21
|
+
*/
|
|
22
|
+
parse(schema: ProtobufSchema, typeName?: string): Type;
|
|
23
|
+
/**
|
|
24
|
+
* Convert a TypeBox schema to a Protobuf schema as a string.
|
|
25
|
+
*/
|
|
26
|
+
createProtobufSchema(schema: TSchema, options?: CreateProtobufSchemaOptions): string;
|
|
27
|
+
/**
|
|
28
|
+
* Parse an object schema with dependencies (sub-messages).
|
|
29
|
+
*/
|
|
30
|
+
protected parseObjectWithDependencies(obj: TSchema, parentName: string): {
|
|
31
|
+
message: string;
|
|
32
|
+
subMessages: string[];
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Convert a primitive TypeBox schema type to a Protobuf spec type.
|
|
36
|
+
*/
|
|
37
|
+
protected convertType(schema: TSchema): string;
|
|
38
|
+
/**
|
|
39
|
+
* Check if a schema is an enum type.
|
|
40
|
+
* TypeBox enums have an "enum" property with an array of values.
|
|
41
|
+
*/
|
|
42
|
+
protected isEnum(schema: TSchema): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Extract enum values from a TypeBox enum schema.
|
|
45
|
+
*/
|
|
46
|
+
protected getEnumValues(schema: TSchema): string[];
|
|
47
|
+
/**
|
|
48
|
+
* Register an enum and return its type name.
|
|
49
|
+
* Generates a PascalCase name from the field name.
|
|
50
|
+
*/
|
|
51
|
+
protected registerEnum(fieldName: string, values: string[]): string;
|
|
52
|
+
/**
|
|
53
|
+
* Generate a protobuf enum definition.
|
|
54
|
+
*/
|
|
55
|
+
protected generateEnumDefinition(enumName: string, values: string[]): string;
|
|
56
|
+
}
|
|
57
|
+
type ProtobufSchema = string;
|
|
58
|
+
interface CreateProtobufSchemaOptions {
|
|
59
|
+
rootName?: string;
|
|
60
|
+
mainMessageName?: string;
|
|
61
|
+
}
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/providers/ProtobufSchemaCodec.d.ts
|
|
64
|
+
/**
|
|
65
|
+
* ProtobufSchemaCodec handles encoding/decoding for Protobuf format.
|
|
66
|
+
*
|
|
67
|
+
* Key differences from JSON codec:
|
|
68
|
+
* - BigInt values are kept as BigInt (not converted to string)
|
|
69
|
+
* - Date values are converted to ISO strings for protobuf compatibility
|
|
70
|
+
* - Binary data (Uint8Array) is kept as-is
|
|
71
|
+
* - Proto3 default values are applied when decoding (to handle omitted fields)
|
|
72
|
+
*/
|
|
73
|
+
declare class ProtobufSchemaCodec extends SchemaCodec {
|
|
74
|
+
protected protobufProvider: ProtobufProvider;
|
|
75
|
+
protected decoder: TextDecoder;
|
|
76
|
+
encodeToString<T extends TSchema>(schema: T, value: Static<T>): string;
|
|
77
|
+
encodeToBinary<T extends TSchema>(schema: T, value: Static<T>): Uint8Array;
|
|
78
|
+
decode<T>(schema: TSchema, value: unknown): T;
|
|
79
|
+
/**
|
|
80
|
+
* Apply proto3 default values for fields that were omitted during encoding.
|
|
81
|
+
* Proto3 omits fields with default values, so we need to restore them.
|
|
82
|
+
* Also converts enum integers back to their string values.
|
|
83
|
+
*/
|
|
84
|
+
protected applyProto3Defaults(schema: TSchema, value: any): any;
|
|
85
|
+
/**
|
|
86
|
+
* Check if a schema is an enum type.
|
|
87
|
+
*/
|
|
88
|
+
protected isEnum(schema: TSchema): boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Convert an enum value from protobuf integer to TypeBox string.
|
|
91
|
+
*/
|
|
92
|
+
protected convertEnumValue(schema: TSchema, value: any): any;
|
|
93
|
+
/**
|
|
94
|
+
* Get the proto3 default value for a schema type.
|
|
95
|
+
*/
|
|
96
|
+
protected getProto3Default(schema: TSchema): any;
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/index.d.ts
|
|
100
|
+
declare const AlephaProtobuf: _alepha_core0.Service<_alepha_core0.Module>;
|
|
101
|
+
//#endregion
|
|
102
|
+
export { AlephaProtobuf, CreateProtobufSchemaOptions, ProtobufProvider, ProtobufSchema, ProtobufSchemaCodec };
|
|
103
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/providers/ProtobufProvider.ts","../src/providers/ProtobufSchemaCodec.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;;;cAIa,gBAAA;6BACc;EADd,mBAAgB,OAAA,EAEC,GAFD,CAAA,MAAA,GAEc,OAFd,EAEuB,IAFvB,CAAA;EACF,mBAAA,QAAA,EAAA,OAEW,UAFX;EACgB,mBAAA,eAAA,EAEL,GAFK,CAAA,MAAA,EAAA,MAAA,EAAA,CAAA;EAAS;;;EAEd,MAAA,CAAA,MAAA,EAKd,cALc,EAAA,OAAA,EAAA,GAAA,CAAA,EAKiB,UALjB;EAKd;;;EAO+B,MAAA,CAAA,IAAA,GAAA,CAAA,CAAA,MA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AA,EAAA,OAAA,CAAA,ECkB8B,CDlB9B;EAA2C;;;;;EA+NvC,UAAA,mBAAA,CAAA,MAAA,ECxKa,ODwKb,EAAA,KAAA,EAAA,GAAA,CAAA,EAAA,GAAA;EAOO;;AA0ClC;EAEiB,UAAA,MAAA,CAAA,MAAA,ECvLU,ODuLiB,CAAA,EAAA,OAAA;;;;ECzR/B,UAAA,gBAAoB,CAAA,MAAA,EAyGI,OAzGJ,EAAA,KAAA,EAAA,GAAA,CAAA,EAAA,GAAA;EACL;;;EAIhB,UAAA,gBAAA,CAAA,MAAA,EAmHyB,OAnHzB,CAAA,EAAA,GAAA;;;;cClBC,gBAAc,aAAA,CAAA,QAOzB,aAAA,CAPyB,MAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alepha/protobuf",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.0.0"
|
|
@@ -13,15 +13,15 @@
|
|
|
13
13
|
"src"
|
|
14
14
|
],
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@alepha/core": "0.11.
|
|
17
|
-
"@alepha/datetime": "0.11.
|
|
16
|
+
"@alepha/core": "0.11.10",
|
|
17
|
+
"@alepha/datetime": "0.11.10",
|
|
18
18
|
"protobufjs": "^7.5.4"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@biomejs/biome": "^2.3.5",
|
|
22
22
|
"tsdown": "^0.16.4",
|
|
23
23
|
"typescript": "^5.9.3",
|
|
24
|
-
"vitest": "^4.0.
|
|
24
|
+
"vitest": "^4.0.9"
|
|
25
25
|
},
|
|
26
26
|
"scripts": {
|
|
27
27
|
"test": "vitest run",
|
|
@@ -43,7 +43,8 @@
|
|
|
43
43
|
"exports": {
|
|
44
44
|
".": {
|
|
45
45
|
"types": "./dist/index.d.ts",
|
|
46
|
-
"import": "./dist/index.js"
|
|
46
|
+
"import": "./dist/index.js",
|
|
47
|
+
"require": "./dist/index.cjs"
|
|
47
48
|
}
|
|
48
49
|
}
|
|
49
50
|
}
|