@bufbuild/protobuf 1.7.2 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/dist/cjs/binary-encoding.d.ts +2 -2
  2. package/dist/cjs/codegen-info.d.ts +6 -1
  3. package/dist/cjs/codegen-info.js +4 -3
  4. package/dist/cjs/create-descriptor-set.js +45 -33
  5. package/dist/cjs/create-registry-from-desc.js +60 -110
  6. package/dist/cjs/descriptor-set.d.ts +13 -9
  7. package/dist/cjs/field.d.ts +25 -51
  8. package/dist/cjs/field.js +0 -66
  9. package/dist/cjs/google/protobuf/compiler/plugin_pb.d.ts +18 -0
  10. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +2 -0
  11. package/dist/cjs/google/protobuf/descriptor_pb.d.ts +33 -33
  12. package/dist/cjs/google/protobuf/descriptor_pb.js +23 -10
  13. package/dist/cjs/google/protobuf/struct_pb.js +1 -1
  14. package/dist/cjs/google/protobuf/type_pb.js +1 -1
  15. package/dist/cjs/google/protobuf/wrappers_pb.js +19 -19
  16. package/dist/cjs/index.d.ts +4 -2
  17. package/dist/cjs/index.js +6 -4
  18. package/dist/cjs/is-message.d.ts +22 -0
  19. package/dist/cjs/is-message.js +52 -0
  20. package/dist/cjs/json-format.d.ts +1 -2
  21. package/dist/cjs/message.d.ts +1 -0
  22. package/dist/cjs/message.js +1 -0
  23. package/dist/cjs/private/binary-format.d.ts +7 -0
  24. package/dist/cjs/private/{binary-format-common.js → binary-format.js} +150 -53
  25. package/dist/cjs/private/extensions.js +1 -1
  26. package/dist/cjs/private/feature-set.js +1 -1
  27. package/dist/cjs/private/field-normalize.d.ts +9 -0
  28. package/dist/cjs/private/field-normalize.js +69 -0
  29. package/dist/cjs/private/field-wrapper.d.ts +1 -1
  30. package/dist/cjs/private/field-wrapper.js +12 -12
  31. package/dist/cjs/private/field.d.ts +1 -0
  32. package/dist/cjs/private/field.js +1 -0
  33. package/dist/cjs/private/json-format.d.ts +2 -0
  34. package/dist/cjs/private/{json-format-common.js → json-format.js} +261 -170
  35. package/dist/cjs/private/names.js +3 -3
  36. package/dist/cjs/private/proto-runtime.d.ts +1 -1
  37. package/dist/cjs/private/proto-runtime.js +8 -4
  38. package/dist/cjs/private/reflect.d.ts +9 -0
  39. package/dist/cjs/private/reflect.js +79 -0
  40. package/dist/cjs/private/reify-wkt.js +11 -11
  41. package/dist/cjs/private/scalars.d.ts +9 -18
  42. package/dist/cjs/private/scalars.js +36 -80
  43. package/dist/cjs/private/text-format.d.ts +1 -1
  44. package/dist/cjs/private/text-format.js +17 -17
  45. package/dist/cjs/private/util-common.js +20 -14
  46. package/dist/cjs/proto-delimited.js +2 -3
  47. package/dist/cjs/proto2.js +29 -68
  48. package/dist/cjs/proto3.js +30 -77
  49. package/dist/cjs/scalar.d.ts +53 -0
  50. package/dist/cjs/scalar.js +81 -0
  51. package/dist/cjs/to-plain-message.js +3 -4
  52. package/dist/esm/binary-encoding.d.ts +2 -2
  53. package/dist/esm/codegen-info.d.ts +6 -1
  54. package/dist/esm/codegen-info.js +6 -5
  55. package/dist/esm/create-descriptor-set.js +22 -10
  56. package/dist/esm/create-registry-from-desc.js +62 -111
  57. package/dist/esm/descriptor-set.d.ts +13 -9
  58. package/dist/esm/descriptor-set.js +1 -1
  59. package/dist/esm/field.d.ts +25 -51
  60. package/dist/esm/field.js +1 -65
  61. package/dist/esm/google/protobuf/compiler/plugin_pb.d.ts +18 -0
  62. package/dist/esm/google/protobuf/compiler/plugin_pb.js +2 -0
  63. package/dist/esm/google/protobuf/descriptor_pb.d.ts +33 -33
  64. package/dist/esm/google/protobuf/descriptor_pb.js +23 -10
  65. package/dist/esm/google/protobuf/struct_pb.js +1 -1
  66. package/dist/esm/google/protobuf/type_pb.js +1 -1
  67. package/dist/esm/google/protobuf/wrappers_pb.js +1 -1
  68. package/dist/esm/index.d.ts +4 -2
  69. package/dist/esm/index.js +2 -1
  70. package/dist/esm/is-message.d.ts +22 -0
  71. package/dist/esm/is-message.js +48 -0
  72. package/dist/esm/json-format.d.ts +1 -2
  73. package/dist/esm/json-format.js +1 -1
  74. package/dist/esm/message.d.ts +1 -0
  75. package/dist/esm/message.js +1 -0
  76. package/dist/esm/private/binary-format.d.ts +7 -0
  77. package/dist/esm/private/{binary-format-common.js → binary-format.js} +126 -25
  78. package/dist/esm/private/extensions.js +2 -2
  79. package/dist/esm/private/feature-set.js +1 -1
  80. package/dist/esm/private/field-normalize.d.ts +9 -0
  81. package/dist/esm/private/field-normalize.js +65 -0
  82. package/dist/esm/private/field-wrapper.d.ts +1 -1
  83. package/dist/esm/private/field-wrapper.js +3 -2
  84. package/dist/esm/private/field.d.ts +1 -0
  85. package/dist/esm/private/field.js +1 -0
  86. package/dist/esm/private/json-format.d.ts +2 -0
  87. package/dist/esm/private/{json-format-common.js → json-format.js} +228 -136
  88. package/dist/esm/private/names.js +3 -3
  89. package/dist/esm/private/proto-runtime.d.ts +1 -1
  90. package/dist/esm/private/proto-runtime.js +8 -4
  91. package/dist/esm/private/reflect.d.ts +9 -0
  92. package/dist/esm/private/reflect.js +74 -0
  93. package/dist/esm/private/reify-wkt.js +1 -1
  94. package/dist/esm/private/scalars.d.ts +9 -18
  95. package/dist/esm/private/scalars.js +15 -59
  96. package/dist/esm/private/text-format.d.ts +1 -1
  97. package/dist/esm/private/text-format.js +1 -1
  98. package/dist/esm/private/util-common.js +13 -6
  99. package/dist/esm/proto-delimited.js +2 -3
  100. package/dist/esm/proto2.js +29 -68
  101. package/dist/esm/proto3.js +31 -78
  102. package/dist/esm/scalar.d.ts +53 -0
  103. package/dist/esm/scalar.js +78 -0
  104. package/dist/esm/to-plain-message.js +3 -2
  105. package/package.json +4 -10
  106. package/dist/cjs/private/binary-format-common.d.ts +0 -13
  107. package/dist/cjs/private/binary-format-proto2.d.ts +0 -2
  108. package/dist/cjs/private/binary-format-proto2.js +0 -91
  109. package/dist/cjs/private/binary-format-proto3.d.ts +0 -2
  110. package/dist/cjs/private/binary-format-proto3.js +0 -83
  111. package/dist/cjs/private/json-format-common.d.ts +0 -9
  112. package/dist/cjs/private/json-format-proto2.d.ts +0 -2
  113. package/dist/cjs/private/json-format-proto2.js +0 -103
  114. package/dist/cjs/private/json-format-proto3.d.ts +0 -2
  115. package/dist/cjs/private/json-format-proto3.js +0 -94
  116. package/dist/esm/private/binary-format-common.d.ts +0 -13
  117. package/dist/esm/private/binary-format-proto2.d.ts +0 -2
  118. package/dist/esm/private/binary-format-proto2.js +0 -87
  119. package/dist/esm/private/binary-format-proto3.d.ts +0 -2
  120. package/dist/esm/private/binary-format-proto3.js +0 -79
  121. package/dist/esm/private/json-format-common.d.ts +0 -9
  122. package/dist/esm/private/json-format-proto2.d.ts +0 -2
  123. package/dist/esm/private/json-format-proto2.js +0 -99
  124. package/dist/esm/private/json-format-proto3.d.ts +0 -2
  125. package/dist/esm/private/json-format-proto3.js +0 -90
  126. package/dist/proxy/index.d.ts +0 -1
  127. package/dist/proxy/index.js +0 -1
