@bufbuild/protobuf 0.2.1 → 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.
@@ -44,6 +44,7 @@ exports.codegenInfo = {
44
44
  ScalarType: { typeOnly: false, privateImportPath: "./field.js", publicImportPath: packageName },
45
45
  MethodKind: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
46
46
  MethodIdempotency: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
47
+ IMessageTypeRegistry: { typeOnly: true, privateImportPath: "./type-registry.js", publicImportPath: packageName },
47
48
  },
48
49
  wktSourceFiles: [
49
50
  "google/protobuf/compiler/plugin.proto",
@@ -204,8 +204,29 @@ class Any extends message_js_1.Message {
204
204
  target.fromBinary(this.value);
205
205
  return true;
206
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
+ }
207
217
  is(type) {
208
- 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;
209
230
  }
210
231
  typeNameToUrl(name) {
211
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.1 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.1 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);
@@ -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
  }
@@ -41,6 +41,7 @@ export const codegenInfo = {
41
41
  ScalarType: { typeOnly: false, privateImportPath: "./field.js", publicImportPath: packageName },
42
42
  MethodKind: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
43
43
  MethodIdempotency: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
44
+ IMessageTypeRegistry: { typeOnly: true, privateImportPath: "./type-registry.js", publicImportPath: packageName },
44
45
  },
45
46
  wktSourceFiles: [
46
47
  "google/protobuf/compiler/plugin.proto",
@@ -201,8 +201,29 @@ export class Any extends Message {
201
201
  target.fromBinary(this.value);
202
202
  return true;
203
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
+ }
204
214
  is(type) {
205
- 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;
206
227
  }
207
228
  typeNameToUrl(name) {
208
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.1 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.1 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";
@@ -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);
@@ -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
  */
@@ -11,7 +11,7 @@ interface CodegenInfo {
11
11
  readonly scalarDefaultValue: typeof scalarDefaultValue;
12
12
  readonly reifyWkt: typeof reifyWkt;
13
13
  }
14
- 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";
15
15
  declare type RuntimeSymbolInfo = {
16
16
  typeOnly: boolean;
17
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;
@@ -30,7 +30,7 @@ export interface Util {
30
30
  * Compares two messages of the same type recursively.
31
31
  * Will also return true if both messages are `undefined` or `null`.
32
32
  */
33
- equals<T extends Message<T>>(type: MessageType, a: T | PlainMessage<T> | undefined | null, b: T | PlainMessage<T> | undefined | null): boolean;
33
+ equals<T extends Message<T>>(type: MessageType<T>, a: T | PlainMessage<T> | undefined | null, b: T | PlainMessage<T> | undefined | null): boolean;
34
34
  /**
35
35
  * Create a deep copy.
36
36
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protobuf",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "license": "(Apache-2.0 AND BSD-3-Clause)",
5
5
  "description": "A complete implementation of Protocol Buffers in TypeScript, suitable for web browsers and Node.js.",
6
6
  "repository": {