@bufbuild/protobuf 0.0.8 → 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 (61) 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 +376 -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/struct_pb.js +1 -1
  8. package/dist/cjs/google/protobuf/type_pb.js +1 -1
  9. package/dist/cjs/index.js +17 -7
  10. package/dist/cjs/{descriptor-registry.js → legacy-descriptor-registry.js} +18 -10
  11. package/dist/cjs/legacy-descriptor-set.js +453 -0
  12. package/dist/cjs/legacy-type-registry.js +104 -0
  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/names.js +168 -29
  17. package/dist/cjs/proto2.js +2 -2
  18. package/dist/cjs/proto3.js +2 -2
  19. package/dist/cjs/type-registry.js +0 -87
  20. package/dist/esm/codegen-info.js +57 -0
  21. package/dist/esm/create-descriptor-set.js +947 -0
  22. package/dist/esm/create-registry-from-desc.js +371 -0
  23. package/dist/esm/create-registry.js +71 -0
  24. package/dist/esm/descriptor-set.js +1 -434
  25. package/dist/esm/google/protobuf/struct_pb.js +1 -1
  26. package/dist/esm/google/protobuf/type_pb.js +1 -1
  27. package/dist/esm/index.js +9 -3
  28. package/dist/esm/{descriptor-registry.js → legacy-descriptor-registry.js} +16 -8
  29. package/dist/esm/legacy-descriptor-set.js +449 -0
  30. package/dist/esm/legacy-type-registry.js +100 -0
  31. package/dist/esm/private/enum.js +22 -31
  32. package/dist/esm/private/field-wrapper.js +28 -0
  33. package/dist/esm/private/field.js +2 -2
  34. package/dist/esm/private/names.js +163 -24
  35. package/dist/esm/proto2.js +3 -3
  36. package/dist/esm/proto3.js +3 -3
  37. package/dist/esm/type-registry.js +1 -85
  38. package/dist/types/codegen-info.d.ts +19 -0
  39. package/dist/types/create-descriptor-set.d.ts +16 -0
  40. package/dist/types/create-registry-from-desc.d.ts +49 -0
  41. package/dist/types/create-registry.d.ts +8 -0
  42. package/dist/types/descriptor-set.d.ts +554 -108
  43. package/dist/types/enum.d.ts +9 -5
  44. package/dist/types/index.d.ts +9 -3
  45. package/dist/types/{descriptor-registry.d.ts → legacy-descriptor-registry.d.ts} +8 -6
  46. package/dist/types/legacy-descriptor-set.d.ts +152 -0
  47. package/dist/types/legacy-type-registry.d.ts +44 -0
  48. package/dist/types/private/enum.d.ts +4 -8
  49. package/dist/types/private/field-wrapper.d.ts +7 -0
  50. package/dist/types/private/message-type.d.ts +1 -4
  51. package/dist/types/private/names.d.ts +26 -8
  52. package/dist/types/private/proto-runtime.d.ts +2 -2
  53. package/dist/types/private/util.d.ts +1 -6
  54. package/dist/types/type-registry.d.ts +0 -38
  55. package/package.json +2 -2
  56. package/dist/protobuf.cjs +0 -2
  57. package/dist/protobuf.cjs.map +0 -1
  58. package/dist/protobuf.esm.js +0 -2
  59. package/dist/protobuf.esm.js.map +0 -1
  60. package/dist/protobuf.modern.js +0 -2
  61. package/dist/protobuf.modern.js.map +0 -1
@@ -12,38 +12,113 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
  /**
15
- * Returns the JSON name for a protobuf field, exactly like protoc does.
15
+ * Returns the name of a protobuf element in generated code.
16
+ *
17
+ * Field names - including oneofs - are converted to lowerCamelCase. For
18
+ * messages, enumerations and services, the package name is stripped from
19
+ * the type name. For nested messages and enumerations, the names are joined
20
+ * with an underscore. For methods, the first character is made lowercase.
16
21
  */
