@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
@@ -7,9 +7,11 @@ export { protoDelimited } from "./proto-delimited.js";
7
7
  export { codegenInfo } from "./codegen-info.js";
8
8
  export { Message } from "./message.js";
9
9
  export type { AnyMessage, PartialMessage, PlainMessage } from "./message.js";
10
- export type { FieldInfo } from "./field.js";
10
+ export { isMessage } from "./is-message.js";
11
+ export type { FieldInfo, OneofInfo } from "./field.js";
11
12
  export type { FieldList } from "./field-list.js";
12
- export { ScalarType, LongType } from "./field.js";
13
+ export { LongType, ScalarType } from "./scalar.js";
14
+ export type { ScalarValue } from "./scalar.js";
13
15
  export type { MessageType } from "./message-type.js";
14
16
  export type { EnumType, EnumValueInfo } from "./enum.js";
15
17
  export type { Extension } from "./extension.js";
package/dist/cjs/index.js CHANGED
@@ -27,7 +27,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
27
27
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
28
28
  };
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.toPlainMessage = exports.createRegistryFromDescriptors = exports.createRegistry = exports.createDescriptorSet = exports.BinaryReader = exports.BinaryWriter = exports.WireType = exports.MethodIdempotency = exports.MethodKind = exports.clearExtension = exports.hasExtension = exports.setExtension = exports.getExtension = exports.LongType = exports.ScalarType = exports.Message = exports.codegenInfo = exports.protoDelimited = exports.protoBase64 = exports.protoInt64 = exports.protoDouble = exports.proto2 = exports.proto3 = void 0;
30
+ exports.toPlainMessage = exports.createRegistryFromDescriptors = exports.createRegistry = exports.createDescriptorSet = exports.BinaryReader = exports.BinaryWriter = exports.WireType = exports.MethodIdempotency = exports.MethodKind = exports.clearExtension = exports.hasExtension = exports.setExtension = exports.getExtension = exports.ScalarType = exports.LongType = exports.isMessage = exports.Message = exports.codegenInfo = exports.protoDelimited = exports.protoBase64 = exports.protoInt64 = exports.protoDouble = exports.proto2 = exports.proto3 = void 0;
31
31
  var proto3_js_1 = require("./proto3.js");
32
32
  Object.defineProperty(exports, "proto3", { enumerable: true, get: function () { return proto3_js_1.proto3; } });
33
33
  var proto2_js_1 = require("./proto2.js");
@@ -44,9 +44,11 @@ var codegen_info_js_1 = require("./codegen-info.js");
44
44
  Object.defineProperty(exports, "codegenInfo", { enumerable: true, get: function () { return codegen_info_js_1.codegenInfo; } });
45
45
  var message_js_1 = require("./message.js");
46
46
  Object.defineProperty(exports, "Message", { enumerable: true, get: function () { return message_js_1.Message; } });
47
- var field_js_1 = require("./field.js");
48
- Object.defineProperty(exports, "ScalarType", { enumerable: true, get: function () { return field_js_1.ScalarType; } });
49
- Object.defineProperty(exports, "LongType", { enumerable: true, get: function () { return field_js_1.LongType; } });
47
+ var is_message_js_1 = require("./is-message.js");
48
+ Object.defineProperty(exports, "isMessage", { enumerable: true, get: function () { return is_message_js_1.isMessage; } });
49
+ var scalar_js_1 = require("./scalar.js");
50
+ Object.defineProperty(exports, "LongType", { enumerable: true, get: function () { return scalar_js_1.LongType; } });
51
+ Object.defineProperty(exports, "ScalarType", { enumerable: true, get: function () { return scalar_js_1.ScalarType; } });
50
52
  var extension_accessor_js_1 = require("./extension-accessor.js");
51
53
  Object.defineProperty(exports, "getExtension", { enumerable: true, get: function () { return extension_accessor_js_1.getExtension; } });
52
54
  Object.defineProperty(exports, "setExtension", { enumerable: true, get: function () { return extension_accessor_js_1.setExtension; } });