@@ -0,0 +1,9 @@
1
+ import type { FieldInfo } from "../field.js";
2
+ /**
3
+ * Returns true if the field is set.
4
+ */
5
+ export declare function isFieldSet(field: FieldInfo, target: Record<string, any>): boolean;
6
+ /**
7
+ * Resets the field, so that isFieldSet() will return false.
8
+ */
9
+ export declare function clearField(field: FieldInfo, target: Record<string, any>): void;
@@ -0,0 +1,74 @@
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { isScalarZeroValue, scalarZeroValue } from "./scalars.js";
15
+ /**
16
+ * Returns true if the field is set.
17
+ */
18
+ export function isFieldSet(field, target) {
19
+ const localName = field.localName;
20
+ if (field.repeated) {
21
+ return target[localName].length > 0;
22
+ }
23
+ if (field.oneof) {
24
+ return target[field.oneof.localName].case === localName; // eslint-disable-line @typescript-eslint/no-unsafe-member-access
25
+ }
26
+ switch (field.kind) {
27
+ case "enum":
28
+ case "scalar":
29
+ if (field.opt || field.req) {
30
+ // explicit presence
31
+ return target[localName] !== undefined;
32
+ }
33
+ // implicit presence
34
+ if (field.kind == "enum") {
35
+ return target[localName] !== field.T.values[0].no;
36
+ }
37
+ return !isScalarZeroValue(field.T, target[localName]);
38
+ case "message":
39
+ return target[localName] !== undefined;
40
+ case "map":
41
+ return Object.keys(target[localName]).length > 0; // eslint-disable-line @typescript-eslint/no-unsafe-argument
42
+ }
43
+ }
44
+ /**
45
+ * Resets the field, so that isFieldSet() will return false.
46
+ */
47
+ export function clearField(field, target) {
48
+ const localName = field.localName;
49
+ const implicitPresence = !field.opt && !field.req;
50
+ if (field.repeated) {
51
+ target[localName] = [];
52
+ }
53
+ else if (field.oneof) {
54
+ target[field.oneof.localName] = { case: undefined };
55
+ }
56
+ else {
57
+ switch (field.kind) {
58
+ case "map":
59
+ target[localName] = {};
60
+ break;
61
+ case "enum":
62
+ target[localName] = implicitPresence ? field.T.values[0].no : undefined;
63
+ break;
64
+ case "scalar":
65
+ target[localName] = implicitPresence
66
+ ? scalarZeroValue(field.T, field.L)
67
+ : undefined;
68
+ break;
69
+ case "message":
70
+ target[localName] = undefined;
71
+ break;
72
+ }
73
+ }
74
+ }
@@ -11,7 +11,7 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- import { ScalarType } from "../field.js";
14
+ import { ScalarType } from "../scalar.js";
15
15
  /**
16
16
  * @deprecated please use reifyWkt from @bufbuild/protoplugin/ecmascript instead
17
17
  *
@@ -1,27 +1,18 @@
1
- import { LongType, ScalarType } from "../field.js";
2
- import type { IBinaryWriter } from "../binary-encoding.js";
3
- import { WireType } from "../binary-encoding.js";
1
+ import { LongType, ScalarType } from "../scalar.js";
2
+ import type { ScalarValue } from "../scalar.js";
4
3
  /**
5
4
  * Returns true if both scalar values are equal.
6
5
  */
