@aptre/protobuf-es-lite 0.2.6 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/.ignore +1 -0
  2. package/README.md +25 -24
  3. package/bin/protoc-gen-es-lite +3 -3
  4. package/dist/binary-encoding.d.ts +420 -0
  5. package/dist/binary-encoding.js +437 -0
  6. package/dist/binary.d.ts +35 -2
  7. package/dist/binary.js +69 -66
  8. package/dist/codegen-info.d.ts +43 -0
  9. package/dist/codegen-info.js +72 -0
  10. package/dist/create-descriptor-set.d.ts +36 -0
  11. package/dist/create-descriptor-set.js +936 -0
  12. package/dist/descriptor-set.d.ts +672 -0
  13. package/dist/{editions.js → descriptor-set.js} +0 -11
  14. package/dist/enum.d.ts +35 -1
  15. package/dist/feature-set.d.ts +19 -0
  16. package/dist/feature-set.js +103 -0
  17. package/dist/field-wrapper.d.ts +7 -3
  18. package/dist/field-wrapper.js +30 -1
  19. package/dist/field.d.ts +8 -19
  20. package/dist/field.js +15 -117
  21. package/dist/google/index.d.ts +9 -0
  22. package/dist/google/index.js +41 -0
  23. package/dist/google/protobuf/any.pb.d.ts +139 -0
  24. package/dist/google/protobuf/any.pb.js +42 -0
  25. package/dist/google/protobuf/api.pb.d.ts +252 -0
  26. package/dist/google/protobuf/api.pb.js +70 -0
  27. package/dist/google/protobuf/compiler/plugin.pb.d.ts +240 -0
  28. package/dist/google/protobuf/compiler/plugin.pb.js +203 -0
  29. package/dist/google/protobuf/descriptor.pb.d.ts +2132 -0
  30. package/dist/google/protobuf/descriptor.pb.js +1080 -0
  31. package/dist/google/protobuf/duration.pb.d.ts +92 -0
  32. package/dist/google/protobuf/duration.pb.js +42 -0
  33. package/dist/google/protobuf/empty.pb.d.ts +18 -0
  34. package/dist/google/protobuf/empty.pb.js +39 -0
  35. package/dist/google/protobuf/source_context.pb.d.ts +21 -0
  36. package/dist/google/protobuf/source_context.pb.js +41 -0
  37. package/dist/google/protobuf/struct.pb.d.ts +121 -0
  38. package/dist/google/protobuf/struct.pb.js +83 -0
  39. package/dist/google/protobuf/timestamp.pb.d.ts +121 -0
  40. package/dist/google/protobuf/timestamp.pb.js +42 -0
  41. package/dist/google/protobuf/type.pb.d.ts +464 -0
  42. package/dist/google/protobuf/type.pb.js +338 -0
  43. package/dist/google/protobuf/wrappers.pb.d.ts +183 -0
  44. package/dist/google/protobuf/wrappers.pb.js +107 -0
  45. package/dist/index.d.ts +8 -3
  46. package/dist/index.js +38 -8
  47. package/dist/is-message.d.ts +10 -2
  48. package/dist/is-message.js +76 -3
  49. package/dist/json.d.ts +68 -1
  50. package/dist/json.js +60 -81
  51. package/dist/message.d.ts +30 -61
  52. package/dist/message.js +63 -18
  53. package/dist/names.d.ts +43 -0
  54. package/dist/names.js +276 -0
  55. package/dist/partial.js +19 -16
  56. package/dist/proto-base64.d.ts +18 -0
  57. package/dist/proto-base64.js +128 -0
  58. package/dist/proto-double.d.ts +5 -0
  59. package/dist/proto-double.js +29 -0
  60. package/dist/proto-int64.d.ts +95 -0
  61. package/dist/proto-int64.js +116 -0
  62. package/dist/protoc-gen-es-lite/protoc-gen-es-lite-plugin.d.ts +1 -0
  63. package/dist/{protoc-gen-es-lite-plugin.js → protoc-gen-es-lite/protoc-gen-es-lite-plugin.js} +2 -2
  64. package/dist/{typescript.d.ts → protoc-gen-es-lite/typescript.d.ts} +3 -2
  65. package/dist/{typescript.js → protoc-gen-es-lite/typescript.js} +48 -36
  66. package/dist/protoplugin/create-es-plugin.d.ts +83 -0
  67. package/dist/protoplugin/create-es-plugin.js +119 -0
  68. package/dist/protoplugin/ecmascript/file-preamble.d.ts +2 -0
  69. package/dist/protoplugin/ecmascript/file-preamble.js +74 -0
  70. package/dist/protoplugin/ecmascript/generated-file.d.ts +116 -0
  71. package/dist/protoplugin/ecmascript/generated-file.js +407 -0
  72. package/dist/protoplugin/ecmascript/import-path.d.ts +65 -0
  73. package/dist/protoplugin/ecmascript/import-path.js +156 -0
  74. package/dist/protoplugin/ecmascript/import-symbol.d.ts +39 -0
  75. package/dist/protoplugin/ecmascript/import-symbol.js +37 -0
  76. package/dist/protoplugin/ecmascript/index.d.ts +7 -0
  77. package/dist/protoplugin/ecmascript/index.js +20 -0
  78. package/dist/protoplugin/ecmascript/jsdoc.d.ts +14 -0
  79. package/dist/protoplugin/ecmascript/jsdoc.js +93 -0
  80. package/dist/protoplugin/ecmascript/opaque-printables.d.ts +27 -0
  81. package/dist/protoplugin/ecmascript/opaque-printables.js +15 -0
  82. package/dist/protoplugin/ecmascript/parameter.d.ts +13 -0
  83. package/dist/protoplugin/ecmascript/parameter.js +161 -0
  84. package/dist/protoplugin/ecmascript/reify-wkt.d.ts +100 -0
  85. package/dist/protoplugin/ecmascript/reify-wkt.js +170 -0
  86. package/dist/protoplugin/ecmascript/runtime-imports.d.ts +6 -0
  87. package/dist/protoplugin/ecmascript/runtime-imports.js +28 -0
  88. package/dist/protoplugin/ecmascript/schema.d.ts +43 -0
  89. package/dist/protoplugin/ecmascript/schema.js +73 -0
  90. package/dist/protoplugin/ecmascript/target.d.ts +4 -0
  91. package/dist/protoplugin/ecmascript/target.js +15 -0
  92. package/dist/protoplugin/ecmascript/transpile.d.ts +2 -0
  93. package/dist/protoplugin/ecmascript/transpile.js +149 -0
  94. package/dist/protoplugin/error.d.ts +6 -0
  95. package/dist/protoplugin/error.js +43 -0
  96. package/dist/protoplugin/index.d.ts +8 -0
  97. package/dist/protoplugin/index.js +20 -0
  98. package/dist/protoplugin/plugin.d.ts +18 -0
  99. package/dist/protoplugin/plugin.js +15 -0
  100. package/dist/protoplugin/run-node.d.ts +12 -0
  101. package/dist/protoplugin/run-node.js +83 -0
  102. package/dist/scalar.d.ts +53 -1
  103. package/dist/scalar.js +87 -22
  104. package/dist/service-type.d.ts +95 -0
  105. package/dist/service-type.js +53 -0
  106. package/dist/text-format.d.ts +4 -0
  107. package/dist/text-format.js +189 -0
  108. package/dist/type-registry.d.ts +43 -0
  109. package/dist/type-registry.js +15 -0
  110. package/dist/unknown.d.ts +1 -1
  111. package/dist/util.d.ts +2 -2
  112. package/dist/util.js +46 -44
  113. package/dist/varint.d.ts +70 -0
  114. package/dist/varint.js +324 -0
  115. package/example/example.pb.ts +87 -0
  116. package/example/example.proto +1 -0
  117. package/example/tsconfig.json +8 -2
  118. package/package.json +19 -12
  119. package/tsconfig.json +4 -4
  120. package/dist/editions.d.ts +0 -6
  121. package/dist/protoc-gen-es-lite-plugin.d.ts +0 -1
