@bufbuild/protobuf 0.0.1-alpha.1 → 0.0.1

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 (127) hide show
  1. package/README.md +21 -1
  2. package/dist/cjs/binary-encoding.js +402 -410
  3. package/dist/cjs/descriptor-registry.js +407 -449
  4. package/dist/cjs/descriptor-set.js +416 -513
  5. package/dist/cjs/field.js +30 -30
  6. package/dist/cjs/google/protobuf/any_pb.js +122 -140
  7. package/dist/cjs/google/protobuf/api_pb.js +177 -224
  8. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +125 -227
  9. package/dist/cjs/google/protobuf/descriptor_pb.js +1029 -1888
  10. package/dist/cjs/google/protobuf/duration_pb.js +72 -88
  11. package/dist/cjs/google/protobuf/empty_pb.js +17 -17
  12. package/dist/cjs/google/protobuf/field_mask_pb.js +77 -94
  13. package/dist/cjs/google/protobuf/source_context_pb.js +25 -25
  14. package/dist/cjs/google/protobuf/struct_pb.js +144 -181
  15. package/dist/cjs/google/protobuf/timestamp_pb.js +86 -123
  16. package/dist/cjs/google/protobuf/type_pb.js +406 -462
  17. package/dist/cjs/google/protobuf/wrappers_pb.js +407 -470
  18. package/dist/cjs/index.js +24 -122
  19. package/dist/cjs/message.js +81 -89
  20. package/dist/cjs/private/assert.js +20 -26
  21. package/dist/cjs/private/base64.js +79 -78
  22. package/dist/cjs/private/binary-format-common.js +195 -208
  23. package/dist/cjs/private/binary-format-proto2.js +75 -109
  24. package/dist/cjs/private/binary-format-proto3.js +57 -90
  25. package/dist/cjs/private/enum.js +38 -43
  26. package/dist/cjs/private/field-list.js +51 -50
  27. package/dist/cjs/private/field-wrapper.js +8 -10
  28. package/dist/cjs/private/field.js +22 -25
  29. package/dist/cjs/private/goog-varint.js +275 -0
  30. package/dist/cjs/private/json-format-common.js +414 -440
  31. package/dist/cjs/private/json-format-proto2.js +78 -89
  32. package/dist/cjs/private/json-format-proto3.js +69 -93
  33. package/dist/cjs/private/message-type.js +26 -27
  34. package/dist/cjs/private/names.js +58 -62
  35. package/dist/cjs/private/proto-runtime.js +11 -16
  36. package/dist/cjs/private/scalars.js +100 -103
  37. package/dist/cjs/private/util-common.js +180 -210
  38. package/dist/cjs/proto-int64.js +111 -112
  39. package/dist/cjs/proto2.js +50 -63
  40. package/dist/cjs/proto3.js +61 -74
  41. package/dist/cjs/service-type.js +14 -17
  42. package/dist/cjs/type-registry.js +33 -31
  43. package/dist/esm/binary-encoding.js +401 -414
  44. package/dist/esm/descriptor-registry.js +409 -458
  45. package/dist/esm/descriptor-set.js +417 -478
  46. package/dist/esm/field.js +29 -29
  47. package/dist/esm/google/protobuf/any_pb.js +122 -140
  48. package/dist/esm/google/protobuf/api_pb.js +177 -197
  49. package/dist/esm/google/protobuf/compiler/plugin_pb.js +123 -216
  50. package/dist/esm/google/protobuf/descriptor_pb.js +1013 -1804
  51. package/dist/esm/google/protobuf/duration_pb.js +72 -88
  52. package/dist/esm/google/protobuf/empty_pb.js +17 -17
  53. package/dist/esm/google/protobuf/field_mask_pb.js +77 -94
  54. package/dist/esm/google/protobuf/source_context_pb.js +25 -25
  55. package/dist/esm/google/protobuf/struct_pb.js +143 -180
  56. package/dist/esm/google/protobuf/timestamp_pb.js +86 -123
  57. package/dist/esm/google/protobuf/type_pb.js +401 -421
  58. package/dist/esm/google/protobuf/wrappers_pb.js +406 -397
  59. package/dist/esm/index.js +1 -1
  60. package/dist/esm/message.js +81 -89
  61. package/dist/esm/private/assert.js +19 -21
  62. package/dist/esm/private/base64.js +79 -78
  63. package/dist/esm/private/binary-format-common.js +196 -207
  64. package/dist/esm/private/binary-format-proto2.js +76 -81
  65. package/dist/esm/private/binary-format-proto3.js +58 -67
  66. package/dist/esm/private/enum.js +38 -43
  67. package/dist/esm/private/field-list.js +51 -50
  68. package/dist/esm/private/field-wrapper.js +8 -10
  69. package/dist/esm/private/field.js +22 -22
  70. package/dist/esm/private/goog-varint.js +266 -0
  71. package/dist/esm/private/json-format-common.js +414 -435
  72. package/dist/esm/private/json-format-proto2.js +76 -81
  73. package/dist/esm/private/json-format-proto3.js +66 -80
  74. package/dist/esm/private/message-type.js +27 -28
  75. package/dist/esm/private/names.js +57 -57
  76. package/dist/esm/private/proto-runtime.js +11 -11
  77. package/dist/esm/private/scalars.js +99 -99
  78. package/dist/esm/private/util-common.js +180 -192
  79. package/dist/esm/proto-int64.js +111 -112
  80. package/dist/esm/proto2.js +50 -57
  81. package/dist/esm/proto3.js +61 -68
  82. package/dist/esm/service-type.js +12 -12
  83. package/dist/esm/type-registry.js +33 -31
  84. package/dist/types/binary-encoding.d.ts +398 -398
  85. package/dist/types/binary-format.d.ts +69 -91
  86. package/dist/types/descriptor-registry.d.ts +23 -29
  87. package/dist/types/descriptor-set.d.ts +107 -128
  88. package/dist/types/enum.d.ts +21 -21
  89. package/dist/types/field-list.d.ts +21 -21
  90. package/dist/types/field.d.ts +191 -231
  91. package/dist/types/google/protobuf/any_pb.d.ts +56 -77
  92. package/dist/types/google/protobuf/api_pb.d.ts +144 -187
  93. package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +195 -257
  94. package/dist/types/google/protobuf/descriptor_pb.d.ts +1360 -1737
  95. package/dist/types/google/protobuf/duration_pb.d.ts +30 -50
  96. package/dist/types/google/protobuf/empty_pb.d.ts +9 -28
  97. package/dist/types/google/protobuf/field_mask_pb.d.ts +17 -37
  98. package/dist/types/google/protobuf/source_context_pb.d.ts +16 -35
  99. package/dist/types/google/protobuf/struct_pb.d.ts +64 -115
  100. package/dist/types/google/protobuf/timestamp_pb.d.ts +31 -51
  101. package/dist/types/google/protobuf/type_pb.d.ts +355 -422
  102. package/dist/types/google/protobuf/wrappers_pb.d.ts +181 -297
  103. package/dist/types/index.d.ts +4 -27
  104. package/dist/types/json-format.d.ts +70 -89
  105. package/dist/types/message-type.d.ts +38 -46
  106. package/dist/types/message.d.ts +73 -99
  107. package/dist/types/private/assert.d.ts +1 -4
  108. package/dist/types/private/binary-format-common.d.ts +6 -32
  109. package/dist/types/private/enum.d.ts +4 -11
  110. package/dist/types/private/field-list.d.ts +14 -21
  111. package/dist/types/private/field-wrapper.d.ts +5 -11
  112. package/dist/types/private/field.d.ts +12 -12
  113. package/dist/types/{google/varint.d.ts → private/goog-varint.d.ts} +6 -13
  114. package/dist/types/private/json-format-common.d.ts +5 -27
  115. package/dist/types/private/message-type.d.ts +3 -8
  116. package/dist/types/private/names.d.ts +1 -4
  117. package/dist/types/private/options-map.d.ts +1 -1
  118. package/dist/types/private/proto-runtime.d.ts +29 -42
  119. package/dist/types/private/scalars.d.ts +5 -12
  120. package/dist/types/private/util-common.d.ts +1 -4
  121. package/dist/types/private/util.d.ts +29 -41
  122. package/dist/types/proto-int64.d.ts +42 -42
  123. package/dist/types/service-type.d.ts +37 -57
  124. package/dist/types/type-registry.d.ts +13 -15
  125. package/package.json +2 -2
  126. package/dist/cjs/google/varint.js +0 -280
  127. package/dist/esm/google/varint.js +0 -265
