@aptre/protobuf-es-lite 0.2.14 → 0.3.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.
Files changed (37) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +10 -15
  3. package/dist/binary.d.ts +13 -10
  4. package/dist/binary.js +24 -23
  5. package/dist/codegen-info.d.ts +8 -0
  6. package/dist/codegen-info.js +12 -4
  7. package/dist/create-descriptor-set.js +1 -1
  8. package/dist/descriptor-set.d.ts +8 -9
  9. package/dist/google/protobuf/any.pb.d.ts +55 -2
  10. package/dist/google/protobuf/any.pb.js +74 -4
  11. package/dist/google/protobuf/api.pb.js +13 -10
  12. package/dist/google/protobuf/descriptor.pb.js +125 -93
  13. package/dist/google/protobuf/duration.pb.d.ts +7 -2
  14. package/dist/google/protobuf/duration.pb.js +54 -4
  15. package/dist/google/protobuf/empty.pb.js +1 -0
  16. package/dist/google/protobuf/source_context.pb.js +3 -2
  17. package/dist/google/protobuf/struct.pb.d.ts +17 -4
  18. package/dist/google/protobuf/struct.pb.js +104 -8
  19. package/dist/google/protobuf/timestamp.pb.d.ts +8 -2
  20. package/dist/google/protobuf/timestamp.pb.js +54 -4
  21. package/dist/google/protobuf/type.pb.js +21 -16
  22. package/dist/google/protobuf/wrappers.pb.d.ts +47 -10
  23. package/dist/google/protobuf/wrappers.pb.js +190 -19
  24. package/dist/index.d.ts +5 -1
  25. package/dist/index.js +4 -1
  26. package/dist/json.d.ts +30 -4
  27. package/dist/json.js +17 -16
  28. package/dist/message.d.ts +8 -15
  29. package/dist/message.js +75 -71
  30. package/dist/protoc-gen-es-lite/typescript.d.ts +2 -2
  31. package/dist/protoc-gen-es-lite/typescript.js +323 -26
  32. package/dist/protoplugin/ecmascript/reify-wkt.d.ts +1 -5
  33. package/dist/protoplugin/ecmascript/reify-wkt.js +0 -10
  34. package/dist/type-registry.d.ts +43 -0
  35. package/dist/type-registry.js +14 -0
  36. package/example/example.pb.ts +6 -7
  37. package/package.json +3 -1
package/LICENSE CHANGED
@@ -188,7 +188,7 @@
188
188
  identification within third-party archives.
189
189
 
190
190
  Copyright 2024 Aperture Robotics, LLC.
191
- Copyright 2021-2023 Buf Technologies, Inc.
191
+ Copyright 2021-2024 Buf Technologies, Inc.
192
192
 
193
193
  Licensed under the Apache License, Version 2.0 (the "License");
194
194
  you may not use this file except in compliance with the License.
package/README.md CHANGED
@@ -1,11 +1,7 @@
1
1
  ## protobuf-es-lite
2
2
 
