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