@bufbuild/protobuf 0.0.1-alpha.1 → 0.0.2-alpha.2

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 (138) hide show
  1. package/dist/cjs/binary-encoding.js +414 -409
  2. package/dist/cjs/binary-format.js +13 -0
  3. package/dist/cjs/descriptor-registry.js +420 -449
  4. package/dist/cjs/descriptor-set.js +429 -513
  5. package/dist/cjs/enum.js +13 -0
  6. package/dist/cjs/field-list.js +13 -0
  7. package/dist/cjs/field.js +43 -30
  8. package/dist/cjs/google/protobuf/any_pb.js +122 -140
  9. package/dist/cjs/google/protobuf/api_pb.js +177 -224
  10. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +125 -227
  11. package/dist/cjs/google/protobuf/descriptor_pb.js +1029 -1888
  12. package/dist/cjs/google/protobuf/duration_pb.js +72 -88
  13. package/dist/cjs/google/protobuf/empty_pb.js +17 -17
  14. package/dist/cjs/google/protobuf/field_mask_pb.js +77 -94
  15. package/dist/cjs/google/protobuf/source_context_pb.js +25 -25
  16. package/dist/cjs/google/protobuf/struct_pb.js +144 -181
  17. package/dist/cjs/google/protobuf/timestamp_pb.js +86 -123
  18. package/dist/cjs/google/protobuf/type_pb.js +406 -462
  19. package/dist/cjs/google/protobuf/wrappers_pb.js +407 -470
  20. package/dist/cjs/google/varint.js +166 -171
  21. package/dist/cjs/index.js +41 -122
  22. package/dist/cjs/json-format.js +13 -0
  23. package/dist/cjs/message-type.js +13 -0
  24. package/dist/cjs/message.js +89 -85
  25. package/dist/cjs/private/assert.js +33 -26
  26. package/dist/cjs/private/base64.js +93 -79
  27. package/dist/cjs/private/binary-format-common.js +208 -208
  28. package/dist/cjs/private/binary-format-proto2.js +88 -109
  29. package/dist/cjs/private/binary-format-proto3.js +70 -90
  30. package/dist/cjs/private/enum.js +77 -44
  31. package/dist/cjs/private/field-list.js +64 -50
  32. package/dist/cjs/private/field-wrapper.js +21 -10
  33. package/dist/cjs/private/field.js +35 -25
  34. package/dist/cjs/private/json-format-common.js +427 -440
  35. package/dist/cjs/private/json-format-proto2.js +91 -89
  36. package/dist/cjs/private/json-format-proto3.js +82 -93
  37. package/dist/cjs/private/message-type.js +39 -27
  38. package/dist/cjs/private/names.js +71 -62
  39. package/dist/cjs/private/options-map.js +13 -0
  40. package/dist/cjs/private/proto-runtime.js +25 -16
  41. package/dist/cjs/private/scalars.js +113 -103
  42. package/dist/cjs/private/util-common.js +193 -210
  43. package/dist/cjs/private/util.js +13 -0
  44. package/dist/cjs/proto-int64.js +123 -111
  45. package/dist/cjs/proto2.js +63 -63
  46. package/dist/cjs/proto3.js +74 -74
  47. package/dist/cjs/service-type.js +27 -17
  48. package/dist/cjs/type-registry.js +46 -31
  49. package/dist/esm/binary-encoding.js +414 -414
  50. package/dist/esm/binary-format.js +13 -0
  51. package/dist/esm/descriptor-registry.js +422 -458
  52. package/dist/esm/descriptor-set.js +430 -478
  53. package/dist/esm/enum.js +13 -0
  54. package/dist/esm/field-list.js +13 -0
  55. package/dist/esm/field.js +42 -29
  56. package/dist/esm/google/protobuf/any_pb.js +122 -140
  57. package/dist/esm/google/protobuf/api_pb.js +177 -197
  58. package/dist/esm/google/protobuf/compiler/plugin_pb.js +123 -216
  59. package/dist/esm/google/protobuf/descriptor_pb.js +1013 -1804
  60. package/dist/esm/google/protobuf/duration_pb.js +72 -88
  61. package/dist/esm/google/protobuf/empty_pb.js +17 -17
  62. package/dist/esm/google/protobuf/field_mask_pb.js +77 -94
  63. package/dist/esm/google/protobuf/source_context_pb.js +25 -25
  64. package/dist/esm/google/protobuf/struct_pb.js +143 -180
  65. package/dist/esm/google/protobuf/timestamp_pb.js +86 -123
  66. package/dist/esm/google/protobuf/type_pb.js +401 -421
  67. package/dist/esm/google/protobuf/wrappers_pb.js +406 -397
  68. package/dist/esm/google/varint.js +165 -164
  69. package/dist/esm/index.js +14 -1
  70. package/dist/esm/json-format.js +13 -0
  71. package/dist/esm/message-type.js +13 -0
  72. package/dist/esm/message.js +89 -85
  73. package/dist/esm/private/assert.js +32 -21
  74. package/dist/esm/private/base64.js +92 -78
  75. package/dist/esm/private/binary-format-common.js +209 -207
  76. package/dist/esm/private/binary-format-proto2.js +89 -81
  77. package/dist/esm/private/binary-format-proto3.js +71 -67
  78. package/dist/esm/private/enum.js +75 -43
  79. package/dist/esm/private/field-list.js +64 -50
  80. package/dist/esm/private/field-wrapper.js +21 -10
  81. package/dist/esm/private/field.js +35 -22
  82. package/dist/esm/private/json-format-common.js +427 -435
  83. package/dist/esm/private/json-format-proto2.js +89 -81
  84. package/dist/esm/private/json-format-proto3.js +79 -80
  85. package/dist/esm/private/message-type.js +40 -28
  86. package/dist/esm/private/names.js +70 -57
  87. package/dist/esm/private/options-map.js +13 -0
  88. package/dist/esm/private/proto-runtime.js +26 -12
  89. package/dist/esm/private/scalars.js +112 -99
  90. package/dist/esm/private/util-common.js +193 -192
  91. package/dist/esm/private/util.js +13 -0
  92. package/dist/esm/proto-int64.js +123 -111
  93. package/dist/esm/proto2.js +63 -57
  94. package/dist/esm/proto3.js +74 -68
  95. package/dist/esm/service-type.js +25 -12
  96. package/dist/esm/type-registry.js +46 -31
  97. package/dist/types/binary-encoding.d.ts +398 -398
  98. package/dist/types/binary-format.d.ts +69 -91
  99. package/dist/types/descriptor-registry.d.ts +23 -29
  100. package/dist/types/descriptor-set.d.ts +107 -128
  101. package/dist/types/enum.d.ts +21 -21
  102. package/dist/types/field-list.d.ts +21 -21
  103. package/dist/types/field.d.ts +191 -231
  104. package/dist/types/google/protobuf/any_pb.d.ts +56 -77
  105. package/dist/types/google/protobuf/api_pb.d.ts +144 -187
  106. package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +195 -257
  107. package/dist/types/google/protobuf/descriptor_pb.d.ts +1360 -1737
  108. package/dist/types/google/protobuf/duration_pb.d.ts +30 -50
  109. package/dist/types/google/protobuf/empty_pb.d.ts +9 -28
  110. package/dist/types/google/protobuf/field_mask_pb.d.ts +17 -37
  111. package/dist/types/google/protobuf/source_context_pb.d.ts +16 -35
  112. package/dist/types/google/protobuf/struct_pb.d.ts +64 -115
  113. package/dist/types/google/protobuf/timestamp_pb.d.ts +31 -51
  114. package/dist/types/google/protobuf/type_pb.d.ts +355 -422
  115. package/dist/types/google/protobuf/wrappers_pb.d.ts +181 -297
  116. package/dist/types/google/varint.d.ts +6 -13
  117. package/dist/types/index.d.ts +4 -27
  118. package/dist/types/json-format.d.ts +70 -89
  119. package/dist/types/message-type.d.ts +37 -45
  120. package/dist/types/message.d.ts +62 -87
  121. package/dist/types/private/assert.d.ts +1 -4
  122. package/dist/types/private/binary-format-common.d.ts +6 -32
  123. package/dist/types/private/enum.d.ts +13 -11
  124. package/dist/types/private/field-list.d.ts +14 -21
  125. package/dist/types/private/field-wrapper.d.ts +4 -10
  126. package/dist/types/private/field.d.ts +12 -12
  127. package/dist/types/private/json-format-common.d.ts +5 -27
  128. package/dist/types/private/message-type.d.ts +12 -7
  129. package/dist/types/private/names.d.ts +1 -4
  130. package/dist/types/private/options-map.d.ts +1 -1
  131. package/dist/types/private/proto-runtime.d.ts +34 -42
  132. package/dist/types/private/scalars.d.ts +5 -12
  133. package/dist/types/private/util-common.d.ts +1 -4
  134. package/dist/types/private/util.d.ts +29 -41
  135. package/dist/types/proto-int64.d.ts +42 -42
  136. package/dist/types/service-type.d.ts +36 -56
  137. package/dist/types/type-registry.d.ts +13 -15
  138. package/package.json +2 -3
