@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
@@ -17,10 +17,12 @@ exports.codegenInfo = void 0;
17
17
  const names_js_1 = require("./private/names.js");
18
18
  const field_wrapper_js_1 = require("./private/field-wrapper.js");
19
19
  const scalars_js_1 = require("./private/scalars.js");
20
+ const reify_wkt_js_1 = require("./private/reify-wkt.js");
20
21
  const packageName = "@bufbuild/protobuf";
21
22
  exports.codegenInfo = {
22
23
  packageName,
23
24
  localName: names_js_1.localName,
25
+ reifyWkt: reify_wkt_js_1.reifyWkt,
24
26
  getUnwrappedFieldType: field_wrapper_js_1.getUnwrappedFieldType,
25
27
  scalarDefaultValue: scalars_js_1.scalarDefaultValue,
26
28
  // prettier-ignore
@@ -42,6 +44,7 @@ exports.codegenInfo = {
42
44
  ScalarType: { typeOnly: false, privateImportPath: "./field.js", publicImportPath: packageName },
43
45
  MethodKind: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
44
46
  MethodIdempotency: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
47
+ IMessageTypeRegistry: { typeOnly: true, privateImportPath: "./type-registry.js", publicImportPath: packageName },
45
48
  },
46
49
  wktSourceFiles: [
47
50
  "google/protobuf/compiler/plugin.proto",
@@ -170,6 +170,9 @@ class Any extends message_js_1.Message {
170
170
  if (json === null || Array.isArray(json) || typeof json != "object") {
171
171
  throw new Error(`cannot decode message google.protobuf.Any from JSON: expected object but got ${json === null ? "null" : Array.isArray(json) ? "array" : typeof json}`);
172
172
  }
173
+ if (Object.keys(json).length == 0) {
174
+ return this;
175
+ }
173
176
  const typeUrl = json["@type"];
174
177
  if (typeof typeUrl != "string" || typeUrl == "") {
175
178
  throw new Error(`cannot decode message google.protobuf.Any from JSON: "@type" is empty`);
@@ -201,8 +204,29 @@ class Any extends message_js_1.Message {
201
204
  target.fromBinary(this.value);
202
205
  return true;
203
206
  }
207
+ unpack(registry) {
208
+ if (this.typeUrl === "") {
209
+ return undefined;
210
+ }
211
+ const messageType = registry.findMessage(this.typeUrlToName(this.typeUrl));
212
+ if (!messageType) {
213
+ return undefined;
214
+ }
215
+ return messageType.fromBinary(this.value);
216
+ }
204
217
  is(type) {
205
- return this.typeUrl === this.typeNameToUrl(type.typeName);
218
+ if (this.typeUrl === '') {
219
+ return false;
220
+ }
221
+ const name = this.typeUrlToName(this.typeUrl);
222
+ let typeName = '';
223
+ if (typeof type === 'string') {
224
+ typeName = type;
225
+ }
226
+ else {
227
+ typeName = type.typeName;
228
+ }
229
+ return name === typeName;
206
230
  }
207
231
  typeNameToUrl(name) {
208
232
  return `type.googleapis.com/${name}`;
@@ -14,7 +14,7 @@
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.ListValue = exports.Value = exports.Struct = exports.NullValue = void 0;
17
- // @generated by protoc-gen-es v0.2.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
17
+ // @generated by protoc-gen-es v0.3.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
18
18
  // @generated from file google/protobuf/struct.proto (package google.protobuf, syntax proto3)
19
19
  /* eslint-disable */
20
20
  const proto3_js_1 = require("../../proto3.js");
@@ -14,7 +14,7 @@
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.Option = exports.EnumValue = exports.Enum = exports.Field_Cardinality = exports.Field_Kind = exports.Field = exports.Type = exports.Syntax = void 0;
17
- // @generated by protoc-gen-es v0.2.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
17
+ // @generated by protoc-gen-es v0.3.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
18
18
  // @generated from file google/protobuf/type.proto (package google.protobuf, syntax proto3)
19
19
  /* eslint-disable */
20
20
  const proto3_js_1 = require("../../proto3.js");
@@ -73,7 +73,7 @@ DoubleValue.fields = proto3_js_1.proto3.util.newFieldList(() => [
73
73
  ]);
74
74
  DoubleValue.fieldWrapper = {
75
75
  wrapField(value) {
76
- return value instanceof DoubleValue ? value : new DoubleValue({ value });
76
+ return new DoubleValue({ value });
77
77
  },
78
78
  unwrapField(value) {
79
79
  return value.value;
@@ -134,7 +134,7 @@ FloatValue.fields = proto3_js_1.proto3.util.newFieldList(() => [
134
134
  ]);
135
135
  FloatValue.fieldWrapper = {
136
136
  wrapField(value) {
137
- return value instanceof FloatValue ? value : new FloatValue({ value });
137
+ return new FloatValue({ value });
138
138
  },
139
139
  unwrapField(value) {
140
140
  return value.value;
@@ -195,7 +195,7 @@ Int64Value.fields = proto3_js_1.proto3.util.newFieldList(() => [
195
195
  ]);
196
196
  Int64Value.fieldWrapper = {
197
197
  wrapField(value) {
198
- return value instanceof Int64Value ? value : new Int64Value({ value });
198
+ return new Int64Value({ value });
199
199
  },
200
200
  unwrapField(value) {
201
201
  return value.value;
@@ -256,7 +256,7 @@ UInt64Value.fields = proto3_js_1.proto3.util.newFieldList(() => [
256
256
  ]);
257
257
  UInt64Value.fieldWrapper = {
258
258
  wrapField(value) {
259
- return value instanceof UInt64Value ? value : new UInt64Value({ value });
259
+ return new UInt64Value({ value });
260
260
  },
261
261
  unwrapField(value) {
262
262
  return value.value;
@@ -317,7 +317,7 @@ Int32Value.fields = proto3_js_1.proto3.util.newFieldList(() => [
317
317
  ]);
318
318
  Int32Value.fieldWrapper = {
319
319
  wrapField(value) {
320
- return value instanceof Int32Value ? value : new Int32Value({ value });
320
+ return new Int32Value({ value });
321
321
  },
322
322
  unwrapField(value) {
323
323
  return value.value;
@@ -378,7 +378,7 @@ UInt32Value.fields = proto3_js_1.proto3.util.newFieldList(() => [
378
378
  ]);
379
379
  UInt32Value.fieldWrapper = {
380
380
  wrapField(value) {
381
- return value instanceof UInt32Value ? value : new UInt32Value({ value });
381
+ return new UInt32Value({ value });
382
382
  },
383
383
  unwrapField(value) {
384
384
  return value.value;
@@ -439,7 +439,7 @@ BoolValue.fields = proto3_js_1.proto3.util.newFieldList(() => [
439
439
  ]);
440
440
  BoolValue.fieldWrapper = {
441
441
  wrapField(value) {
442
- return value instanceof BoolValue ? value : new BoolValue({ value });
442
+ return new BoolValue({ value });
443
443
  },
444
444
  unwrapField(value) {
445
445
  return value.value;
@@ -500,7 +500,7 @@ StringValue.fields = proto3_js_1.proto3.util.newFieldList(() => [
500
500
  ]);
501
501
  StringValue.fieldWrapper = {
502
502
  wrapField(value) {
503
- return value instanceof StringValue ? value : new StringValue({ value });
503
+ return new StringValue({ value });
504
504
  },
505
505
  unwrapField(value) {
506
506
  return value.value;
@@ -561,7 +561,7 @@ BytesValue.fields = proto3_js_1.proto3.util.newFieldList(() => [
561
561
  ]);
562
562
  BytesValue.fieldWrapper = {
563
563
  wrapField(value) {
564
- return value instanceof BytesValue ? value : new BytesValue({ value });
564
+ return new BytesValue({ value });
565
565
  },
566
566
  unwrapField(value) {
567
567
  return value.value;
@@ -32,7 +32,6 @@ class Message {
32
32
  * Create a deep copy.
33
33
  */
34
34
  clone() {
35
- // return this.getType().runtime.util.clone(this);
36
35
  return this.getType().runtime.util.clone(this);
37
36
  }
38
37
  /**
@@ -88,6 +87,21 @@ class Message {
88
87
  const value = this.toJson(options);
89
88
  return JSON.stringify(value, null, (_a = options === null || options === void 0 ? void 0 : options.prettySpaces) !== null && _a !== void 0 ? _a : 0);
90
89
  }
90
+ /**
91
+ * Override for serializaton behavior. This will be invoked when calling
92
+ * JSON.stringify on this message (i.e. JSON.stringify(msg)).
93
+ *
94
+ * Note that this will not serialize google.protobuf.Any with a packed
95
+ * message because the protobuf JSON format specifies that it needs to be
96
+ * unpacked, and this is only possible with a type registry to look up the
97
+ * message type. As a result, attempting to serialize a message with this
98
+ * type will throw an Error.
99
+ */
100
+ toJSON() {
101
+ return this.toJson({
102
+ emitDefaultValues: true,
103
+ });
104
+ }
91
105
  /**
92
106
  * Retrieve the MessageType of this message - a singleton that represents
93
107
  * the protobuf message declaration and provides metadata for reflection-
@@ -15,6 +15,7 @@
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.writePacked = exports.writeScalar = exports.writeMessageField = exports.writeMapEntry = exports.makeBinaryFormatCommon = void 0;
17
17
  const binary_encoding_js_1 = require("../binary-encoding.js");
18
+ const message_js_1 = require("../message.js");
18
19
  const field_js_1 = require("../field.js");
19
20
  const field_wrapper_js_1 = require("./field-wrapper.js");
20
21
  const scalars_js_1 = require("./scalars.js");
@@ -114,11 +115,16 @@ function makeBinaryFormatCommon() {
114
115
  target[localName].push(messageType.fromBinary(reader.bytes(), options));
115
116
  }
116
117
  else {
117
- if (target[localName] instanceof messageType) {
118
+ if (target[localName] instanceof message_js_1.Message) {
118
119
  target[localName].fromBinary(reader.bytes(), options);
119
120
  }
120
121
  else {
121
- target[localName] = (0, field_wrapper_js_1.unwrapField)(messageType, messageType.fromBinary(reader.bytes(), options));
122
+ target[localName] = messageType.fromBinary(reader.bytes(), options);
123
+ if (messageType.fieldWrapper &&
124
+ !field.oneof &&
125
+ !field.repeated) {
126
+ target[localName] = messageType.fieldWrapper.unwrapField(target[localName]);
127
+ }
122
128
  }
123
129
  }
124
130
  break;
@@ -13,31 +13,23 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.getUnwrappedFieldType = exports.unwrapField = exports.wrapField = void 0;
16
+ exports.getUnwrappedFieldType = exports.wrapField = void 0;
17
+ const message_js_1 = require("../message.js");
17
18
  const field_js_1 = require("../field.js");
18
19
  /**
19
- * Wrap field values whose message type has a wrapper.
20
+ * Wrap a primitive message field value in its corresponding wrapper
21
+ * message. This function is idempotent.
20
22
  */
21
23
  function wrapField(type, value) {
22
- if (value instanceof type) {
24
+ if (value instanceof message_js_1.Message || !type.fieldWrapper) {
23
25
  return value;
24
26
  }
25
- if (type.fieldWrapper) {
26
- return type.fieldWrapper.wrapField(value);
27
- }
28
- throw new Error(`cannot unwrap field value, ${type.typeName} does not define a field wrapper`);
27
+ return type.fieldWrapper.wrapField(value);
29
28
  }
30
29
  exports.wrapField = wrapField;
31
- /**
32
- * Unwrap field values whose message type has a wrapper.
33
- */
34
- function unwrapField(type, value) {
35
- return type.fieldWrapper ? type.fieldWrapper.unwrapField(value) : value;
36
- }
37
- exports.unwrapField = unwrapField;
38
30
  /**
39
31
  * If the given field uses one of the well-known wrapper types, return
40
- * the base type it wraps.
32
+ * the primitive type it wraps.
41
33
  */
42
34
  function getUnwrappedFieldType(field) {
43
35
  if (field.fieldKind !== "message") {
@@ -14,7 +14,7 @@
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.makeJsonFormatCommon = void 0;
17
- const field_wrapper_js_1 = require("./field-wrapper.js");
17
+ const message_js_1 = require("../message.js");
18
18
  const field_js_1 = require("../field.js");
19
19
  const assert_js_1 = require("./assert.js");
20
20
  const proto_int64_js_1 = require("../proto-int64.js");
@@ -174,10 +174,15 @@ function makeJsonFormatCommon(makeWriteField) {
174
174
  }
175
175
  continue;
176
176
  }
177
- const targetMessage = target[localName] === undefined
178
- ? new messageType()
179
- : (0, field_wrapper_js_1.wrapField)(messageType, target[localName]);
180
- target[localName] = (0, field_wrapper_js_1.unwrapField)(messageType, targetMessage.fromJson(jsonValue, options));
177
+ if (target[localName] instanceof message_js_1.Message) {
178
+ target[localName].fromJson(jsonValue, options);
179
+ }
180
+ else {
181
+ target[localName] = messageType.fromJson(jsonValue, options);
182
+ if (messageType.fieldWrapper && !field.oneof) {
183
+ target[localName] = messageType.fieldWrapper.unwrapField(target[localName]);
184
+ }
185
+ }
181
186
  break;
182
187
  case "enum":
183
188
  const enumValue = readEnum(field.T, jsonValue, options.ignoreUnknownFields);
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ // Copyright 2021-2022 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.reifyWkt = void 0;
17
+ const field_js_1 = require("../field.js");
18
+ /**
19
+ * Reifies a given DescMessage into a more concrete object representing its
20
+ * respective well-known type. The returned object will contain properties
21
+ * representing the WKT's defined fields.
22
+ *
23
+ * Useful during code generation when immediate access to a particular field
24
+ * is needed without having to search the object's typename and DescField list.
25
+ *
26
+ * Returns undefined if the WKT cannot be completely constructed via the
27
+ * DescMessage.
28
+ */
29
+ function reifyWkt(message) {
30
+ switch (message.typeName) {
31
+ case "google.protobuf.Any": {
32
+ const typeUrl = message.fields.find((f) => f.number == 1 &&
33
+ f.fieldKind == "scalar" &&
34
+ f.scalar === field_js_1.ScalarType.STRING);
35
+ const value = message.fields.find((f) => f.number == 2 &&
36
+ f.fieldKind == "scalar" &&
37
+ f.scalar === field_js_1.ScalarType.BYTES);
38
+ if (typeUrl && value) {
39
+ return {
40
+ typeName: message.typeName,
41
+ typeUrl,
42
+ value,
43
+ };
44
+ }
45
+ break;
46
+ }
47
+ case "google.protobuf.Timestamp": {
48
+ const seconds = message.fields.find((f) => f.number == 1 &&
49
+ f.fieldKind == "scalar" &&
50
+ f.scalar === field_js_1.ScalarType.INT64);
51
+ const nanos = message.fields.find((f) => f.number == 2 &&
52
+ f.fieldKind == "scalar" &&
53
+ f.scalar === field_js_1.ScalarType.INT32);
54
+ if (seconds && nanos) {
55
+ return {
56
+ typeName: message.typeName,
57
+ seconds,
58
+ nanos,
59
+ };
60
+ }
61
+ break;
62
+ }
63
+ case "google.protobuf.Duration": {
64
+ const seconds = message.fields.find((f) => f.number == 1 &&
65
+ f.fieldKind == "scalar" &&
66
+ f.scalar === field_js_1.ScalarType.INT64);
67
+ const nanos = message.fields.find((f) => f.number == 2 &&
68
+ f.fieldKind == "scalar" &&
69
+ f.scalar === field_js_1.ScalarType.INT32);
70
+ if (seconds && nanos) {
71
+ return {
72
+ typeName: message.typeName,
73
+ seconds,
74
+ nanos,
75
+ };
76
+ }
77
+ break;
78
+ }
79
+ case "google.protobuf.Struct": {
80
+ const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
81
+ if ((fields === null || fields === void 0 ? void 0 : fields.fieldKind) !== "map" ||
82
+ fields.mapValue.kind !== "message" ||
83
+ fields.mapValue.message.typeName !== "google.protobuf.Value") {
84
+ break;
85
+ }
86
+ return { typeName: message.typeName, fields };
87
+ }
88
+ case "google.protobuf.Value": {
89
+ const kind = message.oneofs.find((o) => o.name === "kind");
90
+ const nullValue = message.fields.find((f) => f.number == 1 && f.oneof === kind);
91
+ if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.fieldKind) !== "enum" ||
92
+ nullValue.enum.typeName !== "google.protobuf.NullValue") {
93
+ return undefined;
94
+ }
95
+ const numberValue = message.fields.find((f) => f.number == 2 &&
96
+ f.fieldKind == "scalar" &&
97
+ f.scalar === field_js_1.ScalarType.DOUBLE &&
98
+ f.oneof === kind);
99
+ const stringValue = message.fields.find((f) => f.number == 3 &&
100
+ f.fieldKind == "scalar" &&
101
+ f.scalar === field_js_1.ScalarType.STRING &&
102
+ f.oneof === kind);
103
+ const boolValue = message.fields.find((f) => f.number == 4 &&
104
+ f.fieldKind == "scalar" &&
105
+ f.scalar === field_js_1.ScalarType.BOOL &&
106
+ f.oneof === kind);
107
+ const structValue = message.fields.find((f) => f.number == 5 && f.oneof === kind);
108
+ if ((structValue === null || structValue === void 0 ? void 0 : structValue.fieldKind) !== "message" ||
109
+ structValue.message.typeName !== "google.protobuf.Struct") {
110
+ return undefined;
111
+ }
112
+ const listValue = message.fields.find((f) => f.number == 6 && f.oneof === kind);
113
+ if ((listValue === null || listValue === void 0 ? void 0 : listValue.fieldKind) !== "message" ||
114
+ listValue.message.typeName !== "google.protobuf.ListValue") {
115
+ return undefined;
116
+ }
117
+ if (kind && numberValue && stringValue && boolValue) {
118
+ return {
119
+ typeName: message.typeName,
120
+ kind,
121
+ nullValue,
122
+ numberValue,
123
+ stringValue,
124
+ boolValue,
125
+ structValue,
126
+ listValue,
127
+ };
128
+ }
129
+ break;
130
+ }
131
+ case "google.protobuf.ListValue": {
132
+ const values = message.fields.find((f) => f.number == 1 && f.repeated);
133
+ if ((values === null || values === void 0 ? void 0 : values.fieldKind) != "message" ||
134
+ values.message.typeName !== "google.protobuf.Value") {
135
+ break;
136
+ }
137
+ return { typeName: message.typeName, values };
138
+ }
139
+ case "google.protobuf.FieldMask": {
140
+ const paths = message.fields.find((f) => f.number == 1 &&
141
+ f.fieldKind == "scalar" &&
142
+ f.scalar === field_js_1.ScalarType.STRING &&
143
+ f.repeated);
144
+ if (paths) {
145
+ return { typeName: message.typeName, paths };
146
+ }
147
+ break;
148
+ }
149
+ case "google.protobuf.DoubleValue":
150
+ case "google.protobuf.FloatValue":
151
+ case "google.protobuf.Int64Value":
152
+ case "google.protobuf.UInt64Value":
153
+ case "google.protobuf.Int32Value":
154
+ case "google.protobuf.UInt32Value":
155
+ case "google.protobuf.BoolValue":
156
+ case "google.protobuf.StringValue":
157
+ case "google.protobuf.BytesValue": {
158
+ const value = message.fields.find((f) => f.number == 1 && f.name == "value");
159
+ if (!value) {
160
+ break;
161
+ }
162
+ if (value.fieldKind !== "scalar") {
163
+ break;
164
+ }
165
+ return { typeName: message.typeName, value };
166
+ }
167
+ }
168
+ return undefined;
169
+ }
170
+ exports.reifyWkt = reifyWkt;
@@ -15,6 +15,7 @@
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.makeUtilCommon = void 0;
17
17
  const enum_js_1 = require("./enum.js");
18
+ const message_js_1 = require("../message.js");
18
19
  const field_js_1 = require("../field.js");
19
20
  const scalars_js_1 = require("./scalars.js");
20
21
  /* 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 */
@@ -139,10 +140,7 @@ function makeUtilCommon() {
139
140
  }
140
141
  throw new Error(`oneof cannot contain ${s.kind}`);
141
142
  case "map":
142
- const keys = Object.keys(va);
143
- if (keys.some((k) => vb[k] === undefined)) {
144
- return false;
145
- }
143
+ const keys = Object.keys(va).concat(Object.keys(vb));
146
144
  switch (m.V.kind) {
147
145
  case "message":
148
146
  const messageType = m.V.T;
@@ -192,21 +190,13 @@ function cloneSingularField(field, value) {
192
190
  if (value === undefined) {
193
191
  return value;
194
192
  }
195
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- unmatched "map" is unsupported
196
- switch (field.kind) {
197
- case "enum":
198
- return value;
199
- case "scalar":
200
- if (field.T === field_js_1.ScalarType.BYTES) {
201
- const c = new Uint8Array(value.byteLength);
202
- c.set(value);
203
- return c;
204
- }
205
- return value;
206
- case "message":
207
- if (field.T.fieldWrapper) {
208
- return field.T.fieldWrapper.unwrapField(field.T.fieldWrapper.wrapField(value).clone());
209
- }
210
- return value.clone();
193
+ if (value instanceof message_js_1.Message) {
194
+ return value.clone();
195
+ }
196
+ if (value instanceof Uint8Array) {
197
+ const c = new Uint8Array(value.byteLength);
198
+ c.set(value);
199
+ return c;
211
200
  }
201
+ return value;
212
202
  }
@@ -14,10 +14,12 @@
14
14
  import { localName } from "./private/names.js";
15
15
  import { getUnwrappedFieldType } from "./private/field-wrapper.js";
16
16
  import { scalarDefaultValue } from "./private/scalars.js";
17
+ import { reifyWkt } from "./private/reify-wkt.js";
17
18
  const packageName = "@bufbuild/protobuf";
18
19
  export const codegenInfo = {
19
20
  packageName,
20
21
  localName,
22
+ reifyWkt,
21
23
  getUnwrappedFieldType,
22
24
  scalarDefaultValue,
23
25
  // prettier-ignore
@@ -39,6 +41,7 @@ export const codegenInfo = {
39
41
  ScalarType: { typeOnly: false, privateImportPath: "./field.js", publicImportPath: packageName },
40
42
  MethodKind: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
41
43
  MethodIdempotency: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
44
+ IMessageTypeRegistry: { typeOnly: true, privateImportPath: "./type-registry.js", publicImportPath: packageName },
42
45
  },
43
46
  wktSourceFiles: [
44
47
  "google/protobuf/compiler/plugin.proto",
@@ -167,6 +167,9 @@ export class Any extends Message {
167
167
  if (json === null || Array.isArray(json) || typeof json != "object") {
168
168
  throw new Error(`cannot decode message google.protobuf.Any from JSON: expected object but got ${json === null ? "null" : Array.isArray(json) ? "array" : typeof json}`);
169
169
  }
170
+ if (Object.keys(json).length == 0) {
171
+ return this;
172
+ }
170
173
  const typeUrl = json["@type"];
171
174
  if (typeof typeUrl != "string" || typeUrl == "") {
172
175
  throw new Error(`cannot decode message google.protobuf.Any from JSON: "@type" is empty`);
@@ -198,8 +201,29 @@ export class Any extends Message {
198
201
  target.fromBinary(this.value);
199
202
  return true;
200
203
  }
204
+ unpack(registry) {
205
+ if (this.typeUrl === "") {
206
+ return undefined;
207
+ }
208
+ const messageType = registry.findMessage(this.typeUrlToName(this.typeUrl));
209
+ if (!messageType) {
210
+ return undefined;
211
+ }
212
+ return messageType.fromBinary(this.value);
213
+ }
201
214
  is(type) {
202
- return this.typeUrl === this.typeNameToUrl(type.typeName);
215
+ if (this.typeUrl === '') {
216
+ return false;
217
+ }
218
+ const name = this.typeUrlToName(this.typeUrl);
219
+ let typeName = '';
220
+ if (typeof type === 'string') {
221
+ typeName = type;
222
+ }
223
+ else {
224
+ typeName = type.typeName;
225
+ }
226
+ return name === typeName;
203
227
  }
204
228
  typeNameToUrl(name) {
205
229
  return `type.googleapis.com/${name}`;
@@ -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
- // @generated by protoc-gen-es v0.2.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
14
+ // @generated by protoc-gen-es v0.3.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
15
15
  // @generated from file google/protobuf/struct.proto (package google.protobuf, syntax proto3)
16
16
  /* eslint-disable */
17
17
  import { proto3 } from "../../proto3.js";
@@ -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
- // @generated by protoc-gen-es v0.2.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
14
+ // @generated by protoc-gen-es v0.3.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
15
15
  // @generated from file google/protobuf/type.proto (package google.protobuf, syntax proto3)
16
16
  /* eslint-disable */
17
17
  import { proto3 } from "../../proto3.js";