@alepha/protobuf 0.8.0 → 0.9.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/README.md CHANGED
@@ -1,24 +1 @@
1
- # alepha/protobuf
2
-
3
- ```ts
4
- import { Alepha, t } from "@alepha/core";
5
- import { ProtobufProvider } from "@alepha/protobuf";
6
-
7
- const protobuf = Alepha.create().get(ProtobufProvider);
8
-
9
- const userSchema = t.object({
10
- username: t.string(),
11
- createdAt: t.datetime(),
12
- age: t.int(),
13
- isActive: t.boolean()
14
- });
15
-
16
- const buffer = protobuf.encode(userSchema, {
17
- username: "John Doe",
18
- createdAt: new Date().toISOString(),
19
- age: 30,
20
- isActive: true
21
- });
22
-
23
- const user = protobuf.decode(userSchema, buffer);
24
- ```
1
+ # Alepha Protobuf
@@ -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, 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,AA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ACpC,MAAM,iBAAiB,MAAM,MAAM,UAAU,EAAE,WAAW,GAAG,IAAI;AACjE,aAAQ,SAAS,KAAK,YAAY,MAAM,OAAO,gBAAgB;MAC9D,GAAG;MACH,YAAY;KACZ,EAAC;AACF,YAAO,MACL,aAAa,eAAe,GAAG,IAAI,KAAK,QAAQ,aAAa,GAC9D;AACD;IACA;IAED,MAAM,WAAW,KAAK,YAAY,MAAM,MAAM;AAC9C,WAAO,MAAM,aAAa,SAAS,GAAG,IAAI,KAAK,QAAQ,aAAa,GAAG;AACvE;GACA;AAED,OAAI,wBAAU,SAAS,MAAM,EAAE;IAC9B,MAAM,kBAAkB,EAAE,WAAW,GAAG,IAAI;AAC5C,YAAQ,SAAS,KAAK,YAAY,OAAO,gBAAgB,QAAQ;AACjE,WAAO,MAAM,IAAI,eAAe,GAAG,IAAI,KAAK,QAAQ,aAAa,GAAG;AACpE;GACA;AAED,UAAO,MACL,IAAI,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,wBAAU,UAAU,OAAO,CAAE,QAAO;AACxC,MAAI,wBAAU,SAAS,OAAO,CAAE,QAAO;AACvC,MAAI,wBAAU,SAAS,OAAO,CAAE,QAAO;AACvC,MAAI,wBAAU,UAAU,OAAO,CAAE,QAAO;AAExC,QAAM,IAAI,OAAO,oBAAoB,KAAK,UAAU,OAAO,CAAC;CAC5D;AACD"}
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 = $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,SAAS,2BAAQA,qBAAO;CAC3C,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,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,QAAO;AACxC,MAAI,wBAAU,SAAS,OAAO,CAAE,QAAO;AACvC,MAAI,wBAAU,SAAS,OAAO,CAAE,QAAO;AACvC,MAAI,wBAAU,UAAU,OAAO,CAAE,QAAO;AAExC,QAAM,IAAI,OAAO,oBAAoB,KAAK,UAAU,OAAO,CAAC;CAC5D;AACD"}
package/dist/index.d.cts CHANGED
@@ -7,51 +7,51 @@ declare class ProtobufProvider {
7
7
  protected readonly schemas: Map<string | TObject<TProperties>, Type>;
8
8
  protected readonly protobuf: typeof protobufjs;
9
9
  /**
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
- */
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
+ */
15
15
  encode(schema: TObject, data: any): Uint8Array;
16
16
  /**
17
- * Decode a Uint8Array to an object.
18
- *
19
- * @param schema
20
- * @param data
21
- */
17
+ * Decode a Uint8Array to an object.
18
+ *
19
+ * @param schema
20
+ * @param data
21
+ */
22
22
  decode<T extends TObject>(schema: T, data: Uint8Array): Static<T>;
23
23
  /**
24
- * Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.
25
- *
26
- * @param schema
27
- * @param typeName
28
- */
24
+ * Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.
25
+ *
26
+ * @param schema
27
+ * @param typeName
28
+ */
29
29
  parse(schema: ProtobufSchema | TObject, typeName?: string): Type;
30
30
  /**
31
- * Convert a TypeBox schema to a Protobuf schema as a string.
32
- *
33
- * @param schema
34
- * @param options
35
- */
31
+ * Convert a TypeBox schema to a Protobuf schema as a string.
32
+ *
33
+ * @param schema
34
+ * @param options
35
+ */
36
36
  createProtobufSchema(schema: TSchema, options?: CreateProtobufSchemaOptions): string;
37
37
  /**
38
- * Parse an object schema to a Protobuf message.
39
- *
40
- * @param obj
41
- * @param parentName
42
- * @param context
43
- * @protected
44
- */
38
+ * Parse an object schema to a Protobuf message.
39
+ *
40
+ * @param obj
41
+ * @param parentName
42
+ * @param context
43
+ * @protected
44
+ */
45
45
  protected parseObject(obj: TSchema, parentName: string, context: {
46
46
  proto: string;
47
47
  fieldIndex: number;
48
48
  }): string;
49
49
  /**
50
- * Convert a primitive TypeBox schema type to a Protobuf spec type.
51
- *
52
- * @param schema
53
- * @protected
54
- */
50
+ * Convert a primitive TypeBox schema type to a Protobuf spec type.
51
+ *
52
+ * @param schema
53
+ * @protected
54
+ */
55
55
  protected convertType(schema: TSchema): string;
56
56
  }