7
6
  export declare function scalarEquals(type: ScalarType, a: string | boolean | number | bigint | Uint8Array | undefined, b: string | boolean | number | bigint | Uint8Array | undefined): boolean;
8
7
  /**
9
- * Returns the default value for the given scalar type, following
10
- * proto3 semantics.
8
+ * Returns the zero value for the given scalar type.
11
9
  */
12
- export declare function scalarDefaultValue(type: ScalarType, longType: LongType): any;
10
+ export declare function scalarZeroValue<T extends ScalarType, L extends LongType>(type: T, longType: L): ScalarValue<T, L>;
13
11
  /**
14
- * Get information for writing a scalar value.
12
+ * Returns true for a zero-value. For example, an integer has the zero-value `0`,
13
+ * a boolean is `false`, a string is `""`, and bytes is an empty Uint8Array.
15
14
  *
16
- * Returns tuple:
17
- * [0]: appropriate WireType
18
- * [1]: name of the appropriate method of IBinaryWriter
19
- * [2]: whether the given value is a default value for proto3 semantics
20
- *
21
- * If argument `value` is omitted, [2] is always false.
15
+ * In proto3, zero-values are not written to the wire, unless the field is
16
+ * optional or repeated.
22
17
  */
23
- export declare function scalarTypeInfo(type: ScalarType, value?: any): [
24
- WireType,
25
- Exclude<keyof IBinaryWriter, "tag" | "raw" | "fork" | "join" | "finish">,
26
- boolean
27
- ];
18
+ export declare function isScalarZeroValue(type: ScalarType, value: unknown): boolean;
@@ -11,10 +11,8 @@
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 { LongType, ScalarType } from "../field.js";
15
- import { WireType } from "../binary-encoding.js";
16
14
  import { protoInt64 } from "../proto-int64.js";
