@bufbuild/protobuf 2.0.0-alpha.1 → 2.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cjs/clone.d.ts +1 -1
- package/dist/cjs/clone.js +2 -2
- package/dist/cjs/codegenv1/boot.d.ts +1 -1
- package/dist/cjs/codegenv1/boot.js +3 -5
- package/dist/cjs/codegenv1/embed.d.ts +2 -2
- package/dist/cjs/codegenv1/embed.js +51 -46
- package/dist/cjs/codegenv1/enum.d.ts +1 -1
- package/dist/cjs/codegenv1/enum.js +2 -4
- package/dist/cjs/codegenv1/extension.d.ts +1 -1
- package/dist/cjs/codegenv1/file.d.ts +1 -1
- package/dist/cjs/codegenv1/file.js +5 -7
- package/dist/cjs/codegenv1/index.d.ts +1 -0
- package/dist/cjs/codegenv1/index.js +1 -0
- package/dist/cjs/codegenv1/message.d.ts +1 -1
- package/dist/cjs/codegenv1/restore-json-names.d.ts +3 -0
- package/dist/cjs/codegenv1/restore-json-names.js +3 -0
- package/dist/cjs/codegenv1/scalar.d.ts +5 -0
- package/dist/cjs/codegenv1/scalar.js +39 -0
- package/dist/cjs/codegenv1/service.d.ts +3 -3
- package/dist/cjs/codegenv1/symbols.d.ts +9 -0
- package/dist/cjs/codegenv1/symbols.js +9 -0
- package/dist/cjs/codegenv1/types.d.ts +59 -8
- package/dist/cjs/create.d.ts +1 -1
- package/dist/cjs/create.js +13 -23
- package/dist/cjs/{desc-types.d.ts → descriptors.d.ts} +66 -17
- package/dist/cjs/descriptors.js +53 -0
- package/dist/cjs/equals.d.ts +1 -1
- package/dist/cjs/extensions.d.ts +21 -1
- package/dist/cjs/extensions.js +42 -10
- package/dist/cjs/fields.d.ts +17 -16
- package/dist/cjs/fields.js +17 -10
- package/dist/cjs/from-binary.d.ts +2 -2
- package/dist/cjs/from-binary.js +31 -41
- package/dist/cjs/from-json.d.ts +4 -4
- package/dist/cjs/from-json.js +200 -185
- package/dist/cjs/index.d.ts +3 -2
- package/dist/cjs/index.js +5 -2
- package/dist/cjs/is-message.d.ts +1 -1
- package/dist/cjs/proto-int64.d.ts +4 -1
- package/dist/cjs/proto-int64.js +16 -6
- package/dist/cjs/reflect/error.d.ts +1 -1
- package/dist/cjs/reflect/guard.d.ts +1 -1
- package/dist/cjs/reflect/index.d.ts +0 -1
- package/dist/cjs/reflect/index.js +0 -1
- package/dist/cjs/reflect/names.d.ts +1 -13
- package/dist/cjs/reflect/names.js +2 -40
- package/dist/cjs/reflect/nested-types.d.ts +11 -1
- package/dist/cjs/reflect/nested-types.js +34 -1
- package/dist/cjs/reflect/reflect-check.d.ts +10 -1
- package/dist/cjs/reflect/reflect-check.js +126 -9
- package/dist/cjs/reflect/reflect-types.d.ts +162 -14
- package/dist/cjs/reflect/reflect.d.ts +12 -3
- package/dist/cjs/reflect/reflect.js +316 -290
- package/dist/cjs/reflect/scalar.d.ts +3 -55
- package/dist/cjs/reflect/scalar.js +26 -220
- package/dist/cjs/reflect/unsafe.d.ts +1 -1
- package/dist/cjs/reflect/unsafe.js +38 -43
- package/dist/cjs/{reflect/registry.d.ts → registry.d.ts} +2 -2
- package/dist/cjs/{reflect/registry.js → registry.js} +100 -73
- package/dist/cjs/to-binary.d.ts +1 -1
- package/dist/cjs/to-binary.js +28 -30
- package/dist/cjs/to-json.d.ts +7 -6
- package/dist/cjs/to-json.js +63 -70
- package/dist/cjs/types.d.ts +1 -1
- package/dist/cjs/wire/base64-encoding.js +4 -4
- package/dist/cjs/wire/binary-encoding.d.ts +25 -3
- package/dist/cjs/wire/binary-encoding.js +70 -18
- package/dist/cjs/wire/size-delimited.d.ts +1 -1
- package/dist/cjs/wire/text-format.d.ts +1 -2
- package/dist/cjs/wire/text-format.js +21 -19
- package/dist/cjs/wkt/any.d.ts +3 -3
- package/dist/cjs/wkt/gen/google/protobuf/any_pb.d.ts +7 -0
- package/dist/cjs/wkt/gen/google/protobuf/any_pb.js +7 -2
- package/dist/cjs/wkt/gen/google/protobuf/api_pb.d.ts +15 -0
- package/dist/cjs/wkt/gen/google/protobuf/api_pb.js +15 -6
- package/dist/cjs/wkt/gen/google/protobuf/compiler/plugin_pb.d.ts +22 -0
- package/dist/cjs/wkt/gen/google/protobuf/compiler/plugin_pb.js +22 -9
- package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.d.ts +192 -0
- package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.js +187 -84
- package/dist/cjs/wkt/gen/google/protobuf/duration_pb.d.ts +7 -0
- package/dist/cjs/wkt/gen/google/protobuf/duration_pb.js +7 -2
- package/dist/cjs/wkt/gen/google/protobuf/empty_pb.d.ts +7 -0
- package/dist/cjs/wkt/gen/google/protobuf/empty_pb.js +7 -2
- package/dist/cjs/wkt/gen/google/protobuf/field_mask_pb.d.ts +7 -0
- package/dist/cjs/wkt/gen/google/protobuf/field_mask_pb.js +7 -2
- package/dist/cjs/wkt/gen/google/protobuf/source_context_pb.d.ts +7 -0
- package/dist/cjs/wkt/gen/google/protobuf/source_context_pb.js +7 -2
- package/dist/cjs/wkt/gen/google/protobuf/struct_pb.d.ts +18 -0
- package/dist/cjs/wkt/gen/google/protobuf/struct_pb.js +18 -7
- package/dist/cjs/wkt/gen/google/protobuf/timestamp_pb.d.ts +7 -0
- package/dist/cjs/wkt/gen/google/protobuf/timestamp_pb.js +7 -2
- package/dist/cjs/wkt/gen/google/protobuf/type_pb.d.ts +32 -0
- package/dist/cjs/wkt/gen/google/protobuf/type_pb.js +32 -13
- package/dist/cjs/wkt/gen/google/protobuf/wrappers_pb.d.ts +39 -0
- package/dist/cjs/wkt/gen/google/protobuf/wrappers_pb.js +39 -18
- package/dist/cjs/wkt/wrappers.d.ts +1 -1
- package/dist/esm/clone.d.ts +1 -1
- package/dist/esm/clone.js +1 -1
- package/dist/esm/codegenv1/boot.d.ts +1 -1
- package/dist/esm/codegenv1/boot.js +3 -5
- package/dist/esm/codegenv1/embed.d.ts +2 -2
- package/dist/esm/codegenv1/embed.js +34 -29
- package/dist/esm/codegenv1/enum.d.ts +1 -1
- package/dist/esm/codegenv1/enum.js +2 -4
- package/dist/esm/codegenv1/extension.d.ts +1 -1
- package/dist/esm/codegenv1/file.d.ts +1 -1
- package/dist/esm/codegenv1/file.js +4 -6
- package/dist/esm/codegenv1/index.d.ts +1 -0
- package/dist/esm/codegenv1/index.js +1 -0
- package/dist/esm/codegenv1/message.d.ts +1 -1
- package/dist/esm/codegenv1/restore-json-names.d.ts +3 -0
- package/dist/esm/codegenv1/restore-json-names.js +3 -0
- package/dist/esm/codegenv1/scalar.d.ts +5 -0
- package/dist/esm/codegenv1/scalar.js +35 -0
- package/dist/esm/codegenv1/service.d.ts +3 -3
- package/dist/esm/codegenv1/symbols.d.ts +9 -0
- package/dist/esm/codegenv1/symbols.js +9 -0
- package/dist/esm/codegenv1/types.d.ts +59 -8
- package/dist/esm/create.d.ts +1 -1
- package/dist/esm/create.js +11 -21
- package/dist/esm/{desc-types.d.ts → descriptors.d.ts} +66 -17
- package/dist/esm/descriptors.js +50 -0
- package/dist/esm/equals.d.ts +1 -1
- package/dist/esm/extensions.d.ts +21 -1
- package/dist/esm/extensions.js +39 -9
- package/dist/esm/fields.d.ts +17 -16
- package/dist/esm/fields.js +17 -10
- package/dist/esm/from-binary.d.ts +2 -2
- package/dist/esm/from-binary.js +13 -23
- package/dist/esm/from-json.d.ts +4 -4
- package/dist/esm/from-json.js +193 -178
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.js +3 -2
- package/dist/esm/is-message.d.ts +1 -1
- package/dist/esm/proto-int64.d.ts +4 -1
- package/dist/esm/proto-int64.js +16 -6
- package/dist/esm/reflect/error.d.ts +1 -1
- package/dist/esm/reflect/guard.d.ts +1 -1
- package/dist/esm/reflect/index.d.ts +0 -1
- package/dist/esm/reflect/index.js +0 -1
- package/dist/esm/reflect/names.d.ts +1 -13
- package/dist/esm/reflect/names.js +1 -38
- package/dist/esm/reflect/nested-types.d.ts +11 -1
- package/dist/esm/reflect/nested-types.js +32 -0
- package/dist/esm/reflect/reflect-check.d.ts +10 -1
- package/dist/esm/reflect/reflect-check.js +119 -2
- package/dist/esm/reflect/reflect-types.d.ts +162 -14
- package/dist/esm/reflect/reflect-types.js +1 -0
- package/dist/esm/reflect/reflect.d.ts +12 -3
- package/dist/esm/reflect/reflect.js +305 -279
- package/dist/esm/reflect/scalar.d.ts +3 -55
- package/dist/esm/reflect/scalar.js +14 -205
- package/dist/esm/reflect/unsafe.d.ts +1 -1
- package/dist/esm/reflect/unsafe.js +38 -43
- package/dist/esm/{reflect/registry.d.ts → registry.d.ts} +2 -2
- package/dist/esm/{reflect/registry.js → registry.js} +90 -63
- package/dist/esm/to-binary.d.ts +1 -1
- package/dist/esm/to-binary.js +2 -4
- package/dist/esm/to-json.d.ts +7 -6
- package/dist/esm/to-json.js +48 -55
- package/dist/esm/types.d.ts +1 -1
- package/dist/esm/wire/base64-encoding.js +4 -4
- package/dist/esm/wire/binary-encoding.d.ts +25 -3
- package/dist/esm/wire/binary-encoding.js +63 -11
- package/dist/esm/wire/size-delimited.d.ts +1 -1
- package/dist/esm/wire/text-format.d.ts +1 -2
- package/dist/esm/wire/text-format.js +5 -3
- package/dist/esm/wkt/any.d.ts +3 -3
- package/dist/esm/wkt/gen/google/protobuf/any_pb.d.ts +7 -0
- package/dist/esm/wkt/gen/google/protobuf/any_pb.js +7 -2
- package/dist/esm/wkt/gen/google/protobuf/api_pb.d.ts +15 -0
- package/dist/esm/wkt/gen/google/protobuf/api_pb.js +15 -6
- package/dist/esm/wkt/gen/google/protobuf/compiler/plugin_pb.d.ts +22 -0
- package/dist/esm/wkt/gen/google/protobuf/compiler/plugin_pb.js +22 -9
- package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.d.ts +192 -0
- package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.js +187 -84
- package/dist/esm/wkt/gen/google/protobuf/duration_pb.d.ts +7 -0
- package/dist/esm/wkt/gen/google/protobuf/duration_pb.js +7 -2
- package/dist/esm/wkt/gen/google/protobuf/empty_pb.d.ts +7 -0
- package/dist/esm/wkt/gen/google/protobuf/empty_pb.js +7 -2
- package/dist/esm/wkt/gen/google/protobuf/field_mask_pb.d.ts +7 -0
- package/dist/esm/wkt/gen/google/protobuf/field_mask_pb.js +7 -2
- package/dist/esm/wkt/gen/google/protobuf/source_context_pb.d.ts +7 -0
- package/dist/esm/wkt/gen/google/protobuf/source_context_pb.js +7 -2
- package/dist/esm/wkt/gen/google/protobuf/struct_pb.d.ts +18 -0
- package/dist/esm/wkt/gen/google/protobuf/struct_pb.js +18 -7
- package/dist/esm/wkt/gen/google/protobuf/timestamp_pb.d.ts +7 -0
- package/dist/esm/wkt/gen/google/protobuf/timestamp_pb.js +7 -2
- package/dist/esm/wkt/gen/google/protobuf/type_pb.d.ts +32 -0
- package/dist/esm/wkt/gen/google/protobuf/type_pb.js +32 -13
- package/dist/esm/wkt/gen/google/protobuf/wrappers_pb.d.ts +39 -0
- package/dist/esm/wkt/gen/google/protobuf/wrappers_pb.js +39 -18
- package/dist/esm/wkt/wrappers.d.ts +1 -1
- package/package.json +2 -2
- package/dist/cjs/desc-types.js +0 -15
- package/dist/cjs/reflect/assert.d.ts +0 -16
- package/dist/cjs/reflect/assert.js +0 -59
- package/dist/esm/desc-types.js +0 -14
- package/dist/esm/reflect/assert.d.ts +0 -16
- package/dist/esm/reflect/assert.js +0 -52
|
@@ -11,11 +11,11 @@
|
|
|
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 {
|
|
15
|
-
import { parseTextFormatEnumValue, parseTextFormatScalarValue, } from "
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
14
|
+
import { ScalarType, } from "./descriptors.js";
|
|
15
|
+
import { parseTextFormatEnumValue, parseTextFormatScalarValue, } from "./wire/text-format.js";
|
|
16
|
+
import { nestedTypes } from "./reflect/nested-types.js";
|
|
17
|
+
import { unsafeIsSetExplicit } from "./reflect/unsafe.js";
|
|
18
|
+
import { protoCamelCase, safeObjectProperty } from "./reflect/names.js";
|
|
19
19
|
/**
|
|
20
20
|
* Create a registry from the given inputs.
|
|
21
21
|
*
|
|
@@ -200,8 +200,6 @@ const TYPE_BYTES = 12;
|
|
|
200
200
|
const TYPE_ENUM = 14;
|
|
201
201
|
// bootstrap-inject google.protobuf.FieldDescriptorProto.Label.LABEL_REPEATED: const $name: FieldDescriptorProto_Label.$localName = $number;
|
|
202
202
|
const LABEL_REPEATED = 3;
|
|
203
|
-
// bootstrap-inject google.protobuf.FieldDescriptorProto.Label.LABEL_OPTIONAL: const $name: FieldDescriptorProto_Label.$localName = $number;
|
|
204
|
-
const LABEL_OPTIONAL = 1;
|
|
205
203
|
// bootstrap-inject google.protobuf.FieldDescriptorProto.Label.LABEL_REQUIRED: const $name: FieldDescriptorProto_Label.$localName = $number;
|
|
206
204
|
const LABEL_REQUIRED = 2;
|
|
207
205
|
// bootstrap-inject google.protobuf.FieldOptions.JSType.JS_STRING: const $name: FieldOptions_JSType.$localName = $number;
|
|
@@ -210,6 +208,8 @@ const JS_STRING = 1;
|
|
|
210
208
|
const IDEMPOTENCY_UNKNOWN = 0;
|
|
211
209
|
// bootstrap-inject google.protobuf.FeatureSet.FieldPresence.EXPLICIT: const $name: FeatureSet_FieldPresence.$localName = $number;
|
|
212
210
|
const EXPLICIT = 1;
|
|
211
|
+
// bootstrap-inject google.protobuf.FeatureSet.FieldPresence.IMPLICIT: const $name: FeatureSet_FieldPresence.$localName = $number;
|
|
212
|
+
const IMPLICIT = 2;
|
|
213
213
|
// bootstrap-inject google.protobuf.FeatureSet.FieldPresence.LEGACY_REQUIRED: const $name: FeatureSet_FieldPresence.$localName = $number;
|
|
214
214
|
const LEGACY_REQUIRED = 3;
|
|
215
215
|
// bootstrap-inject google.protobuf.FeatureSet.RepeatedFieldEncoding.PACKED: const $name: FeatureSet_RepeatedFieldEncoding.$localName = $number;
|
|
@@ -220,7 +220,7 @@ const DELIMITED = 2;
|
|
|
220
220
|
const OPEN = 1;
|
|
221
221
|
// prettier-ignore
|
|
222
222
|
// bootstrap-inject defaults: EDITION_PROTO2 to EDITION_2023: export const minimumEdition: SupportedEdition = $minimumEdition, maximumEdition: SupportedEdition = $maximumEdition;
|
|
223
|
-
// generated from protoc v27.0
|
|
223
|
+
// generated from protoc v27.0
|
|
224
224
|
export const minimumEdition = 998, maximumEdition = 1000;
|
|
225
225
|
const featureDefaults = {
|
|
226
226
|
// EDITION_PROTO2
|
|
@@ -261,7 +261,7 @@ function addFile(proto, reg) {
|
|
|
261
261
|
proto,
|
|
262
262
|
deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
|
|
263
263
|
edition: getFileEdition(proto),
|
|
264
|
-
name: proto.name.replace(/\.proto
|
|
264
|
+
name: proto.name.replace(/\.proto$/, ""),
|
|
265
265
|
dependencies: findFileDependencies(proto, reg),
|
|
266
266
|
enums: [],
|
|
267
267
|
messages: [],
|
|
@@ -269,7 +269,7 @@ function addFile(proto, reg) {
|
|
|
269
269
|
services: [],
|
|
270
270
|
toString() {
|
|
271
271
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
|
|
272
|
-
return `file ${
|
|
272
|
+
return `file ${proto.name}`;
|
|
273
273
|
},
|
|
274
274
|
};
|
|
275
275
|
const mapEntriesStore = new Map();
|
|
@@ -340,6 +340,7 @@ function addFields(message, reg, mapEntries) {
|
|
|
340
340
|
const oneof = findOneof(proto, allOneofs);
|
|
341
341
|
const field = newField(proto, message, reg, oneof, mapEntries);
|
|
342
342
|
message.fields.push(field);
|
|
343
|
+
message.field[field.localName] = field;
|
|
343
344
|
if (oneof === undefined) {
|
|
344
345
|
message.members.push(field);
|
|
345
346
|
}
|
|
@@ -364,6 +365,7 @@ function addFields(message, reg, mapEntries) {
|
|
|
364
365
|
*/
|
|
365
366
|
function addEnum(proto, file, parent, reg) {
|
|
366
367
|
var _a, _b, _c;
|
|
368
|
+
const sharedPrefix = findEnumSharedPrefix(proto.name, proto.value);
|
|
367
369
|
const desc = {
|
|
368
370
|
kind: "enum",
|
|
369
371
|
proto,
|
|
@@ -374,7 +376,7 @@ function addEnum(proto, file, parent, reg) {
|
|
|
374
376
|
name: proto.name,
|
|
375
377
|
typeName: makeTypeName(proto, parent, file),
|
|
376
378
|
values: [],
|
|
377
|
-
sharedPrefix
|
|
379
|
+
sharedPrefix,
|
|
378
380
|
toString() {
|
|
379
381
|
return `enum ${this.typeName}`;
|
|
380
382
|
},
|
|
@@ -383,12 +385,14 @@ function addEnum(proto, file, parent, reg) {
|
|
|
383
385
|
reg.add(desc);
|
|
384
386
|
proto.value.forEach((proto) => {
|
|
385
387
|
var _a, _b;
|
|
388
|
+
const name = proto.name;
|
|
386
389
|
desc.values.push({
|
|
387
390
|
kind: "enum_value",
|
|
388
391
|
proto,
|
|
389
392
|
deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
|
|
390
393
|
parent: desc,
|
|
391
394
|
name: proto.name,
|
|
395
|
+
localName: safeObjectProperty(sharedPrefix == undefined ? name : name.substring(sharedPrefix.length)),
|
|
392
396
|
number: proto.number,
|
|
393
397
|
toString() {
|
|
394
398
|
return `enum value ${desc.typeName}.${this.name}`;
|
|
@@ -412,6 +416,7 @@ function addMessage(proto, file, parent, reg, mapEntries) {
|
|
|
412
416
|
name: proto.name,
|
|
413
417
|
typeName: makeTypeName(proto, parent, file),
|
|
414
418
|
fields: [],
|
|
419
|
+
field: {},
|
|
415
420
|
oneofs: [],
|
|
416
421
|
members: [],
|
|
417
422
|
nestedEnums: [],
|
|
@@ -449,6 +454,7 @@ function addService(proto, file, reg) {
|
|
|
449
454
|
name: proto.name,
|
|
450
455
|
typeName: makeTypeName(proto, undefined, file),
|
|
451
456
|
methods: [],
|
|
457
|
+
method: {},
|
|
452
458
|
toString() {
|
|
453
459
|
return `service ${this.typeName}`;
|
|
454
460
|
},
|
|
@@ -456,7 +462,9 @@ function addService(proto, file, reg) {
|
|
|
456
462
|
file.services.push(desc);
|
|
457
463
|
reg.add(desc);
|
|
458
464
|
for (const methodProto of proto.method) {
|
|
459
|
-
|
|
465
|
+
const method = newMethod(methodProto, desc, reg);
|
|
466
|
+
desc.methods.push(method);
|
|
467
|
+
desc.method[method.localName] = method;
|
|
460
468
|
}
|
|
461
469
|
}
|
|
462
470
|
/**
|
|
@@ -488,6 +496,9 @@ function newMethod(proto, parent, reg) {
|
|
|
488
496
|
deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
|
|
489
497
|
parent,
|
|
490
498
|
name,
|
|
499
|
+
localName: safeObjectProperty(name.length
|
|
500
|
+
? safeObjectProperty(name[0].toLowerCase() + name.substring(1))
|
|
501
|
+
: name),
|
|
491
502
|
methodKind,
|
|
492
503
|
input,
|
|
493
504
|
output,
|
|
@@ -508,6 +519,7 @@ function newOneof(proto, parent) {
|
|
|
508
519
|
parent,
|
|
509
520
|
fields: [],
|
|
510
521
|
name: proto.name,
|
|
522
|
+
localName: safeObjectProperty(protoCamelCase(proto.name)),
|
|
511
523
|
toString() {
|
|
512
524
|
return `oneof ${parent.typeName}.${this.name}`;
|
|
513
525
|
},
|
|
@@ -515,7 +527,9 @@ function newOneof(proto, parent) {
|
|
|
515
527
|
}
|
|
516
528
|
function newField(proto, parentOrFile, reg, oneof, mapEntries) {
|
|
517
529
|
var _a, _b, _c;
|
|
530
|
+
const isExtension = mapEntries === undefined;
|
|
518
531
|
const field = {
|
|
532
|
+
kind: "field",
|
|
519
533
|
proto,
|
|
520
534
|
deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false,
|
|
521
535
|
name: proto.name,
|
|
@@ -523,8 +537,16 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
|
|
|
523
537
|
scalar: undefined,
|
|
524
538
|
message: undefined,
|
|
525
539
|
enum: undefined,
|
|
540
|
+
presence: getFieldPresence(proto, oneof, isExtension, parentOrFile),
|
|
541
|
+
listKind: undefined,
|
|
542
|
+
mapKind: undefined,
|
|
543
|
+
mapKey: undefined,
|
|
544
|
+
delimitedEncoding: undefined,
|
|
545
|
+
packed: undefined,
|
|
546
|
+
longAsString: false,
|
|
547
|
+
getDefaultValue: undefined,
|
|
526
548
|
};
|
|
527
|
-
if (
|
|
549
|
+
if (isExtension) {
|
|
528
550
|
// extension field
|
|
529
551
|
const file = parentOrFile.kind == "file" ? parentOrFile : parentOrFile.file;
|
|
530
552
|
const parent = parentOrFile.kind == "file" ? undefined : parentOrFile;
|
|
@@ -544,9 +566,11 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
|
|
|
544
566
|
// regular field
|
|
545
567
|
const parent = parentOrFile;
|
|
546
568
|
assert(parent.kind == "message");
|
|
547
|
-
field.kind = "field";
|
|
548
569
|
field.parent = parent;
|
|
549
570
|
field.oneof = oneof;
|
|
571
|
+
field.localName = oneof
|
|
572
|
+
? protoCamelCase(proto.name)
|
|
573
|
+
: safeObjectProperty(protoCamelCase(proto.name));
|
|
550
574
|
field.jsonName = proto.jsonName;
|
|
551
575
|
field.toString = () => `field ${parent.typeName}.${proto.name}`;
|
|
552
576
|
}
|
|
@@ -561,21 +585,13 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
|
|
|
561
585
|
if (mapEntry) {
|
|
562
586
|
// map field
|
|
563
587
|
field.fieldKind = "map";
|
|
564
|
-
const
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
keyField.scalar != ScalarType.FLOAT &&
|
|
569
|
-
keyField.scalar != ScalarType.DOUBLE);
|
|
570
|
-
const valueField = mapEntry.fields.find((f) => f.proto.number === 2);
|
|
571
|
-
assert(valueField);
|
|
572
|
-
assert(valueField.fieldKind != "list" && valueField.fieldKind != "map");
|
|
573
|
-
field.mapKey = keyField.scalar;
|
|
574
|
-
field.mapKind = valueField.fieldKind;
|
|
575
|
-
field.message = valueField.message;
|
|
588
|
+
const { key, value } = findMapEntryFields(mapEntry);
|
|
589
|
+
field.mapKey = key.scalar;
|
|
590
|
+
field.mapKind = value.fieldKind;
|
|
591
|
+
field.message = value.message;
|
|
576
592
|
field.delimitedEncoding = false; // map fields are always LENGTH_PREFIXED
|
|
577
|
-
field.enum =
|
|
578
|
-
field.scalar =
|
|
593
|
+
field.enum = value.enum;
|
|
594
|
+
field.scalar = value.scalar;
|
|
579
595
|
return field;
|
|
580
596
|
}
|
|
581
597
|
// list field
|
|
@@ -596,8 +612,7 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
|
|
|
596
612
|
default:
|
|
597
613
|
field.listKind = "scalar";
|
|
598
614
|
field.scalar = type;
|
|
599
|
-
field.
|
|
600
|
-
jstype == JS_STRING ? LongType.STRING : LongType.BIGINT;
|
|
615
|
+
field.longAsString = jstype == JS_STRING;
|
|
601
616
|
break;
|
|
602
617
|
}
|
|
603
618
|
field.packed = isPackedField(proto, parentOrFile);
|
|
@@ -628,7 +643,7 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
|
|
|
628
643
|
default: {
|
|
629
644
|
field.fieldKind = "scalar";
|
|
630
645
|
field.scalar = type;
|
|
631
|
-
field.
|
|
646
|
+
field.longAsString = jstype == JS_STRING;
|
|
632
647
|
field.getDefaultValue = () => {
|
|
633
648
|
return unsafeIsSetExplicit(proto, "defaultValue")
|
|
634
649
|
? parseTextFormatScalarValue(type, proto.defaultValue)
|
|
@@ -637,8 +652,6 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
|
|
|
637
652
|
break;
|
|
638
653
|
}
|
|
639
654
|
}
|
|
640
|
-
field.presence = getFieldPresence(proto, oneof, parentOrFile);
|
|
641
|
-
field.optional = isOptionalField(field);
|
|
642
655
|
return field;
|
|
643
656
|
}
|
|
644
657
|
/**
|
|
@@ -677,13 +690,13 @@ function findFileDependencies(proto, reg) {
|
|
|
677
690
|
* Finds a prefix shared by enum values, for example `my_enum_` for
|
|
678
691
|
* `enum MyEnum {MY_ENUM_A=0; MY_ENUM_B=1;}`.
|
|
679
692
|
*/
|
|
680
|
-
function findEnumSharedPrefix(enumName,
|
|
693
|
+
function findEnumSharedPrefix(enumName, values) {
|
|
681
694
|
const prefix = camelToSnakeCase(enumName) + "_";
|
|
682
|
-
for (const
|
|
683
|
-
if (!name.toLowerCase().startsWith(prefix)) {
|
|
695
|
+
for (const value of values) {
|
|
696
|
+
if (!value.name.toLowerCase().startsWith(prefix)) {
|
|
684
697
|
return undefined;
|
|
685
698
|
}
|
|
686
|
-
const shortName = name.substring(prefix.length);
|
|
699
|
+
const shortName = value.name.substring(prefix.length);
|
|
687
700
|
if (shortName.length == 0) {
|
|
688
701
|
return undefined;
|
|
689
702
|
}
|
|
@@ -755,39 +768,28 @@ function findOneof(proto, allOneofs) {
|
|
|
755
768
|
* Presence of the field.
|
|
756
769
|
* See https://protobuf.dev/programming-guides/field_presence/
|
|
757
770
|
*/
|
|
758
|
-
function getFieldPresence(proto, oneof, parent) {
|
|
771
|
+
function getFieldPresence(proto, oneof, isExtension, parent) {
|
|
759
772
|
if (proto.label == LABEL_REQUIRED) {
|
|
760
773
|
// proto2 required is LEGACY_REQUIRED
|
|
761
774
|
return LEGACY_REQUIRED;
|
|
762
775
|
}
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
if (oneof != undefined || proto.proto3Optional) {
|
|
767
|
-
return EXPLICIT;
|
|
768
|
-
}
|
|
769
|
-
// proto3 singular message is explicit
|
|
770
|
-
const singularMessage = proto.label != LABEL_REPEATED && proto.type == TYPE_MESSAGE;
|
|
771
|
-
if (singularMessage) {
|
|
772
|
-
return EXPLICIT;
|
|
773
|
-
}
|
|
776
|
+
if (proto.label == LABEL_REPEATED) {
|
|
777
|
+
// repeated fields (including maps) do not track presence
|
|
778
|
+
return IMPLICIT;
|
|
774
779
|
}
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
/**
|
|
779
|
-
* Did the user use the `optional` keyword?
|
|
780
|
-
*/
|
|
781
|
-
function isOptionalField(field) {
|
|
782
|
-
const edition = (field.kind == "extension" ? field.file : field.parent.file)
|
|
783
|
-
.edition;
|
|
784
|
-
if (edition == EDITION_PROTO2) {
|
|
785
|
-
return !field.oneof && field.proto.label == LABEL_OPTIONAL;
|
|
780
|
+
if (!!oneof || proto.proto3Optional) {
|
|
781
|
+
// oneof is always explicit
|
|
782
|
+
return EXPLICIT;
|
|
786
783
|
}
|
|
787
|
-
if (
|
|
788
|
-
|
|
784
|
+
if (proto.type == TYPE_MESSAGE) {
|
|
785
|
+
// singular message field cannot be implicit
|
|
786
|
+
return EXPLICIT;
|
|
789
787
|
}
|
|
790
|
-
|
|
788
|
+
if (isExtension) {
|
|
789
|
+
// extensions always track presence
|
|
790
|
+
return EXPLICIT;
|
|
791
|
+
}
|
|
792
|
+
return resolveFeature("fieldPresence", { proto, parent });
|
|
791
793
|
}
|
|
792
794
|
/**
|
|
793
795
|
* Pack this repeated field?
|
|
@@ -816,6 +818,22 @@ function isPackedField(proto, parent) {
|
|
|
816
818
|
parent,
|
|
817
819
|
}));
|
|
818
820
|
}
|
|
821
|
+
/**
|
|
822
|
+
* Find the key and value fields of a synthetic map entry message.
|
|
823
|
+
*/
|
|
824
|
+
function findMapEntryFields(mapEntry) {
|
|
825
|
+
const key = mapEntry.fields.find((f) => f.number === 1);
|
|
826
|
+
const value = mapEntry.fields.find((f) => f.number === 2);
|
|
827
|
+
assert(key &&
|
|
828
|
+
key.fieldKind == "scalar" &&
|
|
829
|
+
key.scalar != ScalarType.BYTES &&
|
|
830
|
+
key.scalar != ScalarType.FLOAT &&
|
|
831
|
+
key.scalar != ScalarType.DOUBLE &&
|
|
832
|
+
value &&
|
|
833
|
+
value.fieldKind != "list" &&
|
|
834
|
+
value.fieldKind != "map");
|
|
835
|
+
return { key, value };
|
|
836
|
+
}
|
|
819
837
|
/**
|
|
820
838
|
* Enumerations can be open or closed.
|
|
821
839
|
* See https://protobuf.dev/programming-guides/enum/
|
|
@@ -863,3 +881,12 @@ function resolveFeature(name, ref) {
|
|
|
863
881
|
}
|
|
864
882
|
return resolveFeature(name, ref.parent);
|
|
865
883
|
}
|
|
884
|
+
/**
|
|
885
|
+
* Assert that condition is truthy or throw error (with message)
|
|
886
|
+
*/
|
|
887
|
+
function assert(condition, msg) {
|
|
888
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- we want the implicit conversion to boolean
|
|
889
|
+
if (!condition) {
|
|
890
|
+
throw new Error(msg);
|
|
891
|
+
}
|
|
892
|
+
}
|
package/dist/esm/to-binary.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { MessageShape } from "./types.js";
|
|
2
2
|
import { BinaryWriter } from "./wire/binary-encoding.js";
|
|
3
|
-
import type
|
|
3
|
+
import { type DescField, type DescMessage } from "./descriptors.js";
|
|
4
4
|
import type { ReflectMessage } from "./reflect/index.js";
|
|
5
5
|
/**
|
|
6
6
|
* Options for serializing to binary data.
|
package/dist/esm/to-binary.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
import { reflect } from "./reflect/reflect.js";
|
|
15
15
|
import { BinaryWriter, WireType } from "./wire/binary-encoding.js";
|
|
16
|
-
import { ScalarType } from "./
|
|
16
|
+
import { ScalarType } from "./descriptors.js";
|
|
17
17
|
// bootstrap-inject google.protobuf.FeatureSet.FieldPresence.LEGACY_REQUIRED: const $name: FeatureSet_FieldPresence.$localName = $number;
|
|
18
18
|
const LEGACY_REQUIRED = 3;
|
|
19
19
|
// Default options for serializing binary data.
|
|
@@ -30,9 +30,7 @@ function writeFields(writer, opts, msg) {
|
|
|
30
30
|
var _a;
|
|
31
31
|
for (const f of msg.sortedFields) {
|
|
32
32
|
if (!msg.isSet(f)) {
|
|
33
|
-
if (f.
|
|
34
|
-
f.fieldKind != "list" &&
|
|
35
|
-
f.presence == LEGACY_REQUIRED) {
|
|
33
|
+
if (f.presence == LEGACY_REQUIRED) {
|
|
36
34
|
throw new Error(`cannot encode field ${msg.desc.typeName}.${f.name} to binary: required field not set`);
|
|
37
35
|
}
|
|
38
36
|
continue;
|
package/dist/esm/to-json.d.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type DescMessage } from "./descriptors.js";
|
|
2
2
|
import type { JsonValue } from "./json-value.js";
|
|
3
|
-
import type { Registry } from "./
|
|
3
|
+
import type { Registry } from "./registry.js";
|
|
4
4
|
import type { MessageShape } from "./types.js";
|
|
5
5
|
/**
|
|
6
6
|
* Options for serializing to JSON.
|
|
7
7
|
*/
|
|
8
8
|
export interface JsonWriteOptions {
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* By default, fields with implicit presence are not serialized if they are
|
|
11
|
+
* unset. For example, an empty list field or a proto3 int32 field with 0 is
|
|
12
|
+
* not serialized. With this option enabled, such fields are included in the
|
|
13
|
+
* output.
|
|
13
14
|
*/
|
|
14
|
-
|
|
15
|
+
alwaysEmitImplicit: boolean;
|
|
15
16
|
/**
|
|
16
17
|
* Emit enum values as integers instead of strings: The name of an enum
|
|
17
18
|
* value is used by default in JSON output. An option may be provided to
|
package/dist/esm/to-json.js
CHANGED
|
@@ -11,21 +11,22 @@
|
|
|
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 {
|
|
14
|
+
import { ScalarType, } from "./descriptors.js";
|
|
15
15
|
import { protoCamelCase } from "./reflect/names.js";
|
|
16
16
|
import { reflect } from "./reflect/reflect.js";
|
|
17
|
-
import { ScalarType } from "./reflect/scalar.js";
|
|
18
17
|
import { anyUnpack } from "./wkt/index.js";
|
|
19
18
|
import { isWrapperDesc } from "./wkt/wrappers.js";
|
|
20
19
|
import { base64Encode } from "./wire/index.js";
|
|
21
20
|
import { createExtensionContainer, getExtension } from "./extensions.js";
|
|
21
|
+
import { checkField, formatVal } from "./reflect/reflect-check.js";
|
|
22
|
+
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
|
22
23
|
// bootstrap-inject google.protobuf.FeatureSet.FieldPresence.LEGACY_REQUIRED: const $name: FeatureSet_FieldPresence.$localName = $number;
|
|
23
24
|
const LEGACY_REQUIRED = 3;
|
|
24
|
-
// bootstrap-inject google.protobuf.
|
|
25
|
-
const
|
|
25
|
+
// bootstrap-inject google.protobuf.FeatureSet.FieldPresence.IMPLICIT: const $name: FeatureSet_FieldPresence.$localName = $number;
|
|
26
|
+
const IMPLICIT = 2;
|
|
26
27
|
// Default options for serializing to JSON.
|
|
27
28
|
const jsonWriteDefaults = {
|
|
28
|
-
|
|
29
|
+
alwaysEmitImplicit: false,
|
|
29
30
|
enumAsInteger: false,
|
|
30
31
|
useProtoFieldName: false,
|
|
31
32
|
};
|
|
@@ -55,15 +56,11 @@ function reflectToJson(msg, opts) {
|
|
|
55
56
|
const json = {};
|
|
56
57
|
for (const f of msg.sortedFields) {
|
|
57
58
|
if (!msg.isSet(f)) {
|
|
58
|
-
if (f.
|
|
59
|
-
f.
|
|
60
|
-
f.presence == LEGACY_REQUIRED) {
|
|
61
|
-
throw new Error(`cannot encode field ${msg.desc.typeName}.${f.name} to binary: required field not set`);
|
|
59
|
+
if (f.presence == LEGACY_REQUIRED) {
|
|
60
|
+
throw new Error(`cannot encode field ${msg.desc.typeName}.${f.name} to JSON: required field not set`);
|
|
62
61
|
}
|
|
63
|
-
if (!opts.
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
if (!canEmitFieldDefaultValue(f)) {
|
|
62
|
+
if (!opts.alwaysEmitImplicit || f.presence !== IMPLICIT) {
|
|
63
|
+
// Fields with implicit presence omit zero values (e.g. empty string) by default
|
|
67
64
|
continue;
|
|
68
65
|
}
|
|
69
66
|
}
|
|
@@ -85,8 +82,8 @@ function reflectToJson(msg, opts) {
|
|
|
85
82
|
continue;
|
|
86
83
|
}
|
|
87
84
|
const value = getExtension(msg.message, extension);
|
|
88
|
-
const [, field] = createExtensionContainer(extension);
|
|
89
|
-
const jsonValue = fieldToJson(field,
|
|
85
|
+
const [container, field] = createExtensionContainer(extension, value);
|
|
86
|
+
const jsonValue = fieldToJson(field, container.get(field), opts);
|
|
90
87
|
if (jsonValue !== undefined) {
|
|
91
88
|
json[extension.jsonName] = jsonValue;
|
|
92
89
|
}
|
|
@@ -97,7 +94,7 @@ function reflectToJson(msg, opts) {
|
|
|
97
94
|
function fieldToJson(f, val, opts) {
|
|
98
95
|
switch (f.fieldKind) {
|
|
99
96
|
case "scalar":
|
|
100
|
-
return scalarToJson(f
|
|
97
|
+
return scalarToJson(f, val);
|
|
101
98
|
case "message":
|
|
102
99
|
return reflectToJson(val, opts);
|
|
103
100
|
case "enum":
|
|
@@ -114,7 +111,7 @@ function mapToJson(map, opts) {
|
|
|
114
111
|
switch (f.mapKind) {
|
|
115
112
|
case "scalar":
|
|
116
113
|
for (const [entryKey, entryValue] of map) {
|
|
117
|
-
jsonObj[entryKey.toString()] = scalarToJson(f
|
|
114
|
+
jsonObj[entryKey.toString()] = scalarToJson(f, entryValue); // JSON standard allows only (double quoted) string as property key
|
|
118
115
|
}
|
|
119
116
|
break;
|
|
120
117
|
case "message":
|
|
@@ -130,7 +127,7 @@ function mapToJson(map, opts) {
|
|
|
130
127
|
}
|
|
131
128
|
break;
|
|
132
129
|
}
|
|
133
|
-
return opts.
|
|
130
|
+
return opts.alwaysEmitImplicit || map.size > 0 ? jsonObj : undefined;
|
|
134
131
|
}
|
|
135
132
|
function listToJson(list, opts) {
|
|
136
133
|
const f = list.field();
|
|
@@ -138,7 +135,7 @@ function listToJson(list, opts) {
|
|
|
138
135
|
switch (f.listKind) {
|
|
139
136
|
case "scalar":
|
|
140
137
|
for (const item of list) {
|
|
141
|
-
jsonArr.push(scalarToJson(f
|
|
138
|
+
jsonArr.push(scalarToJson(f, item));
|
|
142
139
|
}
|
|
143
140
|
break;
|
|
144
141
|
case "enum":
|
|
@@ -152,11 +149,13 @@ function listToJson(list, opts) {
|
|
|
152
149
|
}
|
|
153
150
|
break;
|
|
154
151
|
}
|
|
155
|
-
return opts.
|
|
152
|
+
return opts.alwaysEmitImplicit || jsonArr.length > 0 ? jsonArr : undefined;
|
|
156
153
|
}
|
|
157
154
|
function enumToJson(desc, value, enumAsInteger) {
|
|
158
155
|
var _a;
|
|
159
|
-
|
|
156
|
+
if (typeof value != "number") {
|
|
157
|
+
throw new Error(`cannot encode ${desc} to JSON: expected number, got ${formatVal(value)}`);
|
|
158
|
+
}
|
|
160
159
|
if (desc.typeName == "google.protobuf.NullValue") {
|
|
161
160
|
return null;
|
|
162
161
|
}
|
|
@@ -166,23 +165,27 @@ function enumToJson(desc, value, enumAsInteger) {
|
|
|
166
165
|
const val = desc.values.find((v) => v.number == value);
|
|
167
166
|
return (_a = val === null || val === void 0 ? void 0 : val.name) !== null && _a !== void 0 ? _a : value; // if we don't know the enum value, just return the number
|
|
168
167
|
}
|
|
169
|
-
function scalarToJson(
|
|
170
|
-
|
|
168
|
+
function scalarToJson(field, value) {
|
|
169
|
+
var _a, _b, _c, _d, _e, _f;
|
|
170
|
+
switch (field.scalar) {
|
|
171
171
|
// int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
|
|
172
172
|
case ScalarType.INT32:
|
|
173
173
|
case ScalarType.SFIXED32:
|
|
174
174
|
case ScalarType.SINT32:
|
|
175
175
|
case ScalarType.FIXED32:
|
|
176
176
|
case ScalarType.UINT32:
|
|
177
|
-
|
|
177
|
+
if (typeof value != "number") {
|
|
178
|
+
throw new Error(`cannot encode ${field} to JSON: ${(_a = checkField(field, value)) === null || _a === void 0 ? void 0 : _a.message}`);
|
|
179
|
+
}
|
|
178
180
|
return value;
|
|
179
181
|
// float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
|
|
180
182
|
// Either numbers or strings are accepted. Exponent notation is also accepted.
|
|
181
183
|
case ScalarType.FLOAT:
|
|
182
|
-
// assertFloat32(value);
|
|
183
184
|
case ScalarType.DOUBLE: // eslint-disable-line no-fallthrough
|
|
184
|
-
|
|
185
|
-
|
|
185
|
+
if (typeof value != "number") {
|
|
186
|
+
throw new Error(`cannot encode ${field} to JSON: ${(_b = checkField(field, value)) === null || _b === void 0 ? void 0 : _b.message}`);
|
|
187
|
+
}
|
|
188
|
+
if (isNaN(value))
|
|
186
189
|
return "NaN";
|
|
187
190
|
if (value === Number.POSITIVE_INFINITY)
|
|
188
191
|
return "Infinity";
|
|
@@ -191,11 +194,15 @@ function scalarToJson(type, value) {
|
|
|
191
194
|
return value;
|
|
192
195
|
// string:
|
|
193
196
|
case ScalarType.STRING:
|
|
194
|
-
|
|
197
|
+
if (typeof value != "string") {
|
|
198
|
+
throw new Error(`cannot encode ${field} to JSON: ${(_c = checkField(field, value)) === null || _c === void 0 ? void 0 : _c.message}`);
|
|
199
|
+
}
|
|
195
200
|
return value;
|
|
196
201
|
// bool:
|
|
197
202
|
case ScalarType.BOOL:
|
|
198
|
-
|
|
203
|
+
if (typeof value != "boolean") {
|
|
204
|
+
throw new Error(`cannot encode ${field} to JSON: ${(_d = checkField(field, value)) === null || _d === void 0 ? void 0 : _d.message}`);
|
|
205
|
+
}
|
|
199
206
|
return value;
|
|
200
207
|
// JSON value will be a decimal string. Either numbers or strings are accepted.
|
|
201
208
|
case ScalarType.UINT64:
|
|
@@ -203,31 +210,17 @@ function scalarToJson(type, value) {
|
|
|
203
210
|
case ScalarType.INT64:
|
|
204
211
|
case ScalarType.SFIXED64:
|
|
205
212
|
case ScalarType.SINT64:
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
213
|
+
if (typeof value != "bigint" && typeof value != "string") {
|
|
214
|
+
throw new Error(`cannot encode ${field} to JSON: ${(_e = checkField(field, value)) === null || _e === void 0 ? void 0 : _e.message}`);
|
|
215
|
+
}
|
|
209
216
|
return value.toString();
|
|
210
217
|
// bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
|
|
211
218
|
// Either standard or URL-safe base64 encoding with/without paddings are accepted.
|
|
212
219
|
case ScalarType.BYTES:
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
// Decide whether an unset field should be emitted with JSON write option `emitDefaultValues`
|
|
218
|
-
function canEmitFieldDefaultValue(field) {
|
|
219
|
-
switch (true) {
|
|
220
|
-
// oneof fields are never emitted
|
|
221
|
-
case field.oneof !== undefined:
|
|
222
|
-
// singular message field are allowed to emit JSON null, but we do not
|
|
223
|
-
// eslint-disable-next-line no-fallthrough
|
|
224
|
-
case field.fieldKind === "message":
|
|
225
|
-
// the field uses explicit presence, so we cannot emit a zero value
|
|
226
|
-
// eslint-disable-next-line no-fallthrough
|
|
227
|
-
case field.proto.label === LABEL_OPTIONAL:
|
|
228
|
-
return false;
|
|
229
|
-
default:
|
|
230
|
-
return true;
|
|
220
|
+
if (value instanceof Uint8Array) {
|
|
221
|
+
return base64Encode(value);
|
|
222
|
+
}
|
|
223
|
+
throw new Error(`cannot encode ${field} to JSON: ${(_f = checkField(field, value)) === null || _f === void 0 ? void 0 : _f.message}`);
|
|
231
224
|
}
|
|
232
225
|
}
|
|
233
226
|
function jsonName(f, opts) {
|
|
@@ -256,7 +249,7 @@ function tryWktToJson(msg, opts) {
|
|
|
256
249
|
default:
|
|
257
250
|
if (isWrapperDesc(msg.desc)) {
|
|
258
251
|
const valueField = msg.desc.fields[0];
|
|
259
|
-
return scalarToJson(valueField
|
|
252
|
+
return scalarToJson(valueField, msg.get(valueField));
|
|
260
253
|
}
|
|
261
254
|
return undefined;
|
|
262
255
|
}
|
|
@@ -290,7 +283,7 @@ function anyToJson(val, opts) {
|
|
|
290
283
|
function durationToJson(val) {
|
|
291
284
|
if (Number(val.seconds) > 315576000000 ||
|
|
292
285
|
Number(val.seconds) < -315576000000) {
|
|
293
|
-
throw new Error(`cannot encode ${val.$typeName} to JSON: value out of range`);
|
|
286
|
+
throw new Error(`cannot encode message ${val.$typeName} to JSON: value out of range`);
|
|
294
287
|
}
|
|
295
288
|
let text = val.seconds.toString();
|
|
296
289
|
if (val.nanos !== 0) {
|
|
@@ -314,7 +307,7 @@ function fieldMaskToJson(val) {
|
|
|
314
307
|
.map((p) => {
|
|
315
308
|
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
316
309
|
if (p.match(/_[0-9]?_/g) || p.match(/[A-Z]/g)) {
|
|
317
|
-
throw new Error(`cannot encode ${val.$typeName} to JSON: lowerCamelCase of path name "` +
|
|
310
|
+
throw new Error(`cannot encode message ${val.$typeName} to JSON: lowerCamelCase of path name "` +
|
|
318
311
|
p +
|
|
319
312
|
'" is irreversible');
|
|
320
313
|
}
|
|
@@ -357,10 +350,10 @@ function timestampToJson(val) {
|
|
|
357
350
|
const ms = Number(val.seconds) * 1000;
|
|
358
351
|
if (ms < Date.parse("0001-01-01T00:00:00Z") ||
|
|
359
352
|
ms > Date.parse("9999-12-31T23:59:59Z")) {
|
|
360
|
-
throw new Error(`cannot encode ${val.$typeName} to JSON: must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive`);
|
|
353
|
+
throw new Error(`cannot encode message ${val.$typeName} to JSON: must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive`);
|
|
361
354
|
}
|
|
362
355
|
if (val.nanos < 0) {
|
|
363
|
-
throw new Error(`cannot encode ${val.$typeName} to JSON: nanos must not be negative`);
|
|
356
|
+
throw new Error(`cannot encode message ${val.$typeName} to JSON: nanos must not be negative`);
|
|
364
357
|
}
|
|
365
358
|
let z = "Z";
|
|
366
359
|
if (val.nanos > 0) {
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { GenDescEnum, GenDescExtension, GenDescMessage } from "./codegenv1/types.js";
|
|
2
|
-
import type { DescEnum, DescExtension, DescMessage } from "./
|
|
2
|
+
import type { DescEnum, DescExtension, DescMessage } from "./descriptors.js";
|
|
3
3
|
import type { OneofADT } from "./reflect/guard.js";
|
|
4
4
|
import type { WireType } from "./wire/index.js";
|
|
5
5
|
/**
|