57
57
  type ProtobufSchema = string;
@@ -59,6 +59,7 @@ interface CreateProtobufSchemaOptions {
59
59
  rootName?: string;
60
60
  mainMessageName?: string;
61
61
  }
62
+ //# sourceMappingURL=ProtobufProvider.d.ts.map
62
63
  //#endregion
63
64
  export { CreateProtobufSchemaOptions, ProtobufProvider, ProtobufSchema };
64
65
  //# sourceMappingURL=index.d.cts.map
@@ -1 +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"}
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/providers/ProtobufProvider.ts"],"sourcesContent":[],"mappings":";;;;AAKa,cAAA,gBAAA,CAAgB;EAAA,mBAAA,MAAA,EACH,MADG;EAAA,mBACH,OAAA,EACG,GADH,CAAA,MAAA,GACgB,OADhB,CACwB,WADxB,CAAA,EACsC,IADtC,CAAA;EAAA,mBACwB,QAAA,EAAA,OAEb,UAFa;EAAW;;;;;;EAUP,MAU7B,CAAA,MAAA,EAVF,OAUE,EAAA,IAAA,EAAA,GAAA,CAAA,EAVmB,UAUnB;EAAO;;;;;;EAWE,MAE9B,CAAA,UAbqB,OAarB,CAAA,CAAA,MAAA,EAbsC,CAatC,EAAA,IAAA,EAb+C,UAa/C,CAAA,EAb4D,MAa5D,CAbmE,CAanE,CAAA;EAAI;;;;AA+F8B;AAUtC;EAEiB,KAAA,CAAA,MAAA,EA7GP,cA6GO,GA7GU,OA6GiB,EAAA,QAAA,CAAA,EAAA,MAAA,CAAA,EA3GxC,IA2GwC;;;;;;;+BAxFlC,mBACC;;;;;;;;;6BAyBJ;;;;;;;;;;gCAkDwB;;KAUnB,cAAA;UAEK,2BAAA"}
package/dist/index.d.ts CHANGED
@@ -7,51 +7,51 @@ declare class ProtobufProvider {
7
7
  protected readonly schemas: Map<string | TObject<TProperties>, Type>;
8
8
  protected readonly protobuf: typeof protobufjs;
9
9
  /**
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
- */
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
+ */
15
15
  encode(schema: TObject, data: any): Uint8Array;
16
16
  /**
17
- * Decode a Uint8Array to an object.
18
- *
19
- * @param schema
20
- * @param data
21
- */
17
+ * Decode a Uint8Array to an object.
18
+ *
19
+ * @param schema
20
+ * @param data
21
+ */
22
22
  decode<T extends TObject>(schema: T, data: Uint8Array): Static<T>;
23
23
  /**
24
- * Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.
25
- *
26
- * @param schema
27
- * @param typeName
28
- */
24
+ * Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.
25
+ *
26
+ * @param schema
27
+ * @param typeName
28
+ */
29
29
  parse(schema: ProtobufSchema | TObject, typeName?: string): Type;
30
30
  /**
31
- * Convert a TypeBox schema to a Protobuf schema as a string.
32
- *
33
- * @param schema
34
- * @param options
35
- */
31
+ * Convert a TypeBox schema to a Protobuf schema as a string.
32
+ *
33
+ * @param schema
34
+ * @param options
35
+ */
36
36
  createProtobufSchema(schema: TSchema, options?: CreateProtobufSchemaOptions): string;
37
37
  /**
38
- * Parse an object schema to a Protobuf message.
39
- *
40
- * @param obj
41
- * @param parentName
42
- * @param context
43
- * @protected
44
- */
38
+ * Parse an object schema to a Protobuf message.
39
+ *
40
+ * @param obj
41
+ * @param parentName
42
+ * @param context
43
+ * @protected
44
+ */
45
45
  protected parseObject(obj: TSchema, parentName: string, context: {
46
46
  proto: string;
47
47
  fieldIndex: number;
48
48
  }): string;
49
49
  /**
50
- * Convert a primitive TypeBox schema type to a Protobuf spec type.
51
- *
52
- * @param schema
53
- * @protected
54
- */
50
+ * Convert a primitive TypeBox schema type to a Protobuf spec type.
51
+ *
52
+ * @param schema
53
+ * @protected
54
+ */
55
55
  protected convertType(schema: TSchema): string;
56
56
  }