@@ -1,8 +1,4 @@
1
- import {
2
- FieldDescriptorProto_Label,
3
- FieldDescriptorProto_Type,
4
- MethodOptions_IdempotencyLevel,
5
- } from "./google/protobuf/descriptor_pb.js";
1
+ import { FieldDescriptorProto_Label, FieldDescriptorProto_Type, MethodOptions_IdempotencyLevel, } from "./google/protobuf/descriptor_pb.js";
6
2
  import { assert } from "./private/assert.js";
7
3
  import { ScalarType } from "./field.js";
8
4
  import { MethodIdempotency, MethodKind } from "./service-type.js";
@@ -14,504 +10,447 @@ import { MethodIdempotency, MethodKind } from "./service-type.js";
14
10
  * with a leading dot.
15
11
  */
16
12
  export class DescriptorSet {
17
- constructor() {
18
- this.enums = {};
19
- this.messages = {};
20
- this.services = {};
21
- }
22
- /**
23
- * May raise an error on invalid descriptors.
24
- */
25
- add(...files) {
26
- for (const file of files) {
27
- newFile(file, this);
28
- }
29
- }
30
- listEnums() {
31
- return Object.values(this.enums).filter(isDefined);
32
- }
33
- listMessages() {
34
- return Object.values(this.messages).filter(isDefined);
35
- }
36
- listServices() {
37
- return Object.values(this.services).filter(isDefined);
38
- }
13
+ constructor() {
14
+ this.enums = {};
15
+ this.messages = {};
16
+ this.services = {};
17
+ }
18
+ /**
19
+ * May raise an error on invalid descriptors.
20
+ */
21
+ add(...files) {
22
+ for (const file of files) {
23
+ newFile(file, this);
24
+ }
25
+ }
26
+ listEnums() {
27
+ return Object.values(this.enums).filter(isDefined);
28
+ }
29
+ listMessages() {
30
+ return Object.values(this.messages).filter(isDefined);
31
+ }
32
+ listServices() {
33
+ return Object.values(this.services).filter(isDefined);
34
+ }
39
35
  }
40
36
  function isDefined(v) {
41
- return v !== undefined;
37
+ return v !== undefined;
42
38
  }