3
- [![GoDoc Widget]][GoDoc] [![Go Report Card Widget]][Go Report Card]
4
-
5
- [GoDoc]: https://godoc.org/github.com/aperturerobotics/protobuf-es-lite
6
- [GoDoc Widget]: https://godoc.org/github.com/aperturerobotics/protobuf-es-lite?status.svg
7
- [Go Report Card Widget]: https://goreportcard.com/badge/github.com/aperturerobotics/protobuf-es-lite
8
- [Go Report Card]: https://goreportcard.com/report/github.com/aperturerobotics/protobuf-es-lite
3
+ [![npm version](https://img.shields.io/npm/v/@aptre/protobuf-es-lite.svg)](https://www.npmjs.com/package/@aptre/protobuf-es-lite)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@aptre/protobuf-es-lite.svg)](https://www.npmjs.com/package/@aptre/protobuf-es-lite)
9
5
 
10
6
  protobuf-es-lite is a TypeScript and JavaScript protobuf implementation.
11
7
 
@@ -33,11 +29,12 @@ Lightweight Protobuf 3 RPCs are implemented in [StaRPC] for Go and TypeScript.
33
29
 
34
30
  ## Purpose
35
31
 
36
- The main protoc-gen-es generates a class for each message type. Partial messages
37
- or plain messages are available with the PartialMessage and PlainMessage
38
- typescript types. The problem with this is that most js applications would
39
- usually construct and pass around PlainMessage in most cases, so we end up
40
- having to write the PlainMessage type very frequently:
32
+ [protobuf-es] generates a class for each message with interfaces for partial or
33
+ plain messages. It is significantly easier to construct and pass around plain
34
+ and partial messages as opposed to using classes, and plain messages work better
35
+ when used in React props/state. This ends up looking like:
36
+
37
+ [protobuf-es]: https://github.com/bufbuild/protobuf-es
41
38
 
42
39
  ```typescript
43
40
  const myMessage: PlainMessage<MyMessage> = {body: "Hello world"}
@@ -46,12 +43,10 @@ const myMessageBin = new MyMessage(myMessage).toBinary()
46
43
 
47
44
  [ts-proto] generates an interface for each protobuf message and a const message
48
45
  declaration object containing the marshal and unmarshal functions. This allows
49
- using interfaces and plain messages everywhere and does not need classes.
46
+ using interfaces and plain messages everywhere:
50
47
 
51
48
  [ts-proto]: https://github.com/stephenh/ts-proto
52
49
 
53
- This fork generates the ts-proto style with the protoc-gen-es tools:
54
-
55
50
  ```typescript
56
51
  // Create a partial MyMessage with just one field sets.
57
52
  const myMessage: MyMessage = {body: "Hello world"}
@@ -61,7 +56,7 @@ const myCompleteMessage: MyMessage = MyMessage.create(myMessage)
61
56
  const myMessageBin = MyMessage.toBinary(myCompleteMessage)
62
57
  ```
63
58
 
64
- **Note that the default Message is equivalent to PartialMessage<T> from protobuf-es.**
59
+ This fork generates the ts-proto style with the protobuf-es code generator.
65
60
 
66
61
  ## Installation
67
62
 
package/dist/binary.d.ts CHANGED
@@ -33,7 +33,9 @@ export interface BinaryWriteOptions {
33
33
  */
34
34
  writerFactory: () => IBinaryWriter;
35
35
  }
36
- export declare function readField(target: Record<string, any>, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
36
+ declare function makeReadOptions(options?: Partial<BinaryReadOptions>): Readonly<BinaryReadOptions>;
37
+ declare function makeWriteOptions(options?: Partial<BinaryWriteOptions>): Readonly<BinaryWriteOptions>;
38
+ declare function readField(target: Record<string, any>, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
37
39
  reader: IBinaryReader, field: FieldInfo, wireType: WireType, options: BinaryReadOptions): void;
38
40
  /**
39
41
  * AnyMessage is an interface implemented by all messages. If you need to
@@ -43,19 +45,20 @@ reader: IBinaryReader, field: FieldInfo, wireType: WireType, options: BinaryRead
43
45
  type AnyMessage = {
44
46
  [k: string]: any;
45
47
  };
46
- export declare function readMapEntry(field: FieldInfo & {
48
+ declare function readMapEntry(field: FieldInfo & {
47
49
  kind: "map";
48
50
  }, reader: IBinaryReader, options: BinaryReadOptions): [string | number, ScalarValue | AnyMessage | undefined];
49
- export declare function readScalar(reader: IBinaryReader, type: ScalarType): ScalarValue;
50
- export declare function readScalarLTString(reader: IBinaryReader, type: ScalarType): Exclude<ScalarValue, bigint>;
51
- export declare function makeReadOptions(options?: Partial<BinaryReadOptions>): Readonly<BinaryReadOptions>;
52
- export declare function makeWriteOptions(options?: Partial<BinaryWriteOptions>): Readonly<BinaryWriteOptions>;
53
- export declare function readMessage<T>(message: T, fields: FieldList, reader: IBinaryReader, lengthOrEndTagFieldNo: number, options: BinaryReadOptions, delimitedMessageEncoding?: boolean): void;
51
+ declare function readScalar(reader: IBinaryReader, type: ScalarType): ScalarValue;
52
+ declare function readScalarLTString(reader: IBinaryReader, type: ScalarType): Exclude<ScalarValue, bigint>;
53
+ declare function readMessage<T>(message: T, fields: FieldList, reader: IBinaryReader, lengthOrEndTagFieldNo: number, options: BinaryReadOptions, delimitedMessageEncoding?: boolean): void;
54
54
  /**
55
55
  * Serialize a message to binary data.
56
56
  */
57
- export declare function writeMessage<T>(message: T, fields: FieldList, writer: IBinaryWriter, options: BinaryWriteOptions): void;
58
- export declare function writeMapEntry(writer: IBinaryWriter, options: BinaryWriteOptions, field: FieldInfo & {
57
+ declare function writeMessage<T>(message: T, fields: FieldList, writer: IBinaryWriter, options: BinaryWriteOptions): void;
58
+ declare function writeField<T>(field: FieldInfo, value: T, writer: IBinaryWriter, options: BinaryWriteOptions): void;
59
+ declare function writeScalar(writer: IBinaryWriter, type: ScalarType, fieldNo: number, value: unknown): void;
60
+ declare function writePacked<T>(writer: IBinaryWriter, type: ScalarType, fieldNo: number, value: T[]): void;
61
+ declare function writeMapEntry(writer: IBinaryWriter, options: BinaryWriteOptions, field: FieldInfo & {
59
62
  kind: "map";
60
63
  }, key: string, value: any): void;
61
- export {};
64
+ export { readField as binaryReadField, readMapEntry as binaryReadMapEntry, readScalar as binaryReadScalar, readScalarLTString as binaryReadScalarLTString, readMessage as binaryReadMessage, writeField as binaryWriteField, writeScalar as binaryWriteScalar, writePacked as binaryWritePacked, writeMapEntry as binaryWriteMapEntry, writeMessage as binaryWriteMessage, makeReadOptions as binaryMakeReadOptions, makeWriteOptions as binaryMakeWriteOptions, };
package/dist/binary.js CHANGED
@@ -4,7 +4,23 @@ import { wrapField } from "./field-wrapper.js";
4
4
  import { LongType, ScalarType, scalarZeroValue, } from "./scalar.js";
5
5
  import { assert } from "./assert.js";
6
6
  import { BinaryReader, BinaryWriter, WireType, } from "./binary-encoding.js";
7
- export function readField(target, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
7
+ // Default options for parsing binary data.
8
+ const readDefaults = {
9
+ readUnknownFields: true,
10
+ readerFactory: (bytes) => new BinaryReader(bytes),
11
+ };
12
+ // Default options for serializing binary data.
13
+ const writeDefaults = {
14
+ writeUnknownFields: true,
15
+ writerFactory: () => new BinaryWriter(),
16
+ };
17
+ function makeReadOptions(options) {
18
+ return options ? { ...readDefaults, ...options } : readDefaults;
19
+ }
20
+ function makeWriteOptions(options) {
21
+ return options ? { ...writeDefaults, ...options } : writeDefaults;
22
+ }
23
+ function readField(target, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
8
24
  reader, field, wireType, options) {
9
25
  let { repeated, localName } = field;
10
26
  if (field.oneof) {
@@ -78,7 +94,7 @@ reader, field, wireType, options) {
78
94
  }
79
95
  }
80
96
  // Read a map field, expecting key field = 1, value field = 2
81
- export function readMapEntry(field, reader, options) {
97
+ function readMapEntry(field, reader, options) {
82
98
  const length = reader.uint32(), end = reader.pos + length;
83
99
  let key, val;
84
100
  while (reader.pos < end) {
@@ -125,7 +141,7 @@ export function readMapEntry(field, reader, options) {
125
141
  }
126
142
  return [key, val];
127
143
  }
128
- export function readScalar(reader, type) {
144
+ function readScalar(reader, type) {
129
145
  switch (type) {
130
146
  case ScalarType.STRING:
131
147
  return reader.string();
@@ -161,7 +177,7 @@ export function readScalar(reader, type) {
161
177
  }
162
178
  // Read a scalar value, but return 64 bit integral types (int64, uint64,
163
179
  // sint64, fixed64, sfixed64) as string instead of bigint.
164
- export function readScalarLTString(reader, type) {
180
+ function readScalarLTString(reader, type) {
165
181
  const v = readScalar(reader, type);
166
182
  return typeof v == "bigint" ? v.toString() : v;
167
183
  }
@@ -173,23 +189,7 @@ function readMessageField(reader, message, fields, options, field) {
173
189
  options, delimited);
174
190
  return message;
175
191
  }
176
- // Default options for parsing binary data.
177
- const readDefaults = {
178
- readUnknownFields: true,
179
- readerFactory: (bytes) => new BinaryReader(bytes),
180
- };
181
- // Default options for serializing binary data.
182
- const writeDefaults = {
183
- writeUnknownFields: true,
184
- writerFactory: () => new BinaryWriter(),
185
- };
186
- export function makeReadOptions(options) {
187
- return options ? { ...readDefaults, ...options } : readDefaults;
188
- }
189
- export function makeWriteOptions(options) {
190
- return options ? { ...writeDefaults, ...options } : writeDefaults;
191
- }
192
- export function readMessage(message, fields, reader, lengthOrEndTagFieldNo, options, delimitedMessageEncoding) {
192
+ function readMessage(message, fields, reader, lengthOrEndTagFieldNo, options, delimitedMessageEncoding) {
193
193
  // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
194
194
  const end = delimitedMessageEncoding ? reader.len : reader.pos + lengthOrEndTagFieldNo;
195
195
  let fieldNo, wireType;
@@ -216,7 +216,7 @@ export function readMessage(message, fields, reader, lengthOrEndTagFieldNo, opti
216
216
  /**
217
217
  * Serialize a message to binary data.
218
218
  */
219
- export function writeMessage(message, fields, writer, options) {
219
+ function writeMessage(message, fields, writer, options) {
220
220
  for (const field of fields.byNumber()) {
221
221
  if (!isFieldSet(field, message)) {
222
222
  if (field.req) {
@@ -347,7 +347,7 @@ function scalarTypeInfo(type) {
347
347
  const method = ScalarType[type].toLowerCase();
348
348
  return [wireType, method];
349
349
  }
350
- export function writeMapEntry(writer, options, field, key, value) {
350
+ function writeMapEntry(writer, options, field, key, value) {
351
351
  writer.tag(field.no, WireType.LengthDelimited);
352
352
  writer.fork();
353
353
  // javascript only allows number or string for object properties
@@ -387,3 +387,4 @@ export function writeMapEntry(writer, options, field, key, value) {
387
387
  }
388
388
  writer.join();
389
389
  }
390
+ export { readField as binaryReadField, readMapEntry as binaryReadMapEntry, readScalar as binaryReadScalar, readScalarLTString as binaryReadScalarLTString, readMessage as binaryReadMessage, writeField as binaryWriteField, writeScalar as binaryWriteScalar, writePacked as binaryWritePacked, writeMapEntry as binaryWriteMapEntry, writeMessage as binaryWriteMessage, makeReadOptions as binaryMakeReadOptions, makeWriteOptions as binaryMakeWriteOptions, };
@@ -20,16 +20,24 @@ export declare const codegenInfo: {
20
20
  readonly PartialFieldInfo: RuntimeSymbolInfo;
21
21
  readonly MessageType: RuntimeSymbolInfo;
22
22
  readonly Extension: RuntimeSymbolInfo;
23
+ readonly IMessageTypeRegistry: RuntimeSymbolInfo;
23
24
  readonly BinaryReadOptions: RuntimeSymbolInfo;
24
25
  readonly BinaryWriteOptions: RuntimeSymbolInfo;
25
26
  readonly JsonReadOptions: RuntimeSymbolInfo;
26
27
  readonly JsonWriteOptions: RuntimeSymbolInfo;
27
28
  readonly JsonValue: RuntimeSymbolInfo;
28
29
  readonly JsonObject: RuntimeSymbolInfo;
30
+ readonly jsonReadEnum: RuntimeSymbolInfo;
31
+ readonly jsonReadScalar: RuntimeSymbolInfo;
32
+ readonly jsonWriteEnum: RuntimeSymbolInfo;
33
+ readonly jsonWriteScalar: RuntimeSymbolInfo;
34
+ readonly jsonDebugValue: RuntimeSymbolInfo;
29
35
  readonly protoDouble: RuntimeSymbolInfo;
30
36
  readonly protoInt64: RuntimeSymbolInfo;
37
+ readonly applyPartialMessage: RuntimeSymbolInfo;
31
38
  readonly ScalarType: RuntimeSymbolInfo;
32
39
  readonly LongType: RuntimeSymbolInfo;
40
+ readonly ScalarValue: RuntimeSymbolInfo;
33
41
  readonly MethodKind: RuntimeSymbolInfo;
34
42
  readonly MethodIdempotency: RuntimeSymbolInfo;
35
43
  readonly createEnumType: RuntimeSymbolInfo;
@@ -27,16 +27,24 @@ const symbols = {
27
27
  PartialFieldInfo: symbolInfo(true, "./field.js"),
28
28
  MessageType: symbolInfo(true, "./message-type.js"),
29
29
  Extension: symbolInfo(true, "./extension.js"),
30
+ IMessageTypeRegistry: symbolInfo(true, "./type-registry.js"),
30
31
  BinaryReadOptions: symbolInfo(true, "./binary-format.js"),
31
32
  BinaryWriteOptions: symbolInfo(true, "./binary-format.js"),
32
- JsonReadOptions: symbolInfo(true, "./json-format.js"),
33
- JsonWriteOptions: symbolInfo(true, "./json-format.js"),
34
- JsonValue: symbolInfo(true, "./json-format.js"),
35
- JsonObject: symbolInfo(true, "./json-format.js"),
33
+ JsonReadOptions: symbolInfo(true, "./json.js"),
34
+ JsonWriteOptions: symbolInfo(true, "./json.js"),
35
+ JsonValue: symbolInfo(true, "./json.js"),
36
+ JsonObject: symbolInfo(true, "./json.js"),
37
+ jsonReadEnum: symbolInfo(false, "./json.js"),
38
+ jsonReadScalar: symbolInfo(false, "./json.js"),
39
+ jsonWriteEnum: symbolInfo(false, "./json.js"),
40
+ jsonWriteScalar: symbolInfo(false, "./json.js"),
41
+ jsonDebugValue: symbolInfo(false, "./json.js"),
36
42
  protoDouble: symbolInfo(false, "./proto-double.js"),
37
43
  protoInt64: symbolInfo(false, "./proto-int64.js"),
44
+ applyPartialMessage: symbolInfo(false, "./partial.js"),
38
45
  ScalarType: symbolInfo(false, "./scalar.js"),
39
46
  LongType: symbolInfo(false, "./scalar.js"),
47
+ ScalarValue: symbolInfo(false, "./scalar.js"),
40
48
  MethodKind: symbolInfo(false, "./service-type.js"),
41
49
  MethodIdempotency: symbolInfo(false, "./service-type.js"),
42
50
  createEnumType: symbolInfo(false, "./enum.js"),
@@ -895,7 +895,7 @@ function declarationString() {
895
895
  if (defaultValue !== undefined) {
896
896
  if (this.proto.type == FieldDescriptorProto_Type.BYTES ||
897
897
  this.proto.type == FieldDescriptorProto_Type.STRING) {
898
- defaultValue = '"' + defaultValue.replace('"', '\\"') + '"';
898
+ defaultValue = '"' + defaultValue.replace(/"/g, '\\"') + '"';
899
899
  }
900
900
  options.push(`default = ${defaultValue}`);
901
901
  }
@@ -294,7 +294,7 @@ export type DescExtension = DescFieldCommon & (DescFieldScalar | DescFieldMessag
294
294
  */
295
295
  readonly extendee: DescMessage;
296
296
  };
297
- interface DescFieldCommon {
297
+ export interface DescFieldCommon {
298
298
  /**
299
299
  * The field name, as specified in the protobuf source
300
300
  */
@@ -354,7 +354,7 @@ interface DescFieldCommon {
354
354
  getFeatures(): MergedFeatureSet;
355
355
  toString(): string;
356
356
  }
357
- interface DescFieldScalar {
357
+ export interface DescFieldScalar {
358
358
  readonly fieldKind: "scalar";
359
359
  /**
360
360
  * Is the field repeated?
@@ -391,7 +391,7 @@ interface DescFieldScalar {
391
391
  */
392
392
  getDefaultValue(): number | boolean | string | bigint | Uint8Array | undefined;
393
393
  }
394
- interface DescFieldMessage {
394
+ export interface DescFieldMessage {
395
395
  readonly fieldKind: "message";
396
396
  /**
397
397
  * Is the field repeated?
@@ -423,7 +423,7 @@ interface DescFieldMessage {
423
423
  */
424
424
  readonly mapValue: undefined;
425
425
  }
426
- interface DescFieldEnum {
426
+ export interface DescFieldEnum {
427
427
  readonly fieldKind: "enum";
428
428
  /**
429
429
  * Is the field repeated?
@@ -460,7 +460,7 @@ interface DescFieldEnum {
460
460
  */
461
461
  getDefaultValue(): number | boolean | string | bigint | Uint8Array | undefined;
462
462
  }
463
- interface DescFieldMap {
463
+ export interface DescFieldMap {
464
464
  readonly fieldKind: "map";
465
465
  /**
466
466
  * Is the field repeated?
@@ -492,7 +492,7 @@ interface DescFieldMap {
492
492
  */
493
493
  readonly mapValue: DescFieldMapValueEnum | DescFieldMapValueMessage | DescFieldMapValueScalar;
494
494
  }
495
- interface DescFieldMapValueEnum {
495
+ export interface DescFieldMapValueEnum {
496
496
  readonly kind: "enum";
497
497
  /**
498
498
  * The enum type, if this is a map field with enum values.
@@ -507,7 +507,7 @@ interface DescFieldMapValueEnum {
507
507
  */
508
508
  readonly scalar: undefined;
509
509
  }
510
- interface DescFieldMapValueMessage {
510
+ export interface DescFieldMapValueMessage {
511
511
  readonly kind: "message";
512
512
  /**
513
513
  * The enum type, if this is a map field with enum values.
@@ -522,7 +522,7 @@ interface DescFieldMapValueMessage {
522
522
  */
523
523
  readonly scalar: undefined;
524
524
  }
525
- interface DescFieldMapValueScalar {
525
+ export interface DescFieldMapValueScalar {
526
526
  readonly kind: "scalar";
527
527
  /**
528
528
  * The enum type, if this is a map field with enum values.
@@ -669,4 +669,3 @@ export interface DescComments {
669
669
  readonly trailing?: string;
670
670
  readonly sourcePath: readonly number[];
671
671
  }
672
- export {};
@@ -1,4 +1,4 @@
1
- import type { MessageType } from "../../index.js";
1
+ import type { IMessageTypeRegistry, JsonReadOptions, JsonValue, JsonWriteOptions, MessageType } from "../../index.js";
2
2
  import { Message } from "../../index.js";
3
3
  export declare const protobufPackage = "google.protobuf";
4
4
  /**
@@ -133,4 +133,57 @@ export type Any = Message<{
133
133
  */
134
134
  value?: Uint8Array;
135
135
  }>;
136
- export declare const Any: MessageType<Any>;
136
+ declare const Any_Wkt: {
137
+ toJson(msg: Any, options?: Partial<JsonWriteOptions>): JsonValue;
138
+ fromJson(json: JsonValue, options?: Partial<JsonReadOptions>): Message<{
139
+ /**
140
+ * A URL/resource name that uniquely identifies the type of the serialized
141
+ * protocol buffer message. This string must contain at least
142
+ * one "/" character. The last segment of the URL's path must represent
143
+ * the fully qualified name of the type (as in
144
+ * `path/google.protobuf.Duration`). The name should be in a canonical form
145
+ * (e.g., leading "." is not accepted).
146
+ *
147
+ * In practice, teams usually precompile into the binary all types that they
148
+ * expect it to use in the context of Any. However, for URLs which use the
149
+ * scheme `http`, `https`, or no scheme, one can optionally set up a type
150
+ * server that maps type URLs to message definitions as follows:
151
+ *
152
+ * * If no scheme is provided, `https` is assumed.
153
+ * * An HTTP GET on the URL must yield a [google.protobuf.Type][]
154
+ * value in binary format, or produce an error.
155
+ * * Applications are allowed to cache lookup results based on the
156
+ * URL, or have them precompiled into a binary to avoid any
157
+ * lookup. Therefore, binary compatibility needs to be preserved
158
+ * on changes to types. (Use versioned type names to manage
159
+ * breaking changes.)
160
+ *
161
+ * Note: this functionality is not currently available in the official
162
+ * protobuf release, and it is not used for type URLs beginning with
163
+ * type.googleapis.com. As of May 2023, there are no widely used type server
164
+ * implementations and no plans to implement one.
165
+ *
166
+ * Schemes other than `http`, `https` (or the empty scheme) might be
167
+ * used with implementation specific semantics.
168
+ *
169
+ *
170
+ * @generated from field: string type_url = 1;
171
+ */
172
+ typeUrl?: string | undefined;
173
+ /**
174
+ * Must be a valid serialized protocol buffer of the above specified type.
175
+ *
176
+ * @generated from field: bytes value = 2;
177
+ */
178
+ value?: Uint8Array | undefined;
179
+ }>;
180
+ packFrom<T extends Message<T>>(out: Any, message: Message<T>, messageType: MessageType<T>): void;
181
+ unpackTo<T_1 extends Message<T_1>>(msg: Any, target: Message<T_1>, targetMessageType: MessageType<T_1>): boolean;
182
+ unpack<T_2 extends Message<T_2>>(msg: Any, registry: IMessageTypeRegistry): {
183
+ message: Message<T_2>;
184
+ messageType: MessageType<T_2>;
185
+ } | undefined;
186
+ is(msg: Any, msgType: MessageType | string): boolean;
187
+ };
188
+ export declare const Any: MessageType<Any> & typeof Any_Wkt;
189
+ export {};
@@ -27,13 +27,83 @@
27
27
  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
28
  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
29
  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
- import { createMessageType } from "../../index.js";
30
+ import { applyPartialMessage, createMessageType, ScalarType } from "../../index.js";
31
31
  export const protobufPackage = "google.protobuf";
32
+ // Any_Wkt contains the well-known-type overrides for Any.
33
+ const Any_Wkt = {
34
+ toJson(msg, options) {
35
+ const typeName = msg?.typeUrl;
36
+ if (!typeName) {
37
+ return {};
38
+ }
39
+ const messageType = options?.typeRegistry?.findMessage(typeName);
40
+ if (!messageType) {
41
+ throw new Error(`cannot encode message google.protobuf.Any to JSON: "${typeName}" is not in the type registry`);
42
+ }
43
+ const message = messageType.fromBinary(msg.value);
44
+ let json = messageType.toJson(message, options);
45
+ if (typeName.startsWith("google.protobuf.") || (json === null || Array.isArray(json) || typeof json !== "object")) {
46
+ json = { value: json };
47
+ }
48
+ json["@type"] = typeName;
49
+ return json;
50
+ },
51
+ fromJson(json, options) {
52
+ if (json === null || Array.isArray(json) || typeof json != "object") {
53
+ throw new Error(`cannot decode message google.protobuf.Any from JSON: expected object but got ${json === null ? "null" : Array.isArray(json) ? "array" : typeof json}`);
54
+ }
55
+ if (Object.keys(json).length == 0) {
56
+ return {};
57
+ }
58
+ const typeUrl = json["@type"];
59
+ if (typeof typeUrl != "string" || typeUrl == "") {
60
+ throw new Error(`cannot decode message google.protobuf.Any from JSON: "@type" is empty`);
61
+ }
62
+ const typeName = typeUrl, messageType = options?.typeRegistry?.findMessage(typeName);
63
+ if (!messageType) {
64
+ throw new Error(`cannot decode message google.protobuf.Any from JSON: ${typeUrl} is not in the type registry`);
65
+ }
66
+ let message;
67
+ if (typeName.startsWith("google.protobuf.") && Object.prototype.hasOwnProperty.call(json, "value")) {
68
+ message = messageType.fromJson(json["value"], options);
69
+ }
70
+ else {
71
+ const copy = Object.assign({}, json);
72
+ delete copy["@type"];
73
+ message = messageType.fromJson(copy, options);
74
+ }
75
+ const out = {};
76
+ Any.packFrom(out, message, messageType);
77
+ return out;
78
+ },
79
+ packFrom(out, message, messageType) {
80
+ out.value = messageType.toBinary(message);
81
+ out.typeUrl = messageType.typeName;
82
+ },
83
+ unpackTo(msg, target, targetMessageType) {
84
+ if (!Any.is(msg, targetMessageType)) {
85
+ return false;
86
+ }
87
+ const partial = targetMessageType.fromBinary(msg.value);
88
+ applyPartialMessage(partial, target, targetMessageType.fields);
89
+ return true;
90
+ },
91
+ unpack(msg, registry) {
92
+ const typeUrl = msg.typeUrl;
93
+ const messageType = !!typeUrl && registry.findMessage(typeUrl);
94
+ return messageType ? { message: messageType.fromBinary(msg.value), messageType } : undefined;
95
+ },
96
+ is(msg, msgType) {
97
+ const name = msg.typeUrl;
98
+ return !!name && (typeof msgType === 'string' ? name === msgType : name === msgType.typeName);
99
+ },
100
+ };
101
+ // Any contains the message type declaration for Any.
32
102
  export const Any = createMessageType({
33
103
  typeName: "google.protobuf.Any",
34
104
  fields: [
35
- { no: 1, name: "type_url", kind: "scalar", T: 9 /* ScalarType.STRING */ },
36
- { no: 2, name: "value", kind: "scalar", T: 12 /* ScalarType.BYTES */ },
105
+ { no: 1, name: "type_url", kind: "scalar", T: ScalarType.STRING },
106
+ { no: 2, name: "value", kind: "scalar", T: ScalarType.BYTES },
37
107
  ],
38
108
  packedByDefault: true,
39
- });
109
+ }, Any_Wkt);
@@ -27,38 +27,41 @@
27
27
  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
28
  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
29
  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
- import { createMessageType } from "../../index.js";
30
+ import { createMessageType, ScalarType } from "../../index.js";
31
31
  import { Option, Syntax_Enum } from "./type.pb.js";
32
32
  import { SourceContext } from "./source_context.pb.js";
33
33
  export const protobufPackage = "google.protobuf";
34
+ // Method contains the message type declaration for Method.
34
35
  export const Method = createMessageType({
35
36
  typeName: "google.protobuf.Method",
36
37
  fields: [
37
- { no: 1, name: "name", kind: "scalar", T: 9 /* ScalarType.STRING */ },
38
- { no: 2, name: "request_type_url", kind: "scalar", T: 9 /* ScalarType.STRING */ },
39
- { no: 3, name: "request_streaming", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
40
- { no: 4, name: "response_type_url", kind: "scalar", T: 9 /* ScalarType.STRING */ },
41
- { no: 5, name: "response_streaming", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
38
+ { no: 1, name: "name", kind: "scalar", T: ScalarType.STRING },
39
+ { no: 2, name: "request_type_url", kind: "scalar", T: ScalarType.STRING },
40
+ { no: 3, name: "request_streaming", kind: "scalar", T: ScalarType.BOOL },
41
+ { no: 4, name: "response_type_url", kind: "scalar", T: ScalarType.STRING },
42
+ { no: 5, name: "response_streaming", kind: "scalar", T: ScalarType.BOOL },
42
43
  { no: 6, name: "options", kind: "message", T: () => Option, repeated: true },
43
44
  { no: 7, name: "syntax", kind: "enum", T: Syntax_Enum },
44
45
  ],
45
46
  packedByDefault: true,
46
47
  });
48
+ // Mixin contains the message type declaration for Mixin.
47
49
  export const Mixin = createMessageType({
48
50
  typeName: "google.protobuf.Mixin",
49
51
  fields: [
50
- { no: 1, name: "name", kind: "scalar", T: 9 /* ScalarType.STRING */ },
51
- { no: 2, name: "root", kind: "scalar", T: 9 /* ScalarType.STRING */ },
52
+ { no: 1, name: "name", kind: "scalar", T: ScalarType.STRING },
53
+ { no: 2, name: "root", kind: "scalar", T: ScalarType.STRING },
52
54
  ],
53
55
  packedByDefault: true,
54
56
  });
57
+ // Api contains the message type declaration for Api.
55
58
  export const Api = createMessageType({
56
59
  typeName: "google.protobuf.Api",
57
60
  fields: [
58
- { no: 1, name: "name", kind: "scalar", T: 9 /* ScalarType.STRING */ },
61
+ { no: 1, name: "name", kind: "scalar", T: ScalarType.STRING },
59
62
  { no: 2, name: "methods", kind: "message", T: () => Method, repeated: true },
60
63
  { no: 3, name: "options", kind: "message", T: () => Option, repeated: true },
61
- { no: 4, name: "version", kind: "scalar", T: 9 /* ScalarType.STRING */ },
64
+ { no: 4, name: "version", kind: "scalar", T: ScalarType.STRING },
62
65
  { no: 5, name: "source_context", kind: "message", T: () => SourceContext },
63
66
  { no: 6, name: "mixins", kind: "message", T: () => Mixin, repeated: true },
64
67
  { no: 7, name: "syntax", kind: "enum", T: Syntax_Enum },