package/dist/message.d.ts CHANGED
@@ -1,61 +1,23 @@
1
- import { BinaryReadOptions, JsonReadOptions, JsonValue, MessageType as BufMessageType, BinaryWriteOptions, JsonWriteOptions, JsonWriteStringOptions } from "@bufbuild/protobuf";
2
1
  import { FieldList, FieldListSource } from "./field.js";
3
- /**
4
- * PartialMessage<T> constructs a type from a message. The resulting type
5
- * only contains the protobuf field members of the message, and all of them
6
- * are optional.
7
- *
8
- * Note that the optionality of the fields is the only difference between
9
- * PartialMessage and PlainMessage.
10
- *
11
- * PartialMessage is similar to the built-in type Partial<T>, but recursive,
12
- * and respects `oneof` groups.
13
- */
14
- export type Message<T extends Message<T>> = {
15
- [k: string]: Field<any>;
16
- };
17
- /**
18
- * AnyMessage is an interface implemented by all messages. If you need to
19
- * handle messages of unknown type, this interface provides a convenient
20
- * index signature to access fields with message["fieldname"].
21
- */
22
- export interface AnyMessage extends Message<AnyMessage> {
23
- [k: string]: Field<any>;
24
- }
25
- export type Field<F> = F extends Date | Uint8Array | bigint | boolean | string | number ? F : F extends Array<infer U> ? Array<Field<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<Field<U>> : F extends CompleteMessage<infer U> ? Message<U> : F extends OneofSelectedMessage<infer C, infer V> ? {
2
+ import { BinaryReadOptions, BinaryWriteOptions } from "./binary.js";
3
+ import { JsonValue, JsonReadOptions, JsonWriteOptions, JsonWriteStringOptions } from "./json.js";
4
+ import { FieldWrapper } from "./field-wrapper.js";
5
+ export type Field<T> = T extends Date | Uint8Array | bigint | boolean | string | number ? T : T extends Array<infer U> ? Array<Field<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<Field<U>> : T extends {
6
+ case: string;
7
+ value: any;
8
+ } ? OneofSelectedMessage<T["case"], T["value"]> : T extends object ? Message<T> : never;
9
+ export type OneofSelectedMessage<C extends string, V> = {
26
10
  case: C;
27
- value: Message<V>;
28
- } : F extends {
29
- case: string | undefined;
30
- value?: unknown;
31
- } ? F : F extends {
32
- [key: string | number]: CompleteMessage<infer U>;
33
- } ? {
34
- [key: string | number]: Message<U>;
35
- } : F;
36
- /**
37
- * CompleteMessage<T> constructs a type from a message which requires all fields
38
- * be present in the object recursively (including zero values if unset).
39
- *
40
- * This type corresponds to Message in protobuf-es.
41
- */
42
- export type CompleteMessage<T extends Message<T>> = {
43
- [P in keyof T as T[P] extends Function ? never : P]?: CompleteField<T[P]>;
11
+ value: Field<V>;
44
12
  };
45
- export type CompleteField<F> = F extends Date | Uint8Array | bigint | boolean | string | number ? F : F extends Array<infer U> ? Array<Field<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<Field<U>> : F extends CompleteMessage<infer U> ? CompleteMessage<U> : F extends OneofSelectedMessage<infer C, infer V> ? {
46
- case: C;
47
- value: CompleteMessage<V>;
48
- } : F extends {
49
- case: string | undefined;
50
- value?: unknown;
51
- } ? F : F extends {
52
- [key: string | number]: CompleteMessage<infer U>;
53
- } ? {
54
- [key: string | number]: CompleteMessage<U>;
55
- } : F;
56
- type OneofSelectedMessage<K extends string, M extends Message<M>> = {
57
- case: K;
58
- value: M;
13
+ export type Message<T extends object> = {
14
+ [K in keyof T]?: Field<T[K]>;
15
+ };
16
+ export type CompleteMessage<T extends object> = {
17
+ [K in keyof T]-?: Field<T[K]>;
18
+ };
19
+ export type AnyMessage = {
20
+ [key: string]: any | undefined;
59
21
  };
60
22
  /**
61
23
  * MessageType represents a protobuf message declaration. It provides:
@@ -63,7 +25,7 @@ type OneofSelectedMessage<K extends string, M extends Message<M>> = {
63
25
  * - metadata for reflection-based operations
64
26
  * - common functionality like serialization
65
27
  */
66
- export interface MessageType<T extends Message<T> = AnyMessage> extends Pick<BufMessageType, "fieldWrapper"> {
28
+ export interface MessageType<T extends Message<T>> {
67
29
  /**
68
30
  * The fully qualified name of the message.
69
31
  */
@@ -72,10 +34,14 @@ export interface MessageType<T extends Message<T> = AnyMessage> extends Pick<Buf
72
34
  * Field metadata.
73
35
  */
74
36
  readonly fields: FieldList;
37
+ /**
38
+ * When used as a field, unwrap this message to a simple value.
39
+ */
40
+ readonly fieldWrapper?: FieldWrapper<T>;
75
41
  /**
76
42
  * Create a new instance of this message with zero values for fields.
77
43
  */
78
- create(partial?: Message<T>): T;
44
+ create(partial?: Message<T>): CompleteMessage<T>;
79
45
  /**
80
46
  * Create a deep copy.
81
47
  */
@@ -89,15 +55,15 @@ export interface MessageType<T extends Message<T> = AnyMessage> extends Pick<Buf
89
55
  * If a message field is already present, it will be merged with the
90
56
  * new data.
91
57
  */
92
- fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): T;
58
+ fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): CompleteMessage<T>;
93
59
  /**
94
60
  * Parse a message from a JSON value.
95
61
  */
96
- fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): T;
62
+ fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): CompleteMessage<T>;
97
63
  /**
98
64
  * Parse a message from a JSON string.
99
65
  */