43
39
  const fieldTypeToScalarType = {
44
- [FieldDescriptorProto_Type.DOUBLE]: ScalarType.DOUBLE,
45
- [FieldDescriptorProto_Type.FLOAT]: ScalarType.FLOAT,
46
- [FieldDescriptorProto_Type.INT64]: ScalarType.INT64,
47
- [FieldDescriptorProto_Type.UINT64]: ScalarType.UINT64,
48
- [FieldDescriptorProto_Type.INT32]: ScalarType.INT32,
49
- [FieldDescriptorProto_Type.FIXED64]: ScalarType.FIXED64,
50
- [FieldDescriptorProto_Type.FIXED32]: ScalarType.FIXED32,
51
- [FieldDescriptorProto_Type.BOOL]: ScalarType.BOOL,
52
- [FieldDescriptorProto_Type.STRING]: ScalarType.STRING,
53
- [FieldDescriptorProto_Type.GROUP]: undefined,
54
- [FieldDescriptorProto_Type.MESSAGE]: undefined,
55
- [FieldDescriptorProto_Type.BYTES]: ScalarType.BYTES,
56
- [FieldDescriptorProto_Type.UINT32]: ScalarType.UINT32,
57
- [FieldDescriptorProto_Type.ENUM]: undefined,
58
- [FieldDescriptorProto_Type.SFIXED32]: ScalarType.SFIXED32,
59
- [FieldDescriptorProto_Type.SFIXED64]: ScalarType.SFIXED64,
60
- [FieldDescriptorProto_Type.SINT32]: ScalarType.SINT32,
61
- [FieldDescriptorProto_Type.SINT64]: ScalarType.SINT64,
40
+ [FieldDescriptorProto_Type.DOUBLE]: ScalarType.DOUBLE,
41
+ [FieldDescriptorProto_Type.FLOAT]: ScalarType.FLOAT,
42
+ [FieldDescriptorProto_Type.INT64]: ScalarType.INT64,
43
+ [FieldDescriptorProto_Type.UINT64]: ScalarType.UINT64,
44
+ [FieldDescriptorProto_Type.INT32]: ScalarType.INT32,
45
+ [FieldDescriptorProto_Type.FIXED64]: ScalarType.FIXED64,
46
+ [FieldDescriptorProto_Type.FIXED32]: ScalarType.FIXED32,
47
+ [FieldDescriptorProto_Type.BOOL]: ScalarType.BOOL,
48
+ [FieldDescriptorProto_Type.STRING]: ScalarType.STRING,
49
+ [FieldDescriptorProto_Type.GROUP]: undefined,
50
+ [FieldDescriptorProto_Type.MESSAGE]: undefined,
51
+ [FieldDescriptorProto_Type.BYTES]: ScalarType.BYTES,
52
+ [FieldDescriptorProto_Type.UINT32]: ScalarType.UINT32,
53
+ [FieldDescriptorProto_Type.ENUM]: undefined,
54
+ [FieldDescriptorProto_Type.SFIXED32]: ScalarType.SFIXED32,
55
+ [FieldDescriptorProto_Type.SFIXED64]: ScalarType.SFIXED64,
56
+ [FieldDescriptorProto_Type.SINT32]: ScalarType.SINT32,
57
+ [FieldDescriptorProto_Type.SINT64]: ScalarType.SINT64,
62
58
  };
63
59
  function newFile(proto, ds) {
64
- assert(proto.name, `missing file descriptor name`);
65
- if (proto.syntax !== undefined && proto.syntax !== "proto3") {
66
- throw new Error(`invalid descriptor: unsupported syntax: ${proto.syntax}`);
67
- }
68
- const file = {
69
- proto,
70
- syntax: proto.syntax === "proto3" ? "proto3" : "proto2",
71
- name: proto.name.endsWith(".proto")
72
- ? proto.name.substring(-".proto".length)
73
- : proto.name,
74
- toString() {
75
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
76
- return `file ${this.proto.name}`;
77
- },
78
- };
79
- for (const messageType of proto.messageType) {
80
- newMessage(messageType, undefined, file, ds);
81
- }
82
- for (const enumType of proto.enumType) {
83
- newEnum(enumType, undefined, file, ds);
84
- }
85
- for (const extension of proto.extension) {
86
- newExtension(extension, undefined, file, ds);
87
- }
88
- for (const service of proto.service) {
89
- newService(service, file, ds);
90
- }
91
- return file;
60
+ assert(proto.name, `missing file descriptor name`);
61
+ if (proto.syntax !== undefined && proto.syntax !== "proto3") {
62
+ throw new Error(`invalid descriptor: unsupported syntax: ${proto.syntax}`);
63
+ }
64
+ const file = {
65
+ proto,
66
+ syntax: proto.syntax === "proto3" ? "proto3" : "proto2",
67
+ name: proto.name.endsWith(".proto")
68
+ ? proto.name.substring(-".proto".length)
69
+ : proto.name,
70
+ toString() {
71
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
72
+ return `file ${this.proto.name}`;
73
+ },
74
+ };
75
+ for (const messageType of proto.messageType) {
76
+ newMessage(messageType, undefined, file, ds);
77
+ }
78
+ for (const enumType of proto.enumType) {
79
+ newEnum(enumType, undefined, file, ds);
80
+ }
81
+ for (const extension of proto.extension) {
82
+ newExtension(extension, undefined, file, ds);
83
+ }
84
+ for (const service of proto.service) {
85
+ newService(service, file, ds);
86
+ }
87
+ return file;
92
88
  }
