@openhps/protobuf 1.0.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 +190 -0
- package/README.md +165 -0
- package/dist/cjs/InternalProtobufDeserializer.js +152 -0
- package/dist/cjs/InternalProtobufDeserializer.js.map +1 -0
- package/dist/cjs/InternalProtobufSerializer.js +137 -0
- package/dist/cjs/InternalProtobufSerializer.js.map +1 -0
- package/dist/cjs/ProtobufSerializer.js +136 -0
- package/dist/cjs/ProtobufSerializer.js.map +1 -0
- package/dist/cjs/bin/generator.js +107 -0
- package/dist/cjs/bin/generator.js.map +1 -0
- package/dist/cjs/decorators/index.js +4 -0
- package/dist/cjs/decorators/index.js.map +1 -0
- package/dist/cjs/decorators/options.js +3 -0
- package/dist/cjs/decorators/options.js.map +1 -0
- package/dist/cjs/generator/ObjectGenerator.js +316 -0
- package/dist/cjs/generator/ObjectGenerator.js.map +1 -0
- package/dist/cjs/generator/ProjectGenerator.js +120 -0
- package/dist/cjs/generator/ProjectGenerator.js.map +1 -0
- package/dist/cjs/generator/ProtobufGenerator.js +31 -0
- package/dist/cjs/generator/ProtobufGenerator.js.map +1 -0
- package/dist/cjs/generator/ServiceGenerator.js +14 -0
- package/dist/cjs/generator/ServiceGenerator.js.map +1 -0
- package/dist/cjs/generator/constants.js +21 -0
- package/dist/cjs/generator/constants.js.map +1 -0
- package/dist/cjs/generator/index.js +7 -0
- package/dist/cjs/generator/index.js.map +1 -0
- package/dist/cjs/generator/types.js +3 -0
- package/dist/cjs/generator/types.js.map +1 -0
- package/dist/cjs/index.js +6 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/InternalProtobufDeserializer.js +147 -0
- package/dist/esm/InternalProtobufDeserializer.js.map +1 -0
- package/dist/esm/InternalProtobufSerializer.js +132 -0
- package/dist/esm/InternalProtobufSerializer.js.map +1 -0
- package/dist/esm/ProtobufSerializer.js +131 -0
- package/dist/esm/ProtobufSerializer.js.map +1 -0
- package/dist/esm/bin/generator.js +104 -0
- package/dist/esm/bin/generator.js.map +1 -0
- package/dist/esm/decorators/index.js +2 -0
- package/dist/esm/decorators/index.js.map +1 -0
- package/dist/esm/decorators/options.js +2 -0
- package/dist/esm/decorators/options.js.map +1 -0
- package/dist/esm/generator/ObjectGenerator.js +311 -0
- package/dist/esm/generator/ObjectGenerator.js.map +1 -0
- package/dist/esm/generator/ProjectGenerator.js +115 -0
- package/dist/esm/generator/ProjectGenerator.js.map +1 -0
- package/dist/esm/generator/ProtobufGenerator.js +27 -0
- package/dist/esm/generator/ProtobufGenerator.js.map +1 -0
- package/dist/esm/generator/ServiceGenerator.js +10 -0
- package/dist/esm/generator/ServiceGenerator.js.map +1 -0
- package/dist/esm/generator/constants.js +18 -0
- package/dist/esm/generator/constants.js.map +1 -0
- package/dist/esm/generator/index.js +4 -0
- package/dist/esm/generator/index.js.map +1 -0
- package/dist/esm/generator/types.js +2 -0
- package/dist/esm/generator/types.js.map +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm5/InternalProtobufDeserializer.js +147 -0
- package/dist/esm5/InternalProtobufDeserializer.js.map +1 -0
- package/dist/esm5/InternalProtobufSerializer.js +132 -0
- package/dist/esm5/InternalProtobufSerializer.js.map +1 -0
- package/dist/esm5/ProtobufSerializer.js +131 -0
- package/dist/esm5/ProtobufSerializer.js.map +1 -0
- package/dist/esm5/bin/generator.js +104 -0
- package/dist/esm5/bin/generator.js.map +1 -0
- package/dist/esm5/decorators/index.js +2 -0
- package/dist/esm5/decorators/index.js.map +1 -0
- package/dist/esm5/decorators/options.js +2 -0
- package/dist/esm5/decorators/options.js.map +1 -0
- package/dist/esm5/generator/ObjectGenerator.js +311 -0
- package/dist/esm5/generator/ObjectGenerator.js.map +1 -0
- package/dist/esm5/generator/ProjectGenerator.js +115 -0
- package/dist/esm5/generator/ProjectGenerator.js.map +1 -0
- package/dist/esm5/generator/ProtobufGenerator.js +27 -0
- package/dist/esm5/generator/ProtobufGenerator.js.map +1 -0
- package/dist/esm5/generator/ServiceGenerator.js +10 -0
- package/dist/esm5/generator/ServiceGenerator.js.map +1 -0
- package/dist/esm5/generator/constants.js +18 -0
- package/dist/esm5/generator/constants.js.map +1 -0
- package/dist/esm5/generator/index.js +4 -0
- package/dist/esm5/generator/index.js.map +1 -0
- package/dist/esm5/generator/types.js +2 -0
- package/dist/esm5/generator/types.js.map +1 -0
- package/dist/esm5/index.js +3 -0
- package/dist/esm5/index.js.map +1 -0
- package/dist/types/InternalProtobufDeserializer.d.ts +9 -0
- package/dist/types/InternalProtobufDeserializer.d.ts.map +1 -0
- package/dist/types/InternalProtobufSerializer.d.ts +23 -0
- package/dist/types/InternalProtobufSerializer.d.ts.map +1 -0
- package/dist/types/ProtobufSerializer.d.ts +32 -0
- package/dist/types/ProtobufSerializer.d.ts.map +1 -0
- package/dist/types/bin/generator.d.ts +3 -0
- package/dist/types/bin/generator.d.ts.map +1 -0
- package/dist/types/decorators/index.d.ts +2 -0
- package/dist/types/decorators/index.d.ts.map +1 -0
- package/dist/types/decorators/options.d.ts +30 -0
- package/dist/types/decorators/options.d.ts.map +1 -0
- package/dist/types/generator/ObjectGenerator.d.ts +22 -0
- package/dist/types/generator/ObjectGenerator.d.ts.map +1 -0
- package/dist/types/generator/ProjectGenerator.d.ts +17 -0
- package/dist/types/generator/ProjectGenerator.d.ts.map +1 -0
- package/dist/types/generator/ProtobufGenerator.d.ts +26 -0
- package/dist/types/generator/ProtobufGenerator.d.ts.map +1 -0
- package/dist/types/generator/ServiceGenerator.d.ts +7 -0
- package/dist/types/generator/ServiceGenerator.d.ts.map +1 -0
- package/dist/types/generator/constants.d.ts +3 -0
- package/dist/types/generator/constants.d.ts.map +1 -0
- package/dist/types/generator/index.d.ts +4 -0
- package/dist/types/generator/index.d.ts.map +1 -0
- package/dist/types/generator/types.d.ts +5 -0
- package/dist/types/generator/types.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/web/openhps-protobuf.es.js +15165 -0
- package/dist/web/openhps-protobuf.es.js.map +1 -0
- package/dist/web/openhps-protobuf.es.min.js +2 -0
- package/dist/web/openhps-protobuf.es.min.js.map +1 -0
- package/dist/web/openhps-protobuf.js +16722 -0
- package/dist/web/openhps-protobuf.js.map +1 -0
- package/dist/web/openhps-protobuf.min.js +2 -0
- package/dist/web/openhps-protobuf.min.js.map +1 -0
- package/package.json +96 -0
- package/src/InternalProtobufDeserializer.ts +245 -0
- package/src/InternalProtobufSerializer.ts +212 -0
- package/src/ProtobufSerializer.ts +168 -0
- package/src/bin/generator.ts +131 -0
- package/src/decorators/index.ts +1 -0
- package/src/decorators/options.ts +30 -0
- package/src/generator/ObjectGenerator.ts +377 -0
- package/src/generator/ProjectGenerator.ts +130 -0
- package/src/generator/ProtobufGenerator.ts +49 -0
- package/src/generator/ServiceGenerator.ts +11 -0
- package/src/generator/constants.ts +19 -0
- package/src/generator/index.ts +3 -0
- package/src/generator/types.ts +4 -0
- package/src/index.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2019-2023 Maxim Van de Wynckel & Vrije Universiteit Brussel
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
<img alt="OpenHPS" src="https://openhps.org/images/logo_text-512.png" width="40%" /><br />
|
|
3
|
+
@openhps/protobuf
|
|
4
|
+
</h1>
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://github.com/OpenHPS/openhps-protobuf/actions/workflows/main.yml" target="_blank">
|
|
7
|
+
<img alt="Build Status" src="https://github.com/OpenHPS/openhps-protobuf/actions/workflows/main.yml/badge.svg">
|
|
8
|
+
</a>
|
|
9
|
+
<a href="https://codecov.io/gh/OpenHPS/openhps-protobuf">
|
|
10
|
+
<img src="https://codecov.io/gh/OpenHPS/openhps-protobuf/branch/master/graph/badge.svg?token=U896HUBDCZ"/>
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://codeclimate.com/github/OpenHPS/openhps-protobuf/" target="_blank">
|
|
13
|
+
<img alt="Maintainability" src="https://img.shields.io/codeclimate/maintainability/OpenHPS/openhps-protobuf">
|
|
14
|
+
</a>
|
|
15
|
+
<a href="https://badge.fury.io/js/@openhps%2Fprotobuf">
|
|
16
|
+
<img src="https://badge.fury.io/js/@openhps%2Fprotobuf.svg" alt="npm version" height="18">
|
|
17
|
+
</a>
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
<h3 align="center">
|
|
21
|
+
<a href="https://github.com/OpenHPS/openhps-core">@openhps/core</a> — <a href="https://openhps.org/docs/protobuf">API</a>
|
|
22
|
+
</h3>
|
|
23
|
+
|
|
24
|
+
<br />
|
|
25
|
+
|
|
26
|
+
When serializing data generated by OpenHPS, the framework will use a modified serialization strategy of [TypedJSON](https://github.com/JohnWeisz/TypedJSON/) to convert the data to JSON format. This serialized JSON data can be used for socket, MQTT and REST communication when transmitting data from client to server. In real-time situations, JSON messages might be too large to transmit from embedded devices to a server.
|
|
27
|
+
|
|
28
|
+
[Protocol buffers](https://protobuf.dev/) are language-neutral and platform-neutral mechanisms for serializing structured data to lightweight buffers. Instead of encoding the structure of your data within the JSON data that is being serialized, a structured *message type* is defined beforehand that can be used by the serializer and deserializer. This structured *message type* will ensure that the data adheres to a specific structure while also prevent the need to encode this structure within the serialized data, lowering the size of the serialized data drastically.
|
|
29
|
+
|
|
30
|
+
This OpenHPS module includes the serialization and deserialization engine for Protocol Buffer Messages to OpenHPS classes and object instances. In addition, this module provides a CLI tool for generating protocol message types of all OpenHPS modules installed in your project. Combined, these two features ensure that you do not have to manually create message types and do not manually have to create a custom deserialization strategy for each class you might need to serialize.
|
|
31
|
+
|
|
32
|
+
## Features
|
|
33
|
+
|
|
34
|
+
- Automatic protocol buffer message type generator
|
|
35
|
+
- Serialization and deserialization of all serializable OpenHPS classes
|
|
36
|
+
|
|
37
|
+
## Known Limitations/Workarounds
|
|
38
|
+
|
|
39
|
+
- JavaScript only uses `Number` as a primitive. Protocol buffers use integers, floats, ... OpenHPS includes the `NumberType` enum option for `SerializableMember` decorators. When no number type is specified a number will be serialized to a string to ensure compatibility.
|
|
40
|
+
- Polymorphism and inheritance is not possible in protocol buffer v3. In order to ensure maintainable messages, any serializable object with known types will be treated as a `google.protobuf.Any` type with an included `type_url` that can be used for deserialization.
|
|
41
|
+
- Objects that use generic types will also make use of `google.protobuf.Any`. However, generic types that include primitives will be serialized using a primitive wrapper.
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
### CLI Generator
|
|
46
|
+
This module can generate the protocol files (*.proto) automatically.
|
|
47
|
+
|
|
48
|
+
1. Install the module using `npm install @openhps/protobuf@latest`
|
|
49
|
+
2. Open the root directory of your project containing your `package.json` file
|
|
50
|
+
3. Execute the CLI command using `openhps-protobuf`
|
|
51
|
+
|
|
52
|
+
#### Parameters
|
|
53
|
+
|
|
54
|
+
`-d <directory>` Output directory\
|
|
55
|
+
`-v` Verbose logging
|
|
56
|
+
|
|
57
|
+
#### Output Data
|
|
58
|
+
The output directory will include a subdirectory for each OpenHPS package.
|
|
59
|
+
|
|
60
|
+
### Serialization and Deserialization
|
|
61
|
+
First the protocol buffer serializer has to be initialized with the messages.
|
|
62
|
+
```typescript
|
|
63
|
+
import { ProtobufSerializer } from '@openhps/protobuf';
|
|
64
|
+
|
|
65
|
+
// Initialize the protocol buffer serializer with the protocol files
|
|
66
|
+
ProtobufSerializer.initialize("/home/openhps/protobuf/");
|
|
67
|
+
|
|
68
|
+
// Or
|
|
69
|
+
|
|
70
|
+
// Initialize the protocol buffer serialization by creating protocol messages in the ./tmp directory
|
|
71
|
+
ProtobufSerializer.initialize();
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
An example protocol buffer message type can be seen below for a `DataObject`. The absolute and relative positions are set to an `Any` type.
|
|
75
|
+
|
|
76
|
+
```proto
|
|
77
|
+
package openhps.core;
|
|
78
|
+
syntax = "proto3";
|
|
79
|
+
import "google/protobuf/any.proto";
|
|
80
|
+
message DataObject {
|
|
81
|
+
string displayName = 1;
|
|
82
|
+
int64 createdTimestamp = 2;
|
|
83
|
+
string uid = 3;
|
|
84
|
+
string parentUID = 4;
|
|
85
|
+
google.protobuf.Any position = 5;
|
|
86
|
+
repeated google.protobuf.Any relativePositions = 6;
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
```proto
|
|
90
|
+
package openhps.core;
|
|
91
|
+
syntax = "proto3";
|
|
92
|
+
import "Velocity.proto";
|
|
93
|
+
import "Orientation.proto";
|
|
94
|
+
import "google/protobuf/any.proto";
|
|
95
|
+
message Absolute3DPosition {
|
|
96
|
+
string timestamp = 1;
|
|
97
|
+
Velocity velocity = 2;
|
|
98
|
+
Orientation orientation = 3;
|
|
99
|
+
google.protobuf.Any unit = 4;
|
|
100
|
+
string referenceSpaceUID = 5;
|
|
101
|
+
google.protobuf.Any accuracy = 6;
|
|
102
|
+
string probability = 7;
|
|
103
|
+
double x = 8;
|
|
104
|
+
double y = 9;
|
|
105
|
+
double z = 10;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### @openhps/socket
|
|
111
|
+
```ts
|
|
112
|
+
ModelBuilder.create()
|
|
113
|
+
.addService(new SocketServer({
|
|
114
|
+
srv: server,
|
|
115
|
+
path: "/api/v1"
|
|
116
|
+
}))
|
|
117
|
+
.from(new SocketServerSource({
|
|
118
|
+
uid: "source",
|
|
119
|
+
// Override serializer and deserializer with protocol buffer
|
|
120
|
+
serialize: (obj) => ProtobufSerializer.serialize(obj),
|
|
121
|
+
deserialize: (obj) => ProtobufSerializer.deserialize(obj)
|
|
122
|
+
}))
|
|
123
|
+
.to()
|
|
124
|
+
.build();
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### @openhps/mqtt
|
|
128
|
+
```ts
|
|
129
|
+
ModelBuilder.create()
|
|
130
|
+
.addService(new MQTTServer({
|
|
131
|
+
port: 1443,
|
|
132
|
+
}))
|
|
133
|
+
.from(new MQTTSourceNode({
|
|
134
|
+
uid: "source",
|
|
135
|
+
// Override frame serializer (not the options)
|
|
136
|
+
serialize: (obj, options) => ({
|
|
137
|
+
frame: ProtobufSerializer.serialize(obj),
|
|
138
|
+
options
|
|
139
|
+
}),
|
|
140
|
+
deserialize: (obj) => ProtobufSerializer.deserialize(obj.frame)
|
|
141
|
+
}))
|
|
142
|
+
.to()
|
|
143
|
+
.build();
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Getting Started
|
|
147
|
+
If you have [npm installed](https://www.npmjs.com/get-npm), start using @openhps/protobuf with the following command.
|
|
148
|
+
```bash
|
|
149
|
+
npm install @openhps/protobuf --save
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Contributors
|
|
153
|
+
The framework is open source and is mainly developed by PhD Student Maxim Van de Wynckel as part of his research towards *Hybrid Positioning and Implicit Human-Computer Interaction* under the supervision of Prof. Dr. Beat Signer.
|
|
154
|
+
|
|
155
|
+
## Contributing
|
|
156
|
+
Use of OpenHPS, contributions and feedback is highly appreciated. Please read our [contributing guidelines](CONTRIBUTING.md) for more information.
|
|
157
|
+
|
|
158
|
+
## License
|
|
159
|
+
Copyright (C) 2019-2024 Maxim Van de Wynckel & Vrije Universiteit Brussel
|
|
160
|
+
|
|
161
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
|
|
162
|
+
|
|
163
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
164
|
+
|
|
165
|
+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InternalProtobufDeserializer = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const core_1 = require("@openhps/core");
|
|
6
|
+
const protobuf = tslib_1.__importStar(require("protobufjs"));
|
|
7
|
+
const typedjson_1 = require("typedjson");
|
|
8
|
+
class InternalProtobufDeserializer extends core_1.Deserializer {
|
|
9
|
+
constructor() {
|
|
10
|
+
super();
|
|
11
|
+
InternalProtobufDeserializer.primitiveWrapper = new protobuf.Type('PrimitiveWrapperMessage');
|
|
12
|
+
InternalProtobufDeserializer.primitiveWrapper.add(new protobuf.Field('value', 1, 'string'));
|
|
13
|
+
this.setDeserializationStrategy(Number, (object) => {
|
|
14
|
+
if (typeof object === 'number') {
|
|
15
|
+
return object;
|
|
16
|
+
}
|
|
17
|
+
else if (typeof object === 'string') {
|
|
18
|
+
return Number(object);
|
|
19
|
+
}
|
|
20
|
+
else if (object.toNumber) {
|
|
21
|
+
return object.toNumber();
|
|
22
|
+
}
|
|
23
|
+
return object;
|
|
24
|
+
});
|
|
25
|
+
this.setDeserializationStrategy(Map, (object, typeDescriptor, knownTypes, memberName, deserializer, memberOptions, serializerOptions) => {
|
|
26
|
+
const result = new Map();
|
|
27
|
+
Object.keys(object).forEach((key) => {
|
|
28
|
+
result.set(key, deserializer.convertSingleValue(object[key], typeDescriptor.valueType, knownTypes, memberName, memberOptions, serializerOptions));
|
|
29
|
+
});
|
|
30
|
+
return result;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
convertSingleValue(sourceObject, typeDescriptor, knownTypes, memberName, memberOptions, serializerOptions) {
|
|
34
|
+
if (this.retrievePreserveNull(memberOptions) && sourceObject === null) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
else if (!core_1.TypedJSON.utils.isValueDefined(sourceObject)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const deserializer = this.deserializationStrategy.get(typeDescriptor.ctor);
|
|
41
|
+
if (deserializer !== undefined) {
|
|
42
|
+
return deserializer(sourceObject, typeDescriptor, knownTypes, memberName, this, memberOptions, serializerOptions);
|
|
43
|
+
}
|
|
44
|
+
if (typeof sourceObject === 'object') {
|
|
45
|
+
return this.convertAsObject(sourceObject, typeDescriptor, knownTypes, memberName, this, memberOptions, serializerOptions);
|
|
46
|
+
}
|
|
47
|
+
let error = `Could not deserialize '${memberName}'; don't know how to deserialize type`;
|
|
48
|
+
if (typeDescriptor.hasFriendlyName()) {
|
|
49
|
+
error += ` '${typeDescriptor.ctor.name}'`;
|
|
50
|
+
}
|
|
51
|
+
this.errorHandler(new TypeError(`${error}.`));
|
|
52
|
+
}
|
|
53
|
+
convertAsObject(sourceObject, typeDescriptor, knownTypes, memberName, deserializer, memberOptions, serializerOptions) {
|
|
54
|
+
let expectedSelfType = typeDescriptor.ctor;
|
|
55
|
+
let sourceObjectMetadata = typedjson_1.JsonObjectMetadata.getFromConstructor(expectedSelfType);
|
|
56
|
+
if (sourceObject.hasOwnProperty('_type') && sourceObject._type !== 0) {
|
|
57
|
+
const enumTypeClassname = sourceObjectMetadata.protobuf.enumMapping.get(sourceObject._type);
|
|
58
|
+
if (enumTypeClassname) {
|
|
59
|
+
const enumType = knownTypes.get(enumTypeClassname);
|
|
60
|
+
if (enumType !== expectedSelfType) {
|
|
61
|
+
expectedSelfType = enumType;
|
|
62
|
+
sourceObjectMetadata = typedjson_1.JsonObjectMetadata.getFromConstructor(expectedSelfType);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else if (sourceObject.type_url && sourceObject.value) {
|
|
67
|
+
const MessageType = serializerOptions.types.get(sourceObject.type_url);
|
|
68
|
+
const message = MessageType.decode(sourceObject.value);
|
|
69
|
+
const data = this.convertAsObject(message, new core_1.ConcreteTypeDescriptor(knownTypes.get(sourceObject.type_url)), knownTypes, memberName, deserializer, memberOptions, serializerOptions);
|
|
70
|
+
return data;
|
|
71
|
+
}
|
|
72
|
+
const sourceMetadata = sourceObjectMetadata;
|
|
73
|
+
// Strong-typed deserialization available, get to it.
|
|
74
|
+
// First deserialize properties into a temporary object.
|
|
75
|
+
const sourceObjectWithDeserializedProperties = {};
|
|
76
|
+
// Deserialize by expected properties.
|
|
77
|
+
sourceMetadata.dataMembers.forEach((objMemberMetadata, propKey) => {
|
|
78
|
+
var _a;
|
|
79
|
+
const objMemberDebugName = `${core_1.TypedJSON.utils.nameof(sourceMetadata.classType)}.${propKey}`;
|
|
80
|
+
const objMemberOptions = core_1.TypedJSON.options.mergeOptions(sourceMetadata.options, objMemberMetadata.options);
|
|
81
|
+
const memberName = (_a = (objMemberOptions && objMemberOptions.protobuf
|
|
82
|
+
? objMemberOptions.protobuf.name
|
|
83
|
+
: objMemberMetadata.name)) !== null && _a !== void 0 ? _a : objMemberMetadata.name;
|
|
84
|
+
const objMemberValue = sourceObject[memberName];
|
|
85
|
+
let revivedValue;
|
|
86
|
+
if (objMemberMetadata.name === 'uid') {
|
|
87
|
+
if (sourceObject.hasOwnProperty('uid_bytes') && sourceObject['uid_bytes'].byteLength > 0) {
|
|
88
|
+
revivedValue = core_1.UUID.fromBuffer(sourceObject['uid_bytes']).toString();
|
|
89
|
+
}
|
|
90
|
+
else if (sourceObject.hasOwnProperty('uid_string')) {
|
|
91
|
+
revivedValue = sourceObject['uid_string'];
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else if (!sourceObject.hasOwnProperty(memberName) || !objMemberValue) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
else if (objMemberMetadata.type() === typedjson_1.AnyT && objMemberValue.type_url) {
|
|
101
|
+
const MessageType = serializerOptions.types.get(sourceObject[memberName].type_url);
|
|
102
|
+
if (MessageType) {
|
|
103
|
+
const message = MessageType.decode(objMemberValue.value);
|
|
104
|
+
revivedValue = deserializer.convertSingleValue(message, new core_1.ConcreteTypeDescriptor(knownTypes.get(objMemberValue.type_url)), knownTypes, objMemberDebugName, objMemberOptions, serializerOptions);
|
|
105
|
+
}
|
|
106
|
+
else if (objMemberValue.value) {
|
|
107
|
+
const message = InternalProtobufDeserializer.primitiveWrapper.decode(objMemberValue.value);
|
|
108
|
+
switch (objMemberValue.type_url) {
|
|
109
|
+
case 'Boolean':
|
|
110
|
+
revivedValue = Boolean(message.value);
|
|
111
|
+
break;
|
|
112
|
+
case 'Number':
|
|
113
|
+
revivedValue = Number(message.value);
|
|
114
|
+
break;
|
|
115
|
+
case 'String':
|
|
116
|
+
default:
|
|
117
|
+
revivedValue = message.value;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
revivedValue = deserializer.convertSingleValue(objMemberValue, objMemberMetadata.type(), knownTypes, objMemberDebugName, objMemberOptions, serializerOptions);
|
|
123
|
+
}
|
|
124
|
+
if (core_1.TypedJSON.utils.isValueDefined(revivedValue)) {
|
|
125
|
+
sourceObjectWithDeserializedProperties[objMemberMetadata.key] = revivedValue;
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
// Next, instantiate target object.
|
|
129
|
+
const targetObject = deserializer.instantiateType(expectedSelfType);
|
|
130
|
+
// Finally, assign deserialized properties to target object.
|
|
131
|
+
Object.assign(targetObject, sourceObjectWithDeserializedProperties);
|
|
132
|
+
// Call onDeserialized method (if any).
|
|
133
|
+
const methodName = sourceObjectMetadata.onDeserializedMethodName;
|
|
134
|
+
if (methodName != null) {
|
|
135
|
+
if (typeof targetObject[methodName] === 'function') {
|
|
136
|
+
// check for member first
|
|
137
|
+
targetObject[methodName]();
|
|
138
|
+
}
|
|
139
|
+
else if (typeof targetObject.constructor[methodName] === 'function') {
|
|
140
|
+
// check for static
|
|
141
|
+
targetObject.constructor[methodName]();
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
deserializer.getErrorHandler()(new TypeError(`onDeserialized callback` +
|
|
145
|
+
`'${core_1.TypedJSON.utils.nameof(sourceObjectMetadata.classType)}.${methodName}' is not a method.`));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return targetObject;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
exports.InternalProtobufDeserializer = InternalProtobufDeserializer;
|
|
152
|
+
//# sourceMappingURL=InternalProtobufDeserializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InternalProtobufDeserializer.js","sourceRoot":"","sources":["../../src/InternalProtobufDeserializer.ts"],"names":[],"mappings":";;;;AAAA,wCAWuB;AACvB,6DAAuC;AACvC,yCAAqD;AAErD,MAAa,4BAA6B,SAAQ,mBAAY;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,QAAQ,EAAE,CAAC;gBACpC,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC;YAC1B,CAAC;iBAAM,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;gBACzB,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;YAC7B,CAAC;YACD,OAAO,MAAM,CAAC;QAClB,CAAC,CAAC,CAAC;QACH,IAAI,CAAC,0BAA0B,CAC3B,GAAG,EACH,CACI,MAAW,EACX,cAAiC,EACjC,UAA0C,EAC1C,UAAkB,EAClB,YAA0B,EAC1B,aAAoC,EACpC,iBAAuB,EACzB,EAAE;YACA,MAAM,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;YACzB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;gBAChC,MAAM,C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AkB,EAClB,gBAAgB,EAChB,iBAAiB,CACpB,CAAC;YACN,CAAC;YAED,IAAI,gBAAS,CAAC,KAAK,CAAC,cAAc,CAAC,YAAY,CAAC,EAAE,CAAC;gBAC/C,sCAAsC,CAAC,iBAAiB,CAAC,GAAG,CAAC,GAAG,YAAY,CAAC;YACjF,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,mCAAmC;QACnC,MAAM,YAAY,GAAkB,YAAY,CAAC,eAAe,CAAC,gBAAgB,CAAC,CAAC;QAEnF,4DAA4D;QAC5D,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,sCAAsC,CAAC,CAAC;QAEpE,uCAAuC;QACvC,MAAM,UAAU,GAAG,oBAAoB,CAAC,wBAAwB,CAAC;QACjE,IAAI,UAAU,IAAI,IAAI,EAAE,CAAC;YACrB,IAAI,OAAQ,YAAoB,CAAC,UAAU,CAAC,KAAK,UAAU,EAAE,CAAC;gBAC1D,yBAAyB;gBACxB,YAAoB,CAAC,UAAU,CAAC,EAAE,CAAC;YACxC,CAAC;iBAAM,IAAI,OAAQ,YAAY,CAAC,WAAmB,CAAC,UAAU,CAAC,KAAK,UAAU,EAAE,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,gBAAS,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;AArOD,oEAqOC"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InternalProtobufSerializer = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const core_1 = require("@openhps/core");
|
|
6
|
+
const typedjson_1 = require("typedjson");
|
|
7
|
+
const protobuf = tslib_1.__importStar(require("protobufjs"));
|
|
8
|
+
class InternalProtobufSerializer extends core_1.Serializer {
|
|
9
|
+
constructor() {
|
|
10
|
+
super();
|
|
11
|
+
this.setSerializationStrategy(Map, this.convertAsMap.bind(this));
|
|
12
|
+
InternalProtobufSerializer.primitiveWrapper = new protobuf.Type('PrimitiveWrapperMessage');
|
|
13
|
+
InternalProtobufSerializer.primitiveWrapper.add(new protobuf.Field('value', 1, 'string'));
|
|
14
|
+
}
|
|
15
|
+
convertSingleValue(sourceObject, typeDescriptor, memberName, memberOptions, serializerOptions) {
|
|
16
|
+
if (!core_1.TypedJSON.utils.isValueDefined(sourceObject)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (!core_1.TypedJSON.utils.isInstanceOf(sourceObject, typeDescriptor.ctor)) {
|
|
20
|
+
const expectedName = core_1.TypedJSON.utils.nameof(typeDescriptor.ctor);
|
|
21
|
+
const actualName = core_1.TypedJSON.utils.nameof(sourceObject.constructor);
|
|
22
|
+
this.errorHandler(new TypeError(`Could not serialize '${memberName}': expected '${expectedName}',` + ` got '${actualName}'.`));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const serializer = this.serializationStrategy.get(typeDescriptor.ctor);
|
|
26
|
+
if (serializer !== undefined) {
|
|
27
|
+
return serializer(sourceObject, typeDescriptor, memberName, this, memberOptions, serializerOptions);
|
|
28
|
+
}
|
|
29
|
+
// if not present in the strategy do property by property serialization
|
|
30
|
+
if (typeof sourceObject === 'object') {
|
|
31
|
+
return this.convertAsObject(sourceObject, typeDescriptor, memberName, this, memberOptions, serializerOptions);
|
|
32
|
+
}
|
|
33
|
+
let error = `Could not serialize '${memberName}'; don't know how to serialize type`;
|
|
34
|
+
if (typeDescriptor.hasFriendlyName()) {
|
|
35
|
+
error += ` '${typeDescriptor.ctor.name}'`;
|
|
36
|
+
}
|
|
37
|
+
this.errorHandler(new TypeError(`${error}.`));
|
|
38
|
+
}
|
|
39
|
+
convertAsObject(sourceObject, typeDescriptor, memberName, serializer, memberOptions, serializerOptions) {
|
|
40
|
+
const typeMetadata = typedjson_1.JsonObjectMetadata.getFromConstructor(typeDescriptor.ctor);
|
|
41
|
+
let sourceTypeMetadata = typedjson_1.JsonObjectMetadata.getFromConstructor(sourceObject.constructor);
|
|
42
|
+
if (!sourceTypeMetadata) {
|
|
43
|
+
sourceTypeMetadata = typeMetadata;
|
|
44
|
+
}
|
|
45
|
+
let targetObject;
|
|
46
|
+
const sourceMeta = sourceTypeMetadata;
|
|
47
|
+
targetObject = {};
|
|
48
|
+
const classOptions = core_1.TypedJSON.options.mergeOptions(serializer.options, sourceMeta.options);
|
|
49
|
+
sourceMeta.dataMembers.forEach((objMemberMetadata) => {
|
|
50
|
+
var _a;
|
|
51
|
+
const objMemberOptions = core_1.TypedJSON.options.mergeOptions(classOptions, objMemberMetadata.options);
|
|
52
|
+
let serialized;
|
|
53
|
+
let memberName = (_a = (objMemberOptions && objMemberOptions.protobuf
|
|
54
|
+
? objMemberOptions.protobuf.name
|
|
55
|
+
: objMemberMetadata.name)) !== null && _a !== void 0 ? _a : objMemberMetadata.name;
|
|
56
|
+
if (sourceObject[objMemberMetadata.key] === undefined) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (objMemberMetadata.type == null) {
|
|
60
|
+
throw new TypeError(`Could not serialize ${objMemberMetadata.name}, there is` +
|
|
61
|
+
` no constructor nor serialization function to use.`);
|
|
62
|
+
}
|
|
63
|
+
else if (objMemberMetadata.name === 'uid') {
|
|
64
|
+
const uuid = core_1.UUID.fromString(sourceObject[objMemberMetadata.key]);
|
|
65
|
+
if (uuid) {
|
|
66
|
+
serialized = uuid.toBuffer();
|
|
67
|
+
memberName += '_bytes';
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
serialized = sourceObject[objMemberMetadata.key];
|
|
71
|
+
memberName += '_string';
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const MessageType = sourceTypeMetadata.protobuf.messageType;
|
|
76
|
+
const field = MessageType.get(objMemberMetadata.name);
|
|
77
|
+
serialized = this.convertSingleValue(sourceObject[objMemberMetadata.key], objMemberMetadata.type(), `${core_1.TypedJSON.utils.nameof(sourceMeta.classType)}.${objMemberMetadata.key}`, objMemberOptions, Object.assign(Object.assign({}, serializerOptions), { field }));
|
|
78
|
+
}
|
|
79
|
+
if (core_1.TypedJSON.utils.isValueDefined(serialized)) {
|
|
80
|
+
if (objMemberMetadata.type() === typedjson_1.AnyT) {
|
|
81
|
+
const MessageType = serializerOptions.types.get(sourceObject[objMemberMetadata.key].constructor.name);
|
|
82
|
+
if (MessageType) {
|
|
83
|
+
const message = MessageType.create(serialized);
|
|
84
|
+
serialized = {
|
|
85
|
+
type_url: sourceObject[objMemberMetadata.key].constructor.name,
|
|
86
|
+
value: MessageType.encode(message).finish(),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
const message = InternalProtobufSerializer.primitiveWrapper.create({
|
|
91
|
+
value: String(sourceObject[objMemberMetadata.key]),
|
|
92
|
+
});
|
|
93
|
+
serialized = {
|
|
94
|
+
type_url: sourceObject[objMemberMetadata.key].constructor.name,
|
|
95
|
+
value: InternalProtobufSerializer.primitiveWrapper.encode(message).finish(),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
targetObject[memberName] = serialized;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
const isAnyField = serializerOptions.field && serializerOptions.field.type === 'google.protobuf.Any';
|
|
103
|
+
if (!isAnyField && typeMetadata.knownTypes.size > 1) {
|
|
104
|
+
const MessageType = sourceTypeMetadata.protobuf.messageType;
|
|
105
|
+
const message = MessageType.create(Object.assign(Object.assign({}, targetObject), { _type: sourceTypeMetadata.protobuf.messageTypeEnum }));
|
|
106
|
+
targetObject = message;
|
|
107
|
+
}
|
|
108
|
+
else if (isAnyField) {
|
|
109
|
+
const MessageType = sourceTypeMetadata.protobuf.messageType;
|
|
110
|
+
const message = MessageType.create(targetObject);
|
|
111
|
+
targetObject = {
|
|
112
|
+
type_url: sourceObject.constructor.name,
|
|
113
|
+
value: MessageType.encode(message).finish(),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
return targetObject;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Performs the conversion of a map of typed objects (or primitive values) into an array
|
|
120
|
+
* of simple javascript objects with `key` and `value` properties.
|
|
121
|
+
* @param sourceObject
|
|
122
|
+
* @param typeDescriptor
|
|
123
|
+
* @param memberName
|
|
124
|
+
* @param serializer
|
|
125
|
+
* @param memberOptions
|
|
126
|
+
* @param serializerOptions
|
|
127
|
+
*/
|
|
128
|
+
convertAsMap(sourceObject, typeDescriptor, memberName, serializer, memberOptions, serializerOptions) {
|
|
129
|
+
const result = {};
|
|
130
|
+
sourceObject.forEach((value, key) => {
|
|
131
|
+
result[key] = serializer.convertSingleValue(value, typeDescriptor.valueType, memberName, memberOptions, serializerOptions);
|
|
132
|
+
});
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.InternalProtobufSerializer = InternalProtobufSerializer;
|
|
137
|
+
//# sourceMappingURL=InternalProtobufSerializer.js.map
|