100
- fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): T;
66
+ fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): CompleteMessage<T>;
101
67
  /**
102
68
  * Returns true if the given arguments have equal field values, recursively.
103
69
  * Will also return true if both messages are `undefined` or `null`.
@@ -149,4 +115,7 @@ export declare function cloneMessage<T extends Message<T>>(message: T, fields: F
149
115
  * `packed` should be packed (proto3) or unpacked (proto2).
150
116
  */
151
117
  export declare function createMessageType<T extends Message<T>>(params: MessageTypeParams<T>): MessageType<T>;
152
- export {};
118
+ /**
119
+ * createMessage recursively builds a message filled with zero values based on the given FieldList.
120
+ */
121
+ export declare function createMessage<T extends Message<T>>(fields: FieldList): CompleteMessage<T>;
package/dist/message.js CHANGED
@@ -13,8 +13,7 @@
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.createMessageType = exports.cloneMessage = exports.compareMessages = void 0;
17
- const protobuf_1 = require("@bufbuild/protobuf");
16
+ exports.createMessage = exports.createMessageType = exports.cloneMessage = exports.compareMessages = void 0;
18
17
  const field_js_1 = require("./field.js");
19
18
  const partial_js_1 = require("./partial.js");
20
19
  const scalar_js_1 = require("./scalar.js");
