@bufbuild/protobuf 0.0.8 → 0.1.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 (70) hide show
  1. package/README.md +34 -4
  2. package/dist/cjs/codegen-info.js +60 -0
  3. package/dist/cjs/create-descriptor-set.js +952 -0
  4. package/dist/cjs/create-registry-from-desc.js +270 -0
  5. package/dist/cjs/create-registry.js +75 -0
  6. package/dist/cjs/descriptor-set.js +0 -436
  7. package/dist/cjs/google/protobuf/descriptor_pb.js +2 -2
  8. package/dist/cjs/google/protobuf/empty_pb.js +0 -1
  9. package/dist/cjs/google/protobuf/struct_pb.js +1 -1
  10. package/dist/cjs/google/protobuf/type_pb.js +1 -1
  11. package/dist/cjs/google/varint.js +85 -37
  12. package/dist/cjs/index.js +11 -7
  13. package/dist/cjs/private/enum.js +22 -31
  14. package/dist/cjs/private/field-wrapper.js +30 -1
  15. package/dist/cjs/private/field.js +1 -1
  16. package/dist/cjs/private/json-format-common.js +6 -6
  17. package/dist/cjs/private/names.js +168 -29
  18. package/dist/cjs/proto-int64.js +23 -40
  19. package/dist/cjs/proto2.js +2 -2
  20. package/dist/cjs/proto3.js +2 -2
  21. package/dist/cjs/type-registry.js +0 -87
  22. package/dist/esm/codegen-info.js +57 -0
  23. package/dist/esm/create-descriptor-set.js +947 -0
  24. package/dist/esm/create-registry-from-desc.js +266 -0
  25. package/dist/esm/create-registry.js +71 -0
  26. package/dist/esm/descriptor-set.js +1 -434
  27. package/dist/esm/google/protobuf/descriptor_pb.js +2 -2
  28. package/dist/esm/google/protobuf/empty_pb.js +0 -1
  29. package/dist/esm/google/protobuf/struct_pb.js +1 -1
  30. package/dist/esm/google/protobuf/type_pb.js +1 -1
  31. package/dist/esm/google/varint.js +81 -34
  32. package/dist/esm/index.js +6 -3
  33. package/dist/esm/private/enum.js +22 -31
  34. package/dist/esm/private/field-wrapper.js +28 -0
  35. package/dist/esm/private/field.js +2 -2
  36. package/dist/esm/private/json-format-common.js +6 -6
  37. package/dist/esm/private/names.js +163 -24
  38. package/dist/esm/proto-int64.js +24 -41
  39. package/dist/esm/proto2.js +3 -3
  40. package/dist/esm/proto3.js +3 -3
  41. package/dist/esm/type-registry.js +1 -85
  42. package/dist/types/codegen-info.d.ts +19 -0
  43. package/dist/types/create-descriptor-set.d.ts +16 -0
  44. package/dist/types/create-registry-from-desc.d.ts +15 -0
  45. package/dist/types/create-registry.d.ts +8 -0
  46. package/dist/types/descriptor-set.d.ts +554 -108
  47. package/dist/types/enum.d.ts +9 -5
  48. package/dist/types/google/protobuf/descriptor_pb.d.ts +6 -6
  49. package/dist/types/google/protobuf/empty_pb.d.ts +0 -1
  50. package/dist/types/google/varint.d.ts +21 -9
  51. package/dist/types/index.d.ts +6 -3
  52. package/dist/types/message.d.ts +3 -1
  53. package/dist/types/private/enum.d.ts +4 -8
  54. package/dist/types/private/field-wrapper.d.ts +7 -0
  55. package/dist/types/private/message-type.d.ts +1 -4
  56. package/dist/types/private/names.d.ts +26 -8
  57. package/dist/types/private/proto-runtime.d.ts +2 -2
  58. package/dist/types/private/util.d.ts +1 -6
  59. package/dist/types/proto-int64.d.ts +14 -13
  60. package/dist/types/type-registry.d.ts +0 -38
  61. package/package.json +6 -10
  62. package/dist/cjs/descriptor-registry.js +0 -460
  63. package/dist/esm/descriptor-registry.js +0 -456
  64. package/dist/protobuf.cjs +0 -2
  65. package/dist/protobuf.cjs.map +0 -1
  66. package/dist/protobuf.esm.js +0 -2
  67. package/dist/protobuf.esm.js.map +0 -1
  68. package/dist/protobuf.modern.js +0 -2
  69. package/dist/protobuf.modern.js.map +0 -1
  70. package/dist/types/descriptor-registry.d.ts +0 -43
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.BinaryReader = exports.BinaryWriter = exports.WireType = exports.DescriptorSet = exports.DescriptorRegistry = exports.TypeRegistry = exports.MethodIdempotency = exports.MethodKind = exports.ScalarType = exports.Message = exports.protoBase64 = exports.protoInt64 = exports.proto2 = exports.proto3 = void 0;
30
+ exports.createRegistryFromDescriptors = exports.createRegistry = exports.createDescriptorSet = exports.BinaryReader = exports.BinaryWriter = exports.WireType = exports.MethodIdempotency = exports.MethodKind = exports.ScalarType = exports.Message = exports.codegenInfo = exports.protoBase64 = exports.protoInt64 = 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");
@@ -36,6 +36,8 @@ var proto_int64_js_1 = require("./proto-int64.js");
36
36
  Object.defineProperty(exports, "protoInt64", { enumerable: true, get: function () { return proto_int64_js_1.protoInt64; } });
