protobufjs 8.5.0 → 8.6.1

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[, editionOrContext])` 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. Direct object descriptor imports accept either an edition string or a descriptor context with `edition`, `features` and `keepCase`.
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`.
@@ -1,6 +1,7 @@
1
1
  import * as $protobuf from "..";
2
2
  import {
3
3
  IDescriptorProto,
4
+ IDescriptorContext,
4
5
  IEnumDescriptorProto,
5
6
  IFieldDescriptorProto,
6
7
  IFileDescriptorSet,
@@ -16,7 +17,7 @@ type DescriptorInput<T> = T | $protobuf.Reader | Uint8Array;
16
17
  declare module ".." {
17
18
  namespace Root {
18
19
  /** Creates a root from a descriptor set. */
19
- function fromDescriptor(descriptor: DescriptorInput<IFileDescriptorSet>): $protobuf.Root;
20
+ function fromDescriptor(descriptor: DescriptorInput<IFileDescriptorSet>, options?: { keepCase?: boolean }): $protobuf.Root;
20
21
  }
21
22
 
22
23
  interface Root {
@@ -26,7 +27,7 @@ declare module ".." {
26
27
 
27
28
  namespace Type {
28
29
  /** Creates a type from a descriptor. */
29
- function fromDescriptor(descriptor: DescriptorInput<IDescriptorProto>, edition?: string, nested?: boolean): $protobuf.Type;
30
+ function fromDescriptor(descriptor: DescriptorInput<IDescriptorProto>, editionOrContext?: string | IDescriptorContext): $protobuf.Type;
30
31
  }
31
32
 
32
33
  interface Type {
@@ -36,7 +37,7 @@ declare module ".." {
36
37
 
37
38
  namespace Field {
38
39
  /** Creates a field from a descriptor. */
39
- function fromDescriptor(descriptor: DescriptorInput<IFieldDescriptorProto>, edition?: string, nested?: boolean): $protobuf.Field;
40
+ function fromDescriptor(descriptor: DescriptorInput<IFieldDescriptorProto>, editionOrContext?: string | IDescriptorContext): $protobuf.Field;
40
41
  }
41
42
 
42
43
  interface Field {
@@ -46,7 +47,7 @@ declare module ".." {
46
47
 
47
48
  namespace Enum {
48
49
  /** Creates an enum from a descriptor. */
49
- function fromDescriptor(descriptor: DescriptorInput<IEnumDescriptorProto>, edition?: string, nested?: boolean): $protobuf.Enum;
50
+ function fromDescriptor(descriptor: DescriptorInput<IEnumDescriptorProto>, editionOrContext?: string | IDescriptorContext): $protobuf.Enum;
50
51
  }
51
52
 
52
53
  interface Enum {
@@ -56,7 +57,7 @@ declare module ".." {
56
57
 
57
58
  namespace OneOf {
58
59
  /** Creates a oneof from a descriptor. */
59
- function fromDescriptor(descriptor: DescriptorInput<IOneofDescriptorProto>): $protobuf.OneOf;
60
+ function fromDescriptor(descriptor: DescriptorInput<IOneofDescriptorProto>, editionOrContext?: string | IDescriptorContext): $protobuf.OneOf;
60
61
  }
61
62
 
62
63
  interface OneOf {
@@ -66,7 +67,7 @@ declare module ".." {
66
67
 
67
68
  namespace Service {
68
69
  /** Creates a service from a descriptor. */
69
- function fromDescriptor(descriptor: DescriptorInput<IServiceDescriptorProto>, edition?: string, nested?: boolean): $protobuf.Service;
70
+ function fromDescriptor(descriptor: DescriptorInput<IServiceDescriptorProto>, editionOrContext?: string | IDescriptorContext): $protobuf.Service;
70
71
  }
71
72
 
72
73
  interface Service {
@@ -76,7 +77,7 @@ declare module ".." {
76
77
 
77
78
  namespace Method {
78
79
  /** Creates a method from a descriptor. */
79
- function fromDescriptor(descriptor: DescriptorInput<IMethodDescriptorProto>): $protobuf.Method;
80
+ function fromDescriptor(descriptor: DescriptorInput<IMethodDescriptorProto>, editionOrContext?: string | IDescriptorContext): $protobuf.Method;
80
81
  }
81
82
 
82
83
  interface Method {
@@ -170,6 +170,19 @@ export interface IFileOptions {
170
170
  /** Values of he FileOptions.OptimizeMode enum. */
171
171
  export type IFileOptionsOptimizeMode = number;
172
172
 
173
+ /** Descriptor context. */
174
+ export interface IDescriptorContext {
175
+
176
+ /** Syntax or edition to use for direct object descriptor imports */
177
+ edition?: string;
178
+
179
+ /** File-level features to apply for direct object descriptor imports */
180
+ features?: IFeatureSet;
181
+
182
+ /** Uses proto field names as reflected field names */
183
+ keepCase?: boolean;
184
+ }
185
+
173
186
  /** Properties of a DescriptorProto message. */
174
187
  export interface IDescriptorProto {
175
188
 
@@ -258,8 +271,8 @@ export interface IFieldDescriptorProto {
258
271
  /** Oneof index if part of a oneof */
259
272
  oneofIndex?: number;
260
273
 
261
- /** Not supported */
262
- jsonName?: any;
274
+ /** JSON name (lowerCamelCase) */
275
+ jsonName?: string;
263
276
 
264
277
  /** Field options */
265
278
  options?: IFieldOptions;
@@ -398,12 +411,7 @@ export interface IMethodDescriptorProto {
398
411
  serverStreaming?: boolean;
399
412
  }
400
413
 
401
- /**
402
- * Properties of a MethodOptions message.
403
- *
404
- * Warning: this is not safe to use with editions protos, since it discards relevant file context.
405
- *
406
- */
414
+ /** Properties of a MethodOptions message. */
407
415
  export interface IMethodOptions {
408
416
  deprecated?: boolean;
409
417
  }