@aptre/protobuf-es-lite 0.2.7 → 0.2.9

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 (121) hide show
  1. package/.ignore +1 -0
  2. package/README.md +23 -24
  3. package/bin/protoc-gen-es-lite +2 -2
  4. package/dist/binary-encoding.d.ts +420 -0
  5. package/dist/binary-encoding.js +437 -0
  6. package/dist/binary.d.ts +35 -2
  7. package/dist/binary.js +64 -65
  8. package/dist/codegen-info.d.ts +43 -0
  9. package/dist/codegen-info.js +72 -0
  10. package/dist/create-descriptor-set.d.ts +36 -0
  11. package/dist/create-descriptor-set.js +936 -0
  12. package/dist/descriptor-set.d.ts +672 -0
  13. package/dist/{editions.js → descriptor-set.js} +0 -11
  14. package/dist/enum.d.ts +35 -1
  15. package/dist/feature-set.d.ts +19 -0
  16. package/dist/feature-set.js +103 -0
  17. package/dist/field-wrapper.d.ts +7 -3
  18. package/dist/field-wrapper.js +30 -1
  19. package/dist/field.d.ts +8 -19
  20. package/dist/field.js +15 -117
  21. package/dist/google/index.d.ts +9 -0
  22. package/dist/google/index.js +41 -0
  23. package/dist/google/protobuf/any.pb.d.ts +139 -0
  24. package/dist/google/protobuf/any.pb.js +42 -0
  25. package/dist/google/protobuf/api.pb.d.ts +252 -0
  26. package/dist/google/protobuf/api.pb.js +70 -0
  27. package/dist/google/protobuf/compiler/plugin.pb.d.ts +240 -0
  28. package/dist/google/protobuf/compiler/plugin.pb.js +203 -0
  29. package/dist/google/protobuf/descriptor.pb.d.ts +2132 -0
  30. package/dist/google/protobuf/descriptor.pb.js +1080 -0
  31. package/dist/google/protobuf/duration.pb.d.ts +92 -0
  32. package/dist/google/protobuf/duration.pb.js +42 -0
  33. package/dist/google/protobuf/empty.pb.d.ts +18 -0
  34. package/dist/google/protobuf/empty.pb.js +39 -0
  35. package/dist/google/protobuf/source_context.pb.d.ts +21 -0
  36. package/dist/google/protobuf/source_context.pb.js +41 -0
  37. package/dist/google/protobuf/struct.pb.d.ts +121 -0
  38. package/dist/google/protobuf/struct.pb.js +83 -0
  39. package/dist/google/protobuf/timestamp.pb.d.ts +121 -0
  40. package/dist/google/protobuf/timestamp.pb.js +42 -0
  41. package/dist/google/protobuf/type.pb.d.ts +464 -0
  42. package/dist/google/protobuf/type.pb.js +338 -0
  43. package/dist/google/protobuf/wrappers.pb.d.ts +183 -0
  44. package/dist/google/protobuf/wrappers.pb.js +107 -0
  45. package/dist/index.d.ts +9 -3
  46. package/dist/index.js +41 -8
  47. package/dist/is-message.d.ts +10 -2
  48. package/dist/is-message.js +74 -3
  49. package/dist/json.d.ts +68 -1
  50. package/dist/json.js +57 -81
  51. package/dist/message.d.ts +30 -61
  52. package/dist/message.js +61 -20
  53. package/dist/names.d.ts +43 -0
  54. package/dist/names.js +276 -0
  55. package/dist/partial.js +14 -15
  56. package/dist/proto-base64.d.ts +18 -0
  57. package/dist/proto-base64.js +128 -0
  58. package/dist/proto-double.d.ts +5 -0
  59. package/dist/proto-double.js +29 -0
  60. package/dist/proto-int64.d.ts +95 -0
  61. package/dist/proto-int64.js +116 -0
  62. package/dist/protoc-gen-es-lite/protoc-gen-es-lite-plugin.d.ts +1 -0
  63. package/dist/{protoc-gen-es-lite-plugin.js → protoc-gen-es-lite/protoc-gen-es-lite-plugin.js} +2 -2
  64. package/dist/{typescript.d.ts → protoc-gen-es-lite/typescript.d.ts} +3 -2
  65. package/dist/{typescript.js → protoc-gen-es-lite/typescript.js} +46 -34
  66. package/dist/protoplugin/create-es-plugin.d.ts +83 -0
  67. package/dist/protoplugin/create-es-plugin.js +119 -0
  68. package/dist/protoplugin/ecmascript/file-preamble.d.ts +2 -0
  69. package/dist/protoplugin/ecmascript/file-preamble.js +74 -0
  70. package/dist/protoplugin/ecmascript/generated-file.d.ts +116 -0
  71. package/dist/protoplugin/ecmascript/generated-file.js +407 -0
  72. package/dist/protoplugin/ecmascript/import-path.d.ts +65 -0
  73. package/dist/protoplugin/ecmascript/import-path.js +156 -0
  74. package/dist/protoplugin/ecmascript/import-symbol.d.ts +39 -0
  75. package/dist/protoplugin/ecmascript/import-symbol.js +37 -0
  76. package/dist/protoplugin/ecmascript/index.d.ts +7 -0
  77. package/dist/protoplugin/ecmascript/index.js +20 -0
  78. package/dist/protoplugin/ecmascript/jsdoc.d.ts +14 -0
  79. package/dist/protoplugin/ecmascript/jsdoc.js +93 -0
  80. package/dist/protoplugin/ecmascript/opaque-printables.d.ts +27 -0
  81. package/dist/protoplugin/ecmascript/opaque-printables.js +15 -0
  82. package/dist/protoplugin/ecmascript/parameter.d.ts +13 -0
  83. package/dist/protoplugin/ecmascript/parameter.js +161 -0
  84. package/dist/protoplugin/ecmascript/reify-wkt.d.ts +100 -0
  85. package/dist/protoplugin/ecmascript/reify-wkt.js +170 -0
  86. package/dist/protoplugin/ecmascript/runtime-imports.d.ts +6 -0
  87. package/dist/protoplugin/ecmascript/runtime-imports.js +28 -0
  88. package/dist/protoplugin/ecmascript/schema.d.ts +43 -0
  89. package/dist/protoplugin/ecmascript/schema.js +73 -0
  90. package/dist/protoplugin/ecmascript/target.d.ts +4 -0
  91. package/dist/protoplugin/ecmascript/target.js +15 -0
  92. package/dist/protoplugin/ecmascript/transpile.d.ts +2 -0
  93. package/dist/protoplugin/ecmascript/transpile.js +149 -0
  94. package/dist/protoplugin/error.d.ts +6 -0
  95. package/dist/protoplugin/error.js +43 -0
  96. package/dist/protoplugin/index.d.ts +8 -0
  97. package/dist/protoplugin/index.js +20 -0
  98. package/dist/protoplugin/plugin.d.ts +18 -0
  99. package/dist/protoplugin/plugin.js +15 -0
  100. package/dist/protoplugin/run-node.d.ts +12 -0
  101. package/dist/protoplugin/run-node.js +83 -0
  102. package/dist/scalar.d.ts +53 -1
  103. package/dist/scalar.js +87 -22
  104. package/dist/service-type.d.ts +95 -0
  105. package/dist/service-type.js +53 -0
  106. package/dist/text-format.d.ts +4 -0
  107. package/dist/text-format.js +189 -0
  108. package/dist/type-registry.d.ts +43 -0
  109. package/dist/type-registry.js +15 -0
  110. package/dist/unknown.d.ts +1 -1
  111. package/dist/util.d.ts +2 -2
  112. package/dist/util.js +46 -44
  113. package/dist/varint.d.ts +70 -0
  114. package/dist/varint.js +324 -0
  115. package/example/example.pb.ts +87 -0
  116. package/example/example.proto +1 -0
  117. package/example/tsconfig.json +8 -2
  118. package/package.json +19 -12
  119. package/tsconfig.json +4 -4
  120. package/dist/editions.d.ts +0 -6
  121. package/dist/protoc-gen-es-lite-plugin.d.ts +0 -1