93
89
  function newEnum(proto, parent, file, us) {
94
- assert(proto.name, `invalid descriptor: missing enum descriptor name`);
95
- let protoTypeName;
96
- if (parent) {
97
- protoTypeName = `${parent.protoTypeName}.${proto.name}`;
98
- } else if (file.proto.package !== undefined) {
99
- protoTypeName = `.${file.proto.package}.${proto.name}`;
100
- } else {
101
- protoTypeName = `.${proto.name}`;
102
- }
103
- const values = [];
104
- const enumT = {
105
- proto,
106
- file,
107
- parent,
108
- name: proto.name,
109
- protoTypeName,
110
- typeName: protoTypeName.startsWith(".")
111
- ? protoTypeName.substring(1)
112
- : protoTypeName,
113
- values,
114
- toString() {
115
- return `enum ${this.typeName}`;
116
- },
117
- };
118
- us.enums[enumT.protoTypeName] = enumT;
119
- for (const value of proto.value) {
120
- values.push(newEnumValue(value, enumT));
121
- }
122
- return enumT;
90
+ assert(proto.name, `invalid descriptor: missing enum descriptor name`);
91
+ let protoTypeName;
92
+ if (parent) {
93
+ protoTypeName = `${parent.protoTypeName}.${proto.name}`;
94
+ }
95
+ else if (file.proto.package !== undefined) {
96
+ protoTypeName = `.${file.proto.package}.${proto.name}`;
97
+ }
98
+ else {
99
+ protoTypeName = `.${proto.name}`;
100
+ }
101
+ const values = [];
102
+ const enumT = {
103
+ proto,
104
+ file,
105
+ parent,
106
+ name: proto.name,
107
+ protoTypeName,
108
+ typeName: protoTypeName.startsWith(".")
109
+ ? protoTypeName.substring(1)
110
+ : protoTypeName,
111
+ values,
112
+ toString() {
113
+ return `enum ${this.typeName}`;
114
+ },
115
+ };
116
+ us.enums[enumT.protoTypeName] = enumT;
117
+ for (const value of proto.value) {
118
+ values.push(newEnumValue(value, enumT));
119
+ }
120
+ return enumT;
123
121
  }
124
122
  function newEnumValue(proto, parent) {
125
- assert(proto.name, `invalid descriptor: missing enum value descriptor name`);
126
- assert(
127
- proto.number !== undefined,
128
- `invalid descriptor: missing enum value descriptor number`
129
- );
130
- return {
131
- proto,
132
- name: proto.name,
133
- number: proto.number,
134
- parent,
135
- toString() {
136
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
137
- return `enum value ${this.parent.typeName}.${this.proto.name}`;
138
- },
139
- };
123
+ assert(proto.name, `invalid descriptor: missing enum value descriptor name`);
124
+ assert(proto.number !== undefined, `invalid descriptor: missing enum value descriptor number`);
125
+ return {
126
+ proto,
127
+ name: proto.name,
128
+ number: proto.number,
129
+ parent,
130
+ toString() {
131
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
132
+ return `enum value ${this.parent.typeName}.${this.proto.name}`;
133
+ },
134
+ };
140
135
  }
141
136
  function newMessage(proto, parent, file, us) {
142
- assert(proto.name, `invalid descriptor: missing name`);
143
- const nestedMessages = [];
144
- const fields = [];
145
- const oneofs = [];
146
- const nestedEnums = [];
147
- const nestedExtensions = [];
148
- let protoTypeName;
149
- if (parent) {
150
- protoTypeName = `${parent.protoTypeName}.${proto.name}`;
151
- } else if (file.proto.package !== undefined) {
152
- protoTypeName = `.${file.proto.package}.${proto.name}`;
153
- } else {
154
- protoTypeName = `.${proto.name}`;
155
- }
156
- const message = {
157
- proto,
158
- file,
159
- parent,
160
- name: proto.name,
161
- typeName: protoTypeName.startsWith(".")
162
- ? protoTypeName.substring(1)
163
- : protoTypeName,
164
- protoTypeName,
165
- fields,
166
- oneofs,
167
- nestedEnums,
168
- nestedMessages,
169
- nestedExtensions,
170
- toString() {
171
- return `message ${this.typeName}`;
172
- },
173
- };
174
- us.messages[message.protoTypeName] = message;
175
- for (const nestedType of proto.nestedType) {
176
- nestedMessages.push(newMessage(nestedType, message, file, us));
177
- }
178
- for (const oneofDecl of proto.oneofDecl) {
179
- oneofs.push(newOneof(oneofDecl, message, file));
180
- }
181
- for (const field of proto.field) {
182
- let oneof = undefined;
183
- if (field.oneofIndex !== undefined) {
184
- oneof = oneofs[field.oneofIndex];
185
- assert(
186
- oneof,
187
- `invalid descriptor: oneof declaration index ${
188
- field.oneofIndex
189
- } specified by field #${field.number ?? -1} not found`
190
- );
191
- }
192
- fields.push(newField(field, message, oneof));
193
- }
194
- for (const enumType of proto.enumType) {
195
- nestedEnums.push(newEnum(enumType, message, file, us));
196
- }
197
- for (const extension of proto.extension) {
198
- nestedExtensions.push(newExtension(extension, message, file, us));
199
- }
200
- return message;
137
+ assert(proto.name, `invalid descriptor: missing name`);
138
+ const nestedMessages = [];
139
+ const fields = [];
140
+ const oneofs = [];
141
+ const nestedEnums = [];
142
+ const nestedExtensions = [];
143
+ let protoTypeName;
144
+ if (parent) {
145
+ protoTypeName = `${parent.protoTypeName}.${proto.name}`;
146
+ }
147
+ else if (file.proto.package !== undefined) {
148
+ protoTypeName = `.${file.proto.package}.${proto.name}`;
149
+ }
150
+ else {
151
+ protoTypeName = `.${proto.name}`;
152
+ }
153
+ const message = {
154
+ proto,
155
+ file,
156
+ parent,
157
+ name: proto.name,
158
+ typeName: protoTypeName.startsWith(".")
159
+ ? protoTypeName.substring(1)
160
+ : protoTypeName,
161
+ protoTypeName,
162
+ fields,
163
+ oneofs,
164
+ nestedEnums,
165
+ nestedMessages,
166
+ nestedExtensions,
167
+ toString() {
168
+ return `message ${this.typeName}`;
169
+ },
170
+ };
171
+ us.messages[message.protoTypeName] = message;
172
+ for (const nestedType of proto.nestedType) {
173
+ nestedMessages.push(newMessage(nestedType, message, file, us));
174
+ }
175
+ for (const oneofDecl of proto.oneofDecl) {
176
+ oneofs.push(newOneof(oneofDecl, message, file));
177
+ }
178
+ for (const field of proto.field) {
179
+ let oneof = undefined;
180
+ if (field.oneofIndex !== undefined) {
181
+ oneof = oneofs[field.oneofIndex];
182
+ assert(oneof, `invalid descriptor: oneof declaration index ${field.oneofIndex} specified by field #${field.number ?? -1} not found`);
183
+ }
184
+ fields.push(newField(field, message, oneof));
185
+ }
186
+ for (const enumType of proto.enumType) {
187
+ nestedEnums.push(newEnum(enumType, message, file, us));
188
+ }
189
+ for (const extension of proto.extension) {
190
+ nestedExtensions.push(newExtension(extension, message, file, us));
191
+ }
192
+ return message;
201
193
  }
