@aptre/protobuf-es-lite 0.2.6 → 0.2.8

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 +25 -24
  3. package/bin/protoc-gen-es-lite +3 -3
  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 +69 -66
  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 +8 -3
  46. package/dist/index.js +38 -8
  47. package/dist/is-message.d.ts +10 -2
  48. package/dist/is-message.js +76 -3
  49. package/dist/json.d.ts +68 -1
  50. package/dist/json.js +60 -81
  51. package/dist/message.d.ts +30 -61
  52. package/dist/message.js +63 -18
  53. package/dist/names.d.ts +43 -0
  54. package/dist/names.js +276 -0
  55. package/dist/partial.js +19 -16
  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} +48 -36
  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,11 +1,6 @@
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.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; } });
@@ -16,8 +11,6 @@ var field_js_1 = require("./field.js");
16
11
  Object.defineProperty(exports, "newFieldList", { enumerable: true, get: function () { return field_js_1.newFieldList; } });
17
12
  Object.defineProperty(exports, "FieldList", { enumerable: true, get: function () { return field_js_1.FieldList; } });
18
13
  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
14
  var scalar_js_1 = require("./scalar.js");
22
15
  Object.defineProperty(exports, "scalarEquals", { enumerable: true, get: function () { return scalar_js_1.scalarEquals; } });
23
16
  Object.defineProperty(exports, "scalarZeroValue", { enumerable: true, get: function () { return scalar_js_1.scalarZeroValue; } });
