@bufbuild/protobuf 0.2.0 → 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 (31) hide show
  1. package/dist/cjs/codegen-info.js +3 -0
  2. package/dist/cjs/google/protobuf/any_pb.js +25 -1
  3. package/dist/cjs/google/protobuf/struct_pb.js +1 -1
  4. package/dist/cjs/google/protobuf/type_pb.js +1 -1
  5. package/dist/cjs/google/protobuf/wrappers_pb.js +9 -9
  6. package/dist/cjs/message.js +15 -1
  7. package/dist/cjs/private/binary-format-common.js +8 -2
  8. package/dist/cjs/private/field-wrapper.js +7 -15
  9. package/dist/cjs/private/json-format-common.js +10 -5
  10. package/dist/cjs/private/reify-wkt.js +170 -0
  11. package/dist/cjs/private/util-common.js +10 -20
  12. package/dist/esm/codegen-info.js +3 -0
  13. package/dist/esm/google/protobuf/any_pb.js +25 -1
  14. package/dist/esm/google/protobuf/struct_pb.js +1 -1
  15. package/dist/esm/google/protobuf/type_pb.js +1 -1
  16. package/dist/esm/google/protobuf/wrappers_pb.js +9 -9
  17. package/dist/esm/message.js +15 -1
  18. package/dist/esm/private/binary-format-common.js +9 -3
  19. package/dist/esm/private/field-wrapper.js +6 -13
  20. package/dist/esm/private/json-format-common.js +10 -5
  21. package/dist/esm/private/reify-wkt.js +166 -0
  22. package/dist/esm/private/util-common.js +10 -20
  23. package/dist/types/binary-format.d.ts +1 -1
  24. package/dist/types/codegen-info.d.ts +3 -1
  25. package/dist/types/google/protobuf/any_pb.d.ts +3 -1
  26. package/dist/types/google/protobuf/wrappers_pb.d.ts +9 -9
  27. package/dist/types/message.d.ts +15 -4
  28. package/dist/types/private/field-wrapper.d.ts +5 -8
  29. package/dist/types/private/reify-wkt.d.ts +100 -0
  30. package/dist/types/private/util.d.ts +1 -1
  31. package/package.json +1 -1
@@ -69,7 +69,7 @@ DoubleValue.fields = proto3.util.newFieldList(() => [
69
69
  ]);