package/dist/index.js CHANGED
@@ -1,14 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.normalizeEnumValue = exports.createEnumType = exports.isScalarZeroValue = exports.scalarZeroValue = exports.scalarEquals = exports.localOneofName = exports.localFieldName = exports.fieldJsonName = exports.FieldList = exports.newFieldList = exports.isCompleteField = exports.isCompleteMessage = exports.createMessageType = exports.compareMessages = exports.LongType = exports.WireType = exports.ScalarType = exports.EnumValue = void 0;
4
- var protobuf_1 = require("@bufbuild/protobuf");
5
- Object.defineProperty(exports, "EnumValue", { enumerable: true, get: function () { return protobuf_1.EnumValue; } });
6
- Object.defineProperty(exports, "ScalarType", { enumerable: true, get: function () { return protobuf_1.ScalarType; } });
7
- Object.defineProperty(exports, "WireType", { enumerable: true, get: function () { return protobuf_1.WireType; } });
8
- Object.defineProperty(exports, "LongType", { enumerable: true, get: function () { return protobuf_1.LongType; } });
3
+ exports.Struct = exports.ListValue = exports.NullValue = exports.Value = exports.BytesValue = exports.StringValue = exports.BoolValue = exports.UInt32Value = exports.Int32Value = exports.UInt64Value = exports.Int64Value = exports.FloatValue = exports.DoubleValue = exports.Empty = exports.Any = exports.Duration = exports.Timestamp = exports.protoDouble = exports.protoBase64 = exports.protoInt64 = exports.uInt64ToString = exports.int64ToString = exports.int64FromString = exports.safeIdentifier = exports.safeObjectProperty = exports.protoCamelCase = exports.camelToSnakeCase = exports.findEnumSharedPrefix = exports.localOneofName = exports.localFieldName = exports.localName = exports.normalizeEnumValue = exports.createEnumType = exports.isScalarZeroValue = exports.scalarZeroValue = exports.scalarEquals = exports.fieldJsonName = exports.FieldList = exports.newFieldList = exports.isCompleteField = exports.isCompleteMessage = exports.MethodIdempotency = exports.MethodKind = exports.createMessageType = exports.compareMessages = void 0;
9
4
  var message_js_1 = require("./message.js");
10
5
  Object.defineProperty(exports, "compareMessages", { enumerable: true, get: function () { return message_js_1.compareMessages; } });
11
6
  Object.defineProperty(exports, "createMessageType", { enumerable: true, get: function () { return message_js_1.createMessageType; } });