@@ -41,19 +40,20 @@ function compareMessages(fields, a, b) {
41
40
  // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- repeated fields are never "map"
42
41
  switch (m.kind) {
43
42
  case "message":
44
- return va.every((a, i) => m.T.equals(a, vb[i]));
43
+ const messageType = (0, field_js_1.resolveMessageType)(m.T);
44
+ return va.every((a, i) => messageType.equals(a, vb[i]));
45
45
  case "scalar":
46
46
  return va.every((a, i) => (0, scalar_js_1.scalarEquals)(m.T, a, vb[i]));
47
47
  case "enum":
48
- return va.every((a, i) => (0, scalar_js_1.scalarEquals)(protobuf_1.ScalarType.INT32, a, vb[i]));
48
+ return va.every((a, i) => (0, scalar_js_1.scalarEquals)(scalar_js_1.ScalarType.INT32, a, vb[i]));
49
49
  }
50
50
  throw new Error(`repeated cannot contain ${m.kind}`);
51
51
  }
52
52
  switch (m.kind) {
53
53
  case "message":
54
- return m.T.equals(va, vb);
54
+ return (0, field_js_1.resolveMessageType)(m.T).equals(va, vb);
55
55
  case "enum":
56
- return (0, scalar_js_1.scalarEquals)(protobuf_1.ScalarType.INT32, va, vb);
56
+ return (0, scalar_js_1.scalarEquals)(scalar_js_1.ScalarType.INT32, va, vb);
57
57
  case "scalar":
58
58
  return (0, scalar_js_1.scalarEquals)(m.T, va, vb);
59
59
  case "oneof":
@@ -67,9 +67,10 @@ function compareMessages(fields, a, b) {
67
67
  // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- oneof fields are never "map"
68
68
  switch (s.kind) {
69
69
  case "message":
70
- return s.T.equals(va.value, vb.value);
70
+ const messageType = (0, field_js_1.resolveMessageType)(s.T);
71
+ return messageType.equals(va.value, vb.value);
71
72
  case "enum":
72
- return (0, scalar_js_1.scalarEquals)(protobuf_1.ScalarType.INT32, va.value, vb.value);
73
+ return (0, scalar_js_1.scalarEquals)(scalar_js_1.ScalarType.INT32, va.value, vb.value);
73
74
  case "scalar":
74
75
  return (0, scalar_js_1.scalarEquals)(s.T, va.value, vb.value);
75
76
  }
@@ -78,10 +79,10 @@ function compareMessages(fields, a, b) {
78
79
  const keys = Object.keys(va).concat(Object.keys(vb));
79
80
  switch (m.V.kind) {
80
81
  case "message":
81
- const messageType = m.V.T;
82
+ const messageType = (0, field_js_1.resolveMessageType)(m.V.T);
82
83
  return keys.every((k) => messageType.equals(va[k], vb[k]));
83
84
  case "enum":
84
- return keys.every((k) => (0, scalar_js_1.scalarEquals)(protobuf_1.ScalarType.INT32, va[k], vb[k]));
85
+ return keys.every((k) => (0, scalar_js_1.scalarEquals)(scalar_js_1.ScalarType.INT32, va[k], vb[k]));
85
86
  case "scalar":
86
87
  const scalarType = m.V.T;
87
88
  return keys.every((k) => (0, scalar_js_1.scalarEquals)(scalarType, va[k], vb[k]));
@@ -96,7 +97,7 @@ function cloneSingularField(value, fieldInfo) {
96
97
  return value;
97
98
  }
98
99
  if (fieldInfo.kind === "message") {
99
- return cloneMessage(value, fieldInfo.T.fields);
100
+ return cloneMessage(value, (0, field_js_1.resolveMessageType)(fieldInfo.T).fields);
100
101
  }
101
102
  if (fieldInfo.kind === "oneof") {
102
103
  if (value.case === undefined) {
@@ -135,9 +136,10 @@ function cloneMessage(message, fields) {
135
136
  }
136
137
  else if (member.kind == "oneof") {
137
138
  const f = member.findField(source.case);
138
- copy = f
139
- ? { case: source.case, value: cloneSingularField(source.value, member) }
140
- : { case: undefined };
139
+ copy =
140
+ f ?
141
+ { case: source.case, value: cloneSingularField(source.value, member) }
142
+ : { case: undefined };
141
143
  }
142
144
  else {
143
145
  copy = cloneSingularField(source, member);
@@ -226,18 +228,60 @@ function createMessage(fields) {
226
228
  const fieldKind = field.kind;
227
229
  switch (fieldKind) {
228
230
  case "scalar":
229
- message[field.localName] = (field.T == protobuf_1.ScalarType.BOOL ? false : 0);
231
+ if (field.repeated) {
232
+ message[field.localName] = [];
233
+ }
234
+ else {
235
+ switch (field.T) {
236
+ case scalar_js_1.ScalarType.DOUBLE:
237
+ case scalar_js_1.ScalarType.FLOAT:
238
+ message[field.localName] = 0;
239
+ break;
240
+ case scalar_js_1.ScalarType.INT64:
241
+ case scalar_js_1.ScalarType.UINT64:
242
+ case scalar_js_1.ScalarType.INT32:
243
+ case scalar_js_1.ScalarType.FIXED64:
244
+ case scalar_js_1.ScalarType.FIXED32:
245
+ case scalar_js_1.ScalarType.UINT32:
246
+ case scalar_js_1.ScalarType.SFIXED32:
247
+ case scalar_js_1.ScalarType.SFIXED64:
248
+ case scalar_js_1.ScalarType.SINT32:
249
+ case scalar_js_1.ScalarType.SINT64:
250
+ message[field.localName] = 0;
251
+ break;
252
+ case scalar_js_1.ScalarType.BOOL:
253
+ message[field.localName] = false;
254
+ break;
255
+ case scalar_js_1.ScalarType.STRING:
256
+ message[field.localName] = "";
257
+ break;
258
+ case scalar_js_1.ScalarType.BYTES:
259
+ message[field.localName] =
260
+ new Uint8Array();
261
+ break;
262
+ }
263
+ }
230
264
  break;
231
265
  case "enum":
232
- message[field.localName] = 0;
266
+ if (field.repeated) {
267
+ message[field.localName] = [];
268
+ }
269
+ else {
270
+ message[field.localName] = 0;
271
+ }
233
272
  break;
234
273
  case "message":
235
274
  if (field.oneof) {
236
- // leave oneofs undefined
237
275
  message[field.localName] = undefined;
238
276
  continue;
239
277
  }
240
- message[field.localName] = createMessage(field.T.fields);
278
+ const messageType = (0, field_js_1.resolveMessageType)(field.T);
279
+ if (field.repeated) {
280
+ message[field.localName] = [];
281
+ }
282
+ else {
283
+ message[field.localName] = createMessage(messageType.fields);
284
+ }
241
285
  break;
242
286
  case "map":
243
287
  message[field.localName] = {};
@@ -248,3 +292,4 @@ function createMessage(fields) {
248
292
  }
249
293
  return message;
250
294
  }
295
+ exports.createMessage = createMessage;
@@ -0,0 +1,43 @@
1
+ import type { DescEnum, DescEnumValue, DescExtension, DescField, DescMessage, DescService } from "./descriptor-set.js";
2
+ import type { DescMethod, DescOneof } from "./descriptor-set.js";
3
+ /**
4
+ * Returns the name of a protobuf element in generated code.
5
+ *
6
+ * Field names - including oneofs - are converted to lowerCamelCase. For
7
+ * messages, enumerations and services, the package name is stripped from
8
+ * the type name. For nested messages and enumerations, the names are joined
9
+ * with an underscore. For methods, the first character is made lowercase.
10
+ */
11
+ export declare function localName(desc: DescEnum | DescEnumValue | DescMessage | DescExtension | DescOneof | DescField | DescService | DescMethod): string;
12
+ /**
13
+ * Returns the name of a field in generated code.
14
+ */
15
+ export declare function localFieldName(protoName: string, inOneof: boolean): string;
16
+ /**
17
+ * Returns the name of a oneof group in generated code.
18
+ */
19
+ export declare function localOneofName(protoName: string): string;
20
+ /**
21
+ * Finds a prefix shared by enum values, for example `MY_ENUM_` for
22
+ * `enum MyEnum {MY_ENUM_A=0; MY_ENUM_B=1;}`.
23
+ */
24
+ export declare function findEnumSharedPrefix(enumName: string, valueNames: string[]): string | undefined;
25
+ /**
26
+ * Converts lowerCamelCase or UpperCamelCase into lower_snake_case.
27
+ * This is used to find shared prefixes in an enum.
28
+ */
29
+ export declare function camelToSnakeCase(camel: string): string;
30
+ /**
31
+ * Converts snake_case to protoCamelCase according to the convention
32
+ * used by protoc to convert a field name to a JSON name.
33
+ */
34
+ export declare function protoCamelCase(snakeCase: string): string;
35
+ /**
36
+ * Names that cannot be used for object properties because they are reserved
37
+ * by built-in JavaScript properties.
38
+ */
39
+ export declare const safeObjectProperty: (name: string) => string;
40
+ /**
41
+ * Names that can be used for identifiers or class properties
42
+ */
43
+ export declare const safeIdentifier: (name: string) => string;
package/dist/names.js ADDED
@@ -0,0 +1,276 @@
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.safeIdentifier = exports.safeObjectProperty = exports.protoCamelCase = exports.camelToSnakeCase = exports.findEnumSharedPrefix = exports.localOneofName = exports.localFieldName = exports.localName = void 0;
17
+ /**
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.
24
+ */
25
+ function localName(desc) {
26
+ switch (desc.kind) {
27
+ case "field":
28
+ return localFieldName(desc.name, desc.oneof !== undefined);
29
+ case "oneof":
30
+ return localOneofName(desc.name);
31
+ case "enum":
32
+ case "message":
33
+ case "service":
34
+ case "extension": {
35
+ const pkg = desc.file.proto.package;
36
+ const offset = pkg === undefined ? 0 : pkg.length + 1;
37
+ const name = desc.typeName.substring(offset).replace(/\./g, "_");
38
+ // For services, we only care about safe identifiers, not safe object properties,
39
+ // but we have shipped v1 with a bug that respected object properties, and we
40
+ // do not want to introduce a breaking change, so we continue to escape for
41
+ // safe object properties.
42
+ // See https://github.com/bufbuild/protobuf-es/pull/391
43
+ return (0, exports.safeObjectProperty)((0, exports.safeIdentifier)(name));
44
+ }
45
+ case "enum_value": {
46
+ let name = desc.name;
47
+ const sharedPrefix = desc.parent.sharedPrefix;
48
+ if (sharedPrefix !== undefined) {
49
+ name = name.substring(sharedPrefix.length);
50
+ }
51
+ return (0, exports.safeObjectProperty)(name);
52
+ }
53
+ case "rpc": {
54
+ let name = desc.name;
55
+ if (name.length == 0) {
56
+ return name;
57
+ }
58
+ name = name[0].toLowerCase() + name.substring(1);
59
+ return (0, exports.safeObjectProperty)(name);
60
+ }
61
+ }
62
+ }
63
+ exports.localName = localName;
64
+ /**
65
+ * Returns the name of a field in generated code.
66
+ */
67
+ function localFieldName(protoName, inOneof) {
68
+ const name = protoCamelCase(protoName);
69
+ if (inOneof) {
70
+ // oneof member names are not properties, but values of the `case` property.
71
+ return name;
72
+ }
73
+ return (0, exports.safeObjectProperty)(safeMessageProperty(name));
74
+ }
75
+ exports.localFieldName = localFieldName;
76
+ /**
77
+ * Returns the name of a oneof group in generated code.
78
+ */
79
+ function localOneofName(protoName) {
80
+ return localFieldName(protoName, false);
81
+ }
82
+ exports.localOneofName = localOneofName;
83
+ /**
84
+ * Finds a prefix shared by enum values, for example `MY_ENUM_` for
85
+ * `enum MyEnum {MY_ENUM_A=0; MY_ENUM_B=1;}`.
86
+ */
87
+ function findEnumSharedPrefix(enumName, valueNames) {
88
+ const prefix = camelToSnakeCase(enumName) + "_";
89
+ for (const name of valueNames) {
90
+ if (!name.toLowerCase().startsWith(prefix)) {
91
+ return undefined;
92
+ }
93
+ const shortName = name.substring(prefix.length);
94
+ if (shortName.length == 0) {
95
+ return undefined;
96
+ }
97
+ if (/^\d/.test(shortName)) {
98
+ // identifiers must not start with numbers
99
+ return undefined;
100
+ }
101
+ }
102
+ return prefix;
103
+ }
104
+ exports.findEnumSharedPrefix = findEnumSharedPrefix;
105
+ /**
106
+ * Converts lowerCamelCase or UpperCamelCase into lower_snake_case.
107
+ * This is used to find shared prefixes in an enum.
108
+ */
109
+ function camelToSnakeCase(camel) {
110
+ return (camel.substring(0, 1) + camel.substring(1).replace(/[A-Z]/g, (c) => "_" + c)).toLowerCase();
111
+ }
112
+ exports.camelToSnakeCase = camelToSnakeCase;
113
+ /**
114
+ * Converts snake_case to protoCamelCase according to the convention
115
+ * used by protoc to convert a field name to a JSON name.
116
+ */
117
+ function protoCamelCase(snakeCase) {
118
+ let capNext = false;
119
+ const b = [];
120
+ for (let i = 0; i < snakeCase.length; i++) {
121
+ let c = snakeCase.charAt(i);
122
+ switch (c) {
123
+ case "_":
124
+ capNext = true;
125
+ break;
126
+ case "0":
127
+ case "1":
128
+ case "2":
129
+ case "3":
130
+ case "4":
131
+ case "5":
132
+ case "6":
133
+ case "7":
134
+ case "8":
135
+ case "9":
136
+ b.push(c);
137
+ capNext = false;
138
+ break;
139
+ default:
140
+ if (capNext) {
141
+ capNext = false;
142
+ c = c.toUpperCase();
143
+ }
144
+ b.push(c);
145
+ break;
146
+ }
147
+ }
148
+ return b.join("");
149
+ }
150
+ exports.protoCamelCase = protoCamelCase;
151
+ /**
152
+ * Names that cannot be used for identifiers, such as class names,
153
+ * but _can_ be used for object properties.
154
+ */
155
+ const reservedIdentifiers = new Set([
156
+ // ECMAScript 2015 keywords
157
+ "break",
158
+ "case",
159
+ "catch",
160
+ "class",
161
+ "const",
162
+ "continue",
163
+ "debugger",
164
+ "default",
165
+ "delete",
166
+ "do",
167
+ "else",
168
+ "export",
169
+ "extends",
170
+ "false",
171
+ "finally",
172
+ "for",
173
+ "function",
174
+ "if",
175
+ "import",
176
+ "in",
177
+ "instanceof",
178
+ "new",
179
+ "null",
180
+ "return",
181
+ "super",
182
+ "switch",
183
+ "this",
184
+ "throw",
185
+ "true",
186
+ "try",
187
+ "typeof",
188
+ "var",
189
+ "void",
190
+ "while",
191
+ "with",
192
+ "yield",
193
+ // ECMAScript 2015 future reserved keywords
194
+ "enum",
195
+ "implements",
196
+ "interface",
197
+ "let",
198
+ "package",
199
+ "private",
200
+ "protected",
201
+ "public",
202
+ "static",
203
+ // Class name cannot be 'Object' when targeting ES5 with module CommonJS
204
+ "Object",
205
+ // TypeScript keywords that cannot be used for types (as opposed to variables)
206
+ "bigint",
207
+ "number",
208
+ "boolean",
209
+ "string",
210
+ "object",
211
+ // Identifiers reserved for the runtime, so we can generate legible code
212
+ "globalThis",
213
+ "Uint8Array",
214
+ "Partial",
215
+ ]);
216
+ /**
217
+ * Names that cannot be used for object properties because they are reserved
218
+ * by built-in JavaScript properties.
219
+ */
220
+ const reservedObjectProperties = new Set([
221
+ // names reserved by JavaScript
222
+ "constructor",
223
+ "toString",
224
+ "toJSON",
225
+ "valueOf",
226
+ ]);
227
+ /**
228
+ * Names that cannot be used for object properties because they are reserved
229
+ * by the runtime.
230
+ */
231
+ const reservedMessageProperties = new Set([
232
+ // names reserved by the runtime
233
+ "getType",
234
+ "clone",
235
+ "equals",
236
+ "fromBinary",
237
+ "fromJson",
238
+ "fromJsonString",
239
+ "toBinary",
240
+ "toJson",
241
+ "toJsonString",
242
+ // names reserved by the runtime for the future
243
+ "toObject",
244
+ ]);
245
+ const fallback = (name) => `${name}$`;
246
+ /**
247
+ * Will wrap names that are Object prototype properties or names reserved
248
+ * for `Message`s.
249
+ */
250
+ const safeMessageProperty = (name) => {
251
+ if (reservedMessageProperties.has(name)) {
252
+ return fallback(name);
253
+ }
254
+ return name;
255
+ };
256
+ /**
257
+ * Names that cannot be used for object properties because they are reserved
258
+ * by built-in JavaScript properties.
259
+ */
260
+ const safeObjectProperty = (name) => {
261
+ if (reservedObjectProperties.has(name)) {
262
+ return fallback(name);
263
+ }
264
+ return name;
265
+ };
266
+ exports.safeObjectProperty = safeObjectProperty;
267
+ /**
268
+ * Names that can be used for identifiers or class properties
269
+ */
270
+ const safeIdentifier = (name) => {
271
+ if (reservedIdentifiers.has(name)) {
272
+ return fallback(name);
273
+ }
274
+ return name;
275
+ };
276
+ exports.safeIdentifier = safeIdentifier;