17
- export function makeJsonName(protoName) {
18
- return protoCamelCase(protoName);
22
+ export function localName(desc) {
23
+ switch (desc.kind) {
24
+ case "enum_field":
25
+ case "message_field":
26
+ case "map_field":
27
+ case "scalar_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
+ const pkg = desc.file.proto.package;
35
+ const offset = pkg === undefined ? 0 : pkg.length + 1;
36
+ const name = desc.typeName.substring(offset).replace(/\./g, "_");
37
+ if (reservedIdent[name]) {
38
+ return name + "$";
39
+ }
40
+ return name;
41
+ }
42
+ case "enum_value": {
43
+ const sharedPrefix = desc.parent.sharedPrefix;
44
+ if (sharedPrefix === undefined) {
45
+ return desc.name;
46
+ }
47
+ const name = desc.name.substring(sharedPrefix.length);
48
+ if (reservedObjectProperties[name]) {
49
+ return name + "$";
50
+ }
51
+ return 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
+ if (reservedObjectProperties[name]) {
60
+ return name + "$";
61
+ }
62
+ return name;
63
+ }
64
+ }
19
65
  }
20
66
  /**
21
- * Returns the local name of a field, exactly like protoc-gen-es does.
67
+ * Returns the name of a field in generated code.
22
68
  */