17
- /* eslint-disable @typescript-eslint/no-explicit-any */
15
+ import { LongType, ScalarType } from "../scalar.js";
18
16
  /**
19
17
  * Returns true if both scalar values are equal.
20
18
  */
@@ -54,10 +52,9 @@ export function scalarEquals(type, a, b) {
54
52
  return false;
55
53
  }
56
54
  /**
57
- * Returns the default value for the given scalar type, following
58
- * proto3 semantics.
55
+ * Returns the zero value for the given scalar type.
59
56
  */
60
- export function scalarDefaultValue(type, longType) {
57
+ export function scalarZeroValue(type, longType) {
61
58
  switch (type) {
62
59
  case ScalarType.BOOL:
63
60
  return false;
@@ -67,7 +64,7 @@ export function scalarDefaultValue(type, longType) {
67
64
  case ScalarType.SFIXED64:
68
65
  case ScalarType.SINT64:
69
66
  // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison -- acceptable since it's covered by tests
70
- return longType == 0 ? protoInt64.zero : "0";
67
+ return (longType == 0 ? protoInt64.zero : "0");
71
68
  case ScalarType.DOUBLE:
72
69
  case ScalarType.FLOAT:
73
70
  return 0.0;
@@ -82,62 +79,21 @@ export function scalarDefaultValue(type, longType) {
82
79
  }
83
80
  }
84
81
  /**
85
- * Get information for writing a scalar value.
82
+ * Returns true for a zero-value. For example, an integer has the zero-value `0`,
83
+ * a boolean is `false`, a string is `""`, and bytes is an empty Uint8Array.
86
84
  *
87
- * Returns tuple:
88
- * [0]: appropriate WireType
89
- * [1]: name of the appropriate method of IBinaryWriter
90
- * [2]: whether the given value is a default value for proto3 semantics
91
- *
92
- * If argument `value` is omitted, [2] is always false.
85
+ * In proto3, zero-values are not written to the wire, unless the field is
86
+ * optional or repeated.
93
87
  */
94
- export function scalarTypeInfo(type, value) {
95
- const isUndefined = value === undefined;
96
- let wireType = WireType.Varint;
97
- let isIntrinsicDefault = value === 0;
98
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- INT32, UINT32, SINT32 are covered by the defaults
88
+ export function isScalarZeroValue(type, value) {
99
89
  switch (type) {
100
- case ScalarType.STRING:
101
- isIntrinsicDefault = isUndefined || !value.length;
102
- wireType = WireType.LengthDelimited;
103
- break;
104
90
  case ScalarType.BOOL:
105
- isIntrinsicDefault = value === false;
106
- break;
107
- case ScalarType.DOUBLE:
108
- wireType = WireType.Bit64;
109
- break;
110
- case ScalarType.FLOAT:
111
- wireType = WireType.Bit32;
112
- break;
113
- case ScalarType.INT64:
114
- isIntrinsicDefault = isUndefined || value == 0; // Loose comparison matches 0n, 0 and "0"
115
- break;
116
- case ScalarType.UINT64:
117
- isIntrinsicDefault = isUndefined || value == 0; // Loose comparison matches 0n, 0 and "0"
118
- break;
119
- case ScalarType.FIXED64:
120
- isIntrinsicDefault = isUndefined || value == 0; // Loose comparison matches 0n, 0 and "0"
121
- wireType = WireType.Bit64;
122
- break;
91
+ return value === false;
92
+ case ScalarType.STRING:
93
+ return value === "";
123
94
  case ScalarType.BYTES:
124
- isIntrinsicDefault = isUndefined || !value.byteLength;
125
- wireType = WireType.LengthDelimited;
126
- break;
127
- case ScalarType.FIXED32:
128
- wireType = WireType.Bit32;
129
- break;
130
- case ScalarType.SFIXED32:
131
- wireType = WireType.Bit32;
132
- break;
133
- case ScalarType.SFIXED64:
134
- isIntrinsicDefault = isUndefined || value == 0;
135
- wireType = WireType.Bit64;
136
- break;
137
- case ScalarType.SINT64:
138
- isIntrinsicDefault = isUndefined || value == 0;
139
- break;
95
+ return value instanceof Uint8Array && !value.byteLength;
96
+ default:
97
+ return value == 0; // Loose comparison matches 0n, 0 and "0"
140
98
  }
141
- const method = ScalarType[type].toLowerCase();
142
- return [wireType, method, isUndefined || isIntrinsicDefault];
143
99
  }
@@ -1,4 +1,4 @@
1
1
  import type { DescEnum } from "../descriptor-set.js";
2
- import { ScalarType } from "../field.js";
2
+ import { ScalarType } from "../scalar.js";
3
3
  export declare function parseTextFormatEnumValue(descEnum: DescEnum, value: string): number;
4
4
  export declare function parseTextFormatScalarValue(type: ScalarType, value: string): number | boolean | string | bigint | Uint8Array;
@@ -12,8 +12,8 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
  import { assert } from "./assert.js";
15
- import { ScalarType } from "../field.js";
16
15
  import { protoInt64 } from "../proto-int64.js";
16
+ import { ScalarType } from "../scalar.js";
17
17
  export function parseTextFormatEnumValue(descEnum, value) {
18
18
  const enumValue = descEnum.values.find((v) => v.name === value);
19
19
  assert(enumValue, `cannot parse ${descEnum.name} default value: ${value}`);
@@ -13,8 +13,9 @@
13
13
  // limitations under the License.
14
14
  import { setEnumType } from "./enum.js";
15
15
  import { Message } from "../message.js";
16
- import { ScalarType } from "../field.js";
17
16
  import { scalarEquals } from "./scalars.js";
17
+ import { ScalarType } from "../scalar.js";
18
+ import { isMessage } from "../is-message.js";
18
19
  /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-argument,no-case-declarations */
19
20
  export function makeUtilCommon() {
20
21
  return {
@@ -27,6 +28,7 @@ export function makeUtilCommon() {
27
28
  for (const member of type.fields.byMember()) {
28
29
  const localName = member.localName, t = target, s = source;
29
30
  if (s[localName] === undefined) {
31
+ // TODO if source is a Message instance, we should use isFieldSet() here to support future field presence
30
32
  continue;
31
33
  }
32
34
  switch (member.kind) {
@@ -39,7 +41,7 @@ export function makeUtilCommon() {
39
41
  let val = s[localName].value;
40
42
  if (sourceField &&
41
43
  sourceField.kind == "message" &&
42
- !(val instanceof sourceField.T)) {
44
+ !isMessage(val, sourceField.T)) {
43
45
  val = new sourceField.T(val);
44
46
  }
45
47
  else if (sourceField &&
@@ -89,9 +91,9 @@ export function makeUtilCommon() {
89
91
  case "message":
90
92
  const mt = member.T;
91
93
  if (member.repeated) {
92
- t[localName] = s[localName].map((val) => val instanceof mt ? val : new mt(val));
94
+ t[localName] = s[localName].map((val) => isMessage(val, mt) ? val : new mt(val));
93
95
  }
94
- else if (s[localName] !== undefined) {
96
+ else {
95
97
  const val = s[localName];
96
98
  if (mt.fieldWrapper) {
97
99
  if (
@@ -104,13 +106,14 @@ export function makeUtilCommon() {
104
106
  }
105
107
  }
106
108
  else {
107
- t[localName] = val instanceof mt ? val : new mt(val);
109
+ t[localName] = isMessage(val, mt) ? val : new mt(val);
108
110
  }
109
111
  }
110
112
  break;
111
113
  }
112
114
  }
113
115
  },
116
+ // TODO use isFieldSet() here to support future field presence
114
117
  equals(type, a, b) {
115
118
  if (a === b) {
116
119
  return true;
@@ -177,6 +180,7 @@ export function makeUtilCommon() {
177
180
  }
178
181
  });
179
182
  },
183
+ // TODO use isFieldSet() here to support future field presence
180
184
  clone(message) {
181
185
  const type = message.getType(), target = new type(), any = target;
182
186
  for (const member of type.fields.byMember()) {
@@ -202,6 +206,9 @@ export function makeUtilCommon() {
202
206
  }
203
207
  any[member.localName] = copy;
204
208
  }
209
+ for (const uf of type.runtime.bin.listUnknownFields(message)) {
210
+ type.runtime.bin.onUnknownField(any, uf.no, uf.wireType, uf.data);
211
+ }
205
212
  return target;
206
213
  },
207
214
  };
@@ -211,7 +218,7 @@ function cloneSingularField(value) {
211
218
  if (value === undefined) {
212
219
  return value;
213
220
  }
214
- if (value instanceof Message) {
221
+ if (isMessage(value)) {
215
222
  return value.clone();
216
223
  }
217
224
  if (value instanceof Uint8Array) {
@@ -31,7 +31,6 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
31
31
  function reject(value) { resume("throw", value); }
32
32
  function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
33
33
  };
34
- import { makeBinaryFormatCommon } from "./private/binary-format-common.js";
35
34
  import { BinaryReader } from "./binary-encoding.js";
36
35
  /**
37
36
  * protoDelimited provides functions to serialize and parse size-delimited
@@ -48,14 +47,14 @@ export const protoDelimited = {
48
47
  * Serialize a message, prefixing it with its size.
49
48
  */
50
49
  enc(message, options) {
51
- const opt = makeBinaryFormatCommon().makeWriteOptions(options);
50
+ const opt = message.getType().runtime.bin.makeWriteOptions(options);
52
51
  return opt.writerFactory().bytes(message.toBinary(opt)).finish();
53
52
  },
54
53
  /**
55
54
  * Parse a size-delimited message, ignoring extra bytes.
56
55
  */
57
56
  dec(type, bytes, options) {
58
- const opt = makeBinaryFormatCommon().makeReadOptions(options);
57
+ const opt = type.runtime.bin.makeReadOptions(options);
59
58
  return type.fromBinary(opt.readerFactory(bytes).bytes(), opt);
60
59
  },
61
60
  /**
@@ -12,78 +12,39 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
  import { makeProtoRuntime } from "./private/proto-runtime.js";
15
- import { makeBinaryFormatProto2 } from "./private/binary-format-proto2.js";
16
- import { makeUtilCommon } from "./private/util-common.js";
17
15
  import { InternalFieldList } from "./private/field-list.js";
18
- import { InternalOneofInfo } from "./private/field.js";
19
- import { localFieldName, fieldJsonName } from "./private/names.js";
20
- import { makeJsonFormatProto2 } from "./private/json-format-proto2.js";
21
- import { LongType } from "./field.js";
16
+ import { normalizeFieldInfos } from "./private/field-normalize.js";
22
17
  /**
23
18
  * Provides functionality for messages defined with the proto2 syntax.
24
19
  */
25
- export const proto2 = makeProtoRuntime("proto2", makeJsonFormatProto2(), makeBinaryFormatProto2(), Object.assign(Object.assign({}, makeUtilCommon()), { newFieldList(fields) {
26
- return new InternalFieldList(fields, normalizeFieldInfosProto2);
27
- },
28
- initFields(target) {
29
- for (const member of target.getType().fields.byMember()) {
30
- const name = member.localName, t = target;
31
- if (member.repeated) {
32
- t[name] = [];
33
- continue;
34
- }
35
- switch (member.kind) {
36
- case "oneof":
37
- t[name] = { case: undefined };
38
- break;
39
- case "map":
40
- t[name] = {};
41
- break;
42
- case "scalar":
43
- case "enum":
44
- case "message":
45
- // In contrast to proto3, enum and scalar fields have no intrinsic default value,
46
- // only an optional explicit default value.
47
- // Unlike proto3 intrinsic default values, proto2 explicit default values are not
48
- // set on construction, because they are not omitted on the wire. If we did set
49
- // default values on construction, a deserialize-serialize round-trip would add
50
- // fields to a message.
51
- break;
52
- }
20
+ export const proto2 = makeProtoRuntime("proto2", (fields) => {
21
+ return new InternalFieldList(fields, (source) => normalizeFieldInfos(source, false));
22
+ },
23
+ // TODO merge with proto3 and initExtensionField, also see initPartial, equals, clone
24
+ (target) => {
25
+ for (const member of target.getType().fields.byMember()) {
26
+ const name = member.localName, t = target;
27
+ if (member.repeated) {
28
+ t[name] = [];
29
+ continue;
53
30
  }
54
- } }));
55
- /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
56
- function normalizeFieldInfosProto2(fieldInfos) {
57
- var _a, _b, _c, _d, _e;
58
- const r = [];
59
- let o;
60
- for (const field of typeof fieldInfos == "function"
61
- ? fieldInfos()
62
- : fieldInfos) {
63
- const f = field;
64
- f.localName = localFieldName(field.name, field.oneof !== undefined);
65
- f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : fieldJsonName(field.name);
66
- f.repeated = (_b = field.repeated) !== null && _b !== void 0 ? _b : false;
67
- if (field.kind == "scalar") {
68
- f.L = (_c = field.L) !== null && _c !== void 0 ? _c : LongType.BIGINT;
31
+ switch (member.kind) {
32
+ case "oneof":
33
+ t[name] = { case: undefined };
34
+ break;
35
+ case "map":
36
+ t[name] = {};
37
+ break;
38
+ case "scalar":
39
+ case "enum":
40
+ case "message":
41
+ // In contrast to proto3, enum and scalar fields have no intrinsic default value,
42
+ // only an optional explicit default value.
43
+ // Unlike proto3 intrinsic default values, proto2 explicit default values are not
44
+ // set on construction, because they are not omitted on the wire. If we did set
45
+ // default values on construction, a deserialize-serialize round-trip would add
46
+ // fields to a message.
47
+ break;
69
48
  }
70
- // We do not surface options at this time
71
- // f.options = field.options ?? emptyReadonlyObject;
72
- if (field.oneof !== undefined) {
73
- const ooname = typeof field.oneof == "string" ? field.oneof : field.oneof.name;
74
- if (!o || o.name != ooname) {
75
- o = new InternalOneofInfo(ooname);
76
- }
77
- f.oneof = o;
78
- o.addField(f);
79
- }
80
- // proto2 specific:
81
- if (field.kind == "message") {
82
- f.delimited = (_d = field.delimited) !== null && _d !== void 0 ? _d : false;
83
- }
84
- // In contrast to proto3, repeated fields are unpacked except when explicitly specified.
85
- f.packed = (_e = field.packed) !== null && _e !== void 0 ? _e : false;
86
- r.push(f);
87
49
  }
88
- return r;
89
- }
50
+ });
@@ -12,89 +12,42 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
  import { makeProtoRuntime } from "./private/proto-runtime.js";
15
- import { makeBinaryFormatProto3 } from "./private/binary-format-proto3.js";
16
- import { makeJsonFormatProto3 } from "./private/json-format-proto3.js";
17
- import { makeUtilCommon } from "./private/util-common.js";
18
15
  import { InternalFieldList } from "./private/field-list.js";
19
- import { scalarDefaultValue } from "./private/scalars.js";
20
- import { LongType, ScalarType } from "./field.js";
21
- import { InternalOneofInfo } from "./private/field.js";
22
- import { localFieldName, fieldJsonName } from "./private/names.js";
16
+ import { scalarZeroValue } from "./private/scalars.js";
17
+ import { normalizeFieldInfos } from "./private/field-normalize.js";
23
18
  /**
24
19
  * Provides functionality for messages defined with the proto3 syntax.
25
20
  */
26
- export const proto3 = makeProtoRuntime("proto3", makeJsonFormatProto3(), makeBinaryFormatProto3(), Object.assign(Object.assign({}, makeUtilCommon()), { newFieldList(fields) {
27
- return new InternalFieldList(fields, normalizeFieldInfosProto3);
28
- },
29
- initFields(target) {
30
- for (const member of target.getType().fields.byMember()) {
31
- if (member.opt) {
32
- continue;
33
- }
34
- const name = member.localName, t = target;
35
- if (member.repeated) {
36
- t[name] = [];
37
- continue;
38
- }
39
- switch (member.kind) {
40
- case "oneof":
41
- t[name] = { case: undefined };
42
- break;
43
- case "enum":
44
- t[name] = 0;
45
- break;
46
- case "map":
47
- t[name] = {};
48
- break;
49
- case "scalar":
50
- t[name] = scalarDefaultValue(member.T, member.L); // eslint-disable-line @typescript-eslint/no-unsafe-assignment
51
- break;
52
- case "message":
53
- // message fields are always optional in proto3
54
- break;
55
- }
21
+ export const proto3 = makeProtoRuntime("proto3", (fields) => {
22
+ return new InternalFieldList(fields, (source) => normalizeFieldInfos(source, true));
23
+ },
24
+ // TODO merge with proto2 and initExtensionField, also see initPartial, equals, clone
25
+ (target) => {
26
+ for (const member of target.getType().fields.byMember()) {
27
+ if (member.opt) {
28
+ continue;
56
29
  }
57
- } }));
58
- /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
59
- function normalizeFieldInfosProto3(fieldInfos) {
60
- var _a, _b, _c, _d;
61
- const r = [];
62
- let o;
63
- for (const field of typeof fieldInfos == "function"
64
- ? fieldInfos()
65
- : fieldInfos) {
66
- const f = field;
67
- f.localName = localFieldName(field.name, field.oneof !== undefined);
68
- f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : fieldJsonName(field.name);
69
- f.repeated = (_b = field.repeated) !== null && _b !== void 0 ? _b : false;
70
- if (field.kind == "scalar") {
71
- f.L = (_c = field.L) !== null && _c !== void 0 ? _c : LongType.BIGINT;
30
+ const name = member.localName, t = target;
31
+ if (member.repeated) {
32
+ t[name] = [];
33
+ continue;
72
34
  }
73
- // We do not surface options at this time
74
- // f.options = field.options ?? emptyReadonlyObject;
75
- if (field.oneof !== undefined) {
76
- const ooname = typeof field.oneof == "string" ? field.oneof : field.oneof.name;
77
- if (!o || o.name != ooname) {
78
- o = new InternalOneofInfo(ooname);
79
- }
80
- f.oneof = o;
81
- o.addField(f);
35
+ switch (member.kind) {
36
+ case "oneof":
37
+ t[name] = { case: undefined };
38
+ break;
39
+ case "enum":
40
+ t[name] = 0;
41
+ break;
42
+ case "map":
43
+ t[name] = {};
44
+ break;
45
+ case "scalar":
46
+ t[name] = scalarZeroValue(member.T, member.L);
47
+ break;
48
+ case "message":
49
+ // message fields are always optional in proto3
50
+ break;
82
51
  }
83
- // proto3 specific:
84
- if (field.kind == "message") {
85
- f.delimited = false;
86
- }
87
- // From the proto3 language guide:
88
- // > In proto3, repeated fields of scalar numeric types are packed by default.
89
- // This information is incomplete - according to the conformance tests, BOOL
90
- // and ENUM are packed by default as well. This means only STRING and BYTES
91
- // are not packed by default, which makes sense because they are length-delimited.
92
- f.packed =
93
- (_d = field.packed) !== null && _d !== void 0 ? _d : (field.kind == "enum" ||
94
- (field.kind == "scalar" &&
95
- field.T != ScalarType.BYTES &&
96
- field.T != ScalarType.STRING));
97
- r.push(f);
98
52
  }
99
- return r;
100
- }
53
+ });
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Scalar value types. This is a subset of field types declared by protobuf
3
+ * enum google.protobuf.FieldDescriptorProto.Type The types GROUP and MESSAGE
4
+ * are omitted, but the numerical values are identical.
5
+ */
6
+ export declare enum ScalarType {
7
+ DOUBLE = 1,
8
+ FLOAT = 2,
9
+ INT64 = 3,
10
+ UINT64 = 4,
11
+ INT32 = 5,
12
+ FIXED64 = 6,
13
+ FIXED32 = 7,
14
+ BOOL = 8,
15
+ STRING = 9,
16
+ BYTES = 12,
17
+ UINT32 = 13,
18
+ SFIXED32 = 15,
19
+ SFIXED64 = 16,
20
+ SINT32 = 17,// Uses ZigZag encoding.
21
+ SINT64 = 18
22
+ }
23
+ /**
24
+ * JavaScript representation of fields with 64 bit integral types (int64, uint64,
25
+ * sint64, fixed64, sfixed64).
26
+ *
27
+ * This is a subset of google.protobuf.FieldOptions.JSType, which defines JS_NORMAL,
28
+ * JS_STRING, and JS_NUMBER. Protobuf-ES uses BigInt by default, but will use
29
+ * String if `[jstype = JS_STRING]` is specified.
30
+ *
31
+ * ```protobuf
32
+ * uint64 field_a = 1; // BigInt
33
+ * uint64 field_b = 2 [jstype = JS_NORMAL]; // BigInt
34
+ * uint64 field_b = 2 [jstype = JS_NUMBER]; // BigInt
35
+ * uint64 field_b = 2 [jstype = JS_STRING]; // String
36
+ * ```
37
+ */
38
+ export declare enum LongType {
39
+ /**
40
+ * Use JavaScript BigInt.
41
+ */
42
+ BIGINT = 0,
43
+ /**
44
+ * Use JavaScript String.
45
+ *
46
+ * Field option `[jstype = JS_STRING]`.
47
+ */
48
+ STRING = 1
49
+ }
50
+ /**
51
+ * ScalarValue maps from a scalar field type to a TypeScript value type.
52
+ */
53
+ export type ScalarValue<T = ScalarType, L extends LongType = LongType.STRING | LongType.BIGINT> = T extends ScalarType.STRING ? string : T extends ScalarType.INT32 ? number : T extends ScalarType.UINT32 ? number : T extends ScalarType.UINT32 ? number : T extends ScalarType.SINT32 ? number : T extends ScalarType.FIXED32 ? number : T extends ScalarType.SFIXED32 ? number : T extends ScalarType.FLOAT ? number : T extends ScalarType.DOUBLE ? number : T extends ScalarType.INT64 ? (L extends LongType.STRING ? string : bigint) : T extends ScalarType.SINT64 ? (L extends LongType.STRING ? string : bigint) : T extends ScalarType.SFIXED64 ? (L extends LongType.STRING ? string : bigint) : T extends ScalarType.UINT64 ? (L extends LongType.STRING ? string : bigint) : T extends ScalarType.FIXED64 ? (L extends LongType.STRING ? string : bigint) : T extends ScalarType.BOOL ? boolean : T extends ScalarType.BYTES ? Uint8Array : never;