7
+ var service_type_js_1 = require("./service-type.js");
8
+ Object.defineProperty(exports, "MethodKind", { enumerable: true, get: function () { return service_type_js_1.MethodKind; } });
9
+ Object.defineProperty(exports, "MethodIdempotency", { enumerable: true, get: function () { return service_type_js_1.MethodIdempotency; } });
12
10
  var is_message_js_1 = require("./is-message.js");
13
11
  Object.defineProperty(exports, "isCompleteMessage", { enumerable: true, get: function () { return is_message_js_1.isCompleteMessage; } });
14
12
  Object.defineProperty(exports, "isCompleteField", { enumerable: true, get: function () { return is_message_js_1.isCompleteField; } });
@@ -16,8 +14,6 @@ var field_js_1 = require("./field.js");
16
14
  Object.defineProperty(exports, "newFieldList", { enumerable: true, get: function () { return field_js_1.newFieldList; } });
17
15
  Object.defineProperty(exports, "FieldList", { enumerable: true, get: function () { return field_js_1.FieldList; } });
18
16
  Object.defineProperty(exports, "fieldJsonName", { enumerable: true, get: function () { return field_js_1.fieldJsonName; } });
19
- Object.defineProperty(exports, "localFieldName", { enumerable: true, get: function () { return field_js_1.localFieldName; } });
20
- Object.defineProperty(exports, "localOneofName", { enumerable: true, get: function () { return field_js_1.localOneofName; } });
21
17
  var scalar_js_1 = require("./scalar.js");
22
18
  Object.defineProperty(exports, "scalarEquals", { enumerable: true, get: function () { return scalar_js_1.scalarEquals; } });
23
19
  Object.defineProperty(exports, "scalarZeroValue", { enumerable: true, get: function () { return scalar_js_1.scalarZeroValue; } });
