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