protobufjs 8.5.0 → 8.6.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/ext/README.md CHANGED
@@ -2,14 +2,14 @@
2
2
 
3
3
  ## descriptor
4
4
 
5
- Optional `google/protobuf/descriptor.proto` interoperability layer for reflected roots and objects.
5
+ Optional `descriptor.proto` support for converting reflected protobuf.js roots and objects to and from descriptor messages.
6
6
 
7
7
  ```js
8
8
  import protobuf from "protobufjs";
9
9
  import descriptor from "protobufjs/ext/descriptor.js";
10
10
 
11
11
  // Convert an existing root to a FileDescriptorSet message.
12
- const root = protobuf.Root.fromJSON(bundle);
12
+ const root = ...;
13
13
  const set = root.toDescriptor("proto2");
14
14
 
15
15
  // Encode descriptor buffers.
@@ -21,61 +21,50 @@ const decodedRoot = protobuf.Root.fromDescriptor(buffer);
21
21
 
22
22
  The extension requires reflection metadata and also works with `protobufjs/light.js` when schemas are loaded from JSON or otherwise provided as reflection objects.
23
23
 
24
- Importing the extension adds `.fromDescriptor(descriptor[, syntaxOrEdition])` and `#toDescriptor([syntaxOrEdition])` methods to reflection objects and exports reflected descriptor types from `google.protobuf`. Descriptor inputs can be decoded messages, readers, or `Uint8Array`s for the corresponding descriptor message.
25
-
26
- | Descriptor type | protobuf.js type | Remarks |
27
- |-------------------------------|------------------|---------|
28
- | **FileDescriptorSet** | Root | |
29
- | FileDescriptorProto | | dependencies and source info are ignored on input and not emitted |
30
- | FileOptions | | |
31
- | FileOptionsOptimizeMode | | |
32
- | SourceCodeInfo | | exported descriptor type; not mapped to reflection |
33
- | SourceCodeInfoLocation | | |
34
- | GeneratedCodeInfo | | exported descriptor type; not mapped to reflection |
35
- | GeneratedCodeInfoAnnotation | | |
36
- | **DescriptorProto** | Type | |
37
- | MessageOptions | | |
38
- | DescriptorProtoExtensionRange | | |
39
- | DescriptorProtoReservedRange | | |
40
- | **FieldDescriptorProto** | Field / MapField | map entries are reconstructed as map fields |
41
- | FieldDescriptorProtoLabel | | |
42
- | FieldDescriptorProtoType | | |
43
- | FieldOptions | | |
44
- | FieldOptionsCType | | |
45
- | FieldOptionsJSType | | |
46
- | **OneofDescriptorProto** | OneOf | |
47
- | OneofOptions | | |
48
- | **EnumDescriptorProto** | Enum | |
49
- | EnumOptions | | |
50
- | EnumValueDescriptorProto | | |
51
- | EnumValueOptions | | |
52
- | **ServiceDescriptorProto** | Service | |
53
- | ServiceOptions | | |
54
- | **MethodDescriptorProto** | Method | |
55
- | MethodOptions | | |
56
- | FeatureSet | | exported descriptor type; used by edition-aware options |
57
- | FeatureSetDefaults | | exported descriptor type |
58
- | UninterpretedOption | | exported descriptor type; options are not interpreted |
59
- | UninterpretedOptionNamePart | | |
60
-
61
- Not all `descriptor.proto` features translate perfectly to a protobuf.js root. A root has only limited knowledge of packages and individual files, for example, which is compensated by guessing and generating file names.
62
-
63
- The exported TypeScript interfaces can be used to reference specific descriptor message instances, for example `protobuf.Message<IDescriptorProto>`.
24
+ Importing the extension adds `.fromDescriptor(descriptor[, syntaxOrEdition])` and `#toDescriptor([syntaxOrEdition])` methods to reflection objects and exports the bundled descriptor types from `google.protobuf`. Descriptor inputs can be decoded messages, readers, or buffers of the corresponding descriptor messages.
25
+
26
+ The conversion covers descriptor messages that correspond to protobuf.js reflection objects: files and file sets, messages, fields and map fields, oneofs, enums, services and methods. Descriptor-only metadata such as source locations, generated-code annotations and uninterpreted options remains available through the exported descriptor message types, but is not mapped onto reflection objects. File names are inferred when generating descriptors because roots do not retain exact file/package boundaries.
27
+
28
+ ## protojson
29
+
30
+ Optional ProtoJSON support for reflected message types.
31
+
32
+ > [!NOTE]
33
+ > Specialized code generation for the `pbjs` static-module target is a potential future extension. If you need this for high-throughput JSON transcoding or REST fallbacks in production, consider getting in touch and supporting the codegen and conformance work.
34
+
35
+ ```js
36
+ import protobuf from "protobufjs";
37
+ import protojson from "protobufjs/ext/protojson.js";
38
+
39
+ const root = ...;
40
+ const MyType = root.lookupType("MyType");
41
+ const message = protojson.fromJson(MyType, { value: 1 });
42
+ const json = protojson.toJson(MyType, message);
43
+ ```
44
+
45
+ ```js
46
+ const messageFromString = protojson.fromJsonString(MyType, '{"value":1}');
47
+ const jsonString = protojson.toJsonString(MyType, messageFromString);
48
+ ```
49
+
50
+ Importing the extension has no prototype side effects. Calling `protojson.install()` installs `fromJson`, `fromJsonString`, `toJson` and `toJsonString` convenience methods on `protobuf.Type.prototype`. It works with `protobufjs/light.js` when schemas are loaded from JSON or otherwise provided as reflection objects.
51
+
52
+ Unknown fields can be ignored while parsing by passing `{ ignoreUnknownFields: true }` to `fromJson` or `fromJsonString`.
64
53
 
