@openhps/protobuf 1.0.1 → 1.1.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/LICENSE +1 -1
- package/README.md +58 -38
- package/dist/cjs/InternalProtobufDeserializer.js +30 -15
- package/dist/cjs/InternalProtobufDeserializer.js.map +1 -1
- package/dist/cjs/InternalProtobufSerializer.js +7 -7
- package/dist/cjs/InternalProtobufSerializer.js.map +1 -1
- package/dist/cjs/PrimitiveWrapper.js +13 -0
- package/dist/cjs/PrimitiveWrapper.js.map +1 -0
- package/dist/cjs/ProtobufSerializer.js +27 -2
- package/dist/cjs/ProtobufSerializer.js.map +1 -1
- package/dist/cjs/bin/generator.js +3 -3
- package/dist/cjs/bin/generator.js.map +1 -1
- package/dist/cjs/generator/ObjectGenerator.js +30 -12
- package/dist/cjs/generator/ObjectGenerator.js.map +1 -1
- package/dist/cjs/generator/ProjectGenerator.js +89 -38
- package/dist/cjs/generator/ProjectGenerator.js.map +1 -1
- package/dist/cjs/generator/constants.js +11 -4
- package/dist/cjs/generator/constants.js.map +1 -1
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/tsconfig.bundle.cjs.tsbuildinfo +1 -0
- package/dist/esm/InternalProtobufDeserializer.js +30 -14
- package/dist/esm/InternalProtobufDeserializer.js.map +1 -1
- package/dist/esm/InternalProtobufSerializer.js +7 -6
- package/dist/esm/InternalProtobufSerializer.js.map +1 -1
- package/dist/esm/PrimitiveWrapper.js +9 -0
- package/dist/esm/PrimitiveWrapper.js.map +1 -0
- package/dist/esm/ProtobufSerializer.js +30 -5
- package/dist/esm/ProtobufSerializer.js.map +1 -1
- package/dist/esm/bin/generator.js +5 -5
- package/dist/esm/bin/generator.js.map +1 -1
- package/dist/esm/decorators/index.js +1 -1
- package/dist/esm/generator/ObjectGenerator.js +32 -14
- package/dist/esm/generator/ObjectGenerator.js.map +1 -1
- package/dist/esm/generator/ProjectGenerator.js +92 -40
- package/dist/esm/generator/ProjectGenerator.js.map +1 -1
- package/dist/esm/generator/ServiceGenerator.js +1 -1
- package/dist/esm/generator/constants.js +11 -4
- package/dist/esm/generator/constants.js.map +1 -1
- package/dist/esm/generator/index.js +3 -3
- package/dist/esm/index.js +2 -2
- package/dist/esm/package.json +3 -0
- package/dist/esm/tsconfig.bundle.esm.tsbuildinfo +1 -0
- package/dist/esm/tsconfig.bundle.types.tsbuildinfo +1 -0
- package/dist/types/InternalProtobufDeserializer.d.ts +0 -2
- package/dist/types/InternalProtobufDeserializer.d.ts.map +1 -1
- package/dist/types/InternalProtobufSerializer.d.ts +0 -2
- package/dist/types/InternalProtobufSerializer.d.ts.map +1 -1
- package/dist/types/PrimitiveWrapper.d.ts +8 -0
- package/dist/types/PrimitiveWrapper.d.ts.map +1 -0
- package/dist/types/ProtobufSerializer.d.ts +6 -0
- package/dist/types/ProtobufSerializer.d.ts.map +1 -1
- package/dist/types/generator/ObjectGenerator.d.ts +2 -8
- package/dist/types/generator/ObjectGenerator.d.ts.map +1 -1
- package/dist/types/generator/ProjectGenerator.d.ts +1 -1
- package/dist/types/generator/ProjectGenerator.d.ts.map +1 -1
- package/dist/types/generator/constants.d.ts +1 -1
- package/dist/types/generator/constants.d.ts.map +1 -1
- package/dist/web/openhps-protobuf.es.js +17112 -12881
- package/dist/web/openhps-protobuf.es.js.map +1 -1
- package/dist/web/openhps-protobuf.es.min.js +2 -1
- package/dist/web/openhps-protobuf.es.min.js.LICENSE.txt +19 -0
- package/dist/web/openhps-protobuf.es.min.js.map +1 -1
- package/dist/web/openhps-protobuf.js +17434 -14605
- package/dist/web/openhps-protobuf.js.map +1 -1
- package/dist/web/openhps-protobuf.min.js +2 -1
- package/dist/web/openhps-protobuf.min.js.LICENSE.txt +19 -0
- package/dist/web/openhps-protobuf.min.js.map +1 -1
- package/package.json +125 -94
- package/src/InternalProtobufDeserializer.ts +52 -34
- package/src/InternalProtobufSerializer.ts +9 -10
- package/src/PrimitiveWrapper.ts +9 -0
- package/src/ProtobufSerializer.ts +33 -2
- package/src/bin/generator.ts +4 -4
- package/src/generator/ObjectGenerator.ts +34 -21
- package/src/generator/ProjectGenerator.ts +100 -42
- package/src/generator/constants.ts +11 -4
- package/dist/esm5/InternalProtobufDeserializer.js +0 -147
- package/dist/esm5/InternalProtobufDeserializer.js.map +0 -1
- package/dist/esm5/InternalProtobufSerializer.js +0 -132
- package/dist/esm5/InternalProtobufSerializer.js.map +0 -1
- package/dist/esm5/ProtobufSerializer.js +0 -131
- package/dist/esm5/ProtobufSerializer.js.map +0 -1
- package/dist/esm5/bin/generator.js +0 -104
- package/dist/esm5/bin/generator.js.map +0 -1
- package/dist/esm5/decorators/index.js +0 -2
- package/dist/esm5/decorators/index.js.map +0 -1
- package/dist/esm5/decorators/options.js +0 -2
- package/dist/esm5/decorators/options.js.map +0 -1
- package/dist/esm5/generator/ObjectGenerator.js +0 -311
- package/dist/esm5/generator/ObjectGenerator.js.map +0 -1
- package/dist/esm5/generator/ProjectGenerator.js +0 -115
- package/dist/esm5/generator/ProjectGenerator.js.map +0 -1
- package/dist/esm5/generator/ProtobufGenerator.js +0 -27
- package/dist/esm5/generator/ProtobufGenerator.js.map +0 -1
- package/dist/esm5/generator/ServiceGenerator.js +0 -10
- package/dist/esm5/generator/ServiceGenerator.js.map +0 -1
- package/dist/esm5/generator/constants.js +0 -18
- package/dist/esm5/generator/constants.js.map +0 -1
- package/dist/esm5/generator/index.js +0 -4
- package/dist/esm5/generator/index.js.map +0 -1
- package/dist/esm5/generator/types.js +0 -2
- package/dist/esm5/generator/types.js.map +0 -1
- package/dist/esm5/index.js +0 -3
- package/dist/esm5/index.js.map +0 -1
|
@@ -12,20 +12,26 @@ import {
|
|
|
12
12
|
import chalk from 'chalk';
|
|
13
13
|
import { AnyT, Constructor } from 'typedjson';
|
|
14
14
|
import { HEADER } from './constants';
|
|
15
|
-
import { ProtobufGenerator, ProtobufMessage } from './ProtobufGenerator';
|
|
15
|
+
import { ProtobufGenerator, ProtobufMessage, TypeMapping } from './ProtobufGenerator';
|
|
16
16
|
import { ProjectBuildOptions } from './types';
|
|
17
17
|
|
|
18
|
+
// Re-exported so the generator's public surface does not change
|
|
19
|
+
export type { ProtobufPrimitive, TypeMapping } from './ProtobufGenerator';
|
|
20
|
+
|
|
18
21
|
/**
|
|
19
22
|
* Protobuf object generator
|
|
20
23
|
*/
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
// `Object` here is the global constructor interface used by Constructor<T>, not the wrapper type.
|
|
25
|
+
/* eslint-disable @typescript-eslint/no-wrapper-object-types */
|
|
26
|
+
export class ObjectGenerator extends ProtobufGenerator<Object> {
|
|
27
|
+
processObject(object: Constructor<Object>, metaData: ObjectMemberMetadata): Promise<void> {
|
|
23
28
|
throw new Error('Method not implemented.');
|
|
24
29
|
}
|
|
25
30
|
|
|
26
|
-
generate(object: Constructor<Object>, metaData: ObjectMemberMetadata): Promise<ProtobufMessage> {
|
|
31
|
+
generate(object: Constructor<Object>, metaData: ObjectMemberMetadata): Promise<ProtobufMessage> {
|
|
27
32
|
throw new Error('Method not implemented.');
|
|
28
33
|
}
|
|
34
|
+
/* eslint-enable @typescript-eslint/no-wrapper-object-types */
|
|
29
35
|
|
|
30
36
|
protected static typeMapping(
|
|
31
37
|
object: ObjectMetadata,
|
|
@@ -35,6 +41,7 @@ export class ObjectGenerator extends ProtobufGenerator<Object> { // eslint-disab
|
|
|
35
41
|
): TypeMapping {
|
|
36
42
|
switch (type.ctor) {
|
|
37
43
|
case String:
|
|
44
|
+
case Date:
|
|
38
45
|
return {
|
|
39
46
|
syntax: 'string',
|
|
40
47
|
};
|
|
@@ -63,6 +70,14 @@ export class ObjectGenerator extends ProtobufGenerator<Object> { // eslint-disab
|
|
|
63
70
|
memberOptions,
|
|
64
71
|
buildOptions,
|
|
65
72
|
);
|
|
73
|
+
// An array of a type protobuf cannot express — Set, Object, Function or
|
|
74
|
+
// a member with a custom serializer — maps to undefined, exactly as the
|
|
75
|
+
// Map case below already handles. Reading .syntax off it threw
|
|
76
|
+
// "Cannot read properties of undefined". The caller treats undefined as
|
|
77
|
+
// "omit this field".
|
|
78
|
+
if (mappings === undefined) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
66
81
|
return {
|
|
67
82
|
syntax: `repeated ${mappings.syntax}`,
|
|
68
83
|
types: [mappings],
|
|
@@ -132,12 +147,17 @@ export class ObjectGenerator extends ProtobufGenerator<Object> { // eslint-disab
|
|
|
132
147
|
}
|
|
133
148
|
|
|
134
149
|
static processObject(object: ObjectMetadata, buildOptions: ProjectBuildOptions): void {
|
|
135
|
-
object.protobuf
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
150
|
+
// object.protobuf is a slot shared with the runtime serializer, which fills it in
|
|
151
|
+
// as {messageType, messageTypeEnum, enumMapping} with no `generator` key. Guarding
|
|
152
|
+
// the whole object with `??` kept that value and then dereferenced a `generator`
|
|
153
|
+
// that was not there, so ProtobufSerializer.initialize() threw if anything had
|
|
154
|
+
// already initialised the serializer in the same process. Guard the two levels
|
|
155
|
+
// separately.
|
|
156
|
+
object.protobuf = object.protobuf ?? ({} as any);
|
|
157
|
+
object.protobuf.generator = object.protobuf.generator ?? {
|
|
158
|
+
subModules: new Set(),
|
|
159
|
+
subTypes: [],
|
|
160
|
+
allowOverride: true,
|
|
141
161
|
};
|
|
142
162
|
|
|
143
163
|
const dataMembers = object.dataMembers;
|
|
@@ -189,7 +209,8 @@ export class ObjectGenerator extends ProtobufGenerator<Object> { // eslint-disab
|
|
|
189
209
|
object.protobuf.generator.type = object.protobuf.generator.type ?? object.classType;
|
|
190
210
|
subTypes.forEach((type) => {
|
|
191
211
|
const subTypeMeta = DataSerializerUtils.getOwnMetadata(type);
|
|
192
|
-
subTypeMeta.protobuf = subTypeMeta.protobuf ?? {
|
|
212
|
+
subTypeMeta.protobuf = subTypeMeta.protobuf ?? ({} as any);
|
|
213
|
+
subTypeMeta.protobuf.generator = subTypeMeta.protobuf.generator ?? {};
|
|
193
214
|
subTypeMeta.protobuf.generator.type = object.classType;
|
|
194
215
|
});
|
|
195
216
|
}
|
|
@@ -249,7 +270,7 @@ export class ObjectGenerator extends ProtobufGenerator<Object> { // eslint-disab
|
|
|
249
270
|
.map((member) => {
|
|
250
271
|
const options: any =
|
|
251
272
|
member.options && (member.options as any).protobuf ? (member.options as any).protobuf : {};
|
|
252
|
-
let type: TypeMapping
|
|
273
|
+
let type: TypeMapping;
|
|
253
274
|
const memberName = options.name ?? member.name;
|
|
254
275
|
|
|
255
276
|
if (memberName === 'uid') {
|
|
@@ -348,7 +369,7 @@ export class ObjectGenerator extends ProtobufGenerator<Object> { // eslint-disab
|
|
|
348
369
|
return [
|
|
349
370
|
packageStr,
|
|
350
371
|
HEADER +
|
|
351
|
-
`package ${packageStr.replace(
|
|
372
|
+
`package ${packageStr.replace(/[@/-]/g, '')};\n` +
|
|
352
373
|
`syntax = "proto3";\n` +
|
|
353
374
|
imports
|
|
354
375
|
.filter((value, idx) => {
|
|
@@ -367,11 +388,3 @@ export class ObjectGenerator extends ProtobufGenerator<Object> { // eslint-disab
|
|
|
367
388
|
];
|
|
368
389
|
}
|
|
369
390
|
}
|
|
370
|
-
|
|
371
|
-
export type ProtobufPrimitive = 'string' | 'int32' | 'bool' | 'bytes';
|
|
372
|
-
export interface TypeMapping {
|
|
373
|
-
syntax: ProtobufPrimitive | string;
|
|
374
|
-
types?: TypeMapping[];
|
|
375
|
-
package?: string;
|
|
376
|
-
name?: string;
|
|
377
|
-
}
|
|
@@ -13,35 +13,79 @@ export class ProjectGenerator extends DataSerializer {
|
|
|
13
13
|
private static _modules: Set<string> = new Set();
|
|
14
14
|
private static _packages: Set<string> = new Set();
|
|
15
15
|
|
|
16
|
+
static findAllModules(dir: string): NodeModule[] {
|
|
17
|
+
const packageFile = path.resolve(path.join(dir, 'package.json'));
|
|
18
|
+
// require.main is undefined when mocha runs the specs through its ESM loader.
|
|
19
|
+
const mainPath = require.main?.path;
|
|
20
|
+
if (mainPath !== undefined && packageFile === path.resolve(mainPath)) {
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (fs.existsSync(packageFile)) {
|
|
25
|
+
const packageJson = JSON.parse(fs.readFileSync(packageFile, { encoding: 'utf-8' }));
|
|
26
|
+
const dependencies = packageJson.dependencies;
|
|
27
|
+
const devDependencies = packageJson.devDependencies;
|
|
28
|
+
|
|
29
|
+
// Combine dependencies and devDependencies
|
|
30
|
+
const allDependencies = { ...dependencies, ...devDependencies };
|
|
31
|
+
|
|
32
|
+
// Get the names of all modules
|
|
33
|
+
const allModuleNames = Object.keys(allDependencies);
|
|
34
|
+
const allModules = allModuleNames
|
|
35
|
+
.map((name) => {
|
|
36
|
+
try {
|
|
37
|
+
require(name);
|
|
38
|
+
return require.cache[require.resolve(name)];
|
|
39
|
+
} catch (error) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
.filter(Boolean) as NodeModule[];
|
|
44
|
+
return allModules;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
16
48
|
static findModule(dir: string): string {
|
|
17
|
-
const
|
|
49
|
+
const resolvedDir = path.resolve(dir);
|
|
50
|
+
const packageFile = path.join(resolvedDir, 'package.json');
|
|
18
51
|
if (fs.existsSync(packageFile)) {
|
|
19
52
|
const packageJson = JSON.parse(fs.readFileSync(packageFile, { encoding: 'utf-8' }));
|
|
20
53
|
return packageJson.name;
|
|
21
54
|
} else {
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
55
|
+
const parentDir = path.dirname(resolvedDir);
|
|
56
|
+
if (parentDir === resolvedDir) {
|
|
57
|
+
// Reached the filesystem root without finding a manifest
|
|
58
|
+
throw new Error(`Unable to find a package.json above '${dir}'`);
|
|
59
|
+
}
|
|
60
|
+
return this.findModule(parentDir);
|
|
25
61
|
}
|
|
26
62
|
}
|
|
27
63
|
|
|
28
64
|
static loadModules(objects: Array<Serializable<any>>, module: NodeModule = require.main) {
|
|
29
65
|
if (module === undefined) {
|
|
30
|
-
// Use cache instead
|
|
31
|
-
|
|
66
|
+
// Use cache instead. require.main is undefined under mocha's ESM loader, and
|
|
67
|
+
// the cache is typed as possibly holding undefined entries.
|
|
68
|
+
Object.values(require.cache)
|
|
69
|
+
.filter((cached): cached is NodeModule => cached !== undefined && Array.isArray(cached.children))
|
|
70
|
+
.forEach((cached) => this.loadModules(objects, cached));
|
|
32
71
|
return;
|
|
33
72
|
}
|
|
34
73
|
this._modules.add(module.id);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
74
|
+
if (module.exports) {
|
|
75
|
+
Object.keys(module.exports).forEach((key) => {
|
|
76
|
+
const childModule = module.exports[key];
|
|
77
|
+
if (objects.includes(childModule)) {
|
|
78
|
+
childModule.prototype._module = this.findModule(path.dirname(require.resolve(module.id)));
|
|
79
|
+
this._packages.add(childModule.prototype._module);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if (!Array.isArray(module.children)) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
module.children.forEach((child) => {
|
|
87
|
+
if (child !== undefined && !this._modules.has(child.id)) {
|
|
88
|
+
this.loadModules(objects, child);
|
|
45
89
|
}
|
|
46
90
|
});
|
|
47
91
|
}
|
|
@@ -55,6 +99,10 @@ export class ProjectGenerator extends DataSerializer {
|
|
|
55
99
|
|
|
56
100
|
static loadClasses(): Array<ObjectMetadata> {
|
|
57
101
|
const declarations: Array<ObjectMetadata> = [];
|
|
102
|
+
// Reset so a second build in the same process does not reuse stale module/package state
|
|
103
|
+
this._modules = new Set();
|
|
104
|
+
this._packages = new Set();
|
|
105
|
+
this.findAllModules('./');
|
|
58
106
|
this.knownTypes.forEach((value) => {
|
|
59
107
|
const metadata = DataSerializerUtils.getOwnMetadata(value);
|
|
60
108
|
if (metadata) {
|
|
@@ -94,37 +142,47 @@ export class ProjectGenerator extends DataSerializer {
|
|
|
94
142
|
|
|
95
143
|
static buildProject(directory: string, options: ProjectBuildOptions = {}): Promise<number> {
|
|
96
144
|
return new Promise((resolve, reject) => {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
145
|
+
(async () => {
|
|
146
|
+
const resolvedDirectory = path.resolve(directory);
|
|
147
|
+
// The CLI passes -d <dir> verbatim: refuse to delete the current working
|
|
148
|
+
// directory, one of its parents, or the filesystem root
|
|
149
|
+
if (
|
|
150
|
+
resolvedDirectory === path.parse(resolvedDirectory).root ||
|
|
151
|
+
resolvedDirectory === process.cwd() ||
|
|
152
|
+
process.cwd().startsWith(resolvedDirectory + path.sep)
|
|
153
|
+
) {
|
|
154
|
+
throw new Error(
|
|
155
|
+
`Refusing to use '${resolvedDirectory}': it is the current working directory, a parent of it, or the filesystem root.`,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
102
158
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
ProjectGenerator.generateProtoMessages(options)
|
|
109
|
-
.then((classes) => {
|
|
110
|
-
if (options.logLevel > 0) console.log('Saving generating protocol buffer messages ...');
|
|
111
|
-
classes.forEach((value, key) => {
|
|
112
|
-
const packageDir = path.join(directory, ...value[0].split('.'));
|
|
113
|
-
if (!fs.existsSync(packageDir)) {
|
|
114
|
-
fs.mkdirsSync(packageDir);
|
|
115
|
-
}
|
|
116
|
-
fs.writeFileSync(path.join(packageDir, key + '.proto'), value[1], {
|
|
117
|
-
encoding: 'utf-8',
|
|
118
|
-
});
|
|
119
|
-
});
|
|
159
|
+
// Prepare directories
|
|
160
|
+
if (await fs.pathExists(resolvedDirectory)) {
|
|
161
|
+
await fs.promises.rm(resolvedDirectory, { recursive: true });
|
|
162
|
+
}
|
|
163
|
+
await fs.promises.mkdir(resolvedDirectory, { recursive: true });
|
|
120
164
|
|
|
121
|
-
|
|
165
|
+
// Get all class sources
|
|
166
|
+
if (options.logLevel > 0) {
|
|
167
|
+
console.log('Generating protocol buffer messages ...');
|
|
168
|
+
console.log('Use of any types = ', options.useAnyType);
|
|
169
|
+
}
|
|
170
|
+
const classes = await ProjectGenerator.generateProtoMessages(options);
|
|
171
|
+
if (options.logLevel > 0) console.log('Saving generating protocol buffer messages ...');
|
|
172
|
+
for (const [key, value] of classes) {
|
|
173
|
+
const packageDir = path.join(resolvedDirectory, ...value[0].split('.'));
|
|
174
|
+
await fs.ensureDir(packageDir);
|
|
175
|
+
await fs.promises.writeFile(path.join(packageDir, key + '.proto'), value[1], {
|
|
122
176
|
encoding: 'utf-8',
|
|
123
177
|
});
|
|
178
|
+
}
|
|
124
179
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
180
|
+
await fs.promises.writeFile(path.join(resolvedDirectory, 'common.proto'), COMMON, {
|
|
181
|
+
encoding: 'utf-8',
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
resolve(classes.size);
|
|
185
|
+
})().catch(reject);
|
|
128
186
|
});
|
|
129
187
|
}
|
|
130
188
|
}
|
|
@@ -2,16 +2,23 @@ export const HEADER = `/**
|
|
|
2
2
|
* OpenHPS Protocol Buffer
|
|
3
3
|
* This file was automatically generated with @openhps/protobuf
|
|
4
4
|
*
|
|
5
|
-
* (c) 2019-
|
|
5
|
+
* (c) 2019-2026 Maxim Van de Wynckel & Vrije Universiteit Brussel
|
|
6
6
|
**/\n
|
|
7
7
|
`;
|
|
8
8
|
|
|
9
9
|
export const COMMON =
|
|
10
10
|
HEADER +
|
|
11
11
|
`syntax = "proto3";` +
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
// EnumValueOptions is declared in descriptor.proto. Extending it without importing
|
|
13
|
+
// that file leaves protobufjs unable to resolve the extension, and loading the
|
|
14
|
+
// generated schema fails with "unresolvable extensions".
|
|
15
|
+
`\nimport "google/protobuf/descriptor.proto";\n` +
|
|
16
|
+
`\nextend google.protobuf.EnumValueOptions {\n` +
|
|
17
|
+
// Field numbers 1 and 2 belong to EnumValueOptions' own fields; reusing them made
|
|
18
|
+
// protobufjs reject the schema with "duplicate id 1". Custom options must sit in
|
|
19
|
+
// the 50000-99999 range protobuf reserves for third-party extensions.
|
|
20
|
+
`\toptional string className = 50001;\n` +
|
|
21
|
+
`\toptional string packageName = 50002;\n` +
|
|
15
22
|
`}\n`;
|
|
16
23
|
// `\nextend google.protobuf.MessageOptions {\n` +
|
|
17
24
|
// `\toptional string className = 1001;\n` +
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { ConcreteTypeDescriptor, Deserializer, TypedJSON, UUID, } from '@openhps/core';
|
|
2
|
-
import * as protobuf from 'protobufjs';
|
|
3
|
-
import { AnyT, JsonObjectMetadata } from 'typedjson';
|
|
4
|
-
export class InternalProtobufDeserializer extends Deserializer {
|
|
5
|
-
constructor() {
|
|
6
|
-
super();
|
|
7
|
-
InternalProtobufDeserializer.primitiveWrapper = new protobuf.Type('PrimitiveWrapperMessage');
|
|
8
|
-
InternalProtobufDeserializer.primitiveWrapper.add(new protobuf.Field('value', 1, 'string'));
|
|
9
|
-
this.setDeserializationStrategy(Number, (object) => {
|
|
10
|
-
if (typeof object === 'number') {
|
|
11
|
-
return object;
|
|
12
|
-
}
|
|
13
|
-
else if (typeof object === 'string') {
|
|
14
|
-
return Number(object);
|
|
15
|
-
}
|
|
16
|
-
else if (object.toNumber) {
|
|
17
|
-
return object.toNumber();
|
|
18
|
-
}
|
|
19
|
-
return object;
|
|
20
|
-
});
|
|
21
|
-
this.setDeserializationStrategy(Map, (object, typeDescriptor, knownTypes, memberName, deserializer, memberOptions, serializerOptions) => {
|
|
22
|
-
const result = new Map();
|
|
23
|
-
Object.keys(object).forEach((key) => {
|
|
24
|
-
result.set(key, deserializer.convertSingleValue(object[key], typeDescriptor.valueType, knownTypes, memberName, memberOptions, serializerOptions));
|
|
25
|
-
});
|
|
26
|
-
return result;
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
convertSingleValue(sourceObject, typeDescriptor, knownTypes, memberName, memberOptions, serializerOptions) {
|
|
30
|
-
if (this.retrievePreserveNull(memberOptions) && sourceObject === null) {
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
33
|
-
else if (!TypedJSON.utils.isValueDefined(sourceObject)) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
const deserializer = this.deserializationStrategy.get(typeDescriptor.ctor);
|
|
37
|
-
if (deserializer !== undefined) {
|
|
38
|
-
return deserializer(sourceObject, typeDescriptor, knownTypes, memberName, this, memberOptions, serializerOptions);
|
|
39
|
-
}
|
|
40
|
-
if (typeof sourceObject === 'object') {
|
|
41
|
-
return this.convertAsObject(sourceObject, typeDescriptor, knownTypes, memberName, this, memberOptions, serializerOptions);
|
|
42
|
-
}
|
|
43
|
-
let error = `Could not deserialize '${memberName}'; don't know how to deserialize type`;
|
|
44
|
-
if (typeDescriptor.hasFriendlyName()) {
|
|
45
|
-
error += ` '${typeDescriptor.ctor.name}'`;
|
|
46
|
-
}
|
|
47
|
-
this.errorHandler(new TypeError(`${error}.`));
|
|
48
|
-
}
|
|
49
|
-
convertAsObject(sourceObject, typeDescriptor, knownTypes, memberName, deserializer, memberOptions, serializerOptions) {
|
|
50
|
-
let expectedSelfType = typeDescriptor.ctor;
|
|
51
|
-
let sourceObjectMetadata = JsonObjectMetadata.getFromConstructor(expectedSelfType);
|
|
52
|
-
if (sourceObject.hasOwnProperty('_type') && sourceObject._type !== 0) {
|
|
53
|
-
const enumTypeClassname = sourceObjectMetadata.protobuf.enumMapping.get(sourceObject._type);
|
|
54
|
-
if (enumTypeClassname) {
|
|
55
|
-
const enumType = knownTypes.get(enumTypeClassname);
|
|
56
|
-
if (enumType !== expectedSelfType) {
|
|
57
|
-
expectedSelfType = enumType;
|
|
58
|
-
sourceObjectMetadata = JsonObjectMetadata.getFromConstructor(expectedSelfType);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
else if (sourceObject.type_url && sourceObject.value) {
|
|
63
|
-
const MessageType = serializerOptions.types.get(sourceObject.type_url);
|
|
64
|
-
const message = MessageType.decode(sourceObject.value);
|
|
65
|
-
const data = this.convertAsObject(message, new ConcreteTypeDescriptor(knownTypes.get(sourceObject.type_url)), knownTypes, memberName, deserializer, memberOptions, serializerOptions);
|
|
66
|
-
return data;
|
|
67
|
-
}
|
|
68
|
-
const sourceMetadata = sourceObjectMetadata;
|
|
69
|
-
// Strong-typed deserialization available, get to it.
|
|
70
|
-
// First deserialize properties into a temporary object.
|
|
71
|
-
const sourceObjectWithDeserializedProperties = {};
|
|
72
|
-
// Deserialize by expected properties.
|
|
73
|
-
sourceMetadata.dataMembers.forEach((objMemberMetadata, propKey) => {
|
|
74
|
-
var _a;
|
|
75
|
-
const objMemberDebugName = `${TypedJSON.utils.nameof(sourceMetadata.classType)}.${propKey}`;
|
|
76
|
-
const objMemberOptions = TypedJSON.options.mergeOptions(sourceMetadata.options, objMemberMetadata.options);
|
|
77
|
-
const memberName = (_a = (objMemberOptions && objMemberOptions.protobuf
|
|
78
|
-
? objMemberOptions.protobuf.name
|
|
79
|
-
: objMemberMetadata.name)) !== null && _a !== void 0 ? _a : objMemberMetadata.name;
|
|
80
|
-
const objMemberValue = sourceObject[memberName];
|
|
81
|
-
let revivedValue;
|
|
82
|
-
if (objMemberMetadata.name === 'uid') {
|
|
83
|
-
if (sourceObject.hasOwnProperty('uid_bytes') && sourceObject['uid_bytes'].byteLength > 0) {
|
|
84
|
-
revivedValue = UUID.fromBuffer(sourceObject['uid_bytes']).toString();
|
|
85
|
-
}
|
|
86
|
-
else if (sourceObject.hasOwnProperty('uid_string')) {
|
|
87
|
-
revivedValue = sourceObject['uid_string'];
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
else if (!sourceObject.hasOwnProperty(memberName) || !objMemberValue) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
else if (objMemberMetadata.type() === AnyT && objMemberValue.type_url) {
|
|
97
|
-
const MessageType = serializerOptions.types.get(sourceObject[memberName].type_url);
|
|
98
|
-
if (MessageType) {
|
|
99
|
-
const message = MessageType.decode(objMemberValue.value);
|
|
100
|
-
revivedValue = deserializer.convertSingleValue(message, new ConcreteTypeDescriptor(knownTypes.get(objMemberValue.type_url)), knownTypes, objMemberDebugName, objMemberOptions, serializerOptions);
|
|
101
|
-
}
|
|
102
|
-
else if (objMemberValue.value) {
|
|
103
|
-
const message = InternalProtobufDeserializer.primitiveWrapper.decode(objMemberValue.value);
|
|
104
|
-
switch (objMemberValue.type_url) {
|
|
105
|
-
case 'Boolean':
|
|
106
|
-
revivedValue = Boolean(message.value);
|
|
107
|
-
break;
|
|
108
|
-
case 'Number':
|
|
109
|
-
revivedValue = Number(message.value);
|
|
110
|
-
break;
|
|
111
|
-
case 'String':
|
|
112
|
-
default:
|
|
113
|
-
revivedValue = message.value;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
revivedValue = deserializer.convertSingleValue(objMemberValue, objMemberMetadata.type(), knownTypes, objMemberDebugName, objMemberOptions, serializerOptions);
|
|
119
|
-
}
|
|
120
|
-
if (TypedJSON.utils.isValueDefined(revivedValue)) {
|
|
121
|
-
sourceObjectWithDeserializedProperties[objMemberMetadata.key] = revivedValue;
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
// Next, instantiate target object.
|
|
125
|
-
const targetObject = deserializer.instantiateType(expectedSelfType);
|
|
126
|
-
// Finally, assign deserialized properties to target object.
|
|
127
|
-
Object.assign(targetObject, sourceObjectWithDeserializedProperties);
|
|
128
|
-
// Call onDeserialized method (if any).
|
|
129
|
-
const methodName = sourceObjectMetadata.onDeserializedMethodName;
|
|
130
|
-
if (methodName != null) {
|
|
131
|
-
if (typeof targetObject[methodName] === 'function') {
|
|
132
|
-
// check for member first
|
|
133
|
-
targetObject[methodName]();
|
|
134
|
-
}
|
|
135
|
-
else if (typeof targetObject.constructor[methodName] === 'function') {
|
|
136
|
-
// check for static
|
|
137
|
-
targetObject.constructor[methodName]();
|
|
138
|
-
}
|
|
139
|
-
else {
|
|
140
|
-
deserializer.getErrorHandler()(new TypeError(`onDeserialized callback` +
|
|
141
|
-
`'${TypedJSON.utils.nameof(sourceObjectMetadata.classType)}.${methodName}' is not a method.`));
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return targetObject;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
//# sourceMappingURL=InternalProtobufDeserializer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"InternalProtobufDeserializer.js","sourceRoot":"","sources":["../../src/InternalProtobufDeserializer.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,sBAAsB,EACtB,YAAY,EAOZ,SAAS,EACT,IAAI,GACP,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAErD,MAAM,OAAO,4BAA6B,SAAQ,YAAY;IAG1D;QACI,KAAK,EAAE,CAAC;QAER,4BAA4B,CAAC,gBAAgB,GAAG,IAAI,QAAQ,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;QAC7F,4BAA4B,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,QAAQ,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC;QAE5F,IAAI,CAAC,0BAA0B,CAAC,MAAM,EAAE,CAAC,MAAW,EAAE,EAAE;YACpD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;gBAC7B,OAAO,MAAM,CAAC;YAClB,CAAC;iBAAM,IAAI,OAAO,MAAM,KAAK,Q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E,CAAC;gBAC7E,mBAAmB;gBAClB,YAAY,CAAC,WAAmB,CAAC,UAAU,CAAC,EAAE,CAAC;YACpD,CAAC;iBAAM,CAAC;gBACJ,YAAY,CAAC,eAAe,EAAE,CAC1B,IAAI,SAAS,CACT,yBAAyB;oBACrB,IAAI,SAAS,CAAC,KAAK,CAAC,MAAM,CACtB,oBAAoB,CAAC,SAAS,CACjC,IAAI,UAAU,oBAAoB,CAC1C,CACJ,CAAC;YACN,CAAC;QACL,CAAC;QAED,OAAO,YAAY,CAAC;IACxB,CAAC;CACJ"}
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { Serializer, TypedJSON, UUID, } from '@openhps/core';
|
|
2
|
-
import { AnyT, JsonObjectMetadata } from 'typedjson';
|
|
3
|
-
import * as protobuf from 'protobufjs';
|
|
4
|
-
export class InternalProtobufSerializer extends Serializer {
|
|
5
|
-
constructor() {
|
|
6
|
-
super();
|
|
7
|
-
this.setSerializationStrategy(Map, this.convertAsMap.bind(this));
|
|
8
|
-
InternalProtobufSerializer.primitiveWrapper = new protobuf.Type('PrimitiveWrapperMessage');
|
|
9
|
-
InternalProtobufSerializer.primitiveWrapper.add(new protobuf.Field('value', 1, 'string'));
|
|
10
|
-
}
|
|
11
|
-
convertSingleValue(sourceObject, typeDescriptor, memberName, memberOptions, serializerOptions) {
|
|
12
|
-
if (!TypedJSON.utils.isValueDefined(sourceObject)) {
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
if (!TypedJSON.utils.isInstanceOf(sourceObject, typeDescriptor.ctor)) {
|
|
16
|
-
const expectedName = TypedJSON.utils.nameof(typeDescriptor.ctor);
|
|
17
|
-
const actualName = TypedJSON.utils.nameof(sourceObject.constructor);
|
|
18
|
-
this.errorHandler(new TypeError(`Could not serialize '${memberName}': expected '${expectedName}',` + ` got '${actualName}'.`));
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
const serializer = this.serializationStrategy.get(typeDescriptor.ctor);
|
|
22
|
-
if (serializer !== undefined) {
|
|
23
|
-
return serializer(sourceObject, typeDescriptor, memberName, this, memberOptions, serializerOptions);
|
|
24
|
-
}
|
|
25
|
-
// if not present in the strategy do property by property serialization
|
|
26
|
-
if (typeof sourceObject === 'object') {
|
|
27
|
-
return this.convertAsObject(sourceObject, typeDescriptor, memberName, this, memberOptions, serializerOptions);
|
|
28
|
-
}
|
|
29
|
-
let error = `Could not serialize '${memberName}'; don't know how to serialize type`;
|
|
30
|
-
if (typeDescriptor.hasFriendlyName()) {
|
|
31
|
-
error += ` '${typeDescriptor.ctor.name}'`;
|
|
32
|
-
}
|
|
33
|
-
this.errorHandler(new TypeError(`${error}.`));
|
|
34
|
-
}
|
|
35
|
-
convertAsObject(sourceObject, typeDescriptor, memberName, serializer, memberOptions, serializerOptions) {
|
|
36
|
-
const typeMetadata = JsonObjectMetadata.getFromConstructor(typeDescriptor.ctor);
|
|
37
|
-
let sourceTypeMetadata = JsonObjectMetadata.getFromConstructor(sourceObject.constructor);
|
|
38
|
-
if (!sourceTypeMetadata) {
|
|
39
|
-
sourceTypeMetadata = typeMetadata;
|
|
40
|
-
}
|
|
41
|
-
let targetObject;
|
|
42
|
-
const sourceMeta = sourceTypeMetadata;
|
|
43
|
-
targetObject = {};
|
|
44
|
-
const classOptions = TypedJSON.options.mergeOptions(serializer.options, sourceMeta.options);
|
|
45
|
-
sourceMeta.dataMembers.forEach((objMemberMetadata) => {
|
|
46
|
-
var _a;
|
|
47
|
-
const objMemberOptions = TypedJSON.options.mergeOptions(classOptions, objMemberMetadata.options);
|
|
48
|
-
let serialized;
|
|
49
|
-
let memberName = (_a = (objMemberOptions && objMemberOptions.protobuf
|
|
50
|
-
? objMemberOptions.protobuf.name
|
|
51
|
-
: objMemberMetadata.name)) !== null && _a !== void 0 ? _a : objMemberMetadata.name;
|
|
52
|
-
if (sourceObject[objMemberMetadata.key] === undefined) {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
if (objMemberMetadata.type == null) {
|
|
56
|
-
throw new TypeError(`Could not serialize ${objMemberMetadata.name}, there is` +
|
|
57
|
-
` no constructor nor serialization function to use.`);
|
|
58
|
-
}
|
|
59
|
-
else if (objMemberMetadata.name === 'uid') {
|
|
60
|
-
const uuid = UUID.fromString(sourceObject[objMemberMetadata.key]);
|
|
61
|
-
if (uuid) {
|
|
62
|
-
serialized = uuid.toBuffer();
|
|
63
|
-
memberName += '_bytes';
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
serialized = sourceObject[objMemberMetadata.key];
|
|
67
|
-
memberName += '_string';
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
const MessageType = sourceTypeMetadata.protobuf.messageType;
|
|
72
|
-
const field = MessageType.get(objMemberMetadata.name);
|
|
73
|
-
serialized = this.convertSingleValue(sourceObject[objMemberMetadata.key], objMemberMetadata.type(), `${TypedJSON.utils.nameof(sourceMeta.classType)}.${objMemberMetadata.key}`, objMemberOptions, Object.assign(Object.assign({}, serializerOptions), { field }));
|
|
74
|
-
}
|
|
75
|
-
if (TypedJSON.utils.isValueDefined(serialized)) {
|
|
76
|
-
if (objMemberMetadata.type() === AnyT) {
|
|
77
|
-
const MessageType = serializerOptions.types.get(sourceObject[objMemberMetadata.key].constructor.name);
|
|
78
|
-
if (MessageType) {
|
|
79
|
-
const message = MessageType.create(serialized);
|
|
80
|
-
serialized = {
|
|
81
|
-
type_url: sourceObject[objMemberMetadata.key].constructor.name,
|
|
82
|
-
value: MessageType.encode(message).finish(),
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
const message = InternalProtobufSerializer.primitiveWrapper.create({
|
|
87
|
-
value: String(sourceObject[objMemberMetadata.key]),
|
|
88
|
-
});
|
|
89
|
-
serialized = {
|
|
90
|
-
type_url: sourceObject[objMemberMetadata.key].constructor.name,
|
|
91
|
-
value: InternalProtobufSerializer.primitiveWrapper.encode(message).finish(),
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
targetObject[memberName] = serialized;
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
const isAnyField = serializerOptions.field && serializerOptions.field.type === 'google.protobuf.Any';
|
|
99
|
-
if (!isAnyField && typeMetadata.knownTypes.size > 1) {
|
|
100
|
-
const MessageType = sourceTypeMetadata.protobuf.messageType;
|
|
101
|
-
const message = MessageType.create(Object.assign(Object.assign({}, targetObject), { _type: sourceTypeMetadata.protobuf.messageTypeEnum }));
|
|
102
|
-
targetObject = message;
|
|
103
|
-
}
|
|
104
|
-
else if (isAnyField) {
|
|
105
|
-
const MessageType = sourceTypeMetadata.protobuf.messageType;
|
|
106
|
-
const message = MessageType.create(targetObject);
|
|
107
|
-
targetObject = {
|
|
108
|
-
type_url: sourceObject.constructor.name,
|
|
109
|
-
value: MessageType.encode(message).finish(),
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
return targetObject;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Performs the conversion of a map of typed objects (or primitive values) into an array
|
|
116
|
-
* of simple javascript objects with `key` and `value` properties.
|
|
117
|
-
* @param sourceObject
|
|
118
|
-
* @param typeDescriptor
|
|
119
|
-
* @param memberName
|
|
120
|
-
* @param serializer
|
|
121
|
-
* @param memberOptions
|
|
122
|
-
* @param serializerOptions
|
|
123
|
-
*/
|
|
124
|
-
convertAsMap(sourceObject, typeDescriptor, memberName, serializer, memberOptions, serializerOptions) {
|
|
125
|
-
const result = {};
|
|
126
|
-
sourceObject.forEach((value, key) => {
|
|
127
|
-
result[key] = serializer.convertSingleValue(value, typeDescriptor.valueType, memberName, memberOptions, serializerOptions);
|
|
128
|
-
});
|
|
129
|
-
return result;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
//# sourceMappingURL=InternalProtobufSerializer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"InternalProtobufSerializer.js","sourceRoot":"","sources":["../../src/InternalProtobuf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