202
194
  function newField(proto, parent, oneof) {
203
- assert(proto.name, `invalid descriptor: missing name`);
204
- assert(proto.number, `invalid descriptor: missing number`);
205
- assert(proto.type, `invalid descriptor: missing type`);
206
- let optional = false;
207
- let packed = proto.options?.packed === true;
208
- switch (parent.file.syntax) {
209
- case "proto2":
210
- optional =
211
- oneof === undefined &&
212
- proto.label === FieldDescriptorProto_Label.OPTIONAL;
213
- break;
214
- case "proto3":
215
- optional = proto.proto3Optional === true;
216
- switch (proto.type) {
217
- case FieldDescriptorProto_Type.DOUBLE:
218
- case FieldDescriptorProto_Type.FLOAT:
219
- case FieldDescriptorProto_Type.INT64:
220
- case FieldDescriptorProto_Type.UINT64:
221
- case FieldDescriptorProto_Type.INT32:
222
- case FieldDescriptorProto_Type.FIXED64:
223
- case FieldDescriptorProto_Type.FIXED32:
224
- case FieldDescriptorProto_Type.UINT32:
225
- case FieldDescriptorProto_Type.SFIXED32:
226
- case FieldDescriptorProto_Type.SFIXED64:
227
- case FieldDescriptorProto_Type.SINT32:
228
- case FieldDescriptorProto_Type.SINT64:
229
- case FieldDescriptorProto_Type.BOOL:
230
- case FieldDescriptorProto_Type.ENUM:
231
- // From the proto3 language guide:
232
- // > In proto3, repeated fields of scalar numeric types are packed by default.
233
- // This information is incomplete - according to the conformance tests, BOOL
234
- // and ENUM are packed by default as well. This means only STRING and BYTES
235
- // are not packed by default, which makes sense because they are length-delimited.
236
- packed = true;
237
- break;
238
- case FieldDescriptorProto_Type.STRING:
239
- case FieldDescriptorProto_Type.GROUP:
240
- case FieldDescriptorProto_Type.MESSAGE:
241
- case FieldDescriptorProto_Type.BYTES:
242
- assert(
243
- !packed,
244
- `invalid descriptor: ${
245
- FieldDescriptorProto_Type[proto.type]
246
- } cannot be packed`
247
- );
248
- break;
249
- }
250
- break;
251
- }
252
- const field = {
253
- proto,
254
- name: proto.name,
255
- number: proto.number,
256
- type: proto.type,
257
- parent,
258
- oneof: proto.proto3Optional === true ? undefined : oneof,
259
- optional,
260
- packed,
261
- toString() {
262
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
263
- return `field ${parent.typeName}.${proto.name}`;
264
- },
265
- resolve(us) {
266
- return resolveField(this, us);
267
- },
268
- };
269
- if (oneof) {
270
- oneof.fields.push(field);
271
- }
272
- return field;
195
+ assert(proto.name, `invalid descriptor: missing name`);
196
+ assert(proto.number, `invalid descriptor: missing number`);
197
+ assert(proto.type, `invalid descriptor: missing type`);
198
+ let optional = false;
199
+ let packed = proto.options?.packed === true;
200
+ switch (parent.file.syntax) {
201
+ case "proto2":
202
+ optional =
203
+ oneof === undefined &&
204
+ proto.label === FieldDescriptorProto_Label.OPTIONAL;
205
+ break;
206
+ case "proto3":
207
+ optional = proto.proto3Optional === true;
208
+ switch (proto.type) {
209
+ case FieldDescriptorProto_Type.DOUBLE:
210
+ case FieldDescriptorProto_Type.FLOAT:
211
+ case FieldDescriptorProto_Type.INT64:
212
+ case FieldDescriptorProto_Type.UINT64:
213
+ case FieldDescriptorProto_Type.INT32:
214
+ case FieldDescriptorProto_Type.FIXED64:
215
+ case FieldDescriptorProto_Type.FIXED32:
216
+ case FieldDescriptorProto_Type.UINT32:
217
+ case FieldDescriptorProto_Type.SFIXED32:
218
+ case FieldDescriptorProto_Type.SFIXED64:
219
+ case FieldDescriptorProto_Type.SINT32:
220
+ case FieldDescriptorProto_Type.SINT64:
221
+ case FieldDescriptorProto_Type.BOOL:
222
+ case FieldDescriptorProto_Type.ENUM:
223
+ // From the proto3 language guide:
224
+ // > In proto3, repeated fields of scalar numeric types are packed by default.
225
+ // This information is incomplete - according to the conformance tests, BOOL
226
+ // and ENUM are packed by default as well. This means only STRING and BYTES
227
+ // are not packed by default, which makes sense because they are length-delimited.
228
+ packed = true;
229
+ break;
230
+ case FieldDescriptorProto_Type.STRING:
231
+ case FieldDescriptorProto_Type.GROUP:
232
+ case FieldDescriptorProto_Type.MESSAGE:
233
+ case FieldDescriptorProto_Type.BYTES:
234
+ assert(!packed, `invalid descriptor: ${FieldDescriptorProto_Type[proto.type]} cannot be packed`);
235
+ break;
236
+ }
237
+ break;
238
+ }
239
+ const field = {
240
+ proto,
241
+ name: proto.name,
242
+ number: proto.number,
243
+ type: proto.type,
244
+ parent,
245
+ oneof: proto.proto3Optional === true ? undefined : oneof,
246
+ optional,
247
+ packed,
248
+ toString() {
249
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
250
+ return `field ${parent.typeName}.${proto.name}`;
251
+ },
252
+ resolve(us) {
253
+ return resolveField(this, us);
254
+ },
255
+ };
256
+ if (oneof) {
257
+ oneof.fields.push(field);
258
+ }
259
+ return field;
273
260
  }