23
- export function makeFieldName(protoName, inOneof) {
24
- const n = protoCamelCase(protoName);
69
+ export function localFieldName(protoName, inOneof) {
70
+ let name = protoCamelCase(protoName);
25
71
  if (inOneof) {
26
- return n;
72
+ // oneof member names are not properties, but values of the `case` property.
73
+ return name;
27
74
  }
28
- return rProp[n] ? n + escapeChar : n;
75
+ if (reservedObjectProperties[name] || reservedMessageProperties[name]) {
76
+ name = name + "$";
77
+ }
78
+ return name;
29
79
  }
30
80
  /**
31
- * Returns the local name of a oneof group, exactly like protoc-gen-es does.
81
+ * Returns the name of a oneof group in generated code.
32
82
  */
33
- export function makeOneofName(protoName) {
34
- return makeFieldName(protoName, false);
83
+ export function localOneofName(protoName) {
84
+ return localFieldName(protoName, false);
35
85
  }
36
86
  /**
37
- * Returns the local name of a rpc, exactly like protoc-gen-es does.
87
+ * Returns the JSON name for a protobuf field, exactly like protoc does.
38
88
  */
39
- export function makeMethodName(protoName) {
40
- if (protoName.length == 0) {
41
- return protoName;
89
+ export const fieldJsonName = protoCamelCase;
90
+ /**
91
+ * Finds a prefix shared by enum values, for example `MY_ENUM_` for
92
+ * `enum MyEnum {MY_ENUM_A=0; MY_ENUM_B=1;}`.
93
+ */
94
+ export function findEnumSharedPrefix(enumName, valueNames) {
95
+ const prefix = camelToSnakeCase(enumName) + "_";
96
+ for (const name of valueNames) {
97
+ if (!name.toLowerCase().startsWith(prefix)) {
98
+ return undefined;
99
+ }
100
+ const shortName = name.substring(prefix.length);
101
+ if (shortName.length == 0) {
102
+ return undefined;
103
+ }
104
+ if (/^\d/.test(shortName)) {
105
+ // identifiers must not start with numbers
106
+ return undefined;
107
+ }
42
108
  }
43
- return protoName[0].toLowerCase() + protoName.substring(1);
109
+ return prefix;
110
+ }
111
+ /**
112
+ * Converts lowerCamelCase or UpperCamelCase into lower_snake_case.
113
+ * This is used to find shared prefixes in an enum.
114
+ */
115
+ function camelToSnakeCase(camel) {
116
+ return (camel.substring(0, 1) + camel.substring(1).replace(/[A-Z]/g, (c) => "_" + c)).toLowerCase();
44
117
  }
45
- // Converts snake_case to protoCamelCase according to the convention
46
- // used by protoc to convert a field name to a JSON name.
118
+ /**
119
+ * Converts snake_case to protoCamelCase according to the convention
120
+ * used by protoc to convert a field name to a JSON name.
121
+ */
47
122
  function protoCamelCase(snakeCase) {
48
123
  let capNext = false;
49
124
  const b = [];
@@ -77,17 +152,81 @@ function protoCamelCase(snakeCase) {
77
152
  }
78
153
  return b.join("");
79
154
  }
80
- // escapeChar must be appended to a reserved name.
81
- // We choose '$' because it is invalid in proto identifiers.
82
- const escapeChar = "$";
83
- // Names that cannot be used for object properties.
84
- // See buf_es/protoc-gen-es/internal/protoplugin/names.go
85
- const rProp = {
155
+ // Names that cannot be used for identifiers, such as class names,
156
+ // but _can_ be used for object properties.
157
+ const reservedIdent = {
158
+ // ECMAScript 2015 keywords
159
+ break: true,
160
+ case: true,
161
+ catch: true,
162
+ class: true,
163
+ const: true,
164
+ continue: true,
165
+ debugger: true,
166
+ default: true,
167
+ delete: true,
168
+ do: true,
169
+ else: true,
170
+ export: true,
171
+ extends: true,
172
+ false: true,
173
+ finally: true,
174
+ for: true,
175
+ function: true,
176
+ if: true,
177
+ import: true,
178
+ in: true,
179
+ instanceof: true,
180
+ new: true,
181
+ null: true,
182
+ return: true,
183
+ super: true,
184
+ switch: true,
185
+ this: true,
186
+ throw: true,
187
+ true: true,
188
+ try: true,
189
+ typeof: true,
190
+ var: true,
191
+ void: true,
192
+ while: true,
193
+ with: true,
194
+ yield: true,
195
+ // ECMAScript 2015 future reserved keywords
196
+ enum: true,
197
+ implements: true,
198
+ interface: true,
199
+ let: true,
200
+ package: true,
201
+ private: true,
202
+ protected: true,
203
+ public: true,
204
+ static: true,
205
+ // Class name cannot be 'Object' when targeting ES5 with module CommonJS
206
+ Object: true,
207
+ // TypeScript keywords that cannot be used for types (as opposed to variables)
208
+ bigint: true,
209
+ number: true,
210
+ boolean: true,
211
+ string: true,
212
+ object: true,
213
+ // Identifiers reserved for the runtime, so we can generate legible code
214
+ globalThis: true,
215
+ Uint8Array: true,
216
+ Partial: true,
217
+ };
218
+ // Names that cannot be used for object properties because they are reserved
219
+ // by built-in JavaScript properties.
220
+ const reservedObjectProperties = {
86
221
  // names reserved by JavaScript
87
222
  constructor: true,
88
223
  toString: true,
89
224
  toJSON: true,
90
225
  valueOf: true,
226
+ };
227
+ // Names that cannot be used for object properties because they are reserved
228
+ // by the runtime.
229
+ const reservedMessageProperties = {
91
230
  // names reserved by the runtime
92
231
  getType: true,
93
232
  clone: true,
@@ -16,7 +16,7 @@ import { makeBinaryFormatProto2 } from "./private/binary-format-proto2.js";
16
16
  import { makeUtilCommon } from "./private/util-common.js";
17
17
  import { InternalFieldList } from "./private/field-list.js";
18
18
  import { InternalOneofInfo } from "./private/field.js";
19
- import { makeFieldName, makeJsonName } from "./private/names.js";
19
+ import { localFieldName, fieldJsonName } from "./private/names.js";
20
20
  import { makeJsonFormatProto2 } from "./private/json-format-proto2.js";
21
21
  /**
22
22
  * Provides functionality for messages defined with the proto2 syntax.
@@ -60,8 +60,8 @@ function normalizeFieldInfosProto2(fieldInfos) {
60
60
  ? fieldInfos()
61
61
  : fieldInfos) {
62
62
  const f = field;
63
- f.localName = makeFieldName(field.name, field.oneof !== undefined);
64
- f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : makeJsonName(field.name);
63
+ f.localName = localFieldName(field.name, field.oneof !== undefined);
64
+ f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : fieldJsonName(field.name);
65
65
  f.repeated = (_b = field.repeated) !== null && _b !== void 0 ? _b : false;
66
66
  // In contrast to proto3, repeated fields are unpacked except when explicitly specified.
67
67
  f.packed = (_c = field.packed) !== null && _c !== void 0 ? _c : false;
@@ -19,7 +19,7 @@ import { InternalFieldList } from "./private/field-list.js";
19
19
  import { scalarDefaultValue } from "./private/scalars.js";
20
20
  import { ScalarType } from "./field.js";
21
21
  import { InternalOneofInfo } from "./private/field.js";
22
- import { makeFieldName, makeJsonName } from "./private/names.js";
22
+ import { localFieldName, fieldJsonName } from "./private/names.js";
23
23
  /**
24
24
  * Provides functionality for messages defined with the proto3 syntax.
25
25
  */
@@ -64,8 +64,8 @@ function normalizeFieldInfosProto3(fieldInfos) {
64
64
  ? fieldInfos()
65
65
  : fieldInfos) {
66
66
  const f = field;
67
- f.localName = makeFieldName(field.name, field.oneof !== undefined);
68
- f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : makeJsonName(field.name);
67
+ f.localName = localFieldName(field.name, field.oneof !== undefined);
68
+ f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : fieldJsonName(field.name);
69
69
  f.repeated = (_b = field.repeated) !== null && _b !== void 0 ? _b : false;
70
70
  // From the proto3 language guide:
71
71
  // > In proto3, repeated fields of scalar numeric types are packed by default.
@@ -11,88 +11,4 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- /**
15
- * TypeRegistry is a simple registry for all message, enum, or service types.
16
- */
17
- export class TypeRegistry {
18
- constructor() {
19
- this.messages = {};
20
- this.enums = {};
21
- this.services = {};
22
- }
23
- /**
24
- * Find a message type by its protobuf type name.
25
- */
26
- findMessage(typeName) {
27
- return this.messages[typeName];
28
- }
29
- /**
30
- * Find an enum type by its protobuf type name.
31
- */
32
- findEnum(typeName) {
33
- return this.enums[typeName];
34
- }
35
- /**
36
- * Find a service type by its protobuf type name.
37
- */
38
- findService(typeName) {
39
- return this.services[typeName];
40
- }
41
- /**
42
- * Create a new TypeRegistry from the given types.
43
- */
44
- static from(...types) {
45
- const registry = new TypeRegistry();
46
- for (const type of types) {
47
- registry.add(type);
48
- }
49
- return registry;
50
- }
51
- /**
52
- * @deprecated use TypeRegistry.from()
53
- */
54
- static fromIterable(types) {
55
- return TypeRegistry.from(...types);
56
- }
57
- /**
58
- * @deprecated use TypeRegistry.from()
59
- */
60
- static fromTypes(...types) {
61
- return TypeRegistry.from(...types);
62
- }
63
- /**
64
- * Add a type to the registry. For messages, the types used in message
65
- * fields are added recursively. For services, the message types used
66
- * for requests and responses are added recursively.
67
- */
68
- add(type) {
69
- if ("fields" in type) {
70
- if (!this.findMessage(type.typeName)) {
71
- this.messages[type.typeName] = type;
72
- for (const field of type.fields.list()) {
73
- if (field.kind == "message") {
74
- this.add(field.T);
75
- }
76
- else if (field.kind == "map" && field.V.kind == "message") {
77
- this.add(field.V.T);
78
- }
79
- else if (field.kind == "enum") {
80
- this.add(field.T);
81
- }
82
- }
83
- }
84
- }
85
- else if ("methods" in type) {
86
- if (!this.findService(type.typeName)) {
87
- this.services[type.typeName] = type;
88
- for (const method of Object.values(type.methods)) {
89
- this.add(method.I);
90
- this.add(method.O);
91
- }
92
- }
93
- }
94
- else {
95
- this.enums[type.typeName] = type;
96
- }
97
- }
98
- }
14
+ export {};
@@ -0,0 +1,19 @@
1
+ import { localName } from "./private/names.js";
2
+ import { getUnwrappedFieldType } from "./private/field-wrapper.js";
3
+ import { scalarDefaultValue } from "./private/scalars.js";
4
+ interface CodegenInfo {
5
+ readonly packageName: string;
6
+ readonly localName: typeof localName;
7
+ readonly symbols: Record<RuntimeSymbolName, RuntimeSymbolInfo>;
8
+ readonly getUnwrappedFieldType: typeof getUnwrappedFieldType;
9
+ readonly wktSourceFiles: readonly string[];
10
+ readonly scalarDefaultValue: typeof scalarDefaultValue;
11
+ }
12
+ declare type RuntimeSymbolName = "proto2" | "proto3" | "Message" | "PartialMessage" | "PlainMessage" | "FieldList" | "MessageType" | "BinaryReadOptions" | "BinaryWriteOptions" | "JsonReadOptions" | "JsonWriteOptions" | "JsonValue" | "JsonObject" | "protoInt64" | "ScalarType" | "MethodKind" | "MethodIdempotency";
13
+ declare type RuntimeSymbolInfo = {
14
+ typeOnly: boolean;
15
+ publicImportPath: string;
16
+ privateImportPath: string;
17
+ };
18
+ export declare const codegenInfo: CodegenInfo;
19
+ export {};
@@ -0,0 +1,16 @@
1
+ import type { FieldDescriptorProto } from "./google/protobuf/descriptor_pb.js";
2
+ import { FileDescriptorProto, FileDescriptorSet } from "./google/protobuf/descriptor_pb.js";
3
+ import type { DescriptorSet } from "./descriptor-set.js";
4
+ /**
5
+ * Create a DescriptorSet, a convenient interface for working with a set of
6
+ * google.protobuf.FileDescriptorProto.
7
+ *
8
+ * Note that files must be given in topological order, so each file appears
9
+ * before any file that imports it. Protocol buffer compilers always produce
10
+ * files in topological order.
11
+ */
12
+ export declare function createDescriptorSet(input: FileDescriptorProto[] | FileDescriptorSet | Uint8Array): DescriptorSet;
13
+ /**
14
+ * Get the default `packed` state of a repeated field.
15
+ */
16
+ export declare function isPackedFieldByDefault(proto: FieldDescriptorProto, syntax: "proto2" | "proto3"): boolean;
@@ -0,0 +1,49 @@
1
+ import type { MessageType } from "./message-type.js";
2
+ import type { EnumType } from "./enum.js";
3
+ import type { IEnumTypeRegistry, IMessageTypeRegistry, IServiceTypeRegistry } from "./type-registry.js";
4
+ import type { ServiceType } from "./service-type.js";
5
+ import { FileDescriptorSet } from "./google/protobuf/descriptor_pb.js";
6
+ import type { DescriptorSet } from "./descriptor-set.js";
7
+ /**
8
+ * Create a registry from a set of descriptors. The types returned by this
9
+ * registry behave exactly like types from generated code.
10
+ *
11
+ * This function accepts google.protobuf.FileDescriptorSet in serialized or
12
+ * deserialized form. Alternatively, it also accepts a DescriptorSet (see
13
+ * createDescriptorSet()).
14
+ *
15
+ * By default, all well-known types with a specialized JSON representation
16
+ * are replaced with their generated counterpart in this package.
17
+ */
18
+ export declare function createRegistryFromDescriptors(input: DescriptorSet | FileDescriptorSet | Uint8Array, replaceWkt?: boolean): IMessageTypeRegistry & IEnumTypeRegistry & IServiceTypeRegistry;
19
+ /**
20
+ * DescriptorRegistry is a type registry that dynamically creates types
21
+ * from a set of google.protobuf.FileDescriptorProto.
22
+ *
23
+ * By default, all well-known types with a specialized JSON representation
24
+ * are replaced with their generated counterpart in this package.
25
+ */
26
+ export declare class DescriptorRegistry implements IMessageTypeRegistry, IEnumTypeRegistry, IServiceTypeRegistry {
27
+ private readonly desc;
28
+ private readonly enums;
29
+ private readonly messages;
30
+ private readonly services;
31
+ constructor(descriptorSet: DescriptorSet, replaceWkt?: boolean);
32
+ /**
33
+ * Conveniently create a DescriptorRegistry from a FileDescriptorSet,
34
+ * or a FileDescriptorSet in binary format.
35
+ */
36
+ static fromFileDescriptorSet(input: FileDescriptorSet | Uint8Array): DescriptorRegistry;
37
+ /**
38
+ * May raise an error on invalid descriptors.
39
+ */
40
+ findEnum(typeName: string): EnumType | undefined;
41
+ /**
42
+ * May raise an error on invalid descriptors.
43
+ */
44
+ findMessage(typeName: string): MessageType | undefined;
45
+ /**
46
+ * May raise an error on invalid descriptors.
47
+ */
48
+ findService(typeName: string): ServiceType | undefined;
49
+ }
@@ -0,0 +1,8 @@
1
+ import type { MessageType } from "./message-type.js";
2
+ import type { EnumType } from "./enum.js";
3
+ import type { ServiceType } from "./service-type.js";
4
+ import type { IMessageTypeRegistry, IEnumTypeRegistry, IServiceTypeRegistry } from "./type-registry.js";
5
+ /**
6
+ * Create a new registry from the given types. Note that
7
+ */
8
+ export declare function createRegistry(...types: Array<MessageType | EnumType | ServiceType>): IMessageTypeRegistry & IEnumTypeRegistry & IServiceTypeRegistry;