@@ -0,0 +1,22 @@
1
+ import type { MessageType } from "./message-type.js";
2
+ import type { AnyMessage } from "./message.js";
3
+ import { Message } from "./message.js";
4
+ /**
5
+ * Check whether the given object is any subtype of Message or is a specific
6
+ * Message by passing the type.
7
+ *
8
+ * Just like `instanceof`, `isMessage` narrows the type. The advantage of
9
+ * `isMessage` is that it compares identity by the message type name, not by
10
+ * class identity. This makes it robust against the dual package hazard and
11
+ * similar situations, where the same message is duplicated.
12
+ *
13
+ * This function is _mostly_ equivalent to the `instanceof` operator. For
14
+ * example, `isMessage(foo, MyMessage)` is the same as `foo instanceof MyMessage`,
15
+ * and `isMessage(foo)` is the same as `foo instanceof Message`. In most cases,
16
+ * `isMessage` should be preferred over `instanceof`.
17
+ *
18
+ * However, due to the fact that `isMessage` does not use class identity, there
19
+ * are subtle differences between this function and `instanceof`. Notably,
20
+ * calling `isMessage` on an explicit type of Message will return false.
21
+ */
22
+ export declare function isMessage<T extends Message<T> = AnyMessage>(arg: unknown, type?: MessageType<T>): arg is T;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.isMessage = void 0;
17
+ const message_js_1 = require("./message.js");
18
+ /**
19
+ * Check whether the given object is any subtype of Message or is a specific
20
+ * Message by passing the type.
21
+ *
22
+ * Just like `instanceof`, `isMessage` narrows the type. The advantage of
23
+ * `isMessage` is that it compares identity by the message type name, not by
24
+ * class identity. This makes it robust against the dual package hazard and
25
+ * similar situations, where the same message is duplicated.
26
+ *
27
+ * This function is _mostly_ equivalent to the `instanceof` operator. For
28
+ * example, `isMessage(foo, MyMessage)` is the same as `foo instanceof MyMessage`,
29
+ * and `isMessage(foo)` is the same as `foo instanceof Message`. In most cases,
30
+ * `isMessage` should be preferred over `instanceof`.
31
+ *
32
+ * However, due to the fact that `isMessage` does not use class identity, there
33
+ * are subtle differences between this function and `instanceof`. Notably,
34
+ * calling `isMessage` on an explicit type of Message will return false.
35
+ */
36
+ function isMessage(arg, type) {
37
+ if (arg === null || typeof arg != "object") {
38
+ return false;
39
+ }
40
+ if (!Object.getOwnPropertyNames(message_js_1.Message.prototype).every((m) => m in arg && typeof arg[m] == "function")) {
41
+ return false;
42
+ }
43
+ const actualType = arg.getType();
44
+ if (actualType === null ||
45
+ typeof actualType != "function" ||
46
+ !("typeName" in actualType) ||
47
+ typeof actualType.typeName != "string") {
48
+ return false;
49
+ }
50
+ return type === undefined ? true : actualType.typeName == type.typeName;
51
+ }
52
+ exports.isMessage = isMessage;
@@ -1,7 +1,6 @@
1
1
  import type { Message } from "./message.js";
2
2
  import type { MessageType } from "./message-type.js";
3
- import type { ScalarType } from "./field.js";
4
- import { LongType } from "./field.js";
3
+ import type { ScalarType, LongType } from "./scalar.js";
5
4
  import type { IExtensionRegistry, IMessageTypeRegistry } from "./type-registry.js";
6
5
  /**
7
6
  * JsonFormat is the contract for serializing messages to and from JSON.
@@ -19,6 +19,7 @@ export interface AnyMessage extends Message<AnyMessage> {
19
19
  export declare class Message<T extends Message<T> = AnyMessage> {
20
20
  /**
21
21
  * Compare with a message of the same type.
22
+ * Note that this function disregards extensions and unknown fields.
22
23
  */
23
24
  equals(other: T | PlainMessage<T> | undefined | null): boolean;
24
25
  /**
@@ -24,6 +24,7 @@ exports.Message = void 0;
24
24
  class Message {
25
25
  /**
26
26
  * Compare with a message of the same type.
27
+ * Note that this function disregards extensions and unknown fields.
27
28
  */