@@ -1,3 +1,16 @@
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.
1
14
  import { assert } from "./private/assert.js";
2
15
  import { proto3 } from "./proto3.js";
3
16
  import { proto2 } from "./proto2.js";
@@ -10,44 +23,29 @@ import { Duration } from "./google/protobuf/duration_pb.js";
10
23
  import { Any } from "./google/protobuf/any_pb.js";
11
24
  import { Empty } from "./google/protobuf/empty_pb.js";
12
25
  import { FieldMask } from "./google/protobuf/field_mask_pb.js";
13
- import {
14
- ListValue,
15
- NullValue,
16
- Struct,
17
- Value,
18
- } from "./google/protobuf/struct_pb.js";
26
+ import { ListValue, NullValue, Struct, Value, } from "./google/protobuf/struct_pb.js";
19
27
  import { getEnumType } from "./private/enum.js";
20
- import {
21
- BoolValue,
22
- BytesValue,
23
- DoubleValue,
24
- FloatValue,
25
- Int32Value,
26
- Int64Value,
27
- StringValue,
28
- UInt32Value,
29
- UInt64Value,
30
- } from "./google/protobuf/wrappers_pb.js";
28
+ import { BoolValue, BytesValue, DoubleValue, FloatValue, Int32Value, Int64Value, StringValue, UInt32Value, UInt64Value, } from "./google/protobuf/wrappers_pb.js";
31
29
  // well-known message types with specialized JSON representation