37
37
  var proto_base64_js_1 = require("./proto-base64.js");
38
38
  Object.defineProperty(exports, "protoBase64", { enumerable: true, get: function () { return proto_base64_js_1.protoBase64; } });
39
+ var codegen_info_js_1 = require("./codegen-info.js");
40
+ Object.defineProperty(exports, "codegenInfo", { enumerable: true, get: function () { return codegen_info_js_1.codegenInfo; } });
39
41
  var message_js_1 = require("./message.js");
40
42
  Object.defineProperty(exports, "Message", { enumerable: true, get: function () { return message_js_1.Message; } });
41
43
  var field_js_1 = require("./field.js");
@@ -43,17 +45,19 @@ Object.defineProperty(exports, "ScalarType", { enumerable: true, get: function (
43
45
  var service_type_js_1 = require("./service-type.js");
44
46
  Object.defineProperty(exports, "MethodKind", { enumerable: true, get: function () { return service_type_js_1.MethodKind; } });
45
47
  Object.defineProperty(exports, "MethodIdempotency", { enumerable: true, get: function () { return service_type_js_1.MethodIdempotency; } });
46
- var type_registry_js_1 = require("./type-registry.js");
47
- Object.defineProperty(exports, "TypeRegistry", { enumerable: true, get: function () { return type_registry_js_1.TypeRegistry; } });
48
- var descriptor_registry_js_1 = require("./descriptor-registry.js");
49
- Object.defineProperty(exports, "DescriptorRegistry", { enumerable: true, get: function () { return descriptor_registry_js_1.DescriptorRegistry; } });
50
- var descriptor_set_js_1 = require("./descriptor-set.js");
51
- Object.defineProperty(exports, "DescriptorSet", { enumerable: true, get: function () { return descriptor_set_js_1.DescriptorSet; } });
52
48
  var binary_encoding_js_1 = require("./binary-encoding.js");
53
49
  Object.defineProperty(exports, "WireType", { enumerable: true, get: function () { return binary_encoding_js_1.WireType; } });
54
50
  Object.defineProperty(exports, "BinaryWriter", { enumerable: true, get: function () { return binary_encoding_js_1.BinaryWriter; } });
55
51
  Object.defineProperty(exports, "BinaryReader", { enumerable: true, get: function () { return binary_encoding_js_1.BinaryReader; } });
56
52
  var json_format_js_1 = require("./json-format.js");
53
+ var descriptor_set_js_1 = require("./descriptor-set.js");
54
+ var create_descriptor_set_js_1 = require("./create-descriptor-set.js");
55
+ Object.defineProperty(exports, "createDescriptorSet", { enumerable: true, get: function () { return create_descriptor_set_js_1.createDescriptorSet; } });
56
+ var type_registry_js_1 = require("./type-registry.js");
57
+ var create_registry_js_1 = require("./create-registry.js");
58
+ Object.defineProperty(exports, "createRegistry", { enumerable: true, get: function () { return create_registry_js_1.createRegistry; } });
59
+ var create_registry_from_desc_js_1 = require("./create-registry-from-desc.js");
60
+ Object.defineProperty(exports, "createRegistryFromDescriptors", { enumerable: true, get: function () { return create_registry_from_desc_js_1.createRegistryFromDescriptors; } });
57
61
  // ideally, we would export these types with sub-path exports:
58
62
  __exportStar(require("./google/protobuf/compiler/plugin_pb.js"), exports);
59
63
  __exportStar(require("./google/protobuf/api_pb.js"), exports);
@@ -31,36 +31,31 @@ exports.getEnumType = getEnumType;
31
31
  /**
32
32
  * Sets reflection information on a generated enum.
33
33
  */
34
- function setEnumType(enumObject, typeName, values
35
- // We do not surface options at this time
36
- // opt?: {
37
- // options?: { readonly [extensionName: string]: JsonValue };
38
- // },
39
- ) {
34
+ function setEnumType(enumObject, typeName, values, opt) {
40
35
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
41
- enumObject[enumTypeSymbol] = makeEnumType(typeName, values);
36
+ enumObject[enumTypeSymbol] = makeEnumType(typeName, values.map((v) => ({
37
+ no: v.no,
38
+ name: v.name,
39
+ localName: enumObject[v.no],
40
+ })), opt);
42
41
  }
43
42
  exports.setEnumType = setEnumType;
44
- // We do not surface options at this time
45
- // export type PartialEnumValue = Omit<EnumValueInfo, 'options'> & Partial<Pick<EnumValueInfo, "options">>;
46
43
  /**
47
44
  * Create a new EnumType with the given values.
48
45
  */
49
- function makeEnumType(typeName, values
50
- // We do not surface options at this time
51
- // opt?: {
52
- // options?: { readonly [extensionName: string]: JsonValue };
53
- // },
54
- ) {
46
+ function makeEnumType(typeName, values,
47
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
48
+ _opt) {
55
49
  const names = Object.create(null);
56
50
  const numbers = Object.create(null);
57
51
  const normalValues = [];
58
52
  for (const value of values) {
59
53
  // We do not surface options at this time
60
54
  // const value: EnumValueInfo = {...v, options: v.options ?? emptyReadonlyObject};
61
- normalValues.push(value);
62
- names[value.name] = value;
63
- numbers[value.no] = value;
55
+ const n = normalizeEnumValue(value);
56
+ normalValues.push(n);
57
+ names[value.name] = n;
58
+ numbers[value.no] = n;
64
59
  }
65
60
  return {
66
61
  typeName,
@@ -78,26 +73,22 @@ function makeEnumType(typeName, values
78
73
  exports.makeEnumType = makeEnumType;
79
74
  /**
80
75
  * Create a new enum object with the given values.
76
+ * Sets reflection information.
81
77
  */
82
78
  function makeEnum(typeName, values, opt) {
83
79
  const enumObject = {};
84
80
  for (const value of values) {
85
- const name = makeEnumValueName(value, opt === null || opt === void 0 ? void 0 : opt.sharedPrefix);
86
- enumObject[name] = value.no;
87
- enumObject[value.no] = name;
81
+ const n = normalizeEnumValue(value);
82
+ enumObject[n.localName] = n.no;
83
+ enumObject[n.no] = n.localName;
88
84
  }
89
- setEnumType(enumObject, typeName, values);
85
+ setEnumType(enumObject, typeName, values, opt);
90
86
  return enumObject;
91
87
  }
92
88
  exports.makeEnum = makeEnum;
93
- // Construct a local name for an enum value.
94
- // This logic matches the Go function with the same name in private/protoplugin/names.go
95
- function makeEnumValueName(value, sharedPrefix) {
96
- if (sharedPrefix === undefined) {
97
- return value.name;
89
+ function normalizeEnumValue(value) {
90
+ if ("localName" in value) {
91
+ return value;
98
92
  }
99
- if (!value.name.startsWith(sharedPrefix)) {
100
- return value.name;
101
- }
102
- return value.name.substring(sharedPrefix.length);
93
+ return Object.assign(Object.assign({}, value), { localName: value.name });
103
94
  }
@@ -13,7 +13,8 @@
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.unwrapField = exports.wrapField = void 0;
16
+ exports.getUnwrappedFieldType = exports.unwrapField = exports.wrapField = void 0;
17
+ const field_js_1 = require("../field.js");
17
18
  /**
18
19
  * Wrap field values whose message type has a wrapper.
19
20
  */
@@ -34,3 +35,31 @@ function unwrapField(type, value) {
34
35
  return type.fieldWrapper ? type.fieldWrapper.unwrapField(value) : value;
35
36
  }
36
37
  exports.unwrapField = unwrapField;
38
+ /**
39
+ * If the given field uses one of the well-known wrapper types, return
40
+ * the base type it wraps.
41
+ */
42
+ function getUnwrappedFieldType(field) {
43
+ if (field.kind !== "message_field") {
44
+ return undefined;
45
+ }
46
+ if (field.repeated) {
47
+ return undefined;
48
+ }
49
+ if (field.oneof != undefined) {
50
+ return undefined;
51
+ }
52
+ return wktWrapperToScalarType[field.message.typeName];
53
+ }
54
+ exports.getUnwrappedFieldType = getUnwrappedFieldType;
55
+ const wktWrapperToScalarType = {
56
+ "google.protobuf.DoubleValue": field_js_1.ScalarType.DOUBLE,
57
+ "google.protobuf.FloatValue": field_js_1.ScalarType.FLOAT,
58
+ "google.protobuf.Int64Value": field_js_1.ScalarType.INT64,
59
+ "google.protobuf.UInt64Value": field_js_1.ScalarType.UINT64,
60
+ "google.protobuf.Int32Value": field_js_1.ScalarType.INT32,
61
+ "google.protobuf.UInt32Value": field_js_1.ScalarType.UINT32,
62
+ "google.protobuf.BoolValue": field_js_1.ScalarType.BOOL,
63
+ "google.protobuf.StringValue": field_js_1.ScalarType.STRING,
64
+ "google.protobuf.BytesValue": field_js_1.ScalarType.BYTES,
65
+ };
@@ -25,7 +25,7 @@ class InternalOneofInfo {
25
25
  this.default = undefined;
26
26
  this.fields = [];
27
27
  this.name = name;
28
- this.localName = (0, names_js_1.makeOneofName)(name);
28
+ this.localName = (0, names_js_1.localOneofName)(name);
29
29
  }
30
30
  addField(field) {
31
31
  (0, assert_js_1.assert)(field.oneof === this, `field ${field.name} not one of ${this.name}`);
@@ -76,12 +76,12 @@ function makeJsonFormatCommon(makeWriteField) {
76
76
  continue;
77
77
  }
78
78
  if (!Array.isArray(jsonValue)) {
79
- throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonValue)}"`);
79
+ throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonValue)}`);
80
80
  }
81
81
  const targetArray = target[localName];
82
82
  for (const jsonItem of jsonValue) {
83
83
  if (jsonItem === null) {
84
- throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonItem)}"`);
84
+ throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonItem)}`);
85
85
  }
86
86
  let val;
87
87
  // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- "map" is invalid for repeated fields
@@ -99,7 +99,7 @@ function makeJsonFormatCommon(makeWriteField) {
99
99
  val = readScalar(field.T, jsonItem);
100
100
  }
101
101
  catch (e) {
102
- let m = `cannot decode field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonItem)}"`;
102
+ let m = `cannot decode field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonItem)}`;
103
103
  if (e instanceof Error && e.message.length > 0) {
104
104
  m += `: ${e.message}`;
105
105
  }
@@ -137,7 +137,7 @@ function makeJsonFormatCommon(makeWriteField) {
137
137
  val = readScalar(field.V.T, jsonMapValue);
138
138
  }
139
139
  catch (e) {
140
- let m = `cannot decode map value for field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonValue)}"`;
140
+ let m = `cannot decode map value for field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonValue)}`;
141
141
  if (e instanceof Error && e.message.length > 0) {
142
142
  m += `: ${e.message}`;
143
143
  }
@@ -155,7 +155,7 @@ function makeJsonFormatCommon(makeWriteField) {
155
155
  : jsonMapKey).toString()] = val;
156
156
  }
157
157
  catch (e) {
158
- let m = `cannot decode map key for field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonValue)}"`;
158
+ let m = `cannot decode map key for field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonValue)}`;
159
159
  if (e instanceof Error && e.message.length > 0) {
160
160
  m += `: ${e.message}`;
161
161
  }
@@ -190,7 +190,7 @@ function makeJsonFormatCommon(makeWriteField) {
190
190
  target[localName] = readScalar(field.T, jsonValue);
191
191
  }
192
192
  catch (e) {
193
- let m = `cannot decode field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonValue)}"`;
193
+ let m = `cannot decode field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonValue)}`;
194
194
  if (e instanceof Error && e.message.length > 0) {
195
195
  m += `: ${e.message}`;
196
196
  }
@@ -13,44 +13,119 @@
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.makeMethodName = exports.makeOneofName = exports.makeFieldName = exports.makeJsonName = void 0;
16
+ exports.findEnumSharedPrefix = exports.fieldJsonName = exports.localOneofName = exports.localFieldName = exports.localName = void 0;
17
17
  /**
18
- * Returns the JSON name for a protobuf field, exactly like protoc does.
18
+ * Returns the name of a protobuf element in generated code.
19
+ *
20
+ * Field names - including oneofs - are converted to lowerCamelCase. For
21
+ * messages, enumerations and services, the package name is stripped from
22
+ * the type name. For nested messages and enumerations, the names are joined
23
+ * with an underscore. For methods, the first character is made lowercase.
19
24
  */
20
- function makeJsonName(protoName) {
21
- return protoCamelCase(protoName);
25
+ function localName(desc) {
26
+ switch (desc.kind) {
27
+ case "enum_field":
28
+ case "message_field":
29
+ case "map_field":
30
+ case "scalar_field":
31
+ return localFieldName(desc.name, desc.oneof !== undefined);
32
+ case "oneof":
33
+ return localOneofName(desc.name);
34
+ case "enum":
35
+ case "message":
36
+ case "service": {
37
+ const pkg = desc.file.proto.package;
38
+ const offset = pkg === undefined ? 0 : pkg.length + 1;
39
+ const name = desc.typeName.substring(offset).replace(/\./g, "_");
40
+ if (reservedIdent[name]) {
41
+ return name + "$";
42
+ }
43
+ return name;
44
+ }
45
+ case "enum_value": {
46
+ const sharedPrefix = desc.parent.sharedPrefix;
47
+ if (sharedPrefix === undefined) {
48
+ return desc.name;
49
+ }
50
+ const name = desc.name.substring(sharedPrefix.length);
51
+ if (reservedObjectProperties[name]) {
52
+ return name + "$";
53
+ }
54
+ return name;
55
+ }
56
+ case "rpc": {
57
+ let name = desc.name;
58
+ if (name.length == 0) {
59
+ return name;
60
+ }
61
+ name = name[0].toLowerCase() + name.substring(1);
62
+ if (reservedObjectProperties[name]) {
63
+ return name + "$";
64
+ }
65
+ return name;
66
+ }
67
+ }
22
68
  }
23
- exports.makeJsonName = makeJsonName;
69
+ exports.localName = localName;
24
70
  /**
25
- * Returns the local name of a field, exactly like protoc-gen-es does.
71
+ * Returns the name of a field in generated code.
26
72
  */
27
- function makeFieldName(protoName, inOneof) {
28
- const n = protoCamelCase(protoName);
73
+ function localFieldName(protoName, inOneof) {
74
+ let name = protoCamelCase(protoName);
29
75
  if (inOneof) {
30
- return n;
76
+ // oneof member names are not properties, but values of the `case` property.
77
+ return name;
31
78
  }
32
- return rProp[n] ? n + escapeChar : n;
79
+ if (reservedObjectProperties[name] || reservedMessageProperties[name]) {
80
+ name = name + "$";
81
+ }
82
+ return name;
33
83
  }
34
- exports.makeFieldName = makeFieldName;
84
+ exports.localFieldName = localFieldName;
35
85
  /**
36
- * Returns the local name of a oneof group, exactly like protoc-gen-es does.
86
+ * Returns the name of a oneof group in generated code.
37
87
  */
38
- function makeOneofName(protoName) {
39
- return makeFieldName(protoName, false);
88
+ function localOneofName(protoName) {
89
+ return localFieldName(protoName, false);
40
90
  }
41
- exports.makeOneofName = makeOneofName;
91
+ exports.localOneofName = localOneofName;
42
92
  /**
43
- * Returns the local name of a rpc, exactly like protoc-gen-es does.
93
+ * Returns the JSON name for a protobuf field, exactly like protoc does.
44
94
  */
45
- function makeMethodName(protoName) {
46
- if (protoName.length == 0) {
47
- return protoName;
95
+ exports.fieldJsonName = protoCamelCase;
96
+ /**
97
+ * Finds a prefix shared by enum values, for example `MY_ENUM_` for
98
+ * `enum MyEnum {MY_ENUM_A=0; MY_ENUM_B=1;}`.
99
+ */
100
+ function findEnumSharedPrefix(enumName, valueNames) {
101
+ const prefix = camelToSnakeCase(enumName) + "_";
102
+ for (const name of valueNames) {
103
+ if (!name.toLowerCase().startsWith(prefix)) {
104
+ return undefined;
105
+ }
106
+ const shortName = name.substring(prefix.length);
107
+ if (shortName.length == 0) {
108
+ return undefined;
109
+ }
110
+ if (/^\d/.test(shortName)) {
111
+ // identifiers must not start with numbers
112
+ return undefined;
113
+ }
48
114
  }
49
- return protoName[0].toLowerCase() + protoName.substring(1);
115
+ return prefix;
116
+ }
117
+ exports.findEnumSharedPrefix = findEnumSharedPrefix;
118
+ /**
119
+ * Converts lowerCamelCase or UpperCamelCase into lower_snake_case.
120
+ * This is used to find shared prefixes in an enum.
121
+ */
122
+ function camelToSnakeCase(camel) {
123
+ return (camel.substring(0, 1) + camel.substring(1).replace(/[A-Z]/g, (c) => "_" + c)).toLowerCase();
50
124
  }
51
- exports.makeMethodName = makeMethodName;
52
- // Converts snake_case to protoCamelCase according to the convention
53
- // used by protoc to convert a field name to a JSON name.
125
+ /**
126
+ * Converts snake_case to protoCamelCase according to the convention
127
+ * used by protoc to convert a field name to a JSON name.
128
+ */
54
129
  function protoCamelCase(snakeCase) {
55
130
  let capNext = false;
56
131
  const b = [];
@@ -84,17 +159,81 @@ function protoCamelCase(snakeCase) {
84
159
  }
85
160
  return b.join("");
86
161
  }
87
- // escapeChar must be appended to a reserved name.
88
- // We choose '$' because it is invalid in proto identifiers.
89
- const escapeChar = "$";
90
- // Names that cannot be used for object properties.
91
- // See buf_es/protoc-gen-es/internal/protoplugin/names.go
92
- const rProp = {
162
+ // Names that cannot be used for identifiers, such as class names,
163
+ // but _can_ be used for object properties.
164
+ const reservedIdent = {
165
+ // ECMAScript 2015 keywords
166
+ break: true,
167
+ case: true,
168
+ catch: true,
169
+ class: true,
170
+ const: true,
171
+ continue: true,
172
+ debugger: true,
173
+ default: true,
174
+ delete: true,
175
+ do: true,
176
+ else: true,
177
+ export: true,
178
+ extends: true,
179
+ false: true,
180
+ finally: true,
181
+ for: true,
182
+ function: true,
183
+ if: true,
184
+ import: true,
185
+ in: true,
186
+ instanceof: true,
187
+ new: true,
188
+ null: true,
189
+ return: true,
190
+ super: true,
191
+ switch: true,
192
+ this: true,
193
+ throw: true,
194
+ true: true,
195
+ try: true,
196
+ typeof: true,
197
+ var: true,
198
+ void: true,
199
+ while: true,
200
+ with: true,
201
+ yield: true,
202
+ // ECMAScript 2015 future reserved keywords
203
+ enum: true,
204
+ implements: true,
205
+ interface: true,
206
+ let: true,
207
+ package: true,
208
+ private: true,
209
+ protected: true,
210
+ public: true,
211
+ static: true,
212
+ // Class name cannot be 'Object' when targeting ES5 with module CommonJS
213
+ Object: true,
214
+ // TypeScript keywords that cannot be used for types (as opposed to variables)
215
+ bigint: true,
216
+ number: true,
217
+ boolean: true,
218
+ string: true,
219
+ object: true,
220
+ // Identifiers reserved for the runtime, so we can generate legible code
221
+ globalThis: true,
222
+ Uint8Array: true,
223
+ Partial: true,
224
+ };
225
+ // Names that cannot be used for object properties because they are reserved
226
+ // by built-in JavaScript properties.
227
+ const reservedObjectProperties = {
93
228
  // names reserved by JavaScript
94
229
  constructor: true,
95
230
  toString: true,
96
231
  toJSON: true,
97
232
  valueOf: true,
233
+ };
234
+ // Names that cannot be used for object properties because they are reserved
235
+ // by the runtime.
236
+ const reservedMessageProperties = {
98
237
  // names reserved by the runtime
99
238
  getType: true,
100
239
  clone: true,
@@ -14,6 +14,7 @@
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.protoInt64 = void 0;
17
+ const assert_js_1 = require("./private/assert.js");
17
18
  const varint_js_1 = require("./google/varint.js");
18
19
  function makeInt64Support() {
19
20
  const dv = new DataView(new ArrayBuffer(8));
@@ -22,7 +23,10 @@ function makeInt64Support() {
22
23
  typeof dv.getBigInt64 === "function" &&
23
24
  typeof dv.getBigUint64 === "function" &&
24
25
  typeof dv.setBigInt64 === "function" &&
25
- typeof dv.setBigUint64 === "function";
26
+ typeof dv.setBigUint64 === "function" &&
27
+ (typeof process != "object" ||
28
+ typeof process.env != "object" ||
29
+ process.env.BUF_BIGINT_DISABLE !== "1");
26
30
  if (ok) {
27
31
  const MIN = BigInt("-9223372036854775808"), MAX = BigInt("9223372036854775807"), UMIN = BigInt("0"), UMAX = BigInt("18446744073709551615");
28
32
  return {
@@ -68,65 +72,44 @@ function makeInt64Support() {
68
72
  },
69
73
  };
70
74
  }
75
+ const assertInt64String = (value) => (0, assert_js_1.assert)(/^-?[0-9]+$/.test(value), `int64 invalid: ${value}`);
76
+ const assertUInt64String = (value) => (0, assert_js_1.assert)(/^[0-9]+$/.test(value), `uint64 invalid: ${value}`);
71
77
  return {
72
78
  zero: "0",
73
79
  supported: false,
74
80
  parse(value) {
75
- if (!/^-?[0-9]+$/.test(value)) {
76
- throw new Error(`int64 invalid: ${value}`);
81
+ if (typeof value != "string") {
82
+ value = value.toString();
77
83
  }
84
+ assertInt64String(value);
78
85
  return value;
79
86
  },
80
87
  uParse(value) {
81
- if (!/^-?[0-9]+$/.test(value)) {
82
- throw new Error(`uint64 invalid: ${value}`);
88
+ if (typeof value != "string") {
89
+ value = value.toString();
83
90
  }
91
+ assertUInt64String(value);
84
92
  return value;
85
93
  },
86
94
  enc(value) {
87
- if (typeof value == "string") {
88
- if (!/^-?[0-9]+$/.test(value)) {
89
- throw new Error(`int64 invalid: ${value}`);
90
- }
91
- }
92
- else {
93
- value = value.toString(10);
95
+ if (typeof value != "string") {
96
+ value = value.toString();
94
97
  }
95
- const [, lo, hi] = (0, varint_js_1.int64fromString)(value);
96
- return { lo, hi };
98
+ assertInt64String(value);
99
+ return (0, varint_js_1.int64FromString)(value);
97
100
  },
98
101
  uEnc(value) {
99
- if (typeof value == "string") {
100
- if (!/^-?[0-9]+$/.test(value)) {
101
- throw new Error(`uint64 invalid: ${value}`);
102
- }
102
+ if (typeof value != "string") {
103
+ value = value.toString();
103
104
  }
104
- else {
105
- value = value.toString(10);
106
- }
107
- const [minus, lo, hi] = (0, varint_js_1.int64fromString)(value);
108
- if (minus) {
109
- throw new Error(`uint64 invalid: ${value}`);
110
- }
111
- return { lo, hi };
105
+ assertUInt64String(value);
106
+ return (0, varint_js_1.int64FromString)(value);
112
107
  },
113
108
  dec(lo, hi) {
114
- const minus = (hi & 0x80000000) !== 0;
115
- if (minus) {
116
- // negate
117
- hi = ~hi;
118
- if (lo) {
119
- lo = ~lo + 1;
120
- }
121
- else {
122
- hi += 1;
123
- }
124
- return ("-" + (0, varint_js_1.int64toString)(lo, hi));
125
- }
126
- return (0, varint_js_1.int64toString)(lo, hi);
109
+ return (0, varint_js_1.int64ToString)(lo, hi);
127
110
  },
128
111
  uDec(lo, hi) {
129
- return (0, varint_js_1.int64toString)(lo, hi);
112
+ return (0, varint_js_1.uInt64ToString)(lo, hi);
130
113
  },
131
114
  };
132
115
  }
@@ -63,8 +63,8 @@ function normalizeFieldInfosProto2(fieldInfos) {
63
63
  ? fieldInfos()
64
64
  : fieldInfos) {
65
65
  const f = field;
66
- f.localName = (0, names_js_1.makeFieldName)(field.name, field.oneof !== undefined);
67
- f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : (0, names_js_1.makeJsonName)(field.name);
66
+ f.localName = (0, names_js_1.localFieldName)(field.name, field.oneof !== undefined);
67
+ f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : (0, names_js_1.fieldJsonName)(field.name);
68
68
  f.repeated = (_b = field.repeated) !== null && _b !== void 0 ? _b : false;
69
69
  // In contrast to proto3, repeated fields are unpacked except when explicitly specified.
70
70
  f.packed = (_c = field.packed) !== null && _c !== void 0 ? _c : false;
@@ -67,8 +67,8 @@ function normalizeFieldInfosProto3(fieldInfos) {
67
67
  ? fieldInfos()
68
68
  : fieldInfos) {
69
69
  const f = field;
70
- f.localName = (0, names_js_1.makeFieldName)(field.name, field.oneof !== undefined);
71
- f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : (0, names_js_1.makeJsonName)(field.name);
70
+ f.localName = (0, names_js_1.localFieldName)(field.name, field.oneof !== undefined);
71
+ f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : (0, names_js_1.fieldJsonName)(field.name);
72
72
  f.repeated = (_b = field.repeated) !== null && _b !== void 0 ? _b : false;
73
73
  // From the proto3 language guide:
74
74
  // > In proto3, repeated fields of scalar numeric types are packed by default.