28
29
  equals(other) {
29
30
  return this.getType().runtime.util.equals(this.getType(), this, other);
@@ -0,0 +1,7 @@
1
+ import type { IBinaryWriter } from "../binary-encoding.js";
2
+ import type { BinaryFormat, BinaryWriteOptions } from "../binary-format.js";
3
+ import type { FieldInfo } from "../field.js";
4
+ export declare function makeBinaryFormat(): BinaryFormat;
5
+ export declare function writeMapEntry(writer: IBinaryWriter, options: BinaryWriteOptions, field: FieldInfo & {
6
+ kind: "map";
7
+ }, key: string, value: any): void;
@@ -13,14 +13,15 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.writePacked = exports.writeScalar = exports.writeMessageField = exports.writeMapEntry = exports.makeBinaryFormatCommon = void 0;
16
+ exports.writeMapEntry = exports.makeBinaryFormat = void 0;
17
17
  const binary_encoding_js_1 = require("../binary-encoding.js");
18
- const message_js_1 = require("../message.js");
19
- const field_js_1 = require("../field.js");
20
18
  const field_wrapper_js_1 = require("./field-wrapper.js");
21
19
  const scalars_js_1 = require("./scalars.js");
22
20
  const assert_js_1 = require("./assert.js");
23
- /* eslint-disable @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unnecessary-condition, no-case-declarations, prefer-const */
21
+ const reflect_js_1 = require("./reflect.js");
22
+ const scalar_js_1 = require("../scalar.js");
23
+ const is_message_js_1 = require("../is-message.js");
24
+ /* eslint-disable prefer-const,no-case-declarations,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return */
24
25
  const unknownFieldsSymbol = Symbol("@bufbuild/protobuf/unknown-fields");
25
26
  // Default options for parsing binary data.
26
27
  const readDefaults = {
@@ -38,7 +39,7 @@ function makeReadOptions(options) {
38
39
  function makeWriteOptions(options) {
39
40
  return options ? Object.assign(Object.assign({}, writeDefaults), options) : writeDefaults;
40
41
  }
41
- function makeBinaryFormatCommon() {
42
+ function makeBinaryFormat() {
42
43
  return {
43
44
  makeReadOptions,
44
45
  makeWriteOptions,
@@ -93,9 +94,38 @@ function makeBinaryFormatCommon() {
93
94
  }
94
95
  },
95
96
  readField,
97
+ writeMessage(message, writer, options) {
98
+ const type = message.getType();
99
+ for (const field of type.fields.byNumber()) {
100
+ if (!(0, reflect_js_1.isFieldSet)(field, message)) {
101
+ if (field.req) {
102
+ throw new Error(`cannot encode field ${type.typeName}.${field.name} to binary: required field not set`);
103
+ }
104
+ continue;
105
+ }
106
+ const value = field.oneof
107
+ ? message[field.oneof.localName].value
108
+ : message[field.localName];
109
+ writeField(field, value, writer, options);
110
+ }
111
+ if (options.writeUnknownFields) {
112
+ this.writeUnknownFields(message, writer);
113
+ }
114
+ return writer;
115
+ },
116
+ writeField(field, value, writer, options) {
117
+ // The behavior of our internal function has changed, it does no longer
118
+ // accept `undefined` values for singular scalar and map.
119
+ // For backwards-compatibility, we support the old form that is part of
120
+ // the public API through the interface BinaryFormat.
121
+ if (value === undefined) {
122
+ return undefined;
123
+ }
124
+ writeField(field, value, writer, options);
125
+ },
96
126
  };
97
127
  }
98
- exports.makeBinaryFormatCommon = makeBinaryFormatCommon;
128
+ exports.makeBinaryFormat = makeBinaryFormat;
99
129
  function readField(target, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
100
130
  reader, field, wireType, options) {
101
131
  let { repeated, localName } = field;
@@ -110,7 +140,7 @@ reader, field, wireType, options) {
110
140
  switch (field.kind) {
111
141
  case "scalar":
112
142
  case "enum":
113
- const scalarType = field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
143
+ const scalarType = field.kind == "enum" ? scalar_js_1.ScalarType.INT32 : field.T;
114
144
  let read = readScalar;
115
145
  // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison -- acceptable since it's covered by tests
116
146
  if (field.kind == "scalar" && field.L > 0) {
@@ -119,8 +149,8 @@ reader, field, wireType, options) {
119
149
  if (repeated) {
120
150
  let arr = target[localName]; // safe to assume presence of array, oneof cannot contain repeated values
121
151
  const isPacked = wireType == binary_encoding_js_1.WireType.LengthDelimited &&
122
- scalarType != field_js_1.ScalarType.STRING &&
123
- scalarType != field_js_1.ScalarType.BYTES;
152
+ scalarType != scalar_js_1.ScalarType.STRING &&
153
+ scalarType != scalar_js_1.ScalarType.BYTES;
124
154
  if (isPacked) {
125
155
  let e = reader.uint32() + reader.pos;
126
156
  while (reader.pos < e) {
@@ -142,7 +172,7 @@ reader, field, wireType, options) {
142
172
  target[localName].push(readMessageField(reader, new messageType(), options, field));
143
173
  }
144
174
  else {
145
- if (target[localName] instanceof message_js_1.Message) {
175
+ if ((0, is_message_js_1.isMessage)(target[localName])) {
146
176
  readMessageField(reader, target[localName], options, field);
147
177
  }
148
178
  else {
@@ -165,7 +195,7 @@ reader, field, wireType, options) {
165
195
  function readMessageField(reader, message, options, field) {
166
196
  const format = message.getType().runtime.bin;
167
197
  const delimited = field === null || field === void 0 ? void 0 : field.delimited;
168
- format.readMessage(message, reader, delimited ? field === null || field === void 0 ? void 0 : field.no : reader.uint32(), // eslint-disable-line @typescript-eslint/strict-boolean-expressions
198
+ format.readMessage(message, reader, delimited ? field.no : reader.uint32(), // eslint-disable-line @typescript-eslint/strict-boolean-expressions
169
199
  options, delimited);
170
200
  return message;
171
201
  }
@@ -174,7 +204,7 @@ function readMapEntry(field, reader, options) {
174
204
  const length = reader.uint32(), end = reader.pos + length;
175
205
  let key, val;
176
206
  while (reader.pos < end) {
177
- let [fieldNo] = reader.tag();
207
+ const [fieldNo] = reader.tag();
178
208
  switch (fieldNo) {
179
209
  case 1:
180
210
  key = readScalar(reader, field.K);
@@ -195,11 +225,7 @@ function readMapEntry(field, reader, options) {
195
225
  }
196
226
  }
197
227
  if (key === undefined) {
198
- let keyRaw = (0, scalars_js_1.scalarDefaultValue)(field.K, field_js_1.LongType.BIGINT);
199
- key =
200
- field.K == field_js_1.ScalarType.BOOL
201
- ? keyRaw.toString()
202
- : keyRaw;
228
+ key = (0, scalars_js_1.scalarZeroValue)(field.K, scalar_js_1.LongType.BIGINT);
203
229
  }
204
230
  if (typeof key != "string" && typeof key != "number") {
205
231
  key = key.toString();
@@ -207,10 +233,10 @@ function readMapEntry(field, reader, options) {
207
233
  if (val === undefined) {
208
234
  switch (field.V.kind) {
209
235
  case "scalar":
210
- val = (0, scalars_js_1.scalarDefaultValue)(field.V.T, field_js_1.LongType.BIGINT);
236
+ val = (0, scalars_js_1.scalarZeroValue)(field.V.T, scalar_js_1.LongType.BIGINT);
211
237
  break;
212
238
  case "enum":
213
- val = 0;
239
+ val = field.V.T.values[0].no;
214
240
  break;
215
241
  case "message":
216
242
  val = new field.V.T();
@@ -228,38 +254,79 @@ function readScalarLTString(reader, type) {
228
254
  // Does not use scalarTypeInfo() for better performance.
229
255
  function readScalar(reader, type) {
230
256
  switch (type) {
231
- case field_js_1.ScalarType.STRING:
257
+ case scalar_js_1.ScalarType.STRING:
232
258
  return reader.string();
233
- case field_js_1.ScalarType.BOOL:
259
+ case scalar_js_1.ScalarType.BOOL:
234
260
  return reader.bool();
235
- case field_js_1.ScalarType.DOUBLE:
261
+ case scalar_js_1.ScalarType.DOUBLE:
236
262
  return reader.double();
237
- case field_js_1.ScalarType.FLOAT:
263
+ case scalar_js_1.ScalarType.FLOAT:
238
264
  return reader.float();
239
- case field_js_1.ScalarType.INT32:
265
+ case scalar_js_1.ScalarType.INT32:
240
266
  return reader.int32();
241
- case field_js_1.ScalarType.INT64:
267
+ case scalar_js_1.ScalarType.INT64:
242
268
  return reader.int64();
243
- case field_js_1.ScalarType.UINT64:
269
+ case scalar_js_1.ScalarType.UINT64:
244
270
  return reader.uint64();
245
- case field_js_1.ScalarType.FIXED64:
271
+ case scalar_js_1.ScalarType.FIXED64:
246
272
  return reader.fixed64();
247
- case field_js_1.ScalarType.BYTES:
273
+ case scalar_js_1.ScalarType.BYTES:
248
274
  return reader.bytes();
249
- case field_js_1.ScalarType.FIXED32:
275
+ case scalar_js_1.ScalarType.FIXED32:
250
276
  return reader.fixed32();
251
- case field_js_1.ScalarType.SFIXED32:
277
+ case scalar_js_1.ScalarType.SFIXED32:
252
278
  return reader.sfixed32();
253
- case field_js_1.ScalarType.SFIXED64:
279
+ case scalar_js_1.ScalarType.SFIXED64:
254
280
  return reader.sfixed64();
255
- case field_js_1.ScalarType.SINT64:
281
+ case scalar_js_1.ScalarType.SINT64:
256
282
  return reader.sint64();
257
- case field_js_1.ScalarType.UINT32:
283
+ case scalar_js_1.ScalarType.UINT32:
258
284
  return reader.uint32();
259
- case field_js_1.ScalarType.SINT32:
285
+ case scalar_js_1.ScalarType.SINT32:
260
286
  return reader.sint32();
261
287
  }
262
288
  }
289
+ function writeField(field, value, writer, options) {
290
+ (0, assert_js_1.assert)(value !== undefined);
291
+ const repeated = field.repeated;
292
+ switch (field.kind) {
293
+ case "scalar":
294
+ case "enum":
295
+ let scalarType = field.kind == "enum" ? scalar_js_1.ScalarType.INT32 : field.T;
296
+ if (repeated) {
297
+ (0, assert_js_1.assert)(Array.isArray(value));
298
+ if (field.packed) {
299
+ writePacked(writer, scalarType, field.no, value);
300
+ }
301
+ else {
302
+ for (const item of value) {
303
+ writeScalar(writer, scalarType, field.no, item);
304
+ }
305
+ }
306
+ }
307
+ else {
308
+ writeScalar(writer, scalarType, field.no, value);
309
+ }
310
+ break;
311
+ case "message":
312
+ if (repeated) {
313
+ (0, assert_js_1.assert)(Array.isArray(value));
314
+ for (const item of value) {
315
+ writeMessageField(writer, options, field, item);
316
+ }
317
+ }
318
+ else {
319
+ writeMessageField(writer, options, field, value);
320
+ }
321
+ break;
322
+ case "map":
323
+ (0, assert_js_1.assert)(typeof value == "object" && value != null);
324
+ for (const [key, val] of Object.entries(value)) {
325
+ writeMapEntry(writer, options, field, key, val);
326
+ }
327
+ break;
328
+ }
329
+ }
263
330
  function writeMapEntry(writer, options, field, key, value) {
264
331
  writer.tag(field.no, binary_encoding_js_1.WireType.LengthDelimited);
265
332
  writer.fork();
@@ -268,29 +335,30 @@ function writeMapEntry(writer, options, field, key, value) {
268
335
  let keyValue = key;
269
336
  // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- we deliberately handle just the special cases for map keys
270
337
  switch (field.K) {
271
- case field_js_1.ScalarType.INT32:
272
- case field_js_1.ScalarType.FIXED32:
273
- case field_js_1.ScalarType.UINT32:
274
- case field_js_1.ScalarType.SFIXED32:
275
- case field_js_1.ScalarType.SINT32:
338
+ case scalar_js_1.ScalarType.INT32:
339
+ case scalar_js_1.ScalarType.FIXED32:
340
+ case scalar_js_1.ScalarType.UINT32:
341
+ case scalar_js_1.ScalarType.SFIXED32:
342
+ case scalar_js_1.ScalarType.SINT32:
276
343
  keyValue = Number.parseInt(key);
277
344
  break;
278
- case field_js_1.ScalarType.BOOL:
345
+ case scalar_js_1.ScalarType.BOOL:
279
346
  (0, assert_js_1.assert)(key == "true" || key == "false");
280
347
  keyValue = key == "true";
281
348
  break;
282
349
  }
283
350
  // write key, expecting key field number = 1
284
- writeScalar(writer, field.K, 1, keyValue, true);
351
+ writeScalar(writer, field.K, 1, keyValue);
285
352
  // write value, expecting value field number = 2
286
353
  switch (field.V.kind) {
287
354
  case "scalar":
288
- writeScalar(writer, field.V.T, 2, value, true);
355
+ writeScalar(writer, field.V.T, 2, value);
289
356
  break;
290
357
  case "enum":
291
- writeScalar(writer, field_js_1.ScalarType.INT32, 2, value, true);
358
+ writeScalar(writer, scalar_js_1.ScalarType.INT32, 2, value);
292
359
  break;
293
360
  case "message":
361
+ (0, assert_js_1.assert)(value !== undefined);
294
362
  writer.tag(2, binary_encoding_js_1.WireType.LengthDelimited).bytes(value.toBinary(options));
295
363
  break;
296
364
  }
@@ -301,7 +369,7 @@ exports.writeMapEntry = writeMapEntry;
301
369
  function writeMessageField(writer, options, field, value) {
302
370
  const message = (0, field_wrapper_js_1.wrapField)(field.T, value);
303
371
  // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
304
- if (field === null || field === void 0 ? void 0 : field.delimited)
372
+ if (field.delimited)
305
373
  writer
306
374
  .tag(field.no, binary_encoding_js_1.WireType.StartGroup)
307
375
  .raw(message.toBinary(options))
@@ -311,23 +379,52 @@ function writeMessageField(writer, options, field, value) {
311
379
  .tag(field.no, binary_encoding_js_1.WireType.LengthDelimited)
312
380
  .bytes(message.toBinary(options));
313
381
  }
314
- exports.writeMessageField = writeMessageField;
315
- function writeScalar(writer, type, fieldNo, value, emitIntrinsicDefault) {
316
- let [wireType, method, isIntrinsicDefault] = (0, scalars_js_1.scalarTypeInfo)(type, value);
317
- if (!isIntrinsicDefault || emitIntrinsicDefault) {
318
- writer.tag(fieldNo, wireType)[method](value);
319
- }
382
+ function writeScalar(writer, type, fieldNo, value) {
383
+ (0, assert_js_1.assert)(value !== undefined);
384
+ let [wireType, method] = scalarTypeInfo(type);
385
+ writer.tag(fieldNo, wireType)[method](value);
320
386
  }
321
- exports.writeScalar = writeScalar;
322
387
  function writePacked(writer, type, fieldNo, value) {
323
388
  if (!value.length) {
324
389
  return;
325
390
  }
326
391
  writer.tag(fieldNo, binary_encoding_js_1.WireType.LengthDelimited).fork();
327
- let [, method] = (0, scalars_js_1.scalarTypeInfo)(type);
392
+ let [, method] = scalarTypeInfo(type);
328
393
  for (let i = 0; i < value.length; i++) {
329
394
  writer[method](value[i]);
330
395
  }
331
396
  writer.join();
332
397
  }
333
- exports.writePacked = writePacked;
398
+ /**
399
+ * Get information for writing a scalar value.
400
+ *
401
+ * Returns tuple:
402
+ * [0]: appropriate WireType
403
+ * [1]: name of the appropriate method of IBinaryWriter
404
+ * [2]: whether the given value is a default value for proto3 semantics
405
+ *
406
+ * If argument `value` is omitted, [2] is always false.
407
+ */
408
+ // TODO replace call-sites writeScalar() and writePacked(), then remove
409
+ function scalarTypeInfo(type) {
410
+ let wireType = binary_encoding_js_1.WireType.Varint;
411
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- INT32, UINT32, SINT32 are covered by the defaults
412
+ switch (type) {
413
+ case scalar_js_1.ScalarType.BYTES:
414
+ case scalar_js_1.ScalarType.STRING:
415
+ wireType = binary_encoding_js_1.WireType.LengthDelimited;
416
+ break;
417
+ case scalar_js_1.ScalarType.DOUBLE:
418
+ case scalar_js_1.ScalarType.FIXED64:
419
+ case scalar_js_1.ScalarType.SFIXED64:
420
+ wireType = binary_encoding_js_1.WireType.Bit64;
421
+ break;
422
+ case scalar_js_1.ScalarType.FIXED32:
423
+ case scalar_js_1.ScalarType.SFIXED32:
424
+ case scalar_js_1.ScalarType.FLOAT:
425
+ wireType = binary_encoding_js_1.WireType.Bit32;
426
+ break;
427
+ }
428
+ const method = scalar_js_1.ScalarType[type].toLowerCase();
429
+ return [wireType, method];
430
+ }
@@ -59,7 +59,7 @@ function initExtensionField(ext) {
59
59
  case "enum":
60
60
  return field.T.values[0].no;
61
61
  case "scalar":
62
- return (0, scalars_js_1.scalarDefaultValue)(field.T, field.L);
62
+ return (0, scalars_js_1.scalarZeroValue)(field.T, field.L);
63
63
  case "message":
64
64
  // eslint-disable-next-line no-case-declarations
65
65
  const T = field.T, value = new T();
@@ -21,7 +21,7 @@ const proto_base64_js_1 = require("../proto-base64.js");
21
21
  */
22
22
  function getFeatureSetDefaults(options) {
23
23
  return descriptor_pb_js_1.FeatureSetDefaults.fromBinary(proto_base64_js_1.protoBase64.dec(
24
- /*upstream-inject-feature-defaults-start*/ "ChESDAgBEAIYAiABKAEwAhjmBwoREgwIAhABGAEgAigBMAEY5wcKERIMCAEQARgBIAIoATABGOgHIOYHKOgH" /*upstream-inject-feature-defaults-end*/), options);
24
+ /*upstream-inject-feature-defaults-start*/ "ChESDAgBEAIYAiADKAEwAhjmBwoREgwIAhABGAEgAigBMAEY5wcKERIMCAEQARgBIAIoATABGOgHIOYHKOgH" /*upstream-inject-feature-defaults-end*/), options);
25
25
  }
26
26
  /**
27
27
  * Create an edition feature resolver with the given feature set defaults, or
@@ -0,0 +1,9 @@
1
+ import type { FieldListSource } from "./field-list.js";
2
+ import type { FieldInfo } from "../field.js";
3
+ /**
4
+ * Convert a collection of field info to an array of normalized FieldInfo.
5
+ *
6
+ * The argument `packedByDefault` specifies whether fields that do not specify
7
+ * `packed` should be packed (proto3) or unpacked (proto2).
8
+ */
9
+ export declare function normalizeFieldInfos(fieldInfos: FieldListSource, packedByDefault: boolean): FieldInfo[];
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.normalizeFieldInfos = void 0;
17
+ const field_js_1 = require("./field.js");
18
+ const names_js_1 = require("./names.js");
19
+ const scalar_js_1 = require("../scalar.js");
20
+ /**
21
+ * Convert a collection of field info to an array of normalized FieldInfo.
22
+ *
23
+ * The argument `packedByDefault` specifies whether fields that do not specify
24
+ * `packed` should be packed (proto3) or unpacked (proto2).
25
+ */
26
+ function normalizeFieldInfos(fieldInfos, packedByDefault) {
27
+ var _a, _b, _c, _d, _e, _f;
28
+ const r = [];
29
+ let o;
30
+ for (const field of typeof fieldInfos == "function"
31
+ ? fieldInfos()
32
+ : fieldInfos) {
33
+ const f = field;
34
+ f.localName = (0, names_js_1.localFieldName)(field.name, field.oneof !== undefined);
35
+ f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : (0, names_js_1.fieldJsonName)(field.name);
36
+ f.repeated = (_b = field.repeated) !== null && _b !== void 0 ? _b : false;
37
+ if (field.kind == "scalar") {
38
+ f.L = (_c = field.L) !== null && _c !== void 0 ? _c : scalar_js_1.LongType.BIGINT;
39
+ }
40
+ f.delimited = (_d = field.delimited) !== null && _d !== void 0 ? _d : false;
41
+ f.req = (_e = field.req) !== null && _e !== void 0 ? _e : false;
42
+ f.opt = (_f = field.opt) !== null && _f !== void 0 ? _f : false;
43
+ if (field.packed === undefined) {
44
+ if (packedByDefault) {
45
+ f.packed =
46
+ field.kind == "enum" ||
47
+ (field.kind == "scalar" &&
48
+ field.T != scalar_js_1.ScalarType.BYTES &&
49
+ field.T != scalar_js_1.ScalarType.STRING);
50
+ }
51
+ else {
52
+ f.packed = false;
53
+ }
54
+ }
55
+ // We do not surface options at this time
56
+ // f.options = field.options ?? emptyReadonlyObject;
57
+ if (field.oneof !== undefined) {
58
+ const ooname = typeof field.oneof == "string" ? field.oneof : field.oneof.name;
59
+ if (!o || o.name != ooname) {
60
+ o = new field_js_1.InternalOneofInfo(ooname);
61
+ }
62
+ f.oneof = o;
63
+ o.addField(f);
64
+ }
65
+ r.push(f);
66
+ }
67
+ return r;
68
+ }
69
+ exports.normalizeFieldInfos = normalizeFieldInfos;
@@ -1,7 +1,7 @@
1
1
  import { Message } from "../message.js";
2
2
  import type { MessageType } from "../message-type.js";
3
3
  import type { DescExtension, DescField } from "../descriptor-set.js";
4
- import { ScalarType } from "../field.js";
4
+ import { ScalarType } from "../scalar.js";
5
5
  /**
6
6
  * A field wrapper unwraps a message to a primitive value that is more
7
7
  * ergonomic for use as a message field.
@@ -14,14 +14,14 @@
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.getUnwrappedFieldType = exports.wrapField = void 0;
17
- const message_js_1 = require("../message.js");
18
- const field_js_1 = require("../field.js");
17
+ const scalar_js_1 = require("../scalar.js");
18
+ const is_message_js_1 = require("../is-message.js");
19
19
  /**
20
20
  * Wrap a primitive message field value in its corresponding wrapper
21
21
  * message. This function is idempotent.
22
22
  */
23
23
  function wrapField(type, value) {
24
- if (value instanceof message_js_1.Message || !type.fieldWrapper) {
24
+ if ((0, is_message_js_1.isMessage)(value) || !type.fieldWrapper) {
25
25
  return value;
26
26
  }
27
27
  return type.fieldWrapper.wrapField(value);
@@ -45,13 +45,13 @@ function getUnwrappedFieldType(field) {
45
45
  }
46
46
  exports.getUnwrappedFieldType = getUnwrappedFieldType;
47
47
  const wktWrapperToScalarType = {
48
- "google.protobuf.DoubleValue": field_js_1.ScalarType.DOUBLE,
49
- "google.protobuf.FloatValue": field_js_1.ScalarType.FLOAT,
50
- "google.protobuf.Int64Value": field_js_1.ScalarType.INT64,
51
- "google.protobuf.UInt64Value": field_js_1.ScalarType.UINT64,
52
- "google.protobuf.Int32Value": field_js_1.ScalarType.INT32,
53
- "google.protobuf.UInt32Value": field_js_1.ScalarType.UINT32,
54
- "google.protobuf.BoolValue": field_js_1.ScalarType.BOOL,
55
- "google.protobuf.StringValue": field_js_1.ScalarType.STRING,
56
- "google.protobuf.BytesValue": field_js_1.ScalarType.BYTES,
48
+ "google.protobuf.DoubleValue": scalar_js_1.ScalarType.DOUBLE,
49
+ "google.protobuf.FloatValue": scalar_js_1.ScalarType.FLOAT,
50
+ "google.protobuf.Int64Value": scalar_js_1.ScalarType.INT64,
51
+ "google.protobuf.UInt64Value": scalar_js_1.ScalarType.UINT64,
52
+ "google.protobuf.Int32Value": scalar_js_1.ScalarType.INT32,
53
+ "google.protobuf.UInt32Value": scalar_js_1.ScalarType.UINT32,
54
+ "google.protobuf.BoolValue": scalar_js_1.ScalarType.BOOL,
55
+ "google.protobuf.StringValue": scalar_js_1.ScalarType.STRING,
56
+ "google.protobuf.BytesValue": scalar_js_1.ScalarType.BYTES,
57
57
  };