@@ -25,3 +21,40 @@ Object.defineProperty(exports, "isScalarZeroValue", { enumerable: true, get: fun
25
21
  var enum_js_1 = require("./enum.js");
26
22
  Object.defineProperty(exports, "createEnumType", { enumerable: true, get: function () { return enum_js_1.createEnumType; } });
27
23
  Object.defineProperty(exports, "normalizeEnumValue", { enumerable: true, get: function () { return enum_js_1.normalizeEnumValue; } });
24
+ var names_js_1 = require("./names.js");
25
+ Object.defineProperty(exports, "localName", { enumerable: true, get: function () { return names_js_1.localName; } });
26
+ Object.defineProperty(exports, "localFieldName", { enumerable: true, get: function () { return names_js_1.localFieldName; } });
27
+ Object.defineProperty(exports, "localOneofName", { enumerable: true, get: function () { return names_js_1.localOneofName; } });
28
+ Object.defineProperty(exports, "findEnumSharedPrefix", { enumerable: true, get: function () { return names_js_1.findEnumSharedPrefix; } });
29
+ Object.defineProperty(exports, "camelToSnakeCase", { enumerable: true, get: function () { return names_js_1.camelToSnakeCase; } });
30
+ Object.defineProperty(exports, "protoCamelCase", { enumerable: true, get: function () { return names_js_1.protoCamelCase; } });
31
+ Object.defineProperty(exports, "safeObjectProperty", { enumerable: true, get: function () { return names_js_1.safeObjectProperty; } });
32
+ Object.defineProperty(exports, "safeIdentifier", { enumerable: true, get: function () { return names_js_1.safeIdentifier; } });
33
+ var varint_js_1 = require("./varint.js");
34
+ Object.defineProperty(exports, "int64FromString", { enumerable: true, get: function () { return varint_js_1.int64FromString; } });
35
+ Object.defineProperty(exports, "int64ToString", { enumerable: true, get: function () { return varint_js_1.int64ToString; } });
36
+ Object.defineProperty(exports, "uInt64ToString", { enumerable: true, get: function () { return varint_js_1.uInt64ToString; } });
37
+ var proto_int64_js_1 = require("./proto-int64.js");
38
+ Object.defineProperty(exports, "protoInt64", { enumerable: true, get: function () { return proto_int64_js_1.protoInt64; } });
39
+ var proto_base64_js_1 = require("./proto-base64.js");
40
+ Object.defineProperty(exports, "protoBase64", { enumerable: true, get: function () { return proto_base64_js_1.protoBase64; } });
41
+ var proto_double_js_1 = require("./proto-double.js");
42
+ Object.defineProperty(exports, "protoDouble", { enumerable: true, get: function () { return proto_double_js_1.protoDouble; } });
43
+ var index_js_1 = require("./google/index.js");
44
+ Object.defineProperty(exports, "Timestamp", { enumerable: true, get: function () { return index_js_1.Timestamp; } });
45
+ Object.defineProperty(exports, "Duration", { enumerable: true, get: function () { return index_js_1.Duration; } });
46
+ Object.defineProperty(exports, "Any", { enumerable: true, get: function () { return index_js_1.Any; } });
47
+ Object.defineProperty(exports, "Empty", { enumerable: true, get: function () { return index_js_1.Empty; } });
48
+ Object.defineProperty(exports, "DoubleValue", { enumerable: true, get: function () { return index_js_1.DoubleValue; } });
49
+ Object.defineProperty(exports, "FloatValue", { enumerable: true, get: function () { return index_js_1.FloatValue; } });
50
+ Object.defineProperty(exports, "Int64Value", { enumerable: true, get: function () { return index_js_1.Int64Value; } });
51
+ Object.defineProperty(exports, "UInt64Value", { enumerable: true, get: function () { return index_js_1.UInt64Value; } });
52
+ Object.defineProperty(exports, "Int32Value", { enumerable: true, get: function () { return index_js_1.Int32Value; } });
53
+ Object.defineProperty(exports, "UInt32Value", { enumerable: true, get: function () { return index_js_1.UInt32Value; } });
54
+ Object.defineProperty(exports, "BoolValue", { enumerable: true, get: function () { return index_js_1.BoolValue; } });
55
+ Object.defineProperty(exports, "StringValue", { enumerable: true, get: function () { return index_js_1.StringValue; } });
56
+ Object.defineProperty(exports, "BytesValue", { enumerable: true, get: function () { return index_js_1.BytesValue; } });
57
+ Object.defineProperty(exports, "Value", { enumerable: true, get: function () { return index_js_1.Value; } });
58
+ Object.defineProperty(exports, "NullValue", { enumerable: true, get: function () { return index_js_1.NullValue; } });
59
+ Object.defineProperty(exports, "ListValue", { enumerable: true, get: function () { return index_js_1.ListValue; } });
60
+ Object.defineProperty(exports, "Struct", { enumerable: true, get: function () { return index_js_1.Struct; } });
@@ -1,5 +1,13 @@
1
1
  import { FieldInfo } from "./field.js";
2
- import { AnyMessage, CompleteMessage, Message, Field, CompleteField } from "./message.js";
2
+ import { AnyMessage, Message, CompleteMessage, Field } from "./message.js";
3
+ /**
4
+ * Check whether the given partial is a valid message.
5
+ */
6
+ export declare function isMessage<T extends Message<T> = AnyMessage>(arg: any, fields: readonly FieldInfo[]): arg is T;
7
+ /**
8
+ * Check whether the given partial field is a valid field value.
9
+ */
10
+ export declare function isField<F>(value: any, field: FieldInfo): value is Field<F>;
3
11
  /**
4
12
  * Check whether the given partial has all fields present recursively.
5
13
  */
@@ -7,4 +15,4 @@ export declare function isCompleteMessage<T extends Message<T> = AnyMessage>(arg
7
15
  /**
8
16
  * Check whether the given partial field has a full value present recursively.
9
17
  */
10
- export declare function isCompleteField<F>(value: Field<F>, field: FieldInfo): value is CompleteField<F>;
18
+ export declare function isCompleteField<F>(value: Field<F>, field: FieldInfo): boolean;
@@ -1,10 +1,80 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isCompleteField = exports.isCompleteMessage = void 0;
3
+ exports.isCompleteField = exports.isCompleteMessage = exports.isField = exports.isMessage = void 0;
4
+ const field_js_1 = require("./field.js");
5
+ /**
6
+ * Check whether the given partial is a valid message.
7
+ */
8
+ function isMessage(arg, fields) {
9
+ if (arg == null || typeof arg !== "object") {
10
+ return false;
11
+ }
12
+ return fields.every((fi) => {
13
+ // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
14
+ const value = arg[fi.localName];
15
+ // if the value is null or undefined this is OK in isMessage.
16
+ if (value == null) {
17
+ return true;
18
+ }
19
+ if (fi.repeated) {
20
+ return Array.isArray(value) && value.every((item) => isField(item, fi));
21
+ }
22
+ else {
23
+ return isField(value, fi);
24
+ }
25
+ });
26
+ }
27
+ exports.isMessage = isMessage;
28
+ /**
29
+ * Check whether the given partial field is a valid field value.
30
+ */
31
+ function isField(value, field) {
32
+ if (value == null) {
33
+ return false;
34
+ }
35
+ if (field.oneof) {
36
+ // For oneof fields, only one field should be set
37
+ const oneofFields = field.oneof.fields;
38
+ const setField = oneofFields.find((f) => value[f.localName] !== undefined);
39
+ return setField === field;
40
+ }
41
+ if (value === undefined) {
42
+ return false;
43
+ }
44
+ const fieldKind = field.kind;
45
+ switch (fieldKind) {
46
+ case "scalar":
47
+ return true;
48
+ case "message":
49
+ const messageType = (0, field_js_1.resolveMessageType)(field.T);
50
+ return isMessage(value, messageType.fields.list());
51
+ case "enum":
52
+ return typeof value === "number";
53
+ case "map":
54
+ return Object.values(value).every((val) => {
55
+ const valueKind = field.V.kind;
56
+ switch (valueKind) {
57
+ case "scalar":
58
+ return true;
59
+ case "enum":
60
+ return typeof val === "number";
61
+ case "message":
62
+ const messageType = (0, field_js_1.resolveMessageType)(field.V.T);
63
+ return isMessage(val, messageType.fields.list());
64
+ default:
65
+ return valueKind;
66
+ }
67
+ });
68
+ default:
69
+ return fieldKind;
70
+ }
71
+ }
72
+ exports.isField = isField;
4
73
  /**
5
74
  * Check whether the given partial has all fields present recursively.
6
75
  */
7
76
  function isCompleteMessage(arg, fields) {
77
+ // arg is CompleteMessage<T> {
8
78
  if (arg == null || typeof arg !== "object") {
9
79
  return false;
10
80
  }
@@ -24,6 +94,7 @@ exports.isCompleteMessage = isCompleteMessage;
24
94
  * Check whether the given partial field has a full value present recursively.
25
95
  */
26
96
  function isCompleteField(value, field) {
97
+ // value is CompleteField<F> {
27
98
  if (field.oneof) {
28
99
  // For oneof fields, only one field should be set
29
100
  const oneofFields = field.oneof.fields;
@@ -38,7 +109,7 @@ function isCompleteField(value, field) {
38
109
  case "scalar":
39
110
  return true;
40
111
  case "message":
41
- const messageType = typeof field.T === "function" ? field.T() : field.T;
112
+ const messageType = (0, field_js_1.resolveMessageType)(field.T);
42
113
  return isCompleteMessage(value, messageType.fields.list());
43
114
  case "enum":
44
115
  return typeof value === "number";
@@ -51,7 +122,7 @@ function isCompleteField(value, field) {
51
122
  case "enum":
52
123
  return typeof val === "number";
53
124
  case "message":
54
- const messageType = typeof field.V.T === "function" ? field.V.T() : field.V.T;
125
+ const messageType = (0, field_js_1.resolveMessageType)(field.V.T);
55
126
  return isCompleteMessage(val, messageType.fields.list());
56
127
  default:
57
128
  return valueKind;
package/dist/json.d.ts CHANGED
@@ -1,5 +1,72 @@
1
- import { JsonReadOptions, JsonValue, JsonWriteOptions, JsonWriteStringOptions } from "@bufbuild/protobuf";
2
1
  import { FieldInfo, FieldList } from "./field.js";
2
+ import { IMessageTypeRegistry } from "./type-registry.js";
3
+ /**
4
+ * Options for parsing JSON data.
5
+ */
6
+ export interface JsonReadOptions {
7
+ /**
8
+ * Ignore unknown fields: Proto3 JSON parser should reject unknown fields
9
+ * by default. This option ignores unknown fields in parsing, as well as
10
+ * unrecognized enum string representations.
11
+ */
12
+ ignoreUnknownFields: boolean;
13
+ /**
14
+ * This option is required to read `google.protobuf.Any`
15
+ */
16
+ typeRegistry?: IMessageTypeRegistry;
17
+ }
18
+ /**
19
+ * Options for serializing to JSON.
20
+ */
21
+ export interface JsonWriteOptions {
22
+ /**
23
+ * Emit fields with default values: Fields with default values are omitted
24
+ * by default in proto3 JSON output. This option overrides this behavior
25
+ * and outputs fields with their default values.
26
+ */
27
+ emitDefaultValues: boolean;
28
+ /**
29
+ * Emit enum values as integers instead of strings: The name of an enum
30
+ * value is used by default in JSON output. An option may be provided to
31
+ * use the numeric value of the enum value instead.
32
+ */
33
+ enumAsInteger: boolean;
34
+ /**
35
+ * Use proto field name instead of lowerCamelCase name: By default proto3
36
+ * JSON printer should convert the field name to lowerCamelCase and use
37
+ * that as the JSON name. An implementation may provide an option to use
38
+ * proto field name as the JSON name instead. Proto3 JSON parsers are
39
+ * required to accept both the converted lowerCamelCase name and the proto
40
+ * field name.
41
+ */
42
+ useProtoFieldName: boolean;
43
+ /**
44
+ * This option is required to write `google.protobuf.Any`
45
+ */
46
+ typeRegistry?: IMessageTypeRegistry;
47
+ }
48
+ /**
49
+ * Options for serializing to JSON.
50
+ */
51
+ export interface JsonWriteStringOptions extends JsonWriteOptions {
52
+ prettySpaces: number;
53
+ }
54
+ /**
55
+ * Represents any possible JSON value:
56
+ * - number
57
+ * - string
58
+ * - boolean
59
+ * - null
60
+ * - object (with any JSON value as property)
61
+ * - array (with any JSON value as element)
62
+ */
63
+ export type JsonValue = number | string | boolean | null | JsonObject | JsonValue[];
64
+ /**
65
+ * Represents a JSON object.
66
+ */
67
+ export type JsonObject = {
68
+ [k: string]: JsonValue;
69
+ };
3
70
  export declare function makeReadOptions(options?: Partial<JsonReadOptions>): Readonly<JsonReadOptions>;
4
71
  export declare function makeWriteOptions(options?: Partial<JsonWriteStringOptions>): Readonly<JsonWriteStringOptions>;
5
72
  export declare function readMessage<T>(fields: FieldList, typeName: string, json: JsonValue, options: JsonReadOptions, message: T): T;
package/dist/json.js CHANGED
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.clearField = exports.writeMessage = exports.readMessage = exports.makeWriteOptions = exports.makeReadOptions = void 0;
4
- const protobuf_1 = require("@bufbuild/protobuf");
5
4
  const field_js_1 = require("./field.js");
6
5
  const assert_js_1 = require("./assert.js");
7
6
  const scalar_js_1 = require("./scalar.js");
8
7
  const field_wrapper_js_1 = require("./field-wrapper.js");
8
+ const proto_int64_js_1 = require("./proto-int64.js");
9
+ const proto_base64_js_1 = require("./proto-base64.js");
9
10
  // Default options for parsing JSON.
10
11
  const jsonReadDefaults = {
11
12
  ignoreUnknownFields: false,
@@ -43,7 +44,6 @@ function readMessage(fields, typeName, json, options, message) {
43
44
  throw new Error(`cannot decode message ${typeName} from JSON: ${debugJsonValue(json)}`);
44
45
  }
45
46
  const oneofSeen = new Map();
46
- const registry = options.typeRegistry;
47
47
  for (const [jsonKey, jsonValue] of Object.entries(json)) {
48
48
  const field = fields.findJsonName(jsonKey);
49
49
  if (field) {
@@ -62,30 +62,6 @@ function readMessage(fields, typeName, json, options, message) {
62
62
  }
63
63
  else {
64
64
  let found = false;
65
- if (registry?.findExtension &&
66
- jsonKey.startsWith("[") &&
67
- jsonKey.endsWith("]")) {
68
- // TODO: handle extension
69
- /*
70
- const ext = registry.findExtension(
71
- jsonKey.substring(1, jsonKey.length - 1),
72
- );
73
- if (ext && ext.extendee.typeName == typeName) {
74
- found = true;
75
- const [container, get] = createExtensionContainer(ext);
76
- readField(container, jsonValue, ext.field, options, ext);
77
- // We pass on the options as BinaryReadOptions/BinaryWriteOptions,
78
- // so that users can bring their own binary reader and writer factories
79
- // if necessary.
80
- setExtension(
81
- message,
82
- ext as Extension<T>,
83
- get(),
84
- options as Partial<BinaryReadOptions & BinaryWriteOptions>,
85
- );
86
- }
87
- */
88
- }
89
65
  if (!found && !options.ignoreUnknownFields) {
90
66
  throw new Error(`cannot decode message ${typeName} from JSON: key "${jsonKey}" is unknown`);
91
67
  }
@@ -111,8 +87,8 @@ function writeMessage(message, fields, options) {
111
87
  continue;
112
88
  }
113
89
  }
114
- const value = field.oneof
115
- ? message[field.oneof.localName].value
90
+ const value = field.oneof ?
91
+ message[field.oneof.localName].value
116
92
  : message[field.localName];
117
93
  const jsonValue = writeField(field, value, options);
118
94
  if (jsonValue !== undefined) {
@@ -144,8 +120,8 @@ function writeMessage(message, fields, options) {
144
120
  */
145
121
  }
146
122
  catch (e) {
147
- const m = field
148
- ? `cannot encode field ${field.name} to JSON`
123
+ const m = field ?
124
+ `cannot encode field ${field.name} to JSON`
149
125
  : `cannot encode message to JSON`;
150
126
  const r = e instanceof Error ? e.message : String(e);
151
127
  throw new Error(m + (r.length > 0 ? `: ${r}` : ""));
@@ -175,7 +151,7 @@ function readField(target, jsonValue, field, options) {
175
151
  }
176
152
  switch (field.kind) {
177
153
  case "message":
178
- const messageType = typeof field.T === "function" ? field.T() : field.T;
154
+ const messageType = (0, field_js_1.resolveMessageType)(field.T);
179
155
  targetArray.push(messageType.fromJson(jsonItem, options));
180
156
  break;
181
157
  case "enum":
@@ -227,7 +203,7 @@ function readField(target, jsonValue, field, options) {
227
203
  }
228
204
  switch (field.V.kind) {
229
205
  case "message":
230
- const messageType = typeof field.V.T === "function" ? field.V.T() : field.V.T;
206
+ const messageType = (0, field_js_1.resolveMessageType)(field.V.T);
231
207
  targetMap[key] = messageType.fromJson(jsonMapValue, options);
232
208
  break;
233
209
  case "enum":
@@ -238,7 +214,7 @@ function readField(target, jsonValue, field, options) {
238
214
  break;
239
215
  case "scalar":
240
216
  try {
241
- targetMap[key] = readScalar(field.V.T, jsonMapValue, protobuf_1.LongType.BIGINT, true);
217
+ targetMap[key] = readScalar(field.V.T, jsonMapValue, scalar_js_1.LongType.BIGINT, true);
242
218
  }
243
219
  catch (e) {
244
220
  let m = `cannot decode map value for field ${field.name} from JSON: ${debugJsonValue(jsonValue)}`;
@@ -258,7 +234,7 @@ function readField(target, jsonValue, field, options) {
258
234
  }
259
235
  switch (field.kind) {
260
236
  case "message":
261
- const messageType = typeof field.T === "function" ? field.T() : field.T;
237
+ const messageType = (0, field_js_1.resolveMessageType)(field.T);
262
238
  if (jsonValue === null &&
263
239
  messageType.typeName != "google.protobuf.Value") {
264
240
  return;
@@ -345,8 +321,8 @@ function readScalar(type, json, longType, nullAsZeroValue) {
345
321
  switch (type) {
346
322
  // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
347
323
  // Either numbers or strings are accepted. Exponent notation is also accepted.
348
- case protobuf_1.ScalarType.DOUBLE:
349
- case protobuf_1.ScalarType.FLOAT:
324
+ case scalar_js_1.ScalarType.DOUBLE:
325
+ case scalar_js_1.ScalarType.FLOAT:
350
326
  if (json === "NaN")
351
327
  return Number.NaN;
352
328
  if (json === "Infinity")
@@ -373,15 +349,15 @@ function readScalar(type, json, longType, nullAsZeroValue) {
373
349
  // infinity and -infinity are handled by string representation above, so this is an error
374
350
  break;
375
351
  }
376
- if (type == protobuf_1.ScalarType.FLOAT)
352
+ if (type == scalar_js_1.ScalarType.FLOAT)
377
353
  (0, assert_js_1.assertFloat32)(float);
378
354
  return float;
379
355
  // int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
380
- case protobuf_1.ScalarType.INT32:
381
- case protobuf_1.ScalarType.FIXED32:
382
- case protobuf_1.ScalarType.SFIXED32:
383
- case protobuf_1.ScalarType.SINT32:
384
- case protobuf_1.ScalarType.UINT32:
356
+ case scalar_js_1.ScalarType.INT32:
357
+ case scalar_js_1.ScalarType.FIXED32:
358
+ case scalar_js_1.ScalarType.SFIXED32:
359
+ case scalar_js_1.ScalarType.SINT32:
360
+ case scalar_js_1.ScalarType.UINT32:
385
361
  let int32;
386
362
  if (typeof json == "number")
387
363
  int32 = json;
@@ -391,34 +367,34 @@ function readScalar(type, json, longType, nullAsZeroValue) {
391
367
  }
392
368
  if (int32 === undefined)
393
369
  break;
394
- if (type == protobuf_1.ScalarType.UINT32 || type == protobuf_1.ScalarType.FIXED32)
370
+ if (type == scalar_js_1.ScalarType.UINT32 || type == scalar_js_1.ScalarType.FIXED32)
395
371
  (0, assert_js_1.assertUInt32)(int32);
396
372
  else
397
373
  (0, assert_js_1.assertInt32)(int32);
398
374
  return int32;
399
375
  // int64, fixed64, uint64: JSON value will be a decimal string. Either numbers or strings are accepted.
400
- case protobuf_1.ScalarType.INT64:
401
- case protobuf_1.ScalarType.SFIXED64:
402
- case protobuf_1.ScalarType.SINT64:
376
+ case scalar_js_1.ScalarType.INT64:
377
+ case scalar_js_1.ScalarType.SFIXED64:
378
+ case scalar_js_1.ScalarType.SINT64:
403
379
  if (typeof json != "number" && typeof json != "string")
404
380
  break;
405
- const long = protobuf_1.protoInt64.parse(json);
381
+ const long = proto_int64_js_1.protoInt64.parse(json);
406
382
  // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
407
383
  return longType ? long.toString() : long;
408
- case protobuf_1.ScalarType.FIXED64:
409
- case protobuf_1.ScalarType.UINT64:
384
+ case scalar_js_1.ScalarType.FIXED64:
385
+ case scalar_js_1.ScalarType.UINT64:
410
386
  if (typeof json != "number" && typeof json != "string")
411
387
  break;
412
- const uLong = protobuf_1.protoInt64.uParse(json);
388
+ const uLong = proto_int64_js_1.protoInt64.uParse(json);
413
389
  // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
414
390
  return longType ? uLong.toString() : uLong;
415
391
  // bool:
416
- case protobuf_1.ScalarType.BOOL:
392
+ case scalar_js_1.ScalarType.BOOL:
417
393
  if (typeof json !== "boolean")
418
394
  break;
419
395
  return json;
420
396
  // string:
421
- case protobuf_1.ScalarType.STRING:
397
+ case scalar_js_1.ScalarType.STRING:
422
398
  if (typeof json !== "string") {
423
399
  break;
424
400
  }
@@ -433,17 +409,17 @@ function readScalar(type, json, longType, nullAsZeroValue) {
433
409
  return json;
434
410
  // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
435
411
  // Either standard or URL-safe base64 encoding with/without paddings are accepted.
436
- case protobuf_1.ScalarType.BYTES:
412
+ case scalar_js_1.ScalarType.BYTES:
437
413
  if (json === "")
438
414
  return new Uint8Array(0);
439
415
  if (typeof json !== "string")
440
416
  break;
441
- return protobuf_1.protoBase64.dec(json);
417
+ return proto_base64_js_1.protoBase64.dec(json);
442
418
  }
443
419
  throw new Error();
444
420
  }
445
421
  function readMapKey(type, json) {
446
- if (type === protobuf_1.ScalarType.BOOL) {
422
+ if (type === scalar_js_1.ScalarType.BOOL) {
447
423
  // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
448
424
  switch (json) {
449
425
  case "true":
@@ -454,7 +430,7 @@ function readMapKey(type, json) {
454
430
  break;
455
431
  }
456
432
  }
457
- return readScalar(type, json, protobuf_1.LongType.BIGINT, true).toString();
433
+ return readScalar(type, json, scalar_js_1.LongType.BIGINT, true).toString();
458
434
  }
459
435
  /**
460
436
  * Resets the field, so that isFieldSet() will return false.
@@ -477,9 +453,8 @@ function clearField(field, target) {
477
453
  target[localName] = implicitPresence ? field.T.values[0].no : undefined;
478
454
  break;
479
455
  case "scalar":
480
- target[localName] = implicitPresence
481
- ? (0, scalar_js_1.scalarZeroValue)(field.T, field.L)
482
- : undefined;
456
+ target[localName] =
457
+ implicitPresence ? (0, scalar_js_1.scalarZeroValue)(field.T, field.L) : undefined;
483
458
  break;
484
459
  case "message":
485
460
  target[localName] = undefined;
@@ -523,7 +498,7 @@ function writeField(field, value, options) {
523
498
  case "message":
524
499
  for (const [entryKey, entryValue] of entries) {
525
500
  // JSON standard allows only (double quoted) string as property key
526
- const messageType = typeof field.V.T === "function" ? field.V.T() : field.V.T;
501
+ const messageType = (0, field_js_1.resolveMessageType)(field.V.T);
527
502
  jsonObj[entryKey.toString()] = messageType.toJson(entryValue, options);
528
503
  }
529
504
  break;
@@ -535,8 +510,8 @@ function writeField(field, value, options) {
535
510
  }
536
511
  break;
537
512
  }
538
- return options.emitDefaultValues || entries.length > 0
539
- ? jsonObj
513
+ return options.emitDefaultValues || entries.length > 0 ?
514
+ jsonObj
540
515
  : undefined;
541
516
  }
542
517
  if (field.repeated) {
@@ -559,8 +534,8 @@ function writeField(field, value, options) {
559
534
  }
560
535
  break;
561
536
  }
562
- return options.emitDefaultValues || jsonArr.length > 0
563
- ? jsonArr
537
+ return options.emitDefaultValues || jsonArr.length > 0 ?
538
+ jsonArr
564
539
  : undefined;
565
540
  }
566
541
  switch (field.kind) {
@@ -569,24 +544,25 @@ function writeField(field, value, options) {
569
544
  case "enum":
570
545
  return writeEnum(field.T, value, options.enumAsInteger);
571
546
  case "message":
572
- return (0, field_wrapper_js_1.wrapField)((typeof field.T === "function" ? field.T() : field.T).fieldWrapper, value).toJson(options);
547
+ const messageType = (0, field_js_1.resolveMessageType)(field.T);
548
+ return messageType.toJson((0, field_wrapper_js_1.wrapField)(messageType.fieldWrapper, value));
573
549
  }
574
550
  }
575
551
  function writeScalar(type, value) {
576
552
  switch (type) {
577
553
  // int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
578
- case protobuf_1.ScalarType.INT32:
579
- case protobuf_1.ScalarType.SFIXED32:
580
- case protobuf_1.ScalarType.SINT32:
581
- case protobuf_1.ScalarType.FIXED32:
582
- case protobuf_1.ScalarType.UINT32:
554
+ case scalar_js_1.ScalarType.INT32:
555
+ case scalar_js_1.ScalarType.SFIXED32:
556
+ case scalar_js_1.ScalarType.SINT32:
557
+ case scalar_js_1.ScalarType.FIXED32:
558
+ case scalar_js_1.ScalarType.UINT32:
583
559
  (0, assert_js_1.assert)(typeof value == "number");
584
560
  return value;
585
561
  // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
586
562
  // Either numbers or strings are accepted. Exponent notation is also accepted.
587
- case protobuf_1.ScalarType.FLOAT:
563
+ case scalar_js_1.ScalarType.FLOAT:
588
564
  // assertFloat32(value);
589
- case protobuf_1.ScalarType.DOUBLE: // eslint-disable-line no-fallthrough
565
+ case scalar_js_1.ScalarType.DOUBLE: // eslint-disable-line no-fallthrough
590
566
  (0, assert_js_1.assert)(typeof value == "number");
591
567
  if (Number.isNaN(value))
592
568
  return "NaN";
@@ -596,28 +572,28 @@ function writeScalar(type, value) {
596
572
  return "-Infinity";
597
573
  return value;
598
574
  // string:
599
- case protobuf_1.ScalarType.STRING:
575
+ case scalar_js_1.ScalarType.STRING:
600
576
  (0, assert_js_1.assert)(typeof value == "string");
601
577
  return value;
602
578
  // bool:
603
- case protobuf_1.ScalarType.BOOL:
579
+ case scalar_js_1.ScalarType.BOOL:
604
580
  (0, assert_js_1.assert)(typeof value == "boolean");
605
581
  return value;
606
582
  // JSON value will be a decimal string. Either numbers or strings are accepted.
607
- case protobuf_1.ScalarType.UINT64:
608
- case protobuf_1.ScalarType.FIXED64:
609
- case protobuf_1.ScalarType.INT64:
610
- case protobuf_1.ScalarType.SFIXED64:
611
- case protobuf_1.ScalarType.SINT64:
583
+ case scalar_js_1.ScalarType.UINT64:
584
+ case scalar_js_1.ScalarType.FIXED64:
585
+ case scalar_js_1.ScalarType.INT64:
586
+ case scalar_js_1.ScalarType.SFIXED64:
587
+ case scalar_js_1.ScalarType.SINT64:
612
588
  (0, assert_js_1.assert)(typeof value == "bigint" ||
613
589
  typeof value == "string" ||
614
590
  typeof value == "number");
615
591
  return value.toString();
616
592
  // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
617
593
  // Either standard or URL-safe base64 encoding with/without paddings are accepted.
618
- case protobuf_1.ScalarType.BYTES:
594
+ case scalar_js_1.ScalarType.BYTES:
619
595
  (0, assert_js_1.assert)(value instanceof Uint8Array);
620
- return protobuf_1.protoBase64.enc(value);
596
+ return proto_base64_js_1.protoBase64.enc(value);
621
597
  }
622
598
  }
623
599
  function writeEnum(type, value, enumAsInteger) {