32
30
  const wkMessages = [
33
- Any,
34
- Duration,
35
- Empty,
36
- FieldMask,
37
- Struct,
38
- Value,
39
- ListValue,
40
- Timestamp,
41
- Duration,
42
- DoubleValue,
43
- FloatValue,
44
- Int64Value,
45
- Int32Value,
46
- UInt32Value,
47
- UInt64Value,
48
- BoolValue,
49
- StringValue,
50
- BytesValue,
31
+ Any,
32
+ Duration,
33
+ Empty,
34
+ FieldMask,
35
+ Struct,
36
+ Value,
37
+ ListValue,
38
+ Timestamp,
39
+ Duration,
40
+ DoubleValue,
41
+ FloatValue,
42
+ Int64Value,
43
+ Int32Value,
44
+ UInt32Value,
45
+ UInt64Value,
46
+ BoolValue,
47
+ StringValue,
48
+ BytesValue,
51
49
  ];
52
50
  // well-known enum types with specialized JSON representation
53
51
  const wkEnums = [getEnumType(NullValue)];
@@ -59,463 +57,429 @@ const wkEnums = [getEnumType(NullValue)];
59
57
  * are replaced with their generated counterpart in this package.
60
58
  */
61
59
  export class DescriptorRegistry {
62
- constructor(descriptorSet, replaceWkt = true) {
63
- this.enums = {};
64
- this.messages = {};
65
- this.services = {};
66
- this.ds = descriptorSet ?? new DescriptorSet();
67
- if (replaceWkt) {
68
- for (const mt of wkMessages) {
69
- this.messages["." + mt.typeName] = mt;
70
- }
71
- for (const et of wkEnums) {
72
- this.enums["." + et.typeName] = et;
73
- }
74
- }
75
- }
76
- /**
77
- * May raise an error on invalid descriptors.
78
- */
79
- add(...files) {
80
- this.ds.add(...files);
81
- }
82
- /**
83
- * May raise an error on invalid descriptors.
84
- */
85
- findEnum(typeName) {
86
- const protoTypeName = "." + typeName;
87
- const existing = this.enums[protoTypeName];
88
- if (existing) {
89
- return existing;
90
- }
91
- const raw = this.ds.enums[protoTypeName];
92
- if (!raw) {
93
- return undefined;
94
- }
95
- const runtime = raw.file.syntax == "proto3" ? proto3 : proto2;
96
- const type = runtime.makeEnumType(
97
- typeName,
98
- raw.values.map((u) => ({
99
- no: u.number,
100
- name: u.name,
101
- })),
102
- {}
103
- );
104
- this.enums[protoTypeName] = type;
105
- return type;
106
- }
107
- /**
108
- * May raise an error on invalid descriptors.
109
- */
110
- findMessage(typeName) {
111
- const protoTypeName = "." + typeName;
112
- const existing = this.messages[protoTypeName];
113
- if (existing) {
114
- return existing;
115
- }
116
- const raw = this.ds.messages[protoTypeName];
117
- if (!raw) {
118
- return undefined;
60
+ constructor(descriptorSet, replaceWkt = true) {
61
+ this.enums = {};
62
+ this.messages = {};
63
+ this.services = {};
64
+ this.ds = descriptorSet ?? new DescriptorSet();
65
+ if (replaceWkt) {
66
+ for (const mt of wkMessages) {
67
+ this.messages["." + mt.typeName] = mt;
68
+ }
69
+ for (const et of wkEnums) {
70
+ this.enums["." + et.typeName] = et;
71
+ }
72
+ }
119
73
  }
120
- const runtime = raw.file.syntax == "proto3" ? proto3 : proto2;
121
- const fields = [];
122
- const type = runtime.makeMessageType(typeName, () => fields, {
123
- localName: makeTypeLocalName(raw),
124
- });
125
- this.messages[protoTypeName] = type;
126
- for (const field of raw.fields.map((f) => f.resolve(this.ds))) {
127
- const fieldInfo = makeFieldInfo(field, this);
128
- fields.push(fieldInfo);
74
+ /**
75
+ * May raise an error on invalid descriptors.
76
+ */
77
+ add(...files) {
78
+ this.ds.add(...files);
129
79
  }
130
- return type;
131
- }
132
- /**
133
- * May raise an error on invalid descriptors.
134
- */
135
- findService(typeName) {
136
- const protoTypeName = "." + typeName;
137
- const existing = this.services[protoTypeName];
138
- if (existing) {
139
- return existing;
80
+ /**
81
+ * May raise an error on invalid descriptors.
82
+ */
83
+ findEnum(typeName) {
84
+ const protoTypeName = "." + typeName;
85
+ const existing = this.enums[protoTypeName];
86
+ if (existing) {
87
+ return existing;
88
+ }
89
+ const raw = this.ds.enums[protoTypeName];
90
+ if (!raw) {
91
+ return undefined;
92
+ }
93
+ const runtime = raw.file.syntax == "proto3" ? proto3 : proto2;
94
+ const type = runtime.makeEnumType(typeName, raw.values.map((u) => ({
95
+ no: u.number,
96
+ name: u.name,
97
+ })), {});
98
+ this.enums[protoTypeName] = type;
99
+ return type;
140
100
  }
141
- const raw = this.ds.services[protoTypeName];
142
- if (!raw) {
143
- return undefined;
101
+ /**
102
+ * May raise an error on invalid descriptors.
103
+ */
104
+ findMessage(typeName) {
105
+ const protoTypeName = "." + typeName;
106
+ const existing = this.messages[protoTypeName];
107
+ if (existing) {
108
+ return existing;
109
+ }
110
+ const raw = this.ds.messages[protoTypeName];
111
+ if (!raw) {
112
+ return undefined;
113
+ }
114
+ const runtime = raw.file.syntax == "proto3" ? proto3 : proto2;
115
+ const fields = [];
116
+ const type = runtime.makeMessageType(typeName, () => fields, {
117
+ localName: makeTypeLocalName(raw),
118
+ });
119
+ this.messages[protoTypeName] = type;
120
+ for (const field of raw.fields.map((f) => f.resolve(this.ds))) {
121
+ const fieldInfo = makeFieldInfo(field, this);
122
+ fields.push(fieldInfo);
123
+ }
124
+ return type;
144
125
  }
145
- const methods = {};
146
- for (const u of raw.methods) {
147
- const it = this.findMessage(u.inputTypeName);
148
- const ot = this.findMessage(u.outputTypeName);
149
- assert(it, `message "${u.inputTypeName}" for ${u.toString()} not found`);
150
- assert(
151
- ot,
152
- `output message "${u.outputTypeName}" for ${u.toString()} not found`
153
- );
154
- const m = {
155
- name: u.name,
156
- localName: makeMethodName(u.name),
157
- I: it,
158
- O: ot,
159
- kind: u.kind,
160
- idempotency: u.idempotency,
161
- options: {},
162
- };
163
- methods[m.localName] = m;
126
+ /**
127
+ * May raise an error on invalid descriptors.
128
+ */
129
+ findService(typeName) {
130
+ const protoTypeName = "." + typeName;
131
+ const existing = this.services[protoTypeName];
132
+ if (existing) {
133
+ return existing;
134
+ }
135
+ const raw = this.ds.services[protoTypeName];
136
+ if (!raw) {
137
+ return undefined;
138
+ }
139
+ const methods = {};
140
+ for (const u of raw.methods) {
141
+ const it = this.findMessage(u.inputTypeName);
142
+ const ot = this.findMessage(u.outputTypeName);
143
+ assert(it, `message "${u.inputTypeName}" for ${u.toString()} not found`);
144
+ assert(ot, `output message "${u.outputTypeName}" for ${u.toString()} not found`);
145
+ const m = {
146
+ name: u.name,
147
+ localName: makeMethodName(u.name),
148
+ I: it,
149
+ O: ot,
150
+ kind: u.kind,
151
+ idempotency: u.idempotency,
152
+ options: {},
153
+ };
154
+ methods[m.localName] = m;
155
+ }
156
+ return (this.services[protoTypeName] = {
157
+ typeName: raw.typeName,
158
+ methods,
159
+ });
164
160
  }
165
- return (this.services[protoTypeName] = {
166
- typeName: raw.typeName,
167
- methods,
168
- });
169
- }
170
161
  }
171
162
  function makeTypeLocalName(type) {
172
- const typeName = type.typeName;
173
- const packagePrefix = (type.file.proto.package ?? "") + ".";
174
- if (!typeName.startsWith(packagePrefix)) {
175
- return type.name;
176
- }
177
- return typeName.substring(packagePrefix.length).split(".").join("_");
163
+ const typeName = type.typeName;
164
+ const packagePrefix = (type.file.proto.package ?? "") + ".";
165
+ if (!typeName.startsWith(packagePrefix)) {
166
+ return type.name;
167
+ }
168
+ return typeName.substring(packagePrefix.length).split(".").join("_");
178
169
  }
179
170
  function makeFieldInfo(field, resolver) {
180
- if (field.map !== undefined) {
181
- return makeMapFieldInfo(field, resolver);
182
- }
183
- if (field.message) {
184
- return makeMessageFieldInfo(field, resolver);
185
- }
186
- const fi = field.enum
187
- ? makeEnumFieldInfo(field, resolver)
188
- : makeScalarFieldInfo(field);
189
- fi.default = parseDefaultValue(field);
190
- return fi;
171
+ if (field.map !== undefined) {
172
+ return makeMapFieldInfo(field, resolver);
173
+ }
174
+ if (field.message) {
175
+ return makeMessageFieldInfo(field, resolver);
176
+ }
177
+ const fi = field.enum
178
+ ? makeEnumFieldInfo(field, resolver)
179
+ : makeScalarFieldInfo(field);
180
+ fi.default = parseDefaultValue(field);
181
+ return fi;
191
182
  }
192
183
  function makeMapFieldInfo(field, resolver) {
193
- const base = {
194
- kind: "map",
195
- name: field.name,
196
- no: field.number,
197
- K: field.map.key,
198
- };
199
- if (field.map.value.message) {
200
- const messageType = resolver.findMessage(field.map.value.message.typeName);
201
- assert(
202
- messageType,
203
- `message "${
204
- field.map.value.message.typeName
205
- }" for ${field.toString()} not found`
206
- );
207
- return {
208
- ...base,
209
- V: {
210
- kind: "message",
211
- T: messageType,
212
- },
184
+ const base = {
185
+ kind: "map",
186
+ name: field.name,
187
+ no: field.number,
188
+ K: field.map.key,
213
189
  };
214
- }
215
- if (field.map.value.enum) {
216
- const enumType = resolver.findEnum(field.map.value.enum.typeName);
217
- assert(
218
- enumType,
219
- `enum "${
220
- field.map.value.enum.typeName
221
- }" for ${field.toString()} not found`
222
- );
190
+ if (field.map.value.message) {
191
+ const messageType = resolver.findMessage(field.map.value.message.typeName);
192
+ assert(messageType, `message "${field.map.value.message.typeName}" for ${field.toString()} not found`);
193
+ return {
194
+ ...base,
195
+ V: {
196
+ kind: "message",
197
+ T: messageType,
198
+ },
199
+ };
200
+ }
201
+ if (field.map.value.enum) {
202
+ const enumType = resolver.findEnum(field.map.value.enum.typeName);
203
+ assert(enumType, `enum "${field.map.value.enum.typeName}" for ${field.toString()} not found`);
204
+ return {
205
+ ...base,
206
+ V: {
207
+ kind: "enum",
208
+ T: enumType,
209
+ },
210
+ };
211
+ }
223
212
  return {
224
- ...base,
225
- V: {
226
- kind: "enum",
227
- T: enumType,
228
- },
213
+ ...base,
214
+ V: {
215
+ kind: "scalar",
216
+ T: field.map.value.scalarType,
217
+ },
229
218
  };
230
- }
231
- return {
232
- ...base,
233
- V: {
234
- kind: "scalar",
235
- T: field.map.value.scalarType,
236
- },
237
- };
238
219
  }
239
220
  function makeScalarFieldInfo(field) {
240
- const base = {
241
- no: field.number,
242
- name: field.name,
243
- kind: "scalar",
244
- T: field.scalarType,
245
- };
246
- if (field.repeated) {
247
- return {
248
- ...base,
249
- repeated: true,
250
- packed: field.packed,
251
- oneof: undefined,
252
- T: field.scalarType,
221
+ const base = {
222
+ no: field.number,
223
+ name: field.name,
224
+ kind: "scalar",
225
+ T: field.scalarType,
253
226
  };
254
- }
255
- if (field.oneof) {
256
- return {
257
- ...base,
258
- oneof: field.oneof.name,
259
- };
260
- }
261
- if (field.optional) {
262
- return {
263
- ...base,
264
- opt: true,
265
- };
266
- }
267
- return base;
227
+ if (field.repeated) {
228
+ return {
229
+ ...base,
230
+ repeated: true,
231
+ packed: field.packed,
232
+ oneof: undefined,
233
+ T: field.scalarType,
234
+ };
235
+ }
236
+ if (field.oneof) {
237
+ return {
238
+ ...base,
239
+ oneof: field.oneof.name,
240
+ };
241
+ }
242
+ if (field.optional) {
243
+ return {
244
+ ...base,
245
+ opt: true,
246
+ };
247
+ }
248
+ return base;
268
249
  }
269
250
  function makeMessageFieldInfo(field, resolver) {
270
- const messageType = resolver.findMessage(field.message.typeName);
271
- assert(
272
- messageType,
273
- `message "${field.message.typeName}" for ${field.toString()} not found`
274
- );
275
- const base = {
276
- no: field.number,
277
- name: field.name,
278
- kind: "message",
279
- T: messageType,
280
- };
281
- if (field.repeated) {
282
- return {
283
- ...base,
284
- repeated: true,
285
- packed: field.packed,
286
- oneof: undefined,
287
- };
288
- }
289
- if (field.oneof) {
290
- return {
291
- ...base,
292
- oneof: field.oneof.name,
293
- };
294
- }
295
- if (field.optional) {
296
- return {
297
- ...base,
298
- opt: true,
251
+ const messageType = resolver.findMessage(field.message.typeName);
252
+ assert(messageType, `message "${field.message.typeName}" for ${field.toString()} not found`);
253
+ const base = {
254
+ no: field.number,
255
+ name: field.name,
256
+ kind: "message",
257
+ T: messageType,
299
258
  };
300
- }
301
- return base;
259
+ if (field.repeated) {
260
+ return {
261
+ ...base,
262
+ repeated: true,
263
+ packed: field.packed,
264
+ oneof: undefined,
265
+ };
266
+ }
267
+ if (field.oneof) {
268
+ return {
269
+ ...base,
270
+ oneof: field.oneof.name,
271
+ };
272
+ }
273
+ if (field.optional) {
274
+ return {
275
+ ...base,
276
+ opt: true,
277
+ };
278
+ }
279
+ return base;
302
280
  }
303
281
  function makeEnumFieldInfo(field, resolver) {
304
- const enumType = resolver.findEnum(field.enum.typeName);
305
- assert(
306
- enumType,
307
- `message "${field.enum.typeName}" for ${field.toString()} not found`
308
- );
309
- const base = {
310
- no: field.number,
311
- name: field.name,
312
- kind: "enum",
313
- T: enumType,
314
- };
315
- if (field.repeated) {
316
- return {
317
- ...base,
318
- repeated: true,
319
- packed: field.packed,
320
- oneof: undefined,
321
- };
322
- }
323
- if (field.oneof) {
324
- return {
325
- ...base,
326
- oneof: field.oneof.name,
327
- };
328
- }
329
- if (field.optional) {
330
- return {
331
- ...base,
332
- opt: true,
282
+ const enumType = resolver.findEnum(field.enum.typeName);
283
+ assert(enumType, `message "${field.enum.typeName}" for ${field.toString()} not found`);
284
+ const base = {
285
+ no: field.number,
286
+ name: field.name,
287
+ kind: "enum",
288
+ T: enumType,
333
289
  };
334
- }
335
- return base;
290
+ if (field.repeated) {
291
+ return {
292
+ ...base,
293
+ repeated: true,
294
+ packed: field.packed,
295
+ oneof: undefined,
296
+ };
297
+ }
298
+ if (field.oneof) {
299
+ return {
300
+ ...base,
301
+ oneof: field.oneof.name,
302
+ };
303
+ }
304
+ if (field.optional) {
305
+ return {
306
+ ...base,
307
+ opt: true,
308
+ };
309
+ }
310
+ return base;
336
311
  }
337
312
  function parseDefaultValue(field) {
338
- const d = field.proto.defaultValue;
339
- if (d === undefined) {
340
- return undefined;
341
- }
342
- if (field.enum) {
343
- const enumValue = field.enum.values.find((v) => v.name === d);
344
- assert(enumValue, `cannot parse ${field.toString()} default value: ${d}`);
345
- return enumValue.number;
346
- }
347
- if (field.scalarType) {
348
- switch (field.scalarType) {
349
- case ScalarType.STRING:
350
- return d;
351
- case ScalarType.BYTES: {
352
- const u = unescapeBytesDefaultValue(d);
353
- if (u === false) {
354
- throw new Error(
355
- `cannot parse ${field.toString()} default value: ${d}`
356
- );
357
- }
358
- return u;
359
- }
360
- case ScalarType.INT64:
361
- case ScalarType.SFIXED64:
362
- case ScalarType.SINT64:
363
- return protoInt64.parse(d);
364
- case ScalarType.UINT64:
365
- case ScalarType.FIXED64:
366
- return protoInt64.uParse(d);
367
- case ScalarType.DOUBLE:
368
- case ScalarType.FLOAT:
369
- switch (d) {
370
- case "inf":
371
- return Number.POSITIVE_INFINITY;
372
- case "-inf":
373
- return Number.NEGATIVE_INFINITY;
374
- case "nan":
375
- return Number.NaN;
376
- default:
377
- return parseFloat(d);
313
+ const d = field.proto.defaultValue;
314
+ if (d === undefined) {
315
+ return undefined;
316
+ }
317
+ if (field.enum) {
318
+ const enumValue = field.enum.values.find((v) => v.name === d);
319
+ assert(enumValue, `cannot parse ${field.toString()} default value: ${d}`);
320
+ return enumValue.number;
321
+ }
322
+ if (field.scalarType) {
323
+ switch (field.scalarType) {
324
+ case ScalarType.STRING:
325
+ return d;
326
+ case ScalarType.BYTES: {
327
+ const u = unescapeBytesDefaultValue(d);
328
+ if (u === false) {
329
+ throw new Error(`cannot parse ${field.toString()} default value: ${d}`);
330
+ }
331
+ return u;
332
+ }
333
+ case ScalarType.INT64:
334
+ case ScalarType.SFIXED64:
335
+ case ScalarType.SINT64:
336
+ return protoInt64.parse(d);
337
+ case ScalarType.UINT64:
338
+ case ScalarType.FIXED64:
339
+ return protoInt64.uParse(d);
340
+ case ScalarType.DOUBLE:
341
+ case ScalarType.FLOAT:
342
+ switch (d) {
343
+ case "inf":
344
+ return Number.POSITIVE_INFINITY;
345
+ case "-inf":
346
+ return Number.NEGATIVE_INFINITY;
347
+ case "nan":
348
+ return Number.NaN;
349
+ default:
350
+ return parseFloat(d);
351
+ }
352
+ case ScalarType.BOOL:
353
+ return Boolean(d);
354
+ case ScalarType.INT32:
355
+ case ScalarType.UINT32:
356
+ case ScalarType.SINT32:
357
+ case ScalarType.FIXED32:
358
+ case ScalarType.SFIXED32:
359
+ return parseInt(d, 10);
378
360
  }
379
- case ScalarType.BOOL:
380
- return Boolean(d);
381
- case ScalarType.INT32:
382
- case ScalarType.UINT32:
383
- case ScalarType.SINT32:
384
- case ScalarType.FIXED32:
385
- case ScalarType.SFIXED32:
386
- return parseInt(d, 10);
387
361
  }
388
- }
389
- return undefined;
362
+ return undefined;
390
363
  }
391
364
  // unescapeBytesDefaultValue parses a text-encoded default value (proto2) of a
392
365
  // BYTES field.
393
366
  function unescapeBytesDefaultValue(str) {
394
- const b = [];
395
- const input = {
396
- tail: str,
397
- c: "",
398
- next() {
399
- if (this.tail.length == 0) {
400
- return false;
401
- }
402
- this.c = this.tail[0];
403
- this.tail = this.tail.substring(1);
404
- return true;
405
- },
406
- take(n) {
407
- if (this.tail.length >= n) {
408
- const r = this.tail.substring(0, n);
409
- this.tail = this.tail.substring(n);
410
- return r;
411
- }
412
- return false;
413
- },
414
- };
415
- while (input.next()) {
416
- switch (input.c) {
417
- case "\\":
418
- if (input.next()) {
419
- switch (input.c) {
420
- case "\\":
421
- b.push(input.c.charCodeAt(0));
422
- break;
423
- case "b":
424
- b.push(0x08);
425
- break;
426
- case "f":
427
- b.push(0x0c);
428
- break;
429
- case "n":
430
- b.push(0x0a);
431
- break;
432
- case "r":
433
- b.push(0x0d);
434
- break;
435
- case "t":
436
- b.push(0x09);
437
- break;
438
- case "v":
439
- b.push(0x0b);
440
- break;
441
- case "0":
442
- case "1":
443
- case "2":
444
- case "3":
445
- case "4":
446
- case "5":
447
- case "6":
448
- case "7": {
449
- const s = input.c;
450
- const t = input.take(2);
451
- if (t === false) {
367
+ const b = [];
368
+ const input = {
369
+ tail: str,
370
+ c: "",
371
+ next() {
372
+ if (this.tail.length == 0) {
452
373
  return false;
453
- }
454
- const n = parseInt(s + t, 8);
455
- if (isNaN(n)) {
456
- return false;
457
- }
458
- b.push(n);
459
- break;
460
374
  }
461
- case "x": {
462
- const s = input.c;
463
- const t = input.take(2);
464
- if (t === false) {
465
- return false;
466
- }
467
- const n = parseInt(s + t, 16);
468
- if (isNaN(n)) {
469
- return false;
470
- }
471
- b.push(n);
472
- break;
473
- }
474
- case "u": {
475
- const s = input.c;
476
- const t = input.take(4);
477
- if (t === false) {
478
- return false;
479
- }
480
- const n = parseInt(s + t, 16);
481
- if (isNaN(n)) {
482
- return false;
483
- }
484
- const chunk = new Uint8Array(4);
485
- const view = new DataView(chunk.buffer);
486
- view.setInt32(0, n, true);
487
- b.push(chunk[0], chunk[1], chunk[2], chunk[3]);
488
- break;
375
+ this.c = this.tail[0];
376
+ this.tail = this.tail.substring(1);
377
+ return true;
378
+ },
379
+ take(n) {
380
+ if (this.tail.length >= n) {
381
+ const r = this.tail.substring(0, n);
382
+ this.tail = this.tail.substring(n);
383
+ return r;
489
384
  }
490
- case "U": {
491
- const s = input.c;
492
- const t = input.take(8);
493
- if (t === false) {
494
- return false;
495
- }
496
- const tc = protoInt64.uEnc(s + t);
497
- const chunk = new Uint8Array(8);
498
- const view = new DataView(chunk.buffer);
499
- view.setInt32(0, tc.lo, true);
500
- view.setInt32(4, tc.hi, true);
501
- b.push(
502
- chunk[0],
503
- chunk[1],
504
- chunk[2],
505
- chunk[3],
506
- chunk[4],
507
- chunk[5],
508
- chunk[6],
509
- chunk[7]
510
- );
511
- break;
512
- }
513
- }
385
+ return false;
386
+ },
387
+ };
388
+ while (input.next()) {
389
+ switch (input.c) {
390
+ case "\\":
391
+ if (input.next()) {
392
+ switch (input.c) {
393
+ case "\\":
394
+ b.push(input.c.charCodeAt(0));
395
+ break;
396
+ case "b":
397
+ b.push(0x08);
398
+ break;
399
+ case "f":
400
+ b.push(0x0c);
401
+ break;
402
+ case "n":
403
+ b.push(0x0a);
404
+ break;
405
+ case "r":
406
+ b.push(0x0d);
407
+ break;
408
+ case "t":
409
+ b.push(0x09);
410
+ break;
411
+ case "v":
412
+ b.push(0x0b);
413
+ break;
414
+ case "0":
415
+ case "1":
416
+ case "2":
417
+ case "3":
418
+ case "4":
419
+ case "5":
420
+ case "6":
421
+ case "7": {
422
+ const s = input.c;
423
+ const t = input.take(2);
424
+ if (t === false) {
425
+ return false;
426
+ }
427
+ const n = parseInt(s + t, 8);
428
+ if (isNaN(n)) {
429
+ return false;
430
+ }
431
+ b.push(n);
432
+ break;
433
+ }
434
+ case "x": {
435
+ const s = input.c;
436
+ const t = input.take(2);
437
+ if (t === false) {
438
+ return false;
439
+ }
440
+ const n = parseInt(s + t, 16);
441
+ if (isNaN(n)) {
442
+ return false;
443
+ }
444
+ b.push(n);
445
+ break;
446
+ }
447
+ case "u": {
448
+ const s = input.c;
449
+ const t = input.take(4);
450
+ if (t === false) {
451
+ return false;
452
+ }
453
+ const n = parseInt(s + t, 16);
454
+ if (isNaN(n)) {
455
+ return false;
456
+ }
457
+ const chunk = new Uint8Array(4);
458
+ const view = new DataView(chunk.buffer);
459
+ view.setInt32(0, n, true);
460
+ b.push(chunk[0], chunk[1], chunk[2], chunk[3]);
461
+ break;
462
+ }
463
+ case "U": {
464
+ const s = input.c;
465
+ const t = input.take(8);
466
+ if (t === false) {
467
+ return false;
468
+ }
469
+ const tc = protoInt64.uEnc(s + t);
470
+ const chunk = new Uint8Array(8);
471
+ const view = new DataView(chunk.buffer);
472
+ view.setInt32(0, tc.lo, true);
473
+ view.setInt32(4, tc.hi, true);
474
+ b.push(chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7]);
475
+ break;
476
+ }
477
+ }
478
+ }
479
+ break;
480
+ default:
481
+ b.push(input.c.charCodeAt(0));
514
482
  }
515
- break;
516
- default:
517
- b.push(input.c.charCodeAt(0));
518
483
  }
519
- }
520
- return new Uint8Array(b);
484
+ return new Uint8Array(b);
521
485
  }