274
261
  function resolveField(u, us) {
275
- const repeated = u.proto.label === FieldDescriptorProto_Label.REPEATED;
276
- switch (u.type) {
277
- case FieldDescriptorProto_Type.MESSAGE:
278
- case FieldDescriptorProto_Type.GROUP: {
279
- assert(
280
- u.proto.typeName,
281
- `invalid descriptor: ${u.toString()} has type ${
282
- FieldDescriptorProto_Type[u.type]
283
- }, but type_name is unset`
284
- );
285
- const refMessage = us.messages[u.proto.typeName];
286
- assert(
287
- refMessage,
288
- `invalid descriptor: cannot find type_name "${
289
- u.proto.typeName
290
- }" specified by ${u.toString()}`
291
- );
292
- if (refMessage.proto.options?.mapEntry !== undefined) {
262
+ const repeated = u.proto.label === FieldDescriptorProto_Label.REPEATED;
263
+ switch (u.type) {
264
+ case FieldDescriptorProto_Type.MESSAGE:
265
+ case FieldDescriptorProto_Type.GROUP: {
266
+ assert(u.proto.typeName, `invalid descriptor: ${u.toString()} has type ${FieldDescriptorProto_Type[u.type]}, but type_name is unset`);
267
+ const refMessage = us.messages[u.proto.typeName];
268
+ assert(refMessage, `invalid descriptor: cannot find type_name "${u.proto.typeName}" specified by ${u.toString()}`);
269
+ if (refMessage.proto.options?.mapEntry !== undefined) {
270
+ return {
271
+ ...u,
272
+ repeated: false,
273
+ scalarType: undefined,
274
+ message: undefined,
275
+ enum: undefined,
276
+ map: resolveMap(refMessage, us),
277
+ };
278
+ }
279
+ return {
280
+ ...u,
281
+ repeated,
282
+ scalarType: undefined,
283
+ message: refMessage,
284
+ enum: undefined,
285
+ map: undefined,
286
+ };
287
+ }
288
+ case FieldDescriptorProto_Type.ENUM: {
289
+ assert(u.proto.typeName, `invalid descriptor: ${u.toString()} has type ${FieldDescriptorProto_Type[u.type]}, but type_name is unset`);
290
+ const refEnum = us.enums[u.proto.typeName];
291
+ assert(refEnum, `invalid descriptor: cannot find type_name "${u.proto.typeName}" specified by ${u.toString()}`);
292
+ return {
293
+ ...u,
294
+ repeated,
295
+ scalarType: undefined,
296
+ message: undefined,
297
+ enum: refEnum,
298
+ map: undefined,
299
+ };
300
+ }
301
+ default: {
302
+ const scalarType = fieldTypeToScalarType[u.type];
303
+ assert(scalarType, `invalid descriptor: unable to convert google.protobuf.FieldDescriptorProto.Type ${FieldDescriptorProto_Type[u.type]} to ScalarType`);
304
+ return {
305
+ ...u,
306
+ repeated,
307
+ scalarType,
308
+ message: undefined,
309
+ enum: undefined,
310
+ map: undefined,
311
+ };
312
+ }
313
+ }
314
+ }
315
+ function resolveMap(mapEntry, us) {
316
+ assert(mapEntry.proto.options?.mapEntry, `invalid descriptor: expected ${mapEntry.toString()} to be a map entry`);
317
+ assert(mapEntry.fields.length === 2, `invalid descriptor: map entry ${mapEntry.toString()} has ${mapEntry.fields.length} fields`);
318
+ const uKeyField = mapEntry.fields.find((f) => f.proto.number === 1);
319
+ assert(uKeyField, `invalid descriptor: map entry ${mapEntry.toString()} is missing key field`);
320
+ const keyField = resolveField(uKeyField, us);
321
+ assert(keyField.scalarType !== undefined &&
322
+ keyField.scalarType !== ScalarType.BYTES &&
323
+ keyField.scalarType !== ScalarType.FLOAT &&
324
+ keyField.scalarType !== ScalarType.DOUBLE, `invalid descriptor: map entry ${mapEntry.toString()} has unexpected key type ${FieldDescriptorProto_Type[keyField.type]}`);
325
+ const uValueField = mapEntry.fields.find((f) => f.proto.number === 2);
326
+ assert(uValueField, `invalid descriptor: map entry ${mapEntry.toString()} is missing value field`);
327
+ const valueField = resolveField(uValueField, us);
328
+ if (valueField.scalarType !== undefined) {
329
+ return {
330
+ key: keyField.scalarType,
331
+ value: {
332
+ scalarType: valueField.scalarType,
333
+ enum: undefined,
334
+ message: undefined,
335
+ },
336
+ };
337
+ }
338
+ if (valueField.enum !== undefined) {
339
+ return {
340
+ key: keyField.scalarType,
341
+ value: { scalar: undefined, enum: valueField.enum, message: undefined },
342
+ };
343
+ }
344
+ if (valueField.message !== undefined) {
293
345
  return {
294
- ...u,
295
- repeated: false,
296
- scalarType: undefined,
297
- message: undefined,
298
- enum: undefined,
299
- map: resolveMap(refMessage, us),
346
+ key: keyField.scalarType,
347
+ value: {
348
+ scalar: undefined,
349
+ enum: undefined,
350
+ message: valueField.message,
351
+ },
300
352
  };
301
- }
302
- return {
303
- ...u,
304
- repeated,
305
- scalarType: undefined,
306
- message: refMessage,
307
- enum: undefined,
308
- map: undefined,
309
- };
310
- }
311
- case FieldDescriptorProto_Type.ENUM: {
312
- assert(
313
- u.proto.typeName,
314
- `invalid descriptor: ${u.toString()} has type ${
315
- FieldDescriptorProto_Type[u.type]
316
- }, but type_name is unset`
317
- );
318
- const refEnum = us.enums[u.proto.typeName];
319
- assert(
320
- refEnum,
321
- `invalid descriptor: cannot find type_name "${
322
- u.proto.typeName
323
- }" specified by ${u.toString()}`
324
- );
325
- return {
326
- ...u,
327
- repeated,
328
- scalarType: undefined,
329
- message: undefined,
330
- enum: refEnum,
331
- map: undefined,
332
- };
333
- }
334
- default: {
335
- const scalarType = fieldTypeToScalarType[u.type];
336
- assert(
337
- scalarType,
338
- `invalid descriptor: unable to convert google.protobuf.FieldDescriptorProto.Type ${
339
- FieldDescriptorProto_Type[u.type]
340
- } to ScalarType`
341
- );
342
- return {
343
- ...u,
344
- repeated,
345
- scalarType,
346
- message: undefined,
347
- enum: undefined,
348
- map: undefined,
349
- };
350
- }
351
- }
353
+ }
354
+ throw new Error(`invalid descriptor: map entry ${mapEntry.toString()} has unexpected value type ${FieldDescriptorProto_Type[keyField.type]}`);
352
355
  }
