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