@bufbuild/protobuf 0.0.7 → 0.0.10

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 (55) 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 +380 -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 +375 -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 +3 -2
@@ -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}`);
@@ -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,
@@ -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.
@@ -13,90 +13,3 @@
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.TypeRegistry = void 0;
17
- /**
18
- * TypeRegistry is a simple registry for all message, enum, or service types.
19
- */
20
- class TypeRegistry {
21
- constructor() {
22
- this.messages = {};
23
- this.enums = {};
24
- this.services = {};
25
- }
26
- /**
27
- * Find a message type by its protobuf type name.
28
- */
29
- findMessage(typeName) {
30
- return this.messages[typeName];
31
- }
32
- /**
33
- * Find an enum type by its protobuf type name.
34
- */
35
- findEnum(typeName) {
36
- return this.enums[typeName];
37
- }
38
- /**
39
- * Find a service type by its protobuf type name.
40
- */
41
- findService(typeName) {
42
- return this.services[typeName];
43
- }
44
- /**
45
- * Create a new TypeRegistry from the given types.
46
- */
47
- static from(...types) {
48
- const registry = new TypeRegistry();
49
- for (const type of types) {
50
- registry.add(type);
51
- }
52
- return registry;
53
- }
54
- /**
55
- * @deprecated use TypeRegistry.from()
56
- */
57
- static fromIterable(types) {
58
- return TypeRegistry.from(...types);
59
- }
60
- /**
61
- * @deprecated use TypeRegistry.from()
62
- */
63
- static fromTypes(...types) {
64
- return TypeRegistry.from(...types);
65
- }
66
- /**
67
- * Add a type to the registry. For messages, the types used in message
68
- * fields are added recursively. For services, the message types used
69
- * for requests and responses are added recursively.
70
- */
71
- add(type) {
72
- if ("fields" in type) {
73
- if (!this.findMessage(type.typeName)) {
74
- this.messages[type.typeName] = type;
75
- for (const field of type.fields.list()) {
76
- if (field.kind == "message") {
77
- this.add(field.T);
78
- }
79
- else if (field.kind == "map" && field.V.kind == "message") {
80
- this.add(field.V.T);
81
- }
82
- else if (field.kind == "enum") {
83
- this.add(field.T);
84
- }
85
- }
86
- }
87
- }
88
- else if ("methods" in type) {
89
- if (!this.findService(type.typeName)) {
90
- this.services[type.typeName] = type;
91
- for (const method of Object.values(type.methods)) {
92
- this.add(method.I);
93
- this.add(method.O);
94
- }
95
- }
96
- }
97
- else {
98
- this.enums[type.typeName] = type;
99
- }
100
- }
101
- }
102
- exports.TypeRegistry = TypeRegistry;
@@ -0,0 +1,57 @@
1
+ // Copyright 2021-2022 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { localName } from "./private/names.js";
15
+ import { getUnwrappedFieldType } from "./private/field-wrapper.js";
16
+ import { scalarDefaultValue } from "./private/scalars.js";
17
+ const packageName = "@bufbuild/protobuf";
18
+ export const codegenInfo = {
19
+ packageName,
20
+ localName,
21
+ getUnwrappedFieldType,
22
+ scalarDefaultValue,
23
+ // prettier-ignore
24
+ symbols: {
25
+ proto2: { typeOnly: false, privateImportPath: "./proto2.js", publicImportPath: packageName },
26
+ proto3: { typeOnly: false, privateImportPath: "./proto3.js", publicImportPath: packageName },
27
+ Message: { typeOnly: false, privateImportPath: "./message.js", publicImportPath: packageName },
28
+ PartialMessage: { typeOnly: true, privateImportPath: "./message.js", publicImportPath: packageName },
29
+ PlainMessage: { typeOnly: true, privateImportPath: "./message.js", publicImportPath: packageName },
30
+ FieldList: { typeOnly: true, privateImportPath: "./field-list.js", publicImportPath: packageName },
31
+ MessageType: { typeOnly: true, privateImportPath: "./message-type.js", publicImportPath: packageName },
32
+ BinaryReadOptions: { typeOnly: true, privateImportPath: "./binary-format.js", publicImportPath: packageName },
33
+ BinaryWriteOptions: { typeOnly: true, privateImportPath: "./binary-format.js", publicImportPath: packageName },
34
+ JsonReadOptions: { typeOnly: true, privateImportPath: "./json-format.js", publicImportPath: packageName },
35
+ JsonWriteOptions: { typeOnly: true, privateImportPath: "./json-format.js", publicImportPath: packageName },
36
+ JsonValue: { typeOnly: true, privateImportPath: "./json-format.js", publicImportPath: packageName },
37
+ JsonObject: { typeOnly: true, privateImportPath: "./json-format.js", publicImportPath: packageName },
38
+ protoInt64: { typeOnly: false, privateImportPath: "./proto-int64.js", publicImportPath: packageName },
39
+ ScalarType: { typeOnly: false, privateImportPath: "./field.js", publicImportPath: packageName },
40
+ MethodKind: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
41
+ MethodIdempotency: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
42
+ },
43
+ wktSourceFiles: [
44
+ "google/protobuf/compiler/plugin.proto",
45
+ "google/protobuf/any.proto",
46
+ "google/protobuf/api.proto",
47
+ "google/protobuf/descriptor.proto",
48
+ "google/protobuf/duration.proto",
49
+ "google/protobuf/empty.proto",
50
+ "google/protobuf/field_mask.proto",
51
+ "google/protobuf/source_context.proto",
52
+ "google/protobuf/struct.proto",
53
+ "google/protobuf/timestamp.proto",
54
+ "google/protobuf/type.proto",
55
+ "google/protobuf/wrappers.proto",
56
+ ],
57
+ };