@alepha/protobuf 0.6.0 → 0.6.2
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 +0 -0
- package/dist/index.js +0 -0
- package/package.json +5 -5
- package/src/index.ts +0 -1
- package/src/providers/ProtobufProvider.ts +0 -150
package/dist/index.cjs
CHANGED
|
File without changes
|
package/dist/index.js
CHANGED
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alepha/protobuf",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@alepha/core": "0.6.
|
|
10
|
-
"protobufjs": "^7.
|
|
9
|
+
"@alepha/core": "0.6.2",
|
|
10
|
+
"protobufjs": "^7.5.0"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"pkgroll": "^2.12.
|
|
14
|
-
"vitest": "^3.1.
|
|
13
|
+
"pkgroll": "^2.12.2",
|
|
14
|
+
"vitest": "^3.1.2"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
17
|
"build": "pkgroll --clean-dist"
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./providers/ProtobufProvider";
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import type { Static, TObject, TSchema } from "@alepha/core";
|
|
2
|
-
import { $inject, Alepha, TypeGuard } from "@alepha/core";
|
|
3
|
-
import type { Type } from "protobufjs";
|
|
4
|
-
import protobufjs from "protobufjs";
|
|
5
|
-
|
|
6
|
-
export class ProtobufProvider {
|
|
7
|
-
protected readonly alepha = $inject(Alepha);
|
|
8
|
-
protected readonly schemas = new Map<TObject | string, Type>();
|
|
9
|
-
protected readonly protobuf = protobufjs;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Encode an object to a Uint8Array.
|
|
13
|
-
*
|
|
14
|
-
* @param schema - TypeBox schema used to generate the Protobuf schema.
|
|
15
|
-
* @param data - Object to encode. Can be any object or string.
|
|
16
|
-
*/
|
|
17
|
-
public encode(schema: TObject, data: any): Uint8Array {
|
|
18
|
-
return this.parse(schema).encode(this.alepha.parse(schema, data)).finish();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Decode a Uint8Array to an object.
|
|
23
|
-
*
|
|
24
|
-
* @param schema
|
|
25
|
-
* @param data
|
|
26
|
-
*/
|
|
27
|
-
public decode<T extends TObject>(schema: T, data: Uint8Array): Static<T> {
|
|
28
|
-
return this.alepha.parse(schema, this.parse(schema).decode(data));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Parse a TypeBox schema to a Protobuf Type schema ready for encoding/decoding.
|
|
33
|
-
*
|
|
34
|
-
* @param schema
|
|
35
|
-
* @param typeName
|
|
36
|
-
*/
|
|
37
|
-
public parse(
|
|
38
|
-
schema: ProtobufSchema | TObject,
|
|
39
|
-
typeName = "root.Target",
|
|
40
|
-
): Type {
|
|
41
|
-
const exists = this.schemas.get(schema);
|
|
42
|
-
if (exists) return exists;
|
|
43
|
-
|
|
44
|
-
const pbSchema =
|
|
45
|
-
typeof schema === "string" ? schema : this.createProtobufSchema(schema);
|
|
46
|
-
const result = this.protobuf.parse(pbSchema);
|
|
47
|
-
const type = result.root.lookupType(typeName);
|
|
48
|
-
this.schemas.set(schema, type);
|
|
49
|
-
return type;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Convert a TypeBox schema to a Protobuf schema as a string.
|
|
54
|
-
*
|
|
55
|
-
* @param schema
|
|
56
|
-
* @param options
|
|
57
|
-
*/
|
|
58
|
-
public createProtobufSchema(
|
|
59
|
-
schema: TSchema,
|
|
60
|
-
options: CreateProtobufSchemaOptions = {},
|
|
61
|
-
): string {
|
|
62
|
-
const { rootName = "root", mainMessageName = "Target" } = options;
|
|
63
|
-
const context = {
|
|
64
|
-
proto: `package ${rootName};\nsyntax = "proto3";\n\n`,
|
|
65
|
-
fieldIndex: 1,
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
if (TypeGuard.IsObject(schema)) {
|
|
69
|
-
const proto = this.parseObject(schema, mainMessageName, context);
|
|
70
|
-
context.proto += proto;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return context.proto;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Parse an object schema to a Protobuf message.
|
|
78
|
-
*
|
|
79
|
-
* @param obj
|
|
80
|
-
* @param parentName
|
|
81
|
-
* @param context
|
|
82
|
-
* @protected
|
|
83
|
-
*/
|
|
84
|
-
protected parseObject(
|
|
85
|
-
obj: TSchema,
|
|
86
|
-
parentName: string,
|
|
87
|
-
context: { proto: string; fieldIndex: number },
|
|
88
|
-
): string {
|
|
89
|
-
if (!TypeGuard.IsObject(obj)) {
|
|
90
|
-
return "";
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const fields: string[] = [];
|
|
94
|
-
|
|
95
|
-
for (const [key, value] of Object.entries(obj.properties)) {
|
|
96
|
-
if (TypeGuard.IsArray(value)) {
|
|
97
|
-
if (TypeGuard.IsObject(value.items)) {
|
|
98
|
-
const subMessageName = value.items.title ?? `${parentName}_${key}`;
|
|
99
|
-
context.proto += this.parseObject(value.items, subMessageName, {
|
|
100
|
-
...context,
|
|
101
|
-
fieldIndex: 1,
|
|
102
|
-
});
|
|
103
|
-
fields.push(
|
|
104
|
-
` repeated ${subMessageName} ${key} = ${context.fieldIndex++};`,
|
|
105
|
-
);
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const itemType = this.convertType(value.items);
|
|
110
|
-
fields.push(` repeated ${itemType} ${key} = ${context.fieldIndex++};`);
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (TypeGuard.IsObject(value)) {
|
|
115
|
-
const subMessageName = `${parentName}_${key}`;
|
|
116
|
-
context.proto += this.parseObject(value, subMessageName, context);
|
|
117
|
-
fields.push(` ${subMessageName} ${key} = ${context.fieldIndex++};`);
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
fields.push(
|
|
122
|
-
` ${this.convertType(value)} ${key} = ${context.fieldIndex++};`,
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return `message ${parentName} {\n${fields.join("\n")}\n}\n`;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Convert a primitive TypeBox schema type to a Protobuf spec type.
|
|
131
|
-
*
|
|
132
|
-
* @param schema
|
|
133
|
-
* @protected
|
|
134
|
-
*/
|
|
135
|
-
protected convertType(schema: TSchema): string {
|
|
136
|
-
if (TypeGuard.IsInteger(schema)) return "int32";
|
|
137
|
-
if (TypeGuard.IsNumber(schema)) return "double";
|
|
138
|
-
if (TypeGuard.IsString(schema)) return "string";
|
|
139
|
-
if (TypeGuard.IsBoolean(schema)) return "bool";
|
|
140
|
-
|
|
141
|
-
throw new Error(`Unsupported type: ${JSON.stringify(schema)}`);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export type ProtobufSchema = string;
|
|
146
|
-
|
|
147
|
-
export interface CreateProtobufSchemaOptions {
|
|
148
|
-
rootName?: string;
|
|
149
|
-
mainMessageName?: string;
|
|
150
|
-
}
|