@alepha/protobuf 0.7.6 → 0.8.0
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.map +1 -1
- package/dist/index.d.cts +34 -35
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +34 -35
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/providers/ProtobufProvider.ts +5 -4
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["Alepha","schema: TObject","data: any","schema: T","data: Uint8Array","schema: ProtobufSchema | TObject","schema: TSchema","options: CreateProtobufSchemaOptions","obj: TSchema","parentName: string","context: { proto: string; fieldIndex: number }","fields: string[]"],"sources":["../src/providers/ProtobufProvider.ts"],"sourcesContent":["import type { Static, TObject, TSchema } from \"@alepha/core\";\nimport { $inject, Alepha, TypeGuard } from \"@alepha/core\";\nimport type { Type } from \"protobufjs\";\nimport protobufjs from \"protobufjs\";\n\nexport class ProtobufProvider {\n\tprotected readonly alepha = $inject(Alepha);\n\tprotected readonly schemas
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["Alepha","schema: TObject","data: any","schema: T","data: Uint8Array","schema: ProtobufSchema | TObject","schema: TSchema","options: CreateProtobufSchemaOptions","obj: TSchema","parentName: string","context: { proto: string; fieldIndex: number }","fields: string[]"],"sources":["../src/providers/ProtobufProvider.ts"],"sourcesContent":["import type { Static, TObject, TProperties, TSchema } from \"@alepha/core\";\nimport { $inject, Alepha, TypeGuard } from \"@alepha/core\";\nimport type { Type } from \"protobufjs\";\nimport protobufjs from \"protobufjs\";\n\nexport class ProtobufProvider {\n\tprotected readonly alepha: Alepha = $inject(Alepha);\n\tprotected readonly schemas: Map<string | TObject<TProperties>, Type> =\n\t\tnew Map();\n\tprotected readonly protobuf: typeof protobufjs = protobufjs;\n\n\t/**\n\t * Encode an object to a Uint8Array.\n\t *\n\t * @param schema - TypeBox schema used to generate the Protobuf schema.\n\t * @param data - Object to encode. Can be any object or string.\n\t */\n\tpublic encode(schema: TObject, data: any): Uint8Array {\n\t\treturn this.parse(schema).encode(this.alepha.parse(schema, data)).finish();\n\t}\n\n\t/**\n\t * Decode a Uint8Array to an object.\n\t *\n\t * @param schema\n\t * @param data\n\t */\n\tpublic decode<T extends TObject>(schema: T, data: Uint8Array): Static<T> {\n\t\treturn this.alepha.parse(schema, this.parse(schema).decode(data));\n\t}\n\n\t/**\n\t * Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.\n\t *\n\t * @param schema\n\t * @param typeName\n\t */\n\tpublic parse(\n\t\tschema: ProtobufSchema | TObject,\n\t\ttypeName = \"root.Target\",\n\t): Type {\n\t\tconst exists = this.schemas.get(schema);\n\t\tif (exists) return exists;\n\n\t\tconst pbSchema =\n\t\t\ttypeof schema === \"string\" ? schema : this.createProtobufSchema(schema);\n\t\tconst result = this.protobuf.parse(pbSchema);\n\t\tconst type = result.root.lookupType(typeName);\n\t\tthis.schemas.set(schema, type);\n\t\treturn type;\n\t}\n\n\t/**\n\t * Convert a TypeBox schema to a Protobuf schema as a string.\n\t *\n\t * @param schema\n\t * @param options\n\t */\n\tpublic createProtobufSchema(\n\t\tschema: TSchema,\n\t\toptions: CreateProtobufSchemaOptions = {},\n\t): string {\n\t\tconst { rootName = \"root\", mainMessageName = \"Target\" } = options;\n\t\tconst context = {\n\t\t\tproto: `package ${rootName};\\nsyntax = \"proto3\";\\n\\n`,\n\t\t\tfieldIndex: 1,\n\t\t};\n\n\t\tif (TypeGuard.IsObject(schema)) {\n\t\t\tconst proto = this.parseObject(schema, mainMessageName, context);\n\t\t\tcontext.proto += proto;\n\t\t}\n\n\t\treturn context.proto;\n\t}\n\n\t/**\n\t * Parse an object schema to a Protobuf message.\n\t *\n\t * @param obj\n\t * @param parentName\n\t * @param context\n\t * @protected\n\t */\n\tprotected parseObject(\n\t\tobj: TSchema,\n\t\tparentName: string,\n\t\tcontext: { proto: string; fieldIndex: number },\n\t): string {\n\t\tif (!TypeGuard.IsObject(obj)) {\n\t\t\treturn \"\";\n\t\t}\n\n\t\tconst fields: string[] = [];\n\n\t\tfor (const [key, value] of Object.entries(obj.properties)) {\n\t\t\tif (TypeGuard.IsArray(value)) {\n\t\t\t\tif (TypeGuard.IsObject(value.items)) {\n\t\t\t\t\tconst subMessageName = value.items.title ?? `${parentName}_${key}`;\n\t\t\t\t\tcontext.proto += this.parseObject(value.items, subMessageName, {\n\t\t\t\t\t\t...context,\n\t\t\t\t\t\tfieldIndex: 1,\n\t\t\t\t\t});\n\t\t\t\t\tfields.push(\n\t\t\t\t\t\t` repeated ${subMessageName} ${key} = ${context.fieldIndex++};`,\n\t\t\t\t\t);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tconst itemType = this.convertType(value.items);\n\t\t\t\tfields.push(` repeated ${itemType} ${key} = ${context.fieldIndex++};`);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (TypeGuard.IsObject(value)) {\n\t\t\t\tconst subMessageName = `${parentName}_${key}`;\n\t\t\t\tcontext.proto += this.parseObject(value, subMessageName, context);\n\t\t\t\tfields.push(` ${subMessageName} ${key} = ${context.fieldIndex++};`);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tfields.push(\n\t\t\t\t` ${this.convertType(value)} ${key} = ${context.fieldIndex++};`,\n\t\t\t);\n\t\t}\n\n\t\treturn `message ${parentName} {\\n${fields.join(\"\\n\")}\\n}\\n`;\n\t}\n\n\t/**\n\t * Convert a primitive TypeBox schema type to a Protobuf spec type.\n\t *\n\t * @param schema\n\t * @protected\n\t */\n\tprotected convertType(schema: TSchema): string {\n\t\tif (TypeGuard.IsInteger(schema)) return \"int32\";\n\t\tif (TypeGuard.IsNumber(schema)) return \"double\";\n\t\tif (TypeGuard.IsString(schema)) return \"string\";\n\t\tif (TypeGuard.IsBoolean(schema)) return \"bool\";\n\n\t\tthrow new Error(`Unsupported type: ${JSON.stringify(schema)}`);\n\t}\n}\n\nexport type ProtobufSchema = string;\n\nexport interface CreateProtobufSchemaOptions {\n\trootName?: string;\n\tmainMessageName?: string;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,IAAa,mBAAb,MAA8B;CAC7B,AAAmB,SAAiB,2BAAQA,qBAAO;CACnD,AAAmB,UAClB,IAAI;CACL,AAAmB,WAA8B;;;;;;;CAQjD,AAAO,OAAOC,QAAiBC,MAAuB;AACrD,SAAO,KAAK,MAAM,OAAO,CAAC,OAAO,KAAK,OAAO,MAAM,QAAQ,KAAK,CAAC,CAAC,QAAQ;CAC1E;;;;;;;CAQD,AAAO,OAA0BC,QAAWC,MAA6B;AACxE,SAAO,KAAK,OAAO,MAAM,QAAQ,KAAK,MAAM,OAAO,CAAC,OAAO,KAAK,CAAC;CACjE;;;;;;;CAQD,AAAO,MACNC,QACA,WAAW,eACJ;EACP,MAAM,SAAS,KAAK,QAAQ,IAAI,OAAO;AACvC,MAAI,OAAQ,QAAO;EAEnB,MAAM,kBACE,WAAW,WAAW,SAAS,KAAK,qBAAqB,OAAO;EACxE,MAAM,SAAS,KAAK,SAAS,MAAM,SAAS;EAC5C,MAAM,OAAO,OAAO,KAAK,WAAW,SAAS;AAC7C,OAAK,QAAQ,IAAI,QAAQ,KAAK;AAC9B,SAAO;CACP;;;;;;;CAQD,AAAO,qBACNC,QACAC,UAAuC,CAAE,GAChC;EACT,MAAM,EAAE,WAAW,QAAQ,kBAAkB,UAAU,GAAG;EAC1D,MAAM,UAAU;GACf,QAAQ,UAAU,SAAS;GAC3B,YAAY;EACZ;AAED,MAAI,wBAAU,SAAS,OAAO,EAAE;GAC/B,MAAM,QAAQ,KAAK,YAAY,QAAQ,iBAAiB,QAAQ;AAChE,WAAQ,SAAS;EACjB;AAED,SAAO,QAAQ;CACf;;;;;;;;;CAUD,AAAU,YACTC,KACAC,YACAC,SACS;AACT,OAAK,wBAAU,SAAS,IAAI,C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|
|
|
1
|
-
import
|
|
2
|
-
import { Alepha, Static, TObject, TSchema } from "@alepha/core";
|
|
1
|
+
import { Alepha, Static, TObject, TProperties, TSchema } from "@alepha/core";
|
|
3
2
|
import protobufjs, { Type } from "protobufjs";
|
|
4
3
|
|
|
5
4
|
//#region src/providers/ProtobufProvider.d.ts
|
|
6
5
|
declare class ProtobufProvider {
|
|
7
6
|
protected readonly alepha: Alepha;
|
|
8
|
-
protected readonly schemas: Map<string | TObject<
|
|
7
|
+
protected readonly schemas: Map<string | TObject<TProperties>, Type>;
|
|
9
8
|
protected readonly protobuf: typeof protobufjs;
|
|
10
9
|
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
* Encode an object to a Uint8Array.
|
|
11
|
+
*
|
|
12
|
+
* @param schema - TypeBox schema used to generate the Protobuf schema.
|
|
13
|
+
* @param data - Object to encode. Can be any object or string.
|
|
14
|
+
*/
|
|
16
15
|
encode(schema: TObject, data: any): Uint8Array;
|
|
17
16
|
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
* Decode a Uint8Array to an object.
|
|
18
|
+
*
|
|
19
|
+
* @param schema
|
|
20
|
+
* @param data
|
|
21
|
+
*/
|
|
23
22
|
decode<T extends TObject>(schema: T, data: Uint8Array): Static<T>;
|
|
24
23
|
/**
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
* Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.
|
|
25
|
+
*
|
|
26
|
+
* @param schema
|
|
27
|
+
* @param typeName
|
|
28
|
+
*/
|
|
30
29
|
parse(schema: ProtobufSchema | TObject, typeName?: string): Type;
|
|
31
30
|
/**
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
* Convert a TypeBox schema to a Protobuf schema as a string.
|
|
32
|
+
*
|
|
33
|
+
* @param schema
|
|
34
|
+
* @param options
|
|
35
|
+
*/
|
|
37
36
|
createProtobufSchema(schema: TSchema, options?: CreateProtobufSchemaOptions): string;
|
|
38
37
|
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
38
|
+
* Parse an object schema to a Protobuf message.
|
|
39
|
+
*
|
|
40
|
+
* @param obj
|
|
41
|
+
* @param parentName
|
|
42
|
+
* @param context
|
|
43
|
+
* @protected
|
|
44
|
+
*/
|
|
46
45
|
protected parseObject(obj: TSchema, parentName: string, context: {
|
|
47
46
|
proto: string;
|
|
48
47
|
fieldIndex: number;
|
|
49
48
|
}): string;
|
|
50
49
|
/**
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
* Convert a primitive TypeBox schema type to a Protobuf spec type.
|
|
51
|
+
*
|
|
52
|
+
* @param schema
|
|
53
|
+
* @protected
|
|
54
|
+
*/
|
|
56
55
|
protected convertType(schema: TSchema): string;
|
|
57
56
|
}
|
|
58
57
|
type ProtobufSchema = string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":["schema: TObject","data: any","schema: T","data: Uint8Array","schema: ProtobufSchema | TObject","schema: TSchema","options: CreateProtobufSchemaOptions","obj: TSchema","parentName: string","context: { proto: string; fieldIndex: number }"],"sources":["../src/providers/ProtobufProvider.ts"],"sourcesContent":[],"mappings":";;;;AAKa,cAAA,gBAAA,CAAA;EAAA,mBAAA,MAAA,EACe,MADf;EAAA,mBACe,OAAA,EACC,GADD,CAAA,MAAA,GACc,OADd,CACsB,WADtB,CAAA,EACoC,IADpC,CAAA;EAAA,mBACsB,QAAA,EAAA,OAEb,UAFa;EAAA;;;;;;EAUN,MAUnB,CAAA,MAAA,EAVF,OAUE,EAAA,IAAA,EAAA,GAAA,CAAA,EAVmB,UAUnB;EAAA;;;;;;EAWE,MAEvB,CAAA,UAbqB,OAarB,CAAA,CAAA,MAAA,EAbsC,CAatC,EAAA,IAAA,EAb+C,UAa/C,CAAA,EAb4D,MAa5D,CAbmE,CAanE,CAAA;EAAA;;;;AA+F2B;AAU/B;EAEA,KAAiB,CAAA,MAAA,EA7GP,cA6GO,GA7GU,OA6GV,EAAA,QAAA,CAAA,EAAA,MAAA,CAAA,EA3Gb,IA2Ga;;;;;;;+BAxFP,mBACC;;;;;;;;;6BAyBJ;;;;;;;;;;gCAkDwB;;KAUnB,cAAA;UAEK,2BAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,58 +1,57 @@
|
|
|
1
|
-
import { Alepha, Static, TObject, TSchema } from "@alepha/core";
|
|
1
|
+
import { Alepha, Static, TObject, TProperties, TSchema } from "@alepha/core";
|
|
2
2
|
import protobufjs, { Type } from "protobufjs";
|
|
3
|
-
import * as _sinclair_typebox0 from "@sinclair/typebox";
|
|
4
3
|
|
|
5
4
|
//#region src/providers/ProtobufProvider.d.ts
|
|
6
5
|
declare class ProtobufProvider {
|
|
7
6
|
protected readonly alepha: Alepha;
|
|
8
|
-
protected readonly schemas: Map<string | TObject<
|
|
7
|
+
protected readonly schemas: Map<string | TObject<TProperties>, Type>;
|
|
9
8
|
protected readonly protobuf: typeof protobufjs;
|
|
10
9
|
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
* Encode an object to a Uint8Array.
|
|
11
|
+
*
|
|
12
|
+
* @param schema - TypeBox schema used to generate the Protobuf schema.
|
|
13
|
+
* @param data - Object to encode. Can be any object or string.
|
|
14
|
+
*/
|
|
16
15
|
encode(schema: TObject, data: any): Uint8Array;
|
|
17
16
|
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
* Decode a Uint8Array to an object.
|
|
18
|
+
*
|
|
19
|
+
* @param schema
|
|
20
|
+
* @param data
|
|
21
|
+
*/
|
|
23
22
|
decode<T extends TObject>(schema: T, data: Uint8Array): Static<T>;
|
|
24
23
|
/**
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
* Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.
|
|
25
|
+
*
|
|
26
|
+
* @param schema
|
|
27
|
+
* @param typeName
|
|
28
|
+
*/
|
|
30
29
|
parse(schema: ProtobufSchema | TObject, typeName?: string): Type;
|
|
31
30
|
/**
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
* Convert a TypeBox schema to a Protobuf schema as a string.
|
|
32
|
+
*
|
|
33
|
+
* @param schema
|
|
34
|
+
* @param options
|
|
35
|
+
*/
|
|
37
36
|
createProtobufSchema(schema: TSchema, options?: CreateProtobufSchemaOptions): string;
|
|
38
37
|
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
38
|
+
* Parse an object schema to a Protobuf message.
|
|
39
|
+
*
|
|
40
|
+
* @param obj
|
|
41
|
+
* @param parentName
|
|
42
|
+
* @param context
|
|
43
|
+
* @protected
|
|
44
|
+
*/
|
|
46
45
|
protected parseObject(obj: TSchema, parentName: string, context: {
|
|
47
46
|
proto: string;
|
|
48
47
|
fieldIndex: number;
|
|
49
48
|
}): string;
|
|
50
49
|
/**
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
* Convert a primitive TypeBox schema type to a Protobuf spec type.
|
|
51
|
+
*
|
|
52
|
+
* @param schema
|
|
53
|
+
* @protected
|
|
54
|
+
*/
|
|
56
55
|
protected convertType(schema: TSchema): string;
|
|
57
56
|
}
|
|
58
57
|
type ProtobufSchema = string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":["schema: TObject","data: any","schema: T","data: Uint8Array","schema: ProtobufSchema | TObject","schema: TSchema","options: CreateProtobufSchemaOptions","obj: TSchema","parentName: string","context: { proto: string; fieldIndex: number }"],"sources":["../src/providers/ProtobufProvider.ts"],"sourcesContent":[],"mappings":";;;;AAKa,cAAA,gBAAA,CAAA;EAAA,mBAAA,MAAA,EACe,MADf;EAAA,mBACe,OAAA,EACC,GADD,CAAA,MAAA,GACc,OADd,CACsB,WADtB,CAAA,EACoC,IADpC,CAAA;EAAA,mBACsB,QAAA,EAAA,OAEb,UAFa;EAAA;;;;;;EAUN,MAUnB,CAAA,MAAA,EAVF,OAUE,EAAA,IAAA,EAAA,GAAA,CAAA,EAVmB,UAUnB;EAAA;;;;;;EAWE,MAEvB,CAAA,UAbqB,OAarB,CAAA,CAAA,MAAA,EAbsC,CAatC,EAAA,IAAA,EAb+C,UAa/C,CAAA,EAb4D,MAa5D,CAbmE,CAanE,CAAA;EAAA;;;;AA+F2B;AAU/B;EAEA,KAAiB,CAAA,MAAA,EA7GP,cA6GO,GA7GU,OA6GV,EAAA,QAAA,CAAA,EAAA,MAAA,CAAA,EA3Gb,IA2Ga;;;;;;;+BAxFP,mBACC;;;;;;;;;6BAyBJ;;;;;;;;;;gCAkDwB;;KAUnB,cAAA;UAEK,2BAAA"}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["schema: TObject","data: any","schema: T","data: Uint8Array","schema: ProtobufSchema | TObject","schema: TSchema","options: CreateProtobufSchemaOptions","obj: TSchema","parentName: string","context: { proto: string; fieldIndex: number }","fields: string[]"],"sources":["../src/providers/ProtobufProvider.ts"],"sourcesContent":["import type { Static, TObject, TSchema } from \"@alepha/core\";\nimport { $inject, Alepha, TypeGuard } from \"@alepha/core\";\nimport type { Type } from \"protobufjs\";\nimport protobufjs from \"protobufjs\";\n\nexport class ProtobufProvider {\n\tprotected readonly alepha = $inject(Alepha);\n\tprotected readonly schemas
|
|
1
|
+
{"version":3,"file":"index.js","names":["schema: TObject","data: any","schema: T","data: Uint8Array","schema: ProtobufSchema | TObject","schema: TSchema","options: CreateProtobufSchemaOptions","obj: TSchema","parentName: string","context: { proto: string; fieldIndex: number }","fields: string[]"],"sources":["../src/providers/ProtobufProvider.ts"],"sourcesContent":["import type { Static, TObject, TProperties, TSchema } from \"@alepha/core\";\nimport { $inject, Alepha, TypeGuard } from \"@alepha/core\";\nimport type { Type } from \"protobufjs\";\nimport protobufjs from \"protobufjs\";\n\nexport class ProtobufProvider {\n\tprotected readonly alepha: Alepha = $inject(Alepha);\n\tprotected readonly schemas: Map<string | TObject<TProperties>, Type> =\n\t\tnew Map();\n\tprotected readonly protobuf: typeof protobufjs = protobufjs;\n\n\t/**\n\t * Encode an object to a Uint8Array.\n\t *\n\t * @param schema - TypeBox schema used to generate the Protobuf schema.\n\t * @param data - Object to encode. Can be any object or string.\n\t */\n\tpublic encode(schema: TObject, data: any): Uint8Array {\n\t\treturn this.parse(schema).encode(this.alepha.parse(schema, data)).finish();\n\t}\n\n\t/**\n\t * Decode a Uint8Array to an object.\n\t *\n\t * @param schema\n\t * @param data\n\t */\n\tpublic decode<T extends TObject>(schema: T, data: Uint8Array): Static<T> {\n\t\treturn this.alepha.parse(schema, this.parse(schema).decode(data));\n\t}\n\n\t/**\n\t * Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.\n\t *\n\t * @param schema\n\t * @param typeName\n\t */\n\tpublic parse(\n\t\tschema: ProtobufSchema | TObject,\n\t\ttypeName = \"root.Target\",\n\t): Type {\n\t\tconst exists = this.schemas.get(schema);\n\t\tif (exists) return exists;\n\n\t\tconst pbSchema =\n\t\t\ttypeof schema === \"string\" ? schema : this.createProtobufSchema(schema);\n\t\tconst result = this.protobuf.parse(pbSchema);\n\t\tconst type = result.root.lookupType(typeName);\n\t\tthis.schemas.set(schema, type);\n\t\treturn type;\n\t}\n\n\t/**\n\t * Convert a TypeBox schema to a Protobuf schema as a string.\n\t *\n\t * @param schema\n\t * @param options\n\t */\n\tpublic createProtobufSchema(\n\t\tschema: TSchema,\n\t\toptions: CreateProtobufSchemaOptions = {},\n\t): string {\n\t\tconst { rootName = \"root\", mainMessageName = \"Target\" } = options;\n\t\tconst context = {\n\t\t\tproto: `package ${rootName};\\nsyntax = \"proto3\";\\n\\n`,\n\t\t\tfieldIndex: 1,\n\t\t};\n\n\t\tif (TypeGuard.IsObject(schema)) {\n\t\t\tconst proto = this.parseObject(schema, mainMessageName, context);\n\t\t\tcontext.proto += proto;\n\t\t}\n\n\t\treturn context.proto;\n\t}\n\n\t/**\n\t * Parse an object schema to a Protobuf message.\n\t *\n\t * @param obj\n\t * @param parentName\n\t * @param context\n\t * @protected\n\t */\n\tprotected parseObject(\n\t\tobj: TSchema,\n\t\tparentName: string,\n\t\tcontext: { proto: string; fieldIndex: number },\n\t): string {\n\t\tif (!TypeGuard.IsObject(obj)) {\n\t\t\treturn \"\";\n\t\t}\n\n\t\tconst fields: string[] = [];\n\n\t\tfor (const [key, value] of Object.entries(obj.properties)) {\n\t\t\tif (TypeGuard.IsArray(value)) {\n\t\t\t\tif (TypeGuard.IsObject(value.items)) {\n\t\t\t\t\tconst subMessageName = value.items.title ?? `${parentName}_${key}`;\n\t\t\t\t\tcontext.proto += this.parseObject(value.items, subMessageName, {\n\t\t\t\t\t\t...context,\n\t\t\t\t\t\tfieldIndex: 1,\n\t\t\t\t\t});\n\t\t\t\t\tfields.push(\n\t\t\t\t\t\t` repeated ${subMessageName} ${key} = ${context.fieldIndex++};`,\n\t\t\t\t\t);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tconst itemType = this.convertType(value.items);\n\t\t\t\tfields.push(` repeated ${itemType} ${key} = ${context.fieldIndex++};`);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (TypeGuard.IsObject(value)) {\n\t\t\t\tconst subMessageName = `${parentName}_${key}`;\n\t\t\t\tcontext.proto += this.parseObject(value, subMessageName, context);\n\t\t\t\tfields.push(` ${subMessageName} ${key} = ${context.fieldIndex++};`);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tfields.push(\n\t\t\t\t` ${this.convertType(value)} ${key} = ${context.fieldIndex++};`,\n\t\t\t);\n\t\t}\n\n\t\treturn `message ${parentName} {\\n${fields.join(\"\\n\")}\\n}\\n`;\n\t}\n\n\t/**\n\t * Convert a primitive TypeBox schema type to a Protobuf spec type.\n\t *\n\t * @param schema\n\t * @protected\n\t */\n\tprotected convertType(schema: TSchema): string {\n\t\tif (TypeGuard.IsInteger(schema)) return \"int32\";\n\t\tif (TypeGuard.IsNumber(schema)) return \"double\";\n\t\tif (TypeGuard.IsString(schema)) return \"string\";\n\t\tif (TypeGuard.IsBoolean(schema)) return \"bool\";\n\n\t\tthrow new Error(`Unsupported type: ${JSON.stringify(schema)}`);\n\t}\n}\n\nexport type ProtobufSchema = string;\n\nexport interface CreateProtobufSchemaOptions {\n\trootName?: string;\n\tmainMessageName?: string;\n}\n"],"mappings":";;;;AAKA,IAAa,mBAAb,MAA8B;CAC7B,AAAmB,SAAiB,QAAQ,OAAO;CACnD,AAAmB,UAClB,IAAI;CACL,AAAmB,WAA8B;;;;;;;CAQjD,AAAO,OAAOA,QAAiBC,MAAuB;AACrD,SAAO,KAAK,MAAM,OAAO,CAAC,OAAO,KAAK,OAAO,MAAM,QAAQ,KAAK,CAAC,CAAC,QAAQ;CAC1E;;;;;;;CAQD,AAAO,OAA0BC,QAAWC,MAA6B;AACxE,SAAO,KAAK,OAAO,MAAM,QAAQ,KAAK,MAAM,OAAO,CAAC,OAAO,KAAK,CAAC;CACjE;;;;;;;CAQD,AAAO,MACNC,QACA,WAAW,eACJ;EACP,MAAM,SAAS,KAAK,QAAQ,IAAI,OAAO;AACvC,MAAI,OAAQ,QAAO;EAEnB,MAAM,kBACE,WAAW,WAAW,SAAS,KAAK,qBAAqB,OAAO;EACxE,MAAM,SAAS,KAAK,SAAS,MAAM,SAAS;EAC5C,MAAM,OAAO,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AI,KAAK,YAAY,MAAM,CAAC,GAAG,IAAI,KAAK,QAAQ,aAAa,GAC9D;EACD;AAED,UAAQ,UAAU,WAAW,MAAM,OAAO,KAAK,KAAK,CAAC;CACrD;;;;;;;CAQD,AAAU,YAAYL,QAAyB;AAC9C,MAAI,UAAU,UAAU,OAAO,CAAE,QAAO;AACxC,MAAI,UAAU,SAAS,OAAO,CAAE,QAAO;AACvC,MAAI,UAAU,SAAS,OAAO,CAAE,QAAO;AACvC,MAAI,UAAU,UAAU,OAAO,CAAE,QAAO;AAExC,QAAM,IAAI,OAAO,oBAAoB,KAAK,UAAU,OAAO,CAAC;CAC5D;AACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alepha/protobuf",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.0.0"
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"src"
|
|
14
14
|
],
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@alepha/core": "0.
|
|
16
|
+
"@alepha/core": "0.8.0",
|
|
17
17
|
"protobufjs": "^7.5.3"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import type { Static, TObject, TSchema } from "@alepha/core";
|
|
1
|
+
import type { Static, TObject, TProperties, TSchema } from "@alepha/core";
|
|
2
2
|
import { $inject, Alepha, TypeGuard } from "@alepha/core";
|
|
3
3
|
import type { Type } from "protobufjs";
|
|
4
4
|
import protobufjs from "protobufjs";
|
|
5
5
|
|
|
6
6
|
export class ProtobufProvider {
|
|
7
|
-
protected readonly alepha = $inject(Alepha);
|
|
8
|
-
protected readonly schemas
|
|
9
|
-
|
|
7
|
+
protected readonly alepha: Alepha = $inject(Alepha);
|
|
8
|
+
protected readonly schemas: Map<string | TObject<TProperties>, Type> =
|
|
9
|
+
new Map();
|
|
10
|
+
protected readonly protobuf: typeof protobufjs = protobufjs;
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Encode an object to a Uint8Array.
|