@@ -25,3 +18,40 @@ Object.defineProperty(exports, "isScalarZeroValue", { enumerable: true, get: fun
25
18
  var enum_js_1 = require("./enum.js");
26
19
  Object.defineProperty(exports, "createEnumType", { enumerable: true, get: function () { return enum_js_1.createEnumType; } });
27
20
  Object.defineProperty(exports, "normalizeEnumValue", { enumerable: true, get: function () { return enum_js_1.normalizeEnumValue; } });
21
+ var names_js_1 = require("./names.js");
22
+ Object.defineProperty(exports, "localName", { enumerable: true, get: function () { return names_js_1.localName; } });
23
+ Object.defineProperty(exports, "localFieldName", { enumerable: true, get: function () { return names_js_1.localFieldName; } });
24
+ Object.defineProperty(exports, "localOneofName", { enumerable: true, get: function () { return names_js_1.localOneofName; } });
25
+ Object.defineProperty(exports, "findEnumSharedPrefix", { enumerable: true, get: function () { return names_js_1.findEnumSharedPrefix; } });
26
+ Object.defineProperty(exports, "camelToSnakeCase", { enumerable: true, get: function () { return names_js_1.camelToSnakeCase; } });
27
+ Object.defineProperty(exports, "protoCamelCase", { enumerable: true, get: function () { return names_js_1.protoCamelCase; } });
28
+ Object.defineProperty(exports, "safeObjectProperty", { enumerable: true, get: function () { return names_js_1.safeObjectProperty; } });
29
+ Object.defineProperty(exports, "safeIdentifier", { enumerable: true, get: function () { return names_js_1.safeIdentifier; } });
30
+ var varint_js_1 = require("./varint.js");
31
+ Object.defineProperty(exports, "int64FromString", { enumerable: true, get: function () { return varint_js_1.int64FromString; } });
32
+ Object.defineProperty(exports, "int64ToString", { enumerable: true, get: function () { return varint_js_1.int64ToString; } });
33
+ Object.defineProperty(exports, "uInt64ToString", { enumerable: true, get: function () { return varint_js_1.uInt64ToString; } });
34
+ var proto_int64_js_1 = require("./proto-int64.js");
35
+ Object.defineProperty(exports, "protoInt64", { enumerable: true, get: function () { return proto_int64_js_1.protoInt64; } });
36
+ var proto_base64_js_1 = require("./proto-base64.js");
37
+ Object.defineProperty(exports, "protoBase64", { enumerable: true, get: function () { return proto_base64_js_1.protoBase64; } });
38
+ var proto_double_js_1 = require("./proto-double.js");
39
+ Object.defineProperty(exports, "protoDouble", { enumerable: true, get: function () { return proto_double_js_1.protoDouble; } });
40
+ var index_js_1 = require("./google/index.js");
41
+ Object.defineProperty(exports, "Timestamp", { enumerable: true, get: function () { return index_js_1.Timestamp; } });
42
+ Object.defineProperty(exports, "Duration", { enumerable: true, get: function () { return index_js_1.Duration; } });
43
+ Object.defineProperty(exports, "Any", { enumerable: true, get: function () { return index_js_1.Any; } });
44
+ Object.defineProperty(exports, "Empty", { enumerable: true, get: function () { return index_js_1.Empty; } });
45
+ Object.defineProperty(exports, "DoubleValue", { enumerable: true, get: function () { return index_js_1.DoubleValue; } });
46
+ Object.defineProperty(exports, "FloatValue", { enumerable: true, get: function () { return index_js_1.FloatValue; } });
47
+ Object.defineProperty(exports, "Int64Value", { enumerable: true, get: function () { return index_js_1.Int64Value; } });
48
+ Object.defineProperty(exports, "UInt64Value", { enumerable: true, get: function () { return index_js_1.UInt64Value; } });
49
+ Object.defineProperty(exports, "Int32Value", { enumerable: true, get: function () { return index_js_1.Int32Value; } });
50
+ Object.defineProperty(exports, "UInt32Value", { enumerable: true, get: function () { return index_js_1.UInt32Value; } });
51
+ Object.defineProperty(exports, "BoolValue", { enumerable: true, get: function () { return index_js_1.BoolValue; } });
52
+ Object.defineProperty(exports, "StringValue", { enumerable: true, get: function () { return index_js_1.StringValue; } });
53
+ Object.defineProperty(exports, "BytesValue", { enumerable: true, get: function () { return index_js_1.BytesValue; } });
54
+ Object.defineProperty(exports, "Value", { enumerable: true, get: function () { return index_js_1.Value; } });
55
+ Object.defineProperty(exports, "NullValue", { enumerable: true, get: function () { return index_js_1.NullValue; } });
56
+ Object.defineProperty(exports, "ListValue", { enumerable: true, get: function () { return index_js_1.ListValue; } });
57
+ 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,8 @@ function isCompleteField(value, field) {
38
109
  case "scalar":
39
110
  return true;
40
111
  case "message":
41
- return isCompleteMessage(value, field.T.fields.list());
112
+ const messageType = (0, field_js_1.resolveMessageType)(field.T);
113
+ return isCompleteMessage(value, messageType.fields.list());
42
114
  case "enum":
43
115
  return typeof value === "number";
44
116
  case "map":
@@ -50,7 +122,8 @@ function isCompleteField(value, field) {
50
122
  case "enum":
51
123
  return typeof val === "number";
52
124
  case "message":
53
- return isCompleteMessage(val, field.V.T.fields.list());
125
+ const messageType = (0, field_js_1.resolveMessageType)(field.V.T);
126
+ return isCompleteMessage(val, messageType.fields.list());
54
127
  default:
55
128
  return valueKind;
56
129
  }
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,8 @@ function readField(target, jsonValue, field, options) {
175
151
  }
176
152
  switch (field.kind) {
177
153
  case "message":
178
- targetArray.push(field.T.fromJson(jsonItem, options));
154
+ const messageType = (0, field_js_1.resolveMessageType)(field.T);
155
+ targetArray.push(messageType.fromJson(jsonItem, options));
179
156
  break;
180
157
  case "enum":
181
158
  const enumValue = readEnum(field.T, jsonItem, options.ignoreUnknownFields, true);
@@ -226,7 +203,8 @@ function readField(target, jsonValue, field, options) {
226
203
  }
227
204
  switch (field.V.kind) {
228
205
  case "message":
229
- targetMap[key] = field.V.T.fromJson(jsonMapValue, options);
206
+ const messageType = (0, field_js_1.resolveMessageType)(field.V.T);
207
+ targetMap[key] = messageType.fromJson(jsonMapValue, options);
230
208
  break;
231
209
  case "enum":
232
210
  const enumValue = readEnum(field.V.T, jsonMapValue, options.ignoreUnknownFields, true);
@@ -236,7 +214,7 @@ function readField(target, jsonValue, field, options) {
236
214
  break;
237
215
  case "scalar":
238
216
  try {
239
- 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);
240
218
  }
241
219
  catch (e) {
242
220
  let m = `cannot decode map value for field ${field.name} from JSON: ${debugJsonValue(jsonValue)}`;
@@ -256,7 +234,7 @@ function readField(target, jsonValue, field, options) {
256
234
  }
257
235
  switch (field.kind) {
258
236
  case "message":
259
- const messageType = field.T;
237
+ const messageType = (0, field_js_1.resolveMessageType)(field.T);
260
238
  if (jsonValue === null &&
261
239
  messageType.typeName != "google.protobuf.Value") {
262
240
  return;
@@ -343,8 +321,8 @@ function readScalar(type, json, longType, nullAsZeroValue) {
343
321
  switch (type) {
344
322
  // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
345
323
  // Either numbers or strings are accepted. Exponent notation is also accepted.
346
- case protobuf_1.ScalarType.DOUBLE:
347
- case protobuf_1.ScalarType.FLOAT:
324
+ case scalar_js_1.ScalarType.DOUBLE:
325
+ case scalar_js_1.ScalarType.FLOAT:
348
326
  if (json === "NaN")
349
327
  return Number.NaN;
350
328
  if (json === "Infinity")
@@ -371,15 +349,15 @@ function readScalar(type, json, longType, nullAsZeroValue) {
371
349
  // infinity and -infinity are handled by string representation above, so this is an error
372
350
  break;
373
351
  }
374
- if (type == protobuf_1.ScalarType.FLOAT)
352
+ if (type == scalar_js_1.ScalarType.FLOAT)
375
353
  (0, assert_js_1.assertFloat32)(float);
376
354
  return float;
377
355
  // int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
378
- case protobuf_1.ScalarType.INT32:
379
- case protobuf_1.ScalarType.FIXED32:
380
- case protobuf_1.ScalarType.SFIXED32:
381
- case protobuf_1.ScalarType.SINT32:
382
- 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:
383
361
  let int32;
384
362
  if (typeof json == "number")
385
363
  int32 = json;
@@ -389,34 +367,34 @@ function readScalar(type, json, longType, nullAsZeroValue) {
389
367
  }
390
368
  if (int32 === undefined)
391
369
  break;
392
- 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)
393
371
  (0, assert_js_1.assertUInt32)(int32);
394
372
  else
395
373
  (0, assert_js_1.assertInt32)(int32);
396
374
  return int32;
397
375
  // int64, fixed64, uint64: JSON value will be a decimal string. Either numbers or strings are accepted.
398
- case protobuf_1.ScalarType.INT64:
399
- case protobuf_1.ScalarType.SFIXED64:
400
- 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:
401
379
  if (typeof json != "number" && typeof json != "string")
402
380
  break;
403
- const long = protobuf_1.protoInt64.parse(json);
381
+ const long = proto_int64_js_1.protoInt64.parse(json);
404
382
  // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
405
383
  return longType ? long.toString() : long;
406
- case protobuf_1.ScalarType.FIXED64:
407
- case protobuf_1.ScalarType.UINT64:
384
+ case scalar_js_1.ScalarType.FIXED64:
385
+ case scalar_js_1.ScalarType.UINT64:
408
386
  if (typeof json != "number" && typeof json != "string")
409
387
  break;
410
- const uLong = protobuf_1.protoInt64.uParse(json);
388
+ const uLong = proto_int64_js_1.protoInt64.uParse(json);
411
389
  // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
412
390
  return longType ? uLong.toString() : uLong;
413
391
  // bool:
414
- case protobuf_1.ScalarType.BOOL:
392
+ case scalar_js_1.ScalarType.BOOL:
415
393
  if (typeof json !== "boolean")
416
394
  break;
417
395
  return json;
418
396
  // string:
419
- case protobuf_1.ScalarType.STRING:
397
+ case scalar_js_1.ScalarType.STRING:
420
398
  if (typeof json !== "string") {
421
399
  break;
422
400
  }
@@ -431,17 +409,17 @@ function readScalar(type, json, longType, nullAsZeroValue) {
431
409
  return json;
432
410
  // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
433
411
  // Either standard or URL-safe base64 encoding with/without paddings are accepted.
434
- case protobuf_1.ScalarType.BYTES:
412
+ case scalar_js_1.ScalarType.BYTES:
435
413
  if (json === "")
436
414
  return new Uint8Array(0);
437
415
  if (typeof json !== "string")
438
416
  break;
439
- return protobuf_1.protoBase64.dec(json);
417
+ return proto_base64_js_1.protoBase64.dec(json);
440
418
  }
441
419
  throw new Error();
442
420
  }
443
421
  function readMapKey(type, json) {
444
- if (type === protobuf_1.ScalarType.BOOL) {
422
+ if (type === scalar_js_1.ScalarType.BOOL) {
445
423
  // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
446
424
  switch (json) {
447
425
  case "true":
@@ -452,7 +430,7 @@ function readMapKey(type, json) {
452
430
  break;
453
431
  }
454
432
  }
455
- return readScalar(type, json, protobuf_1.LongType.BIGINT, true).toString();
433
+ return readScalar(type, json, scalar_js_1.LongType.BIGINT, true).toString();
456
434
  }
457
435
  /**
458
436
  * Resets the field, so that isFieldSet() will return false.
@@ -475,9 +453,8 @@ function clearField(field, target) {
475
453
  target[localName] = implicitPresence ? field.T.values[0].no : undefined;
476
454
  break;
477
455
  case "scalar":
478
- target[localName] = implicitPresence
479
- ? (0, scalar_js_1.scalarZeroValue)(field.T, field.L)
480
- : undefined;
456
+ target[localName] =
457
+ implicitPresence ? (0, scalar_js_1.scalarZeroValue)(field.T, field.L) : undefined;
481
458
  break;
482
459
  case "message":
483
460
  target[localName] = undefined;
@@ -521,7 +498,8 @@ function writeField(field, value, options) {
521
498
  case "message":
522
499
  for (const [entryKey, entryValue] of entries) {
523
500
  // JSON standard allows only (double quoted) string as property key
524
- jsonObj[entryKey.toString()] = field.V.T.toJson(entryValue, options);
501
+ const messageType = (0, field_js_1.resolveMessageType)(field.V.T);
502
+ jsonObj[entryKey.toString()] = messageType.toJson(entryValue, options);
525
503
  }
526
504
  break;
527
505
  case "enum":
@@ -532,8 +510,8 @@ function writeField(field, value, options) {
532
510
  }
533
511
  break;
534
512
  }
535
- return options.emitDefaultValues || entries.length > 0
536
- ? jsonObj
513
+ return options.emitDefaultValues || entries.length > 0 ?
514
+ jsonObj
537
515
  : undefined;
538
516
  }
539
517
  if (field.repeated) {
@@ -556,8 +534,8 @@ function writeField(field, value, options) {
556
534
  }
557
535
  break;
558
536
  }
559
- return options.emitDefaultValues || jsonArr.length > 0
560
- ? jsonArr
537
+ return options.emitDefaultValues || jsonArr.length > 0 ?
538
+ jsonArr
561
539
  : undefined;
562
540
  }
563
541
  switch (field.kind) {
@@ -566,24 +544,25 @@ function writeField(field, value, options) {
566
544
  case "enum":
567
545
  return writeEnum(field.T, value, options.enumAsInteger);
568
546
  case "message":
569
- return (0, field_wrapper_js_1.wrapField)(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));
570
549
  }
571
550
  }
572
551
  function writeScalar(type, value) {
573
552
  switch (type) {
574
553
  // int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
575
- case protobuf_1.ScalarType.INT32:
576
- case protobuf_1.ScalarType.SFIXED32:
577
- case protobuf_1.ScalarType.SINT32:
578
- case protobuf_1.ScalarType.FIXED32:
579
- 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:
580
559
  (0, assert_js_1.assert)(typeof value == "number");
581
560
  return value;
582
561
  // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
583
562
  // Either numbers or strings are accepted. Exponent notation is also accepted.
584
- case protobuf_1.ScalarType.FLOAT:
563
+ case scalar_js_1.ScalarType.FLOAT:
585
564
  // assertFloat32(value);
586
- case protobuf_1.ScalarType.DOUBLE: // eslint-disable-line no-fallthrough
565
+ case scalar_js_1.ScalarType.DOUBLE: // eslint-disable-line no-fallthrough
587
566
  (0, assert_js_1.assert)(typeof value == "number");
588
567
  if (Number.isNaN(value))
589
568
  return "NaN";
@@ -593,28 +572,28 @@ function writeScalar(type, value) {
593
572
  return "-Infinity";
594
573
  return value;
595
574
  // string:
596
- case protobuf_1.ScalarType.STRING:
575
+ case scalar_js_1.ScalarType.STRING:
597
576
  (0, assert_js_1.assert)(typeof value == "string");
598
577
  return value;
599
578
  // bool:
600
- case protobuf_1.ScalarType.BOOL:
579
+ case scalar_js_1.ScalarType.BOOL:
601
580
  (0, assert_js_1.assert)(typeof value == "boolean");
602
581
  return value;
603
582
  // JSON value will be a decimal string. Either numbers or strings are accepted.
604
- case protobuf_1.ScalarType.UINT64:
605
- case protobuf_1.ScalarType.FIXED64:
606
- case protobuf_1.ScalarType.INT64:
607
- case protobuf_1.ScalarType.SFIXED64:
608
- 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:
609
588
  (0, assert_js_1.assert)(typeof value == "bigint" ||
610
589
  typeof value == "string" ||
611
590
  typeof value == "number");
612
591
  return value.toString();
613
592
  // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
614
593
  // Either standard or URL-safe base64 encoding with/without paddings are accepted.
615
- case protobuf_1.ScalarType.BYTES:
594
+ case scalar_js_1.ScalarType.BYTES:
616
595
  (0, assert_js_1.assert)(value instanceof Uint8Array);
617
- return protobuf_1.protoBase64.enc(value);
596
+ return proto_base64_js_1.protoBase64.enc(value);
618
597
  }
619
598
  }
620
599
  function writeEnum(type, value, enumAsInteger) {