353
- function resolveMap(mapEntry, us) {
354
- assert(
355
- mapEntry.proto.options?.mapEntry,
356
- `invalid descriptor: expected ${mapEntry.toString()} to be a map entry`
357
- );
358
- assert(
359
- mapEntry.fields.length === 2,
360
- `invalid descriptor: map entry ${mapEntry.toString()} has ${
361
- mapEntry.fields.length
362
- } fields`
363
- );
364
- const uKeyField = mapEntry.fields.find((f) => f.proto.number === 1);
365
- assert(
366
- uKeyField,
367
- `invalid descriptor: map entry ${mapEntry.toString()} is missing key field`
368
- );
369
- const keyField = resolveField(uKeyField, us);
370
- assert(
371
- keyField.scalarType !== undefined &&
372
- keyField.scalarType !== ScalarType.BYTES &&
373
- keyField.scalarType !== ScalarType.FLOAT &&
374
- keyField.scalarType !== ScalarType.DOUBLE,
375
- `invalid descriptor: map entry ${mapEntry.toString()} has unexpected key type ${
376
- FieldDescriptorProto_Type[keyField.type]
377
- }`
378
- );
379
- const uValueField = mapEntry.fields.find((f) => f.proto.number === 2);
380
- assert(
381
- uValueField,
382
- `invalid descriptor: map entry ${mapEntry.toString()} is missing value field`
383
- );
384
- const valueField = resolveField(uValueField, us);
385
- if (valueField.scalarType !== undefined) {
386
- return {
387
- key: keyField.scalarType,
388
- value: {
389
- scalarType: valueField.scalarType,
390
- enum: undefined,
391
- message: undefined,
392
- },
393
- };
394
- }
395
- if (valueField.enum !== undefined) {
396
- return {
397
- key: keyField.scalarType,
398
- value: { scalar: undefined, enum: valueField.enum, message: undefined },
399
- };
400
- }
401
- if (valueField.message !== undefined) {
356
+ function newOneof(proto, parent, file) {
357
+ assert(proto.name, `invalid descriptor: missing oneof descriptor name`);
402
358
  return {
403
- key: keyField.scalarType,
404
- value: {
405
- scalar: undefined,
406
- enum: undefined,
407
- message: valueField.message,
408
- },
359
+ proto,
360
+ parent,
361
+ file,
362
+ fields: [],
363
+ name: proto.name,
364
+ toString() {
365
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
366
+ return `oneof ${parent.typeName}.${proto.name}`;
367
+ },
409
368
  };
410
- }
411
- throw new Error(
412
- `invalid descriptor: map entry ${mapEntry.toString()} has unexpected value type ${
413
- FieldDescriptorProto_Type[keyField.type]
414
- }`
415
- );
416
369
  }
417
- function newOneof(proto, parent, file) {
418
- assert(proto.name, `invalid descriptor: missing oneof descriptor name`);
419
- return {
420
- proto,
421
- parent,
422
- file,
423
- fields: [],
424
- name: proto.name,
425
- toString() {
426
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
427
- return `oneof ${parent.typeName}.${proto.name}`;
428
- },
429
- };
430
- }
431
- function newExtension(
432
- proto,
433
- parent,
434
- file,
435
- us // eslint-disable-line @typescript-eslint/no-unused-vars
370
+ function newExtension(proto, parent, file, us // eslint-disable-line @typescript-eslint/no-unused-vars
436
371
  ) {
437
- assert(proto.name, `invalid descriptor: missing field descriptor name`);
438
- return {
439
- proto,
440
- file,
441
- parent,
442
- };
372
+ assert(proto.name, `invalid descriptor: missing field descriptor name`);
373
+ return {
374
+ proto,
375
+ file,
376
+ parent,
377
+ };
443
378
  }
444
379
  function newService(proto, file, us) {
445
- assert(proto.name, `invalid descriptor: missing service descriptor name`);
446
- let protoTypeName;
447
- if (file.proto.package !== undefined) {
448
- protoTypeName = `.${file.proto.package}.${proto.name}`;
449
- } else {
450
- protoTypeName = `.${proto.name}`;
451
- }
452
- const methods = [];
453
- const service = {
454
- proto,
455
- file,
456
- typeName: protoTypeName.startsWith(".")
457
- ? protoTypeName.substring(1)
458
- : protoTypeName,
459
- protoTypeName,
460
- methods,
461
- toString() {
462
- return `service ${this.typeName}`;
463
- },
464
- };
465
- for (const method of proto.method) {
466
- methods.push(newMethod(method, service));
467
- }
468
- us.services[service.protoTypeName] = service;
469
- return service;
380
+ assert(proto.name, `invalid descriptor: missing service descriptor name`);
381
+ let protoTypeName;
382
+ if (file.proto.package !== undefined) {
383
+ protoTypeName = `.${file.proto.package}.${proto.name}`;
384
+ }
385
+ else {
386
+ protoTypeName = `.${proto.name}`;
387
+ }
388
+ const methods = [];
389
+ const service = {
390
+ proto,
391
+ file,
392
+ typeName: protoTypeName.startsWith(".")
393
+ ? protoTypeName.substring(1)
394
+ : protoTypeName,
395
+ protoTypeName,
396
+ methods,
397
+ toString() {
398
+ return `service ${this.typeName}`;
399
+ },
400
+ };
401
+ for (const method of proto.method) {
402
+ methods.push(newMethod(method, service));
403
+ }
404
+ us.services[service.protoTypeName] = service;
405
+ return service;
470
406
  }
471
407
  function newMethod(proto, parent) {
472
- assert(proto.name, `invalid descriptor: missing method descriptor name`);
473
- assert(proto.inputType, `invalid descriptor: missing method input type`);
474
- assert(proto.outputType, `invalid descriptor: missing method output type`);
475
- let kind;
476
- if (proto.clientStreaming === true && proto.serverStreaming === true) {
477
- kind = MethodKind.BiDiStreaming;
478
- } else if (proto.clientStreaming === true) {
479
- kind = MethodKind.ClientStreaming;
480
- } else if (proto.serverStreaming === true) {
481
- kind = MethodKind.ServerStreaming;
482
- } else {
483
- kind = MethodKind.Unary;
484
- }
485
- let idempotency;
486
- switch (proto.options?.idempotencyLevel) {
487
- case MethodOptions_IdempotencyLevel.IDEMPOTENT:
488
- idempotency = MethodIdempotency.Idempotent;
489
- break;
490
- case MethodOptions_IdempotencyLevel.NO_SIDE_EFFECTS:
491
- idempotency = MethodIdempotency.NoSideEffects;
492
- break;
493
- case MethodOptions_IdempotencyLevel.IDEMPOTENCY_UNKNOWN:
494
- case undefined:
495
- idempotency = undefined;
496
- break;
497
- }
498
- const inputTypeName = proto.inputType.startsWith(".")
499
- ? proto.inputType.substring(1)
500
- : proto.inputType;
501
- const outputTypeName = proto.outputType.startsWith(".")
502
- ? proto.outputType.substring(1)
503
- : proto.outputType;
504
- return {
505
- proto,
506
- parent,
507
- name: proto.name,
508
- kind,
509
- idempotency,
510
- inputTypeName,
511
- outputTypeName,
512
- toString() {
513
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
514
- return `rpc ${this.parent.typeName}.${proto.name}`;
515
- },
516
- };
408
+ assert(proto.name, `invalid descriptor: missing method descriptor name`);
409
+ assert(proto.inputType, `invalid descriptor: missing method input type`);
410
+ assert(proto.outputType, `invalid descriptor: missing method output type`);
411
+ let kind;
412
+ if (proto.clientStreaming === true && proto.serverStreaming === true) {
413
+ kind = MethodKind.BiDiStreaming;
414
+ }
415
+ else if (proto.clientStreaming === true) {
416
+ kind = MethodKind.ClientStreaming;
417
+ }
418
+ else if (proto.serverStreaming === true) {
419
+ kind = MethodKind.ServerStreaming;
420
+ }
421
+ else {
422
+ kind = MethodKind.Unary;
423
+ }
424
+ let idempotency;
425
+ switch (proto.options?.idempotencyLevel) {
426
+ case MethodOptions_IdempotencyLevel.IDEMPOTENT:
427
+ idempotency = MethodIdempotency.Idempotent;
428
+ break;
429
+ case MethodOptions_IdempotencyLevel.NO_SIDE_EFFECTS:
430
+ idempotency = MethodIdempotency.NoSideEffects;
431
+ break;
432
+ case MethodOptions_IdempotencyLevel.IDEMPOTENCY_UNKNOWN:
433
+ case undefined:
434
+ idempotency = undefined;
435
+ break;
436
+ }
437
+ const inputTypeName = proto.inputType.startsWith(".")
438
+ ? proto.inputType.substring(1)
439
+ : proto.inputType;
440
+ const outputTypeName = proto.outputType.startsWith(".")
441
+ ? proto.outputType.substring(1)
442
+ : proto.outputType;
443
+ return {
444
+ proto,
445
+ parent,
446
+ name: proto.name,
447
+ kind,
448
+ idempotency,
449
+ inputTypeName,
450
+ outputTypeName,
451
+ toString() {
452
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above
453
+ return `rpc ${this.parent.typeName}.${proto.name}`;
454
+ },
455
+ };
517
456
  }