65
54
  ## textformat
66
55
 
67
- Optional text format support for reflected message types.
56
+ Optional Text Format support for reflected message types.
68
57
 
69
58
  ```js
70
59
  import protobuf from "protobufjs";
71
- import "protobufjs/ext/textformat.js";
60
+ import textformat from "protobufjs/ext/textformat.js";
72
61
 
73
- const root = protobuf.Root.fromJSON(bundle);
62
+ const root = ...;
74
63
  const MyType = root.lookupType("MyType");
75
- const message = MyType.fromText("value: 1");
76
- const text = MyType.toText(message);
64
+ const message = textformat.fromText(MyType, "value: 1");
65
+ const text = textformat.toText(MyType, message);
77
66
  ```
78
67
 
79
- The extension patches `protobuf.Type` at runtime and requires reflection metadata. It works with `protobufjs/light.js` when schemas are loaded from JSON or otherwise provided as reflection objects. Static-only code using `protobufjs/minimal.js` is not supported.
68
+ Importing the extension has no prototype side effects. Calling `textformat.install()` installs `fromText` and `toText` convenience methods on `protobuf.Type.prototype`. It works with `protobufjs/light.js` when schemas are loaded from JSON or otherwise provided as reflection objects.
80
69
 
81
- Text output is produced from the message object passed to `toText`. Unknown fields can be printed with numeric field names by passing `{ unknowns: true }` to `toText`.
70
+ Unknown fields can be printed with numeric field names by passing `{ unknowns: true }` to `toText`.
@@ -258,8 +258,8 @@ export interface IFieldDescriptorProto {
258
258
  /** Oneof index if part of a oneof */
259
259
  oneofIndex?: number;
260
260
 
261
- /** Not supported */
262
- jsonName?: any;
261
+ /** JSON name (lowerCamelCase) */
262
+ jsonName?: string;
263
263
 
264
264
  /** Field options */
265
265
  options?: IFieldOptions;
package/ext/descriptor.js CHANGED
@@ -363,7 +363,7 @@ function FieldBase_fromDescriptor(descriptor, edition, nested, mapEntries) {
363
363
  * @property {string} [extendee] Extended type name
364
364
  * @property {string} [defaultValue] Literal default value
365
365
  * @property {number} [oneofIndex] Oneof index if part of a oneof
366
- * @property {*} [jsonName] Not supported
366
+ * @property {string} [jsonName] JSON name (lowerCamelCase)
367
367
  * @property {IFieldOptions} [options] Field options
368
368
  * @property {boolean} [proto3Optional] Whether this is a proto3 optional field
369
369
  */
@@ -460,13 +460,22 @@ Field.fromDescriptor = function fromDescriptor(descriptor, edition, nested) {
460
460
  throw Error("illegal type name: " + extendee);
461
461
  } else
462
462
  extendee = undefined;
463
+ // Prefer json_name as the JS field name when present, while retaining both
464
+ // descriptor names for round-tripping and ProtoJSON lookup.
465
+ var fieldName = descriptor.jsonName && descriptor.jsonName.length
466
+ ? descriptor.jsonName
467
+ : descriptor.name.length ? descriptor.name : "field" + descriptor.number;
463
468
  var field = new Field(
464
- descriptor.name.length ? descriptor.name : "field" + descriptor.number,
469
+ fieldName,
465
470
  descriptor.number,
466
471
  fieldType,
467
472
  fieldRule,
468
473
  extendee
469
474
  );
475
+ if (descriptor.name.length && descriptor.name !== fieldName)
476
+ field.protoName = descriptor.name;
477
+ if (descriptor.jsonName && descriptor.jsonName.length)
478
+ field.jsonName = descriptor.jsonName;
470
479
 
471
480
  if (!nested)
472
481
  field._edition = edition;
@@ -536,7 +545,13 @@ function MapField_fromDescriptor(descriptor, entryDescriptor) {
536
545
  * @param {string} [edition="proto2"] The syntax or edition to use
537
546
  */
538
547
  Field.prototype.toDescriptor = function toDescriptor(edition) {
539
- var descriptor = exports.FieldDescriptorProto.create({ name: this.name, number: this.id });
548
+ // Emit descriptor names in FieldDescriptorProto form, including json_name only
549
+ // when it differs, and derive missing names so unresolved fields serialize consistently.
550
+ var protoName = this.protoName || this.name;
551
+ var jsonName = this.jsonName || $protobuf.util.jsonName(protoName);
552
+ var descriptor = exports.FieldDescriptorProto.create({ name: protoName, number: this.id });
553
+ if (jsonName !== protoName)
554
+ descriptor.jsonName = jsonName;
540
555
 
541
556
  if (this.map) {
542
557
 
@@ -0,0 +1,201 @@
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
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,20 @@
1
+ import * as $protobuf from "..";
2
+ import { IProtoJsonOptions } from "./protojson.generated";
3
+
4
+ export * from "./protojson.generated";
5
+
6
+ declare module ".." {
7
+ interface Type {
8
+ /** Installed by `protojson.install()`. Parses an already-parsed ProtoJSON value. */
9
+ fromJson(json: any, options?: IProtoJsonOptions): $protobuf.Message<{}>;
10
+
11
+ /** Installed by `protojson.install()`. Parses ProtoJSON text. */
12
+ fromJsonString(json: string, options?: IProtoJsonOptions): $protobuf.Message<{}>;
13
+
14
+ /** Installed by `protojson.install()`. Formats a message as ProtoJSON. */
15
+ toJson(message: ($protobuf.Message<{}>|{ [k: string]: any }), options?: IProtoJsonOptions): any;
16
+
17
+ /** Installed by `protojson.install()`. Formats a message as ProtoJSON text. */
18
+ toJsonString(message: ($protobuf.Message<{}>|{ [k: string]: any }), options?: IProtoJsonOptions): string;
19
+ }
20
+ }
@@ -0,0 +1,49 @@
1
+ // DO NOT EDIT! This is a generated file. Edit the source file instead and regenerate.
2
+
3
+ import * as $protobuf from "..";
4
+
5
+ /** ProtoJSON conversion options. */
6
+ export interface IProtoJsonOptions {
7
+
8
+ /** Ignores unknown object members and unrecognized enum names while parsing. */
9
+ ignoreUnknownFields?: boolean;
10
+ }
11
+
12
+ /**
13
+ * Parses a message from an already-parsed ProtoJSON value using the specified reflected type.
14
+ * @param type Reflected message type
15
+ * @param json Already-parsed ProtoJSON value
16
+ * @param [options] Conversion options
17
+ * @returns Message instance
18
+ */
19
+ export function fromJson(type: $protobuf.Type, json: any, options?: IProtoJsonOptions): $protobuf.Message<{}>;
20
+
21
+ /**
22
+ * Parses a message from ProtoJSON text using the specified reflected type.
23
+ * @param type Reflected message type
24
+ * @param json ProtoJSON text
25
+ * @param [options] Conversion options
26
+ * @returns Message instance
27
+ */
28
+ export function fromJsonString(type: $protobuf.Type, json: string, options?: IProtoJsonOptions): $protobuf.Message<{}>;
29
+
30
+ /**
31
+ * Formats a message as ProtoJSON using the specified reflected type.
32
+ * @param type Reflected message type
33
+ * @param message Message instance or plain object
34
+ * @param [options] Conversion options
35
+ * @returns ProtoJSON value (object, array, string, number, boolean or null)
36
+ */
37
+ export function toJson(type: $protobuf.Type, message: ($protobuf.Message<{}>|{ [k: string]: any }), options?: IProtoJsonOptions): any;
38
+
39
+ /**
40
+ * Formats a message as ProtoJSON text using the specified reflected type.
41
+ * @param type Reflected message type
42
+ * @param message Message instance or plain object
43
+ * @param [options] Conversion options
44
+ * @returns ProtoJSON text
45
+ */
46
+ export function toJsonString(type: $protobuf.Type, message: ($protobuf.Message<{}>|{ [k: string]: any }), options?: IProtoJsonOptions): string;
47
+
48
+ /** Installs reflected {@link Type} convenience methods. */
49
+ export function install(): void;