@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
@@ -19,13 +19,11 @@ 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
  */
26
- export const proto3 = makeProtoRuntime("proto3", makeJsonFormatProto3(), makeBinaryFormatProto3(), {
27
- ...makeUtilCommon(),
28
- newFieldList(fields) {
26
+ export const proto3 = makeProtoRuntime("proto3", makeJsonFormatProto3(), makeBinaryFormatProto3(), Object.assign(Object.assign({}, makeUtilCommon()), { newFieldList(fields) {
29
27
  return new InternalFieldList(fields, normalizeFieldInfosProto3);
30
28
  },
31
29
  initFields(target) {
@@ -56,30 +54,29 @@ export const proto3 = makeProtoRuntime("proto3", makeJsonFormatProto3(), makeBin
56
54
  break;
57
55
  }
58
56
  }
59
- },
60
- });
57
+ } }));
61
58
  /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
62
59
  function normalizeFieldInfosProto3(fieldInfos) {
60
+ var _a, _b, _c;
63
61
  const r = [];
64
62
  let o;
65
63
  for (const field of typeof fieldInfos == "function"
66
64
  ? fieldInfos()
67
65
  : fieldInfos) {
68
66
  const f = field;
69
- f.localName = makeFieldName(field.name, field.oneof !== undefined);
70
- f.jsonName = field.jsonName ?? makeJsonName(field.name);
71
- f.repeated = field.repeated ?? false;
67
+ f.localName = localFieldName(field.name, field.oneof !== undefined);
68
+ f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : fieldJsonName(field.name);
69
+ f.repeated = (_b = field.repeated) !== null && _b !== void 0 ? _b : false;
72
70
  // From the proto3 language guide:
73
71
  // > In proto3, repeated fields of scalar numeric types are packed by default.
74
72
  // This information is incomplete - according to the conformance tests, BOOL
75
73
  // and ENUM are packed by default as well. This means only STRING and BYTES
76
74
  // are not packed by default, which makes sense because they are length-delimited.
77
75
  f.packed =
78
- field.packed ??
79
- (field.kind == "enum" ||
80
- (field.kind == "scalar" &&
81
- field.T != ScalarType.BYTES &&
82
- field.T != ScalarType.STRING));
76
+ (_c = field.packed) !== null && _c !== void 0 ? _c : (field.kind == "enum" ||
77
+ (field.kind == "scalar" &&
78
+ field.T != ScalarType.BYTES &&
79
+ field.T != ScalarType.STRING));
83
80
  // We do not surface options at this time
84
81
  // f.options = field.options ?? emptyReadonlyObject;
85
82
  if (field.oneof !== undefined) {
@@ -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;