70
70
  DoubleValue.fieldWrapper = {
71
71
  wrapField(value) {
72
- return value instanceof DoubleValue ? value : new DoubleValue({ value });
72
+ return new DoubleValue({ value });
73
73
  },
74
74
  unwrapField(value) {
75
75
  return value.value;
@@ -129,7 +129,7 @@ FloatValue.fields = proto3.util.newFieldList(() => [
129
129
  ]);
130
130
  FloatValue.fieldWrapper = {
131
131
  wrapField(value) {
132
- return value instanceof FloatValue ? value : new FloatValue({ value });
132
+ return new FloatValue({ value });
133
133
  },
134
134
  unwrapField(value) {
135
135
  return value.value;
@@ -189,7 +189,7 @@ Int64Value.fields = proto3.util.newFieldList(() => [
189
189
  ]);
190
190
  Int64Value.fieldWrapper = {
191
191
  wrapField(value) {
192
- return value instanceof Int64Value ? value : new Int64Value({ value });
192
+ return new Int64Value({ value });
193
193
  },
194
194
  unwrapField(value) {
195
195
  return value.value;
@@ -249,7 +249,7 @@ UInt64Value.fields = proto3.util.newFieldList(() => [
249
249
  ]);
250
250
  UInt64Value.fieldWrapper = {
251
251
  wrapField(value) {
252
- return value instanceof UInt64Value ? value : new UInt64Value({ value });
252
+ return new UInt64Value({ value });
253
253
  },
254
254
  unwrapField(value) {
255
255
  return value.value;
@@ -309,7 +309,7 @@ Int32Value.fields = proto3.util.newFieldList(() => [
309
309
  ]);
310
310
  Int32Value.fieldWrapper = {
311
311
  wrapField(value) {
312
- return value instanceof Int32Value ? value : new Int32Value({ value });
312
+ return new Int32Value({ value });
313
313
  },
314
314
  unwrapField(value) {
315
315
  return value.value;
@@ -369,7 +369,7 @@ UInt32Value.fields = proto3.util.newFieldList(() => [
369
369
  ]);
370
370
  UInt32Value.fieldWrapper = {
371
371
  wrapField(value) {
372
- return value instanceof UInt32Value ? value : new UInt32Value({ value });
372
+ return new UInt32Value({ value });
373
373
  },
374
374
  unwrapField(value) {
375
375
  return value.value;
@@ -429,7 +429,7 @@ BoolValue.fields = proto3.util.newFieldList(() => [
429
429
  ]);
430
430
  BoolValue.fieldWrapper = {
431
431
  wrapField(value) {
432
- return value instanceof BoolValue ? value : new BoolValue({ value });
432
+ return new BoolValue({ value });
433
433
  },
434
434
  unwrapField(value) {
435
435
  return value.value;
@@ -489,7 +489,7 @@ StringValue.fields = proto3.util.newFieldList(() => [
489
489
  ]);
490
490
  StringValue.fieldWrapper = {
491
491
  wrapField(value) {
492
- return value instanceof StringValue ? value : new StringValue({ value });
492
+ return new StringValue({ value });
493
493
  },
494
494
  unwrapField(value) {
495
495
  return value.value;
@@ -549,7 +549,7 @@ BytesValue.fields = proto3.util.newFieldList(() => [
549
549
  ]);
550
550
  BytesValue.fieldWrapper = {
551
551
  wrapField(value) {
552
- return value instanceof BytesValue ? value : new BytesValue({ value });
552
+ return new BytesValue({ value });
553
553
  },
554
554
  unwrapField(value) {
555
555
  return value.value;
@@ -29,7 +29,6 @@ export class Message {
29
29
  * Create a deep copy.
30
30
  */
31
31
  clone() {
32
- // return this.getType().runtime.util.clone(this);
33
32
  return this.getType().runtime.util.clone(this);
34
33
  }
35
34
  /**
@@ -85,6 +84,21 @@ export class Message {
85
84
  const value = this.toJson(options);
86
85
  return JSON.stringify(value, null, (_a = options === null || options === void 0 ? void 0 : options.prettySpaces) !== null && _a !== void 0 ? _a : 0);
87
86
  }
87
+ /**
88
+ * Override for serializaton behavior. This will be invoked when calling
89
+ * JSON.stringify on this message (i.e. JSON.stringify(msg)).
90
+ *
91
+ * Note that this will not serialize google.protobuf.Any with a packed
92
+ * message because the protobuf JSON format specifies that it needs to be
93
+ * unpacked, and this is only possible with a type registry to look up the
94
+ * message type. As a result, attempting to serialize a message with this
95
+ * type will throw an Error.
96
+ */
97
+ toJSON() {
98
+ return this.toJson({
99
+ emitDefaultValues: true,
100
+ });
101
+ }
88
102
  /**
89
103
  * Retrieve the MessageType of this message - a singleton that represents
90
104
  * the protobuf message declaration and provides metadata for reflection-
@@ -12,8 +12,9 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
  import { BinaryReader, BinaryWriter, WireType, } from "../binary-encoding.js";
15
+ import { Message } from "../message.js";
15
16
  import { ScalarType } from "../field.js";
16
- import { unwrapField, wrapField } from "./field-wrapper.js";
17
+ import { wrapField } from "./field-wrapper.js";
17
18
  import { scalarDefaultValue, scalarTypeInfo } from "./scalars.js";
18
19
  import { assert } from "./assert.js";
19
20
  /* eslint-disable @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unnecessary-condition, no-case-declarations, prefer-const */
@@ -111,11 +112,16 @@ export function makeBinaryFormatCommon() {
111
112
  target[localName].push(messageType.fromBinary(reader.bytes(), options));
112
113
  }
113
114
  else {
114
- if (target[localName] instanceof messageType) {
115
+ if (target[localName] instanceof Message) {
115
116
  target[localName].fromBinary(reader.bytes(), options);
116
117
  }
117
118
  else {
118
- target[localName] = unwrapField(messageType, messageType.fromBinary(reader.bytes(), options));
119
+ target[localName] = messageType.fromBinary(reader.bytes(), options);
120
+ if (messageType.fieldWrapper &&
121
+ !field.oneof &&
122
+ !field.repeated) {
123
+ target[localName] = messageType.fieldWrapper.unwrapField(target[localName]);
124
+ }
119
125
  }
120
126
  }
121
127
  break;
@@ -11,28 +11,21 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
+ import { Message } from "../message.js";
14
15
  import { ScalarType } from "../field.js";
15
16
  /**
16
- * Wrap field values whose message type has a wrapper.
17
+ * Wrap a primitive message field value in its corresponding wrapper
18
+ * message. This function is idempotent.
17
19
  */
18
20
  export function wrapField(type, value) {
19
- if (value instanceof type) {
21
+ if (value instanceof Message || !type.fieldWrapper) {
20
22
  return value;
21
23
  }
22
- if (type.fieldWrapper) {
23
- return type.fieldWrapper.wrapField(value);
24
- }
25
- throw new Error(`cannot unwrap field value, ${type.typeName} does not define a field wrapper`);
26
- }
27
- /**
28
- * Unwrap field values whose message type has a wrapper.
29
- */
30
- export function unwrapField(type, value) {
31
- return type.fieldWrapper ? type.fieldWrapper.unwrapField(value) : value;
24
+ return type.fieldWrapper.wrapField(value);
32
25
  }
33
26
  /**
34
27
  * If the given field uses one of the well-known wrapper types, return
35
- * the base type it wraps.
28
+ * the primitive type it wraps.
36
29
  */
37
30
  export function getUnwrappedFieldType(field) {
38
31
  if (field.fieldKind !== "message") {
@@ -11,7 +11,7 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- import { unwrapField, wrapField } from "./field-wrapper.js";
14
+ import { Message } from "../message.js";
15
15
  import { ScalarType } from "../field.js";
16
16
  import { assert, assertFloat32, assertInt32, assertUInt32 } from "./assert.js";
17
17
  import { protoInt64 } from "../proto-int64.js";
@@ -171,10 +171,15 @@ export function makeJsonFormatCommon(makeWriteField) {
171
171
  }
172
172
  continue;
173
173
  }
174
- const targetMessage = target[localName] === undefined
175
- ? new messageType()
176
- : wrapField(messageType, target[localName]);
177
- target[localName] = unwrapField(messageType, targetMessage.fromJson(jsonValue, options));
174
+ if (target[localName] instanceof Message) {
175
+ target[localName].fromJson(jsonValue, options);
176
+ }
177
+ else {
178
+ target[localName] = messageType.fromJson(jsonValue, options);
179
+ if (messageType.fieldWrapper && !field.oneof) {
180
+ target[localName] = messageType.fieldWrapper.unwrapField(target[localName]);
181
+ }
182
+ }
178
183
  break;
179
184
  case "enum":
180
185
  const enumValue = readEnum(field.T, jsonValue, options.ignoreUnknownFields);
@@ -0,0 +1,166 @@
1
+ // Copyright 2021-2022 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { ScalarType } from "../field.js";
15
+ /**
16
+ * Reifies a given DescMessage into a more concrete object representing its
17
+ * respective well-known type. The returned object will contain properties
18
+ * representing the WKT's defined fields.
19
+ *
20
+ * Useful during code generation when immediate access to a particular field
21
+ * is needed without having to search the object's typename and DescField list.
22
+ *
23
+ * Returns undefined if the WKT cannot be completely constructed via the
24
+ * DescMessage.
25
+ */
26
+ export function reifyWkt(message) {
27
+ switch (message.typeName) {
28
+ case "google.protobuf.Any": {
29
+ const typeUrl = message.fields.find((f) => f.number == 1 &&
30
+ f.fieldKind == "scalar" &&
31
+ f.scalar === ScalarType.STRING);
32
+ const value = message.fields.find((f) => f.number == 2 &&
33
+ f.fieldKind == "scalar" &&
34
+ f.scalar === ScalarType.BYTES);
35
+ if (typeUrl && value) {
36
+ return {
37
+ typeName: message.typeName,
38
+ typeUrl,
39
+ value,
40
+ };
41
+ }
42
+ break;
43
+ }
44
+ case "google.protobuf.Timestamp": {
45
+ const seconds = message.fields.find((f) => f.number == 1 &&
46
+ f.fieldKind == "scalar" &&
47
+ f.scalar === ScalarType.INT64);
48
+ const nanos = message.fields.find((f) => f.number == 2 &&
49
+ f.fieldKind == "scalar" &&
50
+ f.scalar === ScalarType.INT32);
51
+ if (seconds && nanos) {
52
+ return {
53
+ typeName: message.typeName,
54
+ seconds,
55
+ nanos,
56
+ };
57
+ }
58
+ break;
59
+ }
60
+ case "google.protobuf.Duration": {
61
+ const seconds = message.fields.find((f) => f.number == 1 &&
62
+ f.fieldKind == "scalar" &&
63
+ f.scalar === ScalarType.INT64);
64
+ const nanos = message.fields.find((f) => f.number == 2 &&
65
+ f.fieldKind == "scalar" &&
66
+ f.scalar === ScalarType.INT32);
67
+ if (seconds && nanos) {
68
+ return {
69
+ typeName: message.typeName,
70
+ seconds,
71
+ nanos,
72
+ };
73
+ }
74
+ break;
75
+ }
76
+ case "google.protobuf.Struct": {
77
+ const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
78
+ if ((fields === null || fields === void 0 ? void 0 : fields.fieldKind) !== "map" ||
79
+ fields.mapValue.kind !== "message" ||
80
+ fields.mapValue.message.typeName !== "google.protobuf.Value") {
81
+ break;
82
+ }
83
+ return { typeName: message.typeName, fields };
84
+ }
85
+ case "google.protobuf.Value": {
86
+ const kind = message.oneofs.find((o) => o.name === "kind");
87
+ const nullValue = message.fields.find((f) => f.number == 1 && f.oneof === kind);
88
+ if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.fieldKind) !== "enum" ||
89
+ nullValue.enum.typeName !== "google.protobuf.NullValue") {
90
+ return undefined;
91
+ }
92
+ const numberValue = message.fields.find((f) => f.number == 2 &&
93
+ f.fieldKind == "scalar" &&
94
+ f.scalar === ScalarType.DOUBLE &&
95
+ f.oneof === kind);
96
+ const stringValue = message.fields.find((f) => f.number == 3 &&
97
+ f.fieldKind == "scalar" &&
98
+ f.scalar === ScalarType.STRING &&
99
+ f.oneof === kind);
100
+ const boolValue = message.fields.find((f) => f.number == 4 &&
101
+ f.fieldKind == "scalar" &&
102
+ f.scalar === ScalarType.BOOL &&
103
+ f.oneof === kind);
104
+ const structValue = message.fields.find((f) => f.number == 5 && f.oneof === kind);
105
+ if ((structValue === null || structValue === void 0 ? void 0 : structValue.fieldKind) !== "message" ||
106
+ structValue.message.typeName !== "google.protobuf.Struct") {
107
+ return undefined;
108
+ }
109
+ const listValue = message.fields.find((f) => f.number == 6 && f.oneof === kind);
110
+ if ((listValue === null || listValue === void 0 ? void 0 : listValue.fieldKind) !== "message" ||
111
+ listValue.message.typeName !== "google.protobuf.ListValue") {
112
+ return undefined;
113
+ }
114
+ if (kind && numberValue && stringValue && boolValue) {
115
+ return {
116
+ typeName: message.typeName,
117
+ kind,
118
+ nullValue,
119
+ numberValue,
120
+ stringValue,
121
+ boolValue,
122
+ structValue,
123
+ listValue,
124
+ };
125
+ }
126
+ break;
127
+ }
128
+ case "google.protobuf.ListValue": {
129
+ const values = message.fields.find((f) => f.number == 1 && f.repeated);
130
+ if ((values === null || values === void 0 ? void 0 : values.fieldKind) != "message" ||
131
+ values.message.typeName !== "google.protobuf.Value") {
132
+ break;
133
+ }
134
+ return { typeName: message.typeName, values };
135
+ }
136
+ case "google.protobuf.FieldMask": {
137
+ const paths = message.fields.find((f) => f.number == 1 &&
138
+ f.fieldKind == "scalar" &&
139
+ f.scalar === ScalarType.STRING &&
140
+ f.repeated);
141
+ if (paths) {
142
+ return { typeName: message.typeName, paths };
143
+ }
144
+ break;
145
+ }
146
+ case "google.protobuf.DoubleValue":
147
+ case "google.protobuf.FloatValue":
148
+ case "google.protobuf.Int64Value":
149
+ case "google.protobuf.UInt64Value":
150
+ case "google.protobuf.Int32Value":
151
+ case "google.protobuf.UInt32Value":
152
+ case "google.protobuf.BoolValue":
153
+ case "google.protobuf.StringValue":
154
+ case "google.protobuf.BytesValue": {
155
+ const value = message.fields.find((f) => f.number == 1 && f.name == "value");
156
+ if (!value) {
157
+ break;
158
+ }
159
+ if (value.fieldKind !== "scalar") {
160
+ break;
161
+ }
162
+ return { typeName: message.typeName, value };
163
+ }
164
+ }
165
+ return undefined;
166
+ }
@@ -12,6 +12,7 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
  import { setEnumType } from "./enum.js";
15
+ import { Message, } from "../message.js";
15
16
  import { ScalarType } from "../field.js";
16
17
  import { scalarEquals } from "./scalars.js";
17
18
  /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-argument,no-case-declarations */
@@ -136,10 +137,7 @@ export function makeUtilCommon() {
136
137
  }
137
138
  throw new Error(`oneof cannot contain ${s.kind}`);
138
139
  case "map":
139
- const keys = Object.keys(va);
140
- if (keys.some((k) => vb[k] === undefined)) {
141
- return false;
142
- }
140
+ const keys = Object.keys(va).concat(Object.keys(vb));
143
141
  switch (m.V.kind) {
144
142
  case "message":
145
143
  const messageType = m.V.T;
@@ -188,21 +186,13 @@ function cloneSingularField(field, value) {
188
186
  if (value === undefined) {
189
187
  return value;
190
188
  }
191
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- unmatched "map" is unsupported
192
- switch (field.kind) {
193
- case "enum":
194
- return value;
195
- case "scalar":
196
- if (field.T === ScalarType.BYTES) {
197
- const c = new Uint8Array(value.byteLength);
198
- c.set(value);
199
- return c;
200
- }
201
- return value;
202
- case "message":
203
- if (field.T.fieldWrapper) {
204
- return field.T.fieldWrapper.unwrapField(field.T.fieldWrapper.wrapField(value).clone());
205
- }
206
- return value.clone();
189
+ if (value instanceof Message) {
190
+ return value.clone();
191
+ }
192
+ if (value instanceof Uint8Array) {
193
+ const c = new Uint8Array(value.byteLength);
194
+ c.set(value);
195
+ return c;
207
196
  }
197
+ return value;
208
198
  }
@@ -17,7 +17,7 @@ export interface BinaryFormat {
17
17
  /**
18
18
  * Parse a message from binary data, merging fields.
19
19
  */
20
- readMessage<T extends Message>(message: T, reader: IBinaryReader, length: number, options: BinaryReadOptions): void;
20
+ readMessage(message: Message, reader: IBinaryReader, length: number, options: BinaryReadOptions): void;
21
21
  /**
22
22
  * Serialize a message to binary data.
23
23
  */
@@ -1,6 +1,7 @@
1
1
  import { localName } from "./private/names.js";
2
2
  import { getUnwrappedFieldType } from "./private/field-wrapper.js";
3
3
  import { scalarDefaultValue } from "./private/scalars.js";
4
+ import { reifyWkt } from "./private/reify-wkt.js";
4
5
  interface CodegenInfo {
5
6
  readonly packageName: string;
6
7
  readonly localName: typeof localName;
@@ -8,8 +9,9 @@ interface CodegenInfo {
8
9
  readonly getUnwrappedFieldType: typeof getUnwrappedFieldType;
9
10
  readonly wktSourceFiles: readonly string[];
10
11
  readonly scalarDefaultValue: typeof scalarDefaultValue;
12
+ readonly reifyWkt: typeof reifyWkt;
11
13
  }
12
- declare type RuntimeSymbolName = "proto2" | "proto3" | "Message" | "PartialMessage" | "PlainMessage" | "FieldList" | "MessageType" | "BinaryReadOptions" | "BinaryWriteOptions" | "JsonReadOptions" | "JsonWriteOptions" | "JsonValue" | "JsonObject" | "protoInt64" | "ScalarType" | "MethodKind" | "MethodIdempotency";
14
+ declare type RuntimeSymbolName = "proto2" | "proto3" | "Message" | "PartialMessage" | "PlainMessage" | "FieldList" | "MessageType" | "BinaryReadOptions" | "BinaryWriteOptions" | "JsonReadOptions" | "JsonWriteOptions" | "JsonValue" | "JsonObject" | "protoInt64" | "ScalarType" | "MethodKind" | "MethodIdempotency" | "IMessageTypeRegistry";
13
15
  declare type RuntimeSymbolInfo = {
14
16
  typeOnly: boolean;
15
17
  publicImportPath: string;
@@ -1,6 +1,7 @@
1
1
  import type { PartialMessage, PlainMessage } from "../../message.js";
2
2
  import { Message } from "../../message.js";
3
3
  import type { JsonReadOptions, JsonValue, JsonWriteOptions } from "../../json-format.js";
4
+ import type { IMessageTypeRegistry } from "../../type-registry.js";
4
5
  import type { MessageType } from "../../message-type.js";
5
6
  import type { FieldList } from "../../field-list.js";
6
7
  import type { BinaryReadOptions } from "../../binary-format.js";
@@ -136,7 +137,8 @@ export declare class Any extends Message<Any> {
136
137
  fromJson(json: JsonValue, options?: Partial<JsonReadOptions>): this;
137
138
  packFrom(message: Message): void;
138
139
  unpackTo(target: Message): boolean;
139
- is(type: MessageType): boolean;
140
+ unpack(registry: IMessageTypeRegistry): Message | undefined;
141
+ is(type: MessageType | string): boolean;
140
142
  private typeNameToUrl;
141
143
  private typeUrlToName;
142
144
  static readonly runtime: import("../../private/proto-runtime.js").ProtoRuntime;
@@ -24,7 +24,7 @@ export declare class DoubleValue extends Message<DoubleValue> {
24
24
  static readonly typeName = "google.protobuf.DoubleValue";
25
25
  static readonly fields: FieldList;
26
26
  static readonly fieldWrapper: {
27
- wrapField(value: number | DoubleValue): DoubleValue;
27
+ wrapField(value: number): DoubleValue;
28
28
  unwrapField(value: DoubleValue): number;
29
29
  };
30
30
  static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): DoubleValue;
@@ -53,7 +53,7 @@ export declare class FloatValue extends Message<FloatValue> {
53
53
  static readonly typeName = "google.protobuf.FloatValue";
54
54
  static readonly fields: FieldList;
55
55
  static readonly fieldWrapper: {
56
- wrapField(value: number | FloatValue): FloatValue;
56
+ wrapField(value: number): FloatValue;
57
57
  unwrapField(value: FloatValue): number;
58
58
  };
59
59
  static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): FloatValue;
@@ -82,7 +82,7 @@ export declare class Int64Value extends Message<Int64Value> {
82
82
  static readonly typeName = "google.protobuf.Int64Value";
83
83
  static readonly fields: FieldList;
84
84
  static readonly fieldWrapper: {
85
- wrapField(value: bigint | Int64Value): Int64Value;
85
+ wrapField(value: bigint): Int64Value;
86
86
  unwrapField(value: Int64Value): bigint;
87
87
  };
88
88
  static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): Int64Value;
@@ -111,7 +111,7 @@ export declare class UInt64Value extends Message<UInt64Value> {
111
111
  static readonly typeName = "google.protobuf.UInt64Value";
112
112
  static readonly fields: FieldList;
113
113
  static readonly fieldWrapper: {
114
- wrapField(value: bigint | UInt64Value): UInt64Value;
114
+ wrapField(value: bigint): UInt64Value;
115
115
  unwrapField(value: UInt64Value): bigint;
116
116
  };
117
117
  static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): UInt64Value;
@@ -140,7 +140,7 @@ export declare class Int32Value extends Message<Int32Value> {
140
140
  static readonly typeName = "google.protobuf.Int32Value";
141
141
  static readonly fields: FieldList;
142
142
  static readonly fieldWrapper: {
143
- wrapField(value: number | Int32Value): Int32Value;
143
+ wrapField(value: number): Int32Value;
144
144
  unwrapField(value: Int32Value): number;
145
145
  };
146
146
  static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): Int32Value;
@@ -169,7 +169,7 @@ export declare class UInt32Value extends Message<UInt32Value> {
169
169
  static readonly typeName = "google.protobuf.UInt32Value";
170
170
  static readonly fields: FieldList;
171
171
  static readonly fieldWrapper: {
172
- wrapField(value: number | UInt32Value): UInt32Value;
172
+ wrapField(value: number): UInt32Value;
173
173
  unwrapField(value: UInt32Value): number;
174
174
  };
175
175
  static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): UInt32Value;
@@ -198,7 +198,7 @@ export declare class BoolValue extends Message<BoolValue> {
198
198
  static readonly typeName = "google.protobuf.BoolValue";
199
199
  static readonly fields: FieldList;
200
200
  static readonly fieldWrapper: {
201
- wrapField(value: boolean | BoolValue): BoolValue;
201
+ wrapField(value: boolean): BoolValue;
202
202
  unwrapField(value: BoolValue): boolean;
203
203
  };
204
204
  static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): BoolValue;
@@ -227,7 +227,7 @@ export declare class StringValue extends Message<StringValue> {
227
227
  static readonly typeName = "google.protobuf.StringValue";
228
228
  static readonly fields: FieldList;
229
229
  static readonly fieldWrapper: {
230
- wrapField(value: string | StringValue): StringValue;
230
+ wrapField(value: string): StringValue;
231
231
  unwrapField(value: StringValue): string;
232
232
  };
233
233
  static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): StringValue;
@@ -256,7 +256,7 @@ export declare class BytesValue extends Message<BytesValue> {
256
256
  static readonly typeName = "google.protobuf.BytesValue";
257
257
  static readonly fields: FieldList;
258
258
  static readonly fieldWrapper: {
259
- wrapField(value: Uint8Array | BytesValue): BytesValue;
259
+ wrapField(value: Uint8Array): BytesValue;
260
260
  unwrapField(value: BytesValue): Uint8Array;
261
261
  };
262
262
  static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): BytesValue;
@@ -56,6 +56,17 @@ export declare class Message<T extends Message<T> = AnyMessage> {
56
56
  * Serialize the message to a JSON string.
57
57
  */
58
58
  toJsonString(options?: Partial<JsonWriteStringOptions>): string;
59
+ /**
60
+ * Override for serializaton behavior. This will be invoked when calling
61
+ * JSON.stringify on this message (i.e. JSON.stringify(msg)).
62
+ *
63
+ * Note that this will not serialize google.protobuf.Any with a packed
64
+ * message because the protobuf JSON format specifies that it needs to be
65
+ * unpacked, and this is only possible with a type registry to look up the
66
+ * message type. As a result, attempting to serialize a message with this
67
+ * type will throw an Error.
68
+ */
69
+ toJSON(): JsonValue;
59
70
  /**
60
71
  * Retrieve the MessageType of this message - a singleton that represents
61
72
  * the protobuf message declaration and provides metadata for reflection-
@@ -67,7 +78,7 @@ export declare class Message<T extends Message<T> = AnyMessage> {
67
78
  * PlainMessage<T> strips all methods from a message, leaving only fields
68
79
  * and oneof groups.
69
80
  */
70
- export declare type PlainMessage<T extends Message> = {
81
+ export declare type PlainMessage<T extends Message<T>> = {
71
82
  [P in keyof T as T[P] extends Function ? never : P]: T[P];
72
83
  };
73
84
  /**
@@ -78,10 +89,10 @@ export declare type PlainMessage<T extends Message> = {
78
89
  * PartialMessage is similar to the built-in type Partial<T>, but recursive,
79
90
  * and respects `oneof` groups.
80
91
  */
81
- export declare type PartialMessage<T extends Message> = {
92
+ export declare type PartialMessage<T extends Message<T>> = {
82
93
  [P in keyof T as T[P] extends Function ? never : P]?: PartialField<T[P]>;
83
94
  };
84
- declare type PartialField<F> = F extends (Date | Uint8Array | bigint | boolean | string | number) ? F : F extends Array<infer U> ? Array<PartialField<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<PartialField<U>> : F extends Message ? PartialMessage<F> : F extends OneofSelectedMessage<infer C, infer V> ? {
95
+ declare type PartialField<F> = F extends (Date | Uint8Array | bigint | boolean | string | number) ? F : F extends Array<infer U> ? Array<PartialField<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<PartialField<U>> : F extends Message<infer U> ? PartialMessage<U> : F extends OneofSelectedMessage<infer C, infer V> ? {
85
96
  case: C;
86
97
  value: PartialMessage<V>;
87
98
  } : F extends {
@@ -92,7 +103,7 @@ declare type PartialField<F> = F extends (Date | Uint8Array | bigint | boolean |
92
103
  } ? {
93
104
  [key: string | number]: PartialMessage<U>;
94
105
  } : F;
95
- declare type OneofSelectedMessage<K extends string, M extends Message> = {
106
+ declare type OneofSelectedMessage<K extends string, M extends Message<M>> = {
96
107
  case: K;
97
108
  value: M;
98
109
  };
@@ -1,4 +1,4 @@
1
- import type { Message } from "../message.js";
1
+ import { Message } from "../message.js";
2
2
  import type { MessageType } from "../message-type.js";
3
3
  import type { DescField } from "../descriptor-set.js";
4
4
  import { ScalarType } from "../field.js";
@@ -6,7 +6,7 @@ import { ScalarType } from "../field.js";
6
6
  * A field wrapper unwraps a message to a primitive value that is more
7
7
  * ergonomic for use as a message field.
8
8
  *
9
- * Note that this feature exists for google/protobuf/{wrappers,struct}.proto
9
+ * Note that this feature exists for google/protobuf/wrappers.proto
10
10
  * and cannot be used to arbitrarily modify types in generated code.
11
11
  */
12
12
  export interface FieldWrapper<T extends Message<T> = any, U = any> {
@@ -14,15 +14,12 @@ export interface FieldWrapper<T extends Message<T> = any, U = any> {
14
14
  unwrapField(value: T): U;
15
15
  }
16
16
  /**
17
- * Wrap field values whose message type has a wrapper.
17
+ * Wrap a primitive message field value in its corresponding wrapper
18
+ * message. This function is idempotent.
18
19
  */
19
20
  export declare function wrapField<T extends Message<T>>(type: MessageType<T>, value: any): T;
20
- /**
21
- * Unwrap field values whose message type has a wrapper.
22
- */
23
- export declare function unwrapField<T extends Message<T>>(type: MessageType<T>, value: T): any;
24
21
  /**
25
22
  * If the given field uses one of the well-known wrapper types, return
26
- * the base type it wraps.
23
+ * the primitive type it wraps.
27
24
  */
28
25
  export declare function getUnwrappedFieldType(field: DescField): ScalarType | undefined;