57
57
  type ProtobufSchema = string;
@@ -59,6 +59,7 @@ interface CreateProtobufSchemaOptions {
59
59
  rootName?: string;
60
60
  mainMessageName?: string;
61
61
  }
62
+ //# sourceMappingURL=ProtobufProvider.d.ts.map
62
63
  //#endregion
63
64
  export { CreateProtobufSchemaOptions, ProtobufProvider, ProtobufSchema };
64
65
  //# sourceMappingURL=index.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/providers/ProtobufProvider.ts"],"sourcesContent":[],"mappings":";;;;AAKa,cAAA,gBAAA,CAAgB;EAAA,mBAAA,MAAA,EACH,MADG;EAAA,mBACH,OAAA,EACG,GADH,CAAA,MAAA,GACgB,OADhB,CACwB,WADxB,CAAA,EACsC,IADtC,CAAA;EAAA,mBACwB,QAAA,EAAA,OAEb,UAFa;EAAW;;;;;;EAUP,MAU7B,CAAA,MAAA,EAVF,OAUE,EAAA,IAAA,EAAA,GAAA,CAAA,EAVmB,UAUnB;EAAO;;;;;;EAWE,MAE9B,CAAA,UAbqB,OAarB,CAAA,CAAA,MAAA,EAbsC,CAatC,EAAA,IAAA,EAb+C,UAa/C,CAAA,EAb4D,MAa5D,CAbmE,CAanE,CAAA;EAAI;;;;AA+F8B;AAUtC;EAEiB,KAAA,CAAA,MAAA,EA7GP,cA6GO,GA7GU,OA6GiB,EAAA,QAAA,CAAA,EAAA,MAAA,CAAA,EA3GxC,IA2GwC;;;;;;;+BAxFlC,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, 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,I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G,IAAI;AACjE,aAAQ,SAAS,KAAK,YAAY,MAAM,OAAO,gBAAgB;MAC9D,GAAG;MACH,YAAY;KACZ,EAAC;AACF,YAAO,MACL,aAAa,eAAe,GAAG,IAAI,KAAK,QAAQ,aAAa,GAC9D;AACD;IACA;IAED,MAAM,WAAW,KAAK,YAAY,MAAM,MAAM;AAC9C,WAAO,MAAM,aAAa,SAAS,GAAG,IAAI,KAAK,QAAQ,aAAa,GAAG;AACvE;GACA;AAED,OAAI,UAAU,SAAS,MAAM,EAAE;IAC9B,MAAM,kBAAkB,EAAE,WAAW,GAAG,IAAI;AAC5C,YAAQ,SAAS,KAAK,YAAY,OAAO,gBAAgB,QAAQ;AACjE,WAAO,MAAM,IAAI,eAAe,GAAG,IAAI,KAAK,QAAQ,aAAa,GAAG;AACpE;GACA;AAED,UAAO,MACL,IAAI,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"}
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 = $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,SAAS,QAAQ,OAAO;CAC3C,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,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,UAAU,SAAS,OAAO,EAAE;GAC/B,MAAM,QAAQ,KAA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AAU,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.8.0",
3
+ "version": "0.9.0",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=22.0.0"
@@ -13,11 +13,11 @@
13
13
  "src"
14
14
  ],
15
15
  "dependencies": {
16
- "@alepha/core": "0.8.0",
16
+ "@alepha/core": "0.9.0",
17
17
  "protobufjs": "^7.5.3"
18
18
  },
19
19
  "devDependencies": {
20
- "tsdown": "^0.12.9",
20
+ "tsdown": "^0.13.0",
21
21
  "vitest": "^3.2.4"
22
22
  },
23
23
  "scripts": {
@@ -4,7 +4,7 @@ import type { Type } from "protobufjs";
4
4
  import protobufjs from "protobufjs";
5
5
 
6
6
  export class ProtobufProvider {
7
- protected readonly alepha: Alepha = $inject(Alepha);
7
+ protected readonly alepha = $inject(Alepha);
8
8
  protected readonly schemas: Map<string | TObject<TProperties>, Type> =
9
9
  new Map();
10
10
  protected readonly protobuf: typeof protobufjs = protobufjs;