@bufbuild/protobuf 0.0.6 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/README.md +34 -4
  2. package/dist/cjs/binary-encoding.js +2 -2
  3. package/dist/cjs/codegen-info.js +60 -0
  4. package/dist/cjs/create-descriptor-set.js +952 -0
  5. package/dist/cjs/create-registry-from-desc.js +376 -0
  6. package/dist/cjs/create-registry.js +75 -0
  7. package/dist/cjs/descriptor-set.js +0 -458
  8. package/dist/cjs/google/protobuf/any_pb.js +4 -2
  9. package/dist/cjs/google/protobuf/duration_pb.js +1 -1
  10. package/dist/cjs/google/protobuf/struct_pb.js +2 -2
  11. package/dist/cjs/google/protobuf/type_pb.js +1 -1
  12. package/dist/cjs/index.js +17 -7
  13. package/dist/cjs/{descriptor-registry.js → legacy-descriptor-registry.js} +35 -69
  14. package/dist/cjs/legacy-descriptor-set.js +453 -0
  15. package/dist/cjs/legacy-type-registry.js +104 -0
  16. package/dist/cjs/message.js +2 -1
  17. package/dist/cjs/private/binary-format-common.js +4 -3
  18. package/dist/cjs/private/binary-format-proto2.js +3 -6
  19. package/dist/cjs/private/binary-format-proto3.js +2 -5
  20. package/dist/cjs/private/enum.js +22 -31
  21. package/dist/cjs/private/field-wrapper.js +30 -1
  22. package/dist/cjs/private/field.js +1 -1
  23. package/dist/cjs/private/json-format-common.js +6 -5
  24. package/dist/cjs/private/message-type.js +2 -1
  25. package/dist/cjs/private/names.js +168 -29
  26. package/dist/cjs/proto-int64.js +1 -1
  27. package/dist/cjs/proto2.js +7 -9
  28. package/dist/cjs/proto3.js +10 -13
  29. package/dist/cjs/type-registry.js +0 -87
  30. package/dist/esm/binary-encoding.js +2 -2
  31. package/dist/esm/codegen-info.js +57 -0
  32. package/dist/esm/create-descriptor-set.js +947 -0
  33. package/dist/esm/create-registry-from-desc.js +371 -0
  34. package/dist/esm/create-registry.js +71 -0
  35. package/dist/esm/descriptor-set.js +1 -456
  36. package/dist/esm/google/protobuf/any_pb.js +4 -2
  37. package/dist/esm/google/protobuf/duration_pb.js +1 -1
  38. package/dist/esm/google/protobuf/struct_pb.js +2 -2
  39. package/dist/esm/google/protobuf/type_pb.js +1 -1
  40. package/dist/esm/index.js +9 -3
  41. package/dist/esm/{descriptor-registry.js → legacy-descriptor-registry.js} +33 -67
  42. package/dist/esm/legacy-descriptor-set.js +449 -0
  43. package/dist/esm/legacy-type-registry.js +100 -0
  44. package/dist/esm/message.js +2 -1
  45. package/dist/esm/private/binary-format-common.js +4 -3
  46. package/dist/esm/private/binary-format-proto2.js +3 -6
  47. package/dist/esm/private/binary-format-proto3.js +2 -5
  48. package/dist/esm/private/enum.js +22 -31
  49. package/dist/esm/private/field-wrapper.js +28 -0
  50. package/dist/esm/private/field.js +2 -2
  51. package/dist/esm/private/json-format-common.js +6 -5
  52. package/dist/esm/private/message-type.js +2 -1
  53. package/dist/esm/private/names.js +163 -24
  54. package/dist/esm/proto-int64.js +1 -1
  55. package/dist/esm/proto2.js +8 -10
  56. package/dist/esm/proto3.js +11 -14
  57. package/dist/esm/type-registry.js +1 -85
  58. package/dist/types/codegen-info.d.ts +19 -0
  59. package/dist/types/create-descriptor-set.d.ts +16 -0
  60. package/dist/types/create-registry-from-desc.d.ts +49 -0
  61. package/dist/types/create-registry.d.ts +8 -0
  62. package/dist/types/descriptor-set.d.ts +554 -108
  63. package/dist/types/enum.d.ts +9 -5
  64. package/dist/types/index.d.ts +9 -3
  65. package/dist/types/{descriptor-registry.d.ts → legacy-descriptor-registry.d.ts} +8 -6
  66. package/dist/types/legacy-descriptor-set.d.ts +152 -0
  67. package/dist/types/legacy-type-registry.d.ts +44 -0
  68. package/dist/types/private/enum.d.ts +4 -8
  69. package/dist/types/private/field-wrapper.d.ts +7 -0
  70. package/dist/types/private/message-type.d.ts +1 -4
  71. package/dist/types/private/names.d.ts +26 -8
  72. package/dist/types/private/proto-runtime.d.ts +2 -2
  73. package/dist/types/private/util.d.ts +1 -6
  74. package/dist/types/type-registry.d.ts +0 -38
  75. package/package.json +4 -3
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ // Copyright 2021-2022 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.TypeRegistry = void 0;
17
+ /**
18
+ * @deprecated use createRegistry() instead
19
+ *
20
+ * TypeRegistry is a simple registry for all message, enum, or service types.
21
+ */
22
+ class TypeRegistry {
23
+ constructor() {
24
+ this.messages = {};
25
+ this.enums = {};
26
+ this.services = {};
27
+ }
28
+ /**
29
+ * Find a message type by its protobuf type name.
30
+ */
31
+ findMessage(typeName) {
32
+ return this.messages[typeName];
33
+ }
34
+ /**
35
+ * Find an enum type by its protobuf type name.
36
+ */
37
+ findEnum(typeName) {
38
+ return this.enums[typeName];
39
+ }
40
+ /**
41
+ * Find a service type by its protobuf type name.
42
+ */
43
+ findService(typeName) {
44
+ return this.services[typeName];
45
+ }
46
+ /**
47
+ * Create a new TypeRegistry from the given types.
48
+ */
49
+ static from(...types) {
50
+ const registry = new TypeRegistry();
51
+ for (const type of types) {
52
+ registry.add(type);
53
+ }
54
+ return registry;
55
+ }
56
+ /**
57
+ * @deprecated use TypeRegistry.from()
58
+ */
59
+ static fromIterable(types) {
60
+ return TypeRegistry.from(...types);
61
+ }
62
+ /**
63
+ * @deprecated use TypeRegistry.from()
64
+ */
65
+ static fromTypes(...types) {
66
+ return TypeRegistry.from(...types);
67
+ }
68
+ /**
69
+ * Add a type to the registry. For messages, the types used in message
70
+ * fields are added recursively. For services, the message types used
71
+ * for requests and responses are added recursively.
72
+ */
73
+ add(type) {
74
+ if ("fields" in type) {
75
+ if (!this.findMessage(type.typeName)) {
76
+ this.messages[type.typeName] = type;
77
+ for (const field of type.fields.list()) {
78
+ if (field.kind == "message") {
79
+ this.add(field.T);
80
+ }
81
+ else if (field.kind == "map" && field.V.kind == "message") {
82
+ this.add(field.V.T);
83
+ }
84
+ else if (field.kind == "enum") {
85
+ this.add(field.T);
86
+ }
87
+ }
88
+ }
89
+ }
90
+ else if ("methods" in type) {
91
+ if (!this.findService(type.typeName)) {
92
+ this.services[type.typeName] = type;
93
+ for (const method of Object.values(type.methods)) {
94
+ this.add(method.I);
95
+ this.add(method.O);
96
+ }
97
+ }
98
+ }
99
+ else {
100
+ this.enums[type.typeName] = type;
101
+ }
102
+ }
103
+ }
104
+ exports.TypeRegistry = TypeRegistry;
@@ -84,8 +84,9 @@ class Message {
84
84
  * Serialize the message to a JSON string.
85
85
  */
86
86
  toJsonString(options) {
87
+ var _a;
87
88
  const value = this.toJson(options);
88
- return JSON.stringify(value, null, options?.prettySpaces ?? 0);
89
+ return JSON.stringify(value, null, (_a = options === null || options === void 0 ? void 0 : options.prettySpaces) !== null && _a !== void 0 ? _a : 0);
89
90
  }
90
91
  /**
91
92
  * Retrieve the MessageType of this message - a singleton that represents
@@ -32,17 +32,18 @@ const writeDefaults = {
32
32
  writerFactory: () => new binary_encoding_js_1.BinaryWriter(),
33
33
  };
34
34
  function makeReadOptions(options) {
35
- return options ? { ...readDefaults, ...options } : readDefaults;
35
+ return options ? Object.assign(Object.assign({}, readDefaults), options) : readDefaults;
36
36
  }
37
37
  function makeWriteOptions(options) {
38
- return options ? { ...writeDefaults, ...options } : writeDefaults;
38
+ return options ? Object.assign(Object.assign({}, writeDefaults), options) : writeDefaults;
39
39
  }
40
40
  function makeBinaryFormatCommon() {
41
41
  return {
42
42
  makeReadOptions,
43
43
  makeWriteOptions,
44
44
  listUnknownFields(message) {
45
- return message[unknownFieldsSymbol] ?? [];
45
+ var _a;
46
+ return (_a = message[unknownFieldsSymbol]) !== null && _a !== void 0 ? _a : [];
46
47
  },
47
48
  discardUnknownFields(message) {
48
49
  delete message[unknownFieldsSymbol];
@@ -18,9 +18,7 @@ const field_js_1 = require("../field.js");
18
18
  const binary_format_common_js_1 = require("./binary-format-common.js");
19
19
  /* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions, no-case-declarations, prefer-const */
20
20
  function makeBinaryFormatProto2() {
21
- return {
22
- ...(0, binary_format_common_js_1.makeBinaryFormatCommon)(),
23
- writeMessage(message, writer, options) {
21
+ return Object.assign(Object.assign({}, (0, binary_format_common_js_1.makeBinaryFormatCommon)()), { writeMessage(message, writer, options) {
24
22
  const type = message.getType();
25
23
  let field;
26
24
  try {
@@ -84,7 +82,7 @@ function makeBinaryFormatProto2() {
84
82
  }
85
83
  catch (e) {
86
84
  let m = field
87
- ? `cannot encode field ${type.typeName}.${field?.name} to binary`
85
+ ? `cannot encode field ${type.typeName}.${field === null || field === void 0 ? void 0 : field.name} to binary`
88
86
  : `cannot encode message ${type.typeName} to binary`;
89
87
  let r = e instanceof Error ? e.message : String(e);
90
88
  throw new Error(m + (r.length > 0 ? `: ${r}` : ""));
@@ -93,7 +91,6 @@ function makeBinaryFormatProto2() {
93
91
  this.writeUnknownFields(message, writer);
94
92
  }
95
93
  return writer;
96
- },
97
- };
94
+ } });
98
95
  }
99
96
  exports.makeBinaryFormatProto2 = makeBinaryFormatProto2;
@@ -18,9 +18,7 @@ const field_js_1 = require("../field.js");
18
18
  const binary_format_common_js_1 = require("./binary-format-common.js");
19
19
  /* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions, prefer-const, no-case-declarations */
20
20
  function makeBinaryFormatProto3() {
21
- return {
22
- ...(0, binary_format_common_js_1.makeBinaryFormatCommon)(),
23
- writeMessage(message, writer, options) {
21
+ return Object.assign(Object.assign({}, (0, binary_format_common_js_1.makeBinaryFormatCommon)()), { writeMessage(message, writer, options) {
24
22
  const type = message.getType();
25
23
  for (const field of type.fields.byNumber()) {
26
24
  let value, // this will be our field value, whether it is member of a oneof or regular field
@@ -76,7 +74,6 @@ function makeBinaryFormatProto3() {
76
74
  this.writeUnknownFields(message, writer);
77
75
  }
78
76
  return writer;
79
- },
80
- };
77
+ } });
81
78
  }
82
79
  exports.makeBinaryFormatProto3 = makeBinaryFormatProto3;
@@ -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?.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}`);
@@ -32,10 +32,10 @@ const jsonWriteDefaults = {
32
32
  prettySpaces: 0,
33
33
  };
34
34
  function makeReadOptions(options) {
35
- return options ? { ...jsonReadDefaults, ...options } : jsonReadDefaults;
35
+ return options ? Object.assign(Object.assign({}, jsonReadDefaults), options) : jsonReadDefaults;
36
36
  }
37
37
  function makeWriteOptions(options) {
38
- return options ? { ...jsonWriteDefaults, ...options } : jsonWriteDefaults;
38
+ return options ? Object.assign(Object.assign({}, jsonWriteDefaults), options) : jsonWriteDefaults;
39
39
  }
40
40
  function makeJsonFormatCommon(makeWriteField) {
41
41
  const writeField = makeWriteField(writeEnum, writeScalar);
@@ -46,7 +46,7 @@ function makeJsonFormatCommon(makeWriteField) {
46
46
  if (json == null || Array.isArray(json) || typeof json != "object") {
47
47
  throw new Error(`cannot decode message ${type.typeName} from JSON: ${this.debug(json)}`);
48
48
  }
49
- message = message ?? new type();
49
+ message = message !== null && message !== void 0 ? message : new type();
50
50
  const oneofSeen = {};
51
51
  for (const [jsonKey, jsonValue] of Object.entries(json)) {
52
52
  const field = type.fields.findJsonName(jsonKey);
@@ -386,13 +386,14 @@ function readEnum(type, json, ignoreUnknownFields) {
386
386
  case "string":
387
387
  const value = type.findName(json);
388
388
  if (value || ignoreUnknownFields) {
389
- return value?.no;
389
+ return value === null || value === void 0 ? void 0 : value.no;
390
390
  }
391
391
  break;
392
392
  }
393
393
  throw new Error(`cannot decode enum ${type.typeName} from JSON: ${debugJsonValue(json)}`);
394
394
  }
395
395
  function writeEnum(type, value, emitIntrinsicDefault, enumAsInteger) {
396
+ var _a;
396
397
  if (value === undefined) {
397
398
  return value;
398
399
  }
@@ -407,7 +408,7 @@ function writeEnum(type, value, emitIntrinsicDefault, enumAsInteger) {
407
408
  return null;
408
409
  }
409
410
  const val = type.findNumber(value);
410
- return val?.name ?? value; // if we don't know the enum value, just return the number
411
+ return (_a = val === null || val === void 0 ? void 0 : val.name) !== null && _a !== void 0 ? _a : value; // if we don't know the enum value, just return the number
411
412
  }
412
413
  function writeScalar(type, value, emitIntrinsicDefault) {
413
414
  if (value === undefined) {
@@ -19,7 +19,8 @@ const message_js_1 = require("../message.js");
19
19
  * Create a new message type using the given runtime.
20
20
  */
21
21
  function makeMessageType(runtime, typeName, fields, opt) {
22
- const localName = opt?.localName ?? typeName.substring(typeName.lastIndexOf(".") + 1);
22
+ var _a;
23
+ const localName = (_a = opt === null || opt === void 0 ? void 0 : opt.localName) !== null && _a !== void 0 ? _a : typeName.substring(typeName.lastIndexOf(".") + 1);
23
24
  const type = {
24
25
  [localName]: function (data) {
25
26
  runtime.util.initFields(this);
@@ -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,
@@ -18,7 +18,7 @@ const varint_js_1 = require("./google/varint.js");
18
18
  function makeInt64Support() {
19
19
  const dv = new DataView(new ArrayBuffer(8));
20
20
  // note that Safari 14 implements BigInt, but not the DataView methods
21
- const ok = globalThis.BigInt !== undefined && // eslint-disable-line @typescript-eslint/no-unnecessary-condition -- conditional for BigInt is very much necessary
21
+ const ok = globalThis.BigInt !== undefined &&
22
22
  typeof dv.getBigInt64 === "function" &&
23
23
  typeof dv.getBigUint64 === "function" &&
24
24
  typeof dv.setBigInt64 === "function" &&
@@ -24,9 +24,7 @@ const json_format_proto2_js_1 = require("./private/json-format-proto2.js");
24
24
  /**
25
25
  * Provides functionality for messages defined with the proto2 syntax.
26
26
  */
27
- exports.proto2 = (0, proto_runtime_js_1.makeProtoRuntime)("proto2", (0, json_format_proto2_js_1.makeJsonFormatProto2)(), (0, binary_format_proto2_js_1.makeBinaryFormatProto2)(), {
28
- ...(0, util_common_js_1.makeUtilCommon)(),
29
- newFieldList(fields) {
27
+ exports.proto2 = (0, proto_runtime_js_1.makeProtoRuntime)("proto2", (0, json_format_proto2_js_1.makeJsonFormatProto2)(), (0, binary_format_proto2_js_1.makeBinaryFormatProto2)(), Object.assign(Object.assign({}, (0, util_common_js_1.makeUtilCommon)()), { newFieldList(fields) {
30
28
  return new field_list_js_1.InternalFieldList(fields, normalizeFieldInfosProto2);
31
29
  },
32
30
  initFields(target) {
@@ -55,21 +53,21 @@ exports.proto2 = (0, proto_runtime_js_1.makeProtoRuntime)("proto2", (0, json_for
55
53
  break;
56
54
  }
57
55
  }
58
- },
59
- });
56
+ } }));
60
57
  /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
61
58
  function normalizeFieldInfosProto2(fieldInfos) {
59
+ var _a, _b, _c;
62
60
  const r = [];
63
61
  let o;
64
62
  for (const field of typeof fieldInfos == "function"
65
63
  ? fieldInfos()
66
64
  : fieldInfos) {
67
65
  const f = field;
68
- f.localName = (0, names_js_1.makeFieldName)(field.name, field.oneof !== undefined);
69
- f.jsonName = field.jsonName ?? (0, names_js_1.makeJsonName)(field.name);
70
- f.repeated = field.repeated ?? false;
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
+ f.repeated = (_b = field.repeated) !== null && _b !== void 0 ? _b : false;
71
69
  // In contrast to proto3, repeated fields are unpacked except when explicitly specified.
72
- f.packed = field.packed ?? false;
70
+ f.packed = (_c = field.packed) !== null && _c !== void 0 ? _c : false;
73
71
  // We do not surface options at this time
74
72
  // f.options = field.options ?? emptyReadonlyObject;
75
73
  if (field.oneof !== undefined) {