@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.BinaryReader = exports.BinaryWriter = exports.WireType = void 0;
4
17
  const varint_js_1 = require("./google/varint.js");
@@ -15,420 +28,412 @@ const proto_int64_js_1 = require("./proto-int64.js");
15
28
  */
16
29
  var WireType;
17
30
  (function (WireType) {
18
- /**
19
- * Used for int32, int64, uint32, uint64, sint32, sint64, bool, enum
20
- */
21
- WireType[(WireType["Varint"] = 0)] = "Varint";
22
- /**
23
- * Used for fixed64, sfixed64, double.
24
- * Always 8 bytes with little-endian byte order.
25
- */
26
- WireType[(WireType["Bit64"] = 1)] = "Bit64";
27
- /**
28
- * Used for string, bytes, embedded messages, packed repeated fields
29
- *
30
- * Only repeated numeric types (types which use the varint, 32-bit,
31
- * or 64-bit wire types) can be packed. In proto3, such fields are
32
- * packed by default.
33
- */
34
- WireType[(WireType["LengthDelimited"] = 2)] = "LengthDelimited";
35
- /**
36
- * Used for groups
37
- * @deprecated
38
- */
39
- WireType[(WireType["StartGroup"] = 3)] = "StartGroup";
40
- /**
41
- * Used for groups
42
- * @deprecated
43
- */
44
- WireType[(WireType["EndGroup"] = 4)] = "EndGroup";
45
- /**
46
- * Used for fixed32, sfixed32, float.
47
- * Always 4 bytes with little-endian byte order.
48
- */
49
- WireType[(WireType["Bit32"] = 5)] = "Bit32";
50
- })((WireType = exports.WireType || (exports.WireType = {})));
31
+ /**
32
+ * Used for int32, int64, uint32, uint64, sint32, sint64, bool, enum
33
+ */
34
+ WireType[WireType["Varint"] = 0] = "Varint";
35
+ /**
36
+ * Used for fixed64, sfixed64, double.
37
+ * Always 8 bytes with little-endian byte order.
38
+ */
39
+ WireType[WireType["Bit64"] = 1] = "Bit64";
40
+ /**
41
+ * Used for string, bytes, embedded messages, packed repeated fields
42
+ *
43
+ * Only repeated numeric types (types which use the varint, 32-bit,
44
+ * or 64-bit wire types) can be packed. In proto3, such fields are
45
+ * packed by default.
46
+ */
47
+ WireType[WireType["LengthDelimited"] = 2] = "LengthDelimited";
48
+ /**
49
+ * Used for groups
50
+ * @deprecated
51
+ */
52
+ WireType[WireType["StartGroup"] = 3] = "StartGroup";
53
+ /**
54
+ * Used for groups
55
+ * @deprecated
56
+ */
57
+ WireType[WireType["EndGroup"] = 4] = "EndGroup";
58
+ /**
59
+ * Used for fixed32, sfixed32, float.
60
+ * Always 4 bytes with little-endian byte order.
61
+ */
62
+ WireType[WireType["Bit32"] = 5] = "Bit32";
63
+ })(WireType = exports.WireType || (exports.WireType = {}));
51
64
  class BinaryWriter {
52
- constructor(textEncoder) {
53
- /**
54
- * Previous fork states.
55
- */
56
- this.stack = [];
57
- this.textEncoder = textEncoder ?? new TextEncoder();
58
- this.chunks = [];
59
- this.buf = [];
60
- }
61
- /**
62
- * Return all bytes written and reset this writer.
63
- */
64
- finish() {
65
- this.chunks.push(new Uint8Array(this.buf)); // flush the buffer
66
- let len = 0;
67
- for (let i = 0; i < this.chunks.length; i++) len += this.chunks[i].length;
68
- let bytes = new Uint8Array(len);
69
- let offset = 0;
70
- for (let i = 0; i < this.chunks.length; i++) {
71
- bytes.set(this.chunks[i], offset);
72
- offset += this.chunks[i].length;
73
- }
74
- this.chunks = [];
75
- return bytes;
76
- }
77
- /**
78
- * Start a new fork for length-delimited data like a message
79
- * or a packed repeated field.
80
- *
81
- * Must be joined later with `join()`.
82
- */
83
- fork() {
84
- this.stack.push({ chunks: this.chunks, buf: this.buf });
85
- this.chunks = [];
86
- this.buf = [];
87
- return this;
88
- }
89
- /**
90
- * Join the last fork. Write its length and bytes, then
91
- * return to the previous state.
92
- */
93
- join() {
94
- // get chunk of fork
95
- let chunk = this.finish();
96
- // restore previous state
97
- let prev = this.stack.pop();
98
- if (!prev) throw new Error("invalid state, fork stack empty");
99
- this.chunks = prev.chunks;
100
- this.buf = prev.buf;
101
- // write length of chunk as varint
102
- this.uint32(chunk.byteLength);
103
- return this.raw(chunk);
104
- }
105
- /**
106
- * Writes a tag (field number and wire type).
107
- *
108
- * Equivalent to `uint32( (fieldNo << 3 | type) >>> 0 )`.
109
- *
110
- * Generated code should compute the tag ahead of time and call `uint32()`.
111
- */
112
- tag(fieldNo, type) {
113
- return this.uint32(((fieldNo << 3) | type) >>> 0);
114
- }
115
- /**
116
- * Write a chunk of raw bytes.
117
- */
118
- raw(chunk) {
119
- if (this.buf.length) {
120
- this.chunks.push(new Uint8Array(this.buf));
121
- this.buf = [];
122
- }
123
- this.chunks.push(chunk);
124
- return this;
125
- }
126
- /**
127
- * Write a `uint32` value, an unsigned 32 bit varint.
128
- */
129
- uint32(value) {
130
- (0, assert_js_1.assertUInt32)(value);
131
- // write value as varint 32, inlined for speed
132
- while (value > 0x7f) {
133
- this.buf.push((value & 0x7f) | 0x80);
134
- value = value >>> 7;
135
- }
136
- this.buf.push(value);
137
- return this;
138
- }
139
- /**
140
- * Write a `int32` value, a signed 32 bit varint.
141
- */
142
- int32(value) {
143
- (0, assert_js_1.assertInt32)(value);
144
- (0, varint_js_1.varint32write)(value, this.buf);
145
- return this;
146
- }
147
- /**
148
- * Write a `bool` value, a variant.
149
- */
150
- bool(value) {
151
- this.buf.push(value ? 1 : 0);
152
- return this;
153
- }
154
- /**
155
- * Write a `bytes` value, length-delimited arbitrary data.
156
- */
157
- bytes(value) {
158
- this.uint32(value.byteLength); // write length of chunk as varint
159
- return this.raw(value);
160
- }
161
- /**
162
- * Write a `string` value, length-delimited data converted to UTF-8 text.
163
- */
164
- string(value) {
165
- let chunk = this.textEncoder.encode(value);
166
- this.uint32(chunk.byteLength); // write length of chunk as varint
167
- return this.raw(chunk);
168
- }
169
- /**
170
- * Write a `float` value, 32-bit floating point number.
171
- */
172
- float(value) {
173
- (0, assert_js_1.assertFloat32)(value);
174
- let chunk = new Uint8Array(4);
175
- new DataView(chunk.buffer).setFloat32(0, value, true);
176
- return this.raw(chunk);
177
- }
178
- /**
179
- * Write a `double` value, a 64-bit floating point number.
180
- */
181
- double(value) {
182
- let chunk = new Uint8Array(8);
183
- new DataView(chunk.buffer).setFloat64(0, value, true);
184
- return this.raw(chunk);
185
- }
186
- /**
187
- * Write a `fixed32` value, an unsigned, fixed-length 32-bit integer.
188
- */
189
- fixed32(value) {
190
- (0, assert_js_1.assertUInt32)(value);
191
- let chunk = new Uint8Array(4);
192
- new DataView(chunk.buffer).setUint32(0, value, true);
193
- return this.raw(chunk);
194
- }
195
- /**
196
- * Write a `sfixed32` value, a signed, fixed-length 32-bit integer.
197
- */
198
- sfixed32(value) {
199
- (0, assert_js_1.assertInt32)(value);
200
- let chunk = new Uint8Array(4);
201
- new DataView(chunk.buffer).setInt32(0, value, true);
202
- return this.raw(chunk);
203
- }
204
- /**
205
- * Write a `sint32` value, a signed, zigzag-encoded 32-bit varint.
206
- */
207
- sint32(value) {
208
- (0, assert_js_1.assertInt32)(value);
209
- // zigzag encode
210
- value = ((value << 1) ^ (value >> 31)) >>> 0;
211
- (0, varint_js_1.varint32write)(value, this.buf);
212
- return this;
213
- }
214
- /**
215
- * Write a `fixed64` value, a signed, fixed-length 64-bit integer.
216
- */
217
- sfixed64(value) {
218
- let chunk = new Uint8Array(8),
219
- view = new DataView(chunk.buffer),
220
- tc = proto_int64_js_1.protoInt64.enc(value);
221
- view.setInt32(0, tc.lo, true);
222
- view.setInt32(4, tc.hi, true);
223
- return this.raw(chunk);
224
- }
225
- /**
226
- * Write a `fixed64` value, an unsigned, fixed-length 64 bit integer.
227
- */
228
- fixed64(value) {
229
- let chunk = new Uint8Array(8),
230
- view = new DataView(chunk.buffer),
231
- tc = proto_int64_js_1.protoInt64.uEnc(value);
232
- view.setInt32(0, tc.lo, true);
233
- view.setInt32(4, tc.hi, true);
234
- return this.raw(chunk);
235
- }
236
- /**
237
- * Write a `int64` value, a signed 64-bit varint.
238
- */
239
- int64(value) {
240
- let tc = proto_int64_js_1.protoInt64.enc(value);
241
- (0, varint_js_1.varint64write)(tc.lo, tc.hi, this.buf);
242
- return this;
243
- }
244
- /**
245
- * Write a `sint64` value, a signed, zig-zag-encoded 64-bit varint.
246
- */
247
- sint64(value) {
248
- let tc = proto_int64_js_1.protoInt64.enc(value),
249
- // zigzag encode
250
- sign = tc.hi >> 31,
251
- lo = (tc.lo << 1) ^ sign,
252
- hi = ((tc.hi << 1) | (tc.lo >>> 31)) ^ sign;
253
- (0, varint_js_1.varint64write)(lo, hi, this.buf);
254
- return this;
255
- }
256
- /**
257
- * Write a `uint64` value, an unsigned 64-bit varint.
258
- */
259
- uint64(value) {
260
- let tc = proto_int64_js_1.protoInt64.uEnc(value);
261
- (0, varint_js_1.varint64write)(tc.lo, tc.hi, this.buf);
262
- return this;
263
- }
65
+ constructor(textEncoder) {
66
+ /**
67
+ * Previous fork states.
68
+ */
69
+ this.stack = [];
70
+ this.textEncoder = textEncoder ?? new TextEncoder();
71
+ this.chunks = [];
72
+ this.buf = [];
73
+ }
74
+ /**
75
+ * Return all bytes written and reset this writer.
76
+ */
77
+ finish() {
78
+ this.chunks.push(new Uint8Array(this.buf)); // flush the buffer
79
+ let len = 0;
80
+ for (let i = 0; i < this.chunks.length; i++)
81
+ len += this.chunks[i].length;
82
+ let bytes = new Uint8Array(len);
83
+ let offset = 0;
84
+ for (let i = 0; i < this.chunks.length; i++) {
85
+ bytes.set(this.chunks[i], offset);
86
+ offset += this.chunks[i].length;
87
+ }
88
+ this.chunks = [];
89
+ return bytes;
90
+ }
91
+ /**
92
+ * Start a new fork for length-delimited data like a message
93
+ * or a packed repeated field.
94
+ *
95
+ * Must be joined later with `join()`.
96
+ */
97
+ fork() {
98
+ this.stack.push({ chunks: this.chunks, buf: this.buf });
99
+ this.chunks = [];
100
+ this.buf = [];
101
+ return this;
102
+ }
103
+ /**
104
+ * Join the last fork. Write its length and bytes, then
105
+ * return to the previous state.
106
+ */
107
+ join() {
108
+ // get chunk of fork
109
+ let chunk = this.finish();
110
+ // restore previous state
111
+ let prev = this.stack.pop();
112
+ if (!prev)
113
+ throw new Error("invalid state, fork stack empty");
114
+ this.chunks = prev.chunks;
115
+ this.buf = prev.buf;
116
+ // write length of chunk as varint
117
+ this.uint32(chunk.byteLength);
118
+ return this.raw(chunk);
119
+ }
120
+ /**
121
+ * Writes a tag (field number and wire type).
122
+ *
123
+ * Equivalent to `uint32( (fieldNo << 3 | type) >>> 0 )`.
124
+ *
125
+ * Generated code should compute the tag ahead of time and call `uint32()`.
126
+ */
127
+ tag(fieldNo, type) {
128
+ return this.uint32(((fieldNo << 3) | type) >>> 0);
129
+ }
130
+ /**
131
+ * Write a chunk of raw bytes.
132
+ */
133
+ raw(chunk) {
134
+ if (this.buf.length) {
135
+ this.chunks.push(new Uint8Array(this.buf));
136
+ this.buf = [];
137
+ }
138
+ this.chunks.push(chunk);
139
+ return this;
140
+ }
141
+ /**
142
+ * Write a `uint32` value, an unsigned 32 bit varint.
143
+ */
144
+ uint32(value) {
145
+ (0, assert_js_1.assertUInt32)(value);
146
+ // write value as varint 32, inlined for speed
147
+ while (value > 0x7f) {
148
+ this.buf.push((value & 0x7f) | 0x80);
149
+ value = value >>> 7;
150
+ }
151
+ this.buf.push(value);
152
+ return this;
153
+ }
154
+ /**
155
+ * Write a `int32` value, a signed 32 bit varint.
156
+ */
157
+ int32(value) {
158
+ (0, assert_js_1.assertInt32)(value);
159
+ (0, varint_js_1.varint32write)(value, this.buf);
160
+ return this;
161
+ }
162
+ /**
163
+ * Write a `bool` value, a variant.
164
+ */
165
+ bool(value) {
166
+ this.buf.push(value ? 1 : 0);
167
+ return this;
168
+ }
169
+ /**
170
+ * Write a `bytes` value, length-delimited arbitrary data.
171
+ */
172
+ bytes(value) {
173
+ this.uint32(value.byteLength); // write length of chunk as varint
174
+ return this.raw(value);
175
+ }
176
+ /**
177
+ * Write a `string` value, length-delimited data converted to UTF-8 text.
178
+ */
179
+ string(value) {
180
+ let chunk = this.textEncoder.encode(value);
181
+ this.uint32(chunk.byteLength); // write length of chunk as varint
182
+ return this.raw(chunk);
183
+ }
184
+ /**
185
+ * Write a `float` value, 32-bit floating point number.
186
+ */
187
+ float(value) {
188
+ (0, assert_js_1.assertFloat32)(value);
189
+ let chunk = new Uint8Array(4);
190
+ new DataView(chunk.buffer).setFloat32(0, value, true);
191
+ return this.raw(chunk);
192
+ }
193
+ /**
194
+ * Write a `double` value, a 64-bit floating point number.
195
+ */
196
+ double(value) {
197
+ let chunk = new Uint8Array(8);
198
+ new DataView(chunk.buffer).setFloat64(0, value, true);
199
+ return this.raw(chunk);
200
+ }
201
+ /**
202
+ * Write a `fixed32` value, an unsigned, fixed-length 32-bit integer.
203
+ */
204
+ fixed32(value) {
205
+ (0, assert_js_1.assertUInt32)(value);
206
+ let chunk = new Uint8Array(4);
207
+ new DataView(chunk.buffer).setUint32(0, value, true);
208
+ return this.raw(chunk);
209
+ }
210
+ /**
211
+ * Write a `sfixed32` value, a signed, fixed-length 32-bit integer.
212
+ */
213
+ sfixed32(value) {
214
+ (0, assert_js_1.assertInt32)(value);
215
+ let chunk = new Uint8Array(4);
216
+ new DataView(chunk.buffer).setInt32(0, value, true);
217
+ return this.raw(chunk);
218
+ }
219
+ /**
220
+ * Write a `sint32` value, a signed, zigzag-encoded 32-bit varint.
221
+ */
222
+ sint32(value) {
223
+ (0, assert_js_1.assertInt32)(value);
224
+ // zigzag encode
225
+ value = ((value << 1) ^ (value >> 31)) >>> 0;
226
+ (0, varint_js_1.varint32write)(value, this.buf);
227
+ return this;
228
+ }
229
+ /**
230
+ * Write a `fixed64` value, a signed, fixed-length 64-bit integer.
231
+ */
232
+ sfixed64(value) {
233
+ let chunk = new Uint8Array(8), view = new DataView(chunk.buffer), tc = proto_int64_js_1.protoInt64.enc(value);
234
+ view.setInt32(0, tc.lo, true);
235
+ view.setInt32(4, tc.hi, true);
236
+ return this.raw(chunk);
237
+ }
238
+ /**
239
+ * Write a `fixed64` value, an unsigned, fixed-length 64 bit integer.
240
+ */
241
+ fixed64(value) {
242
+ let chunk = new Uint8Array(8), view = new DataView(chunk.buffer), tc = proto_int64_js_1.protoInt64.uEnc(value);
243
+ view.setInt32(0, tc.lo, true);
244
+ view.setInt32(4, tc.hi, true);
245
+ return this.raw(chunk);
246
+ }
247
+ /**
248
+ * Write a `int64` value, a signed 64-bit varint.
249
+ */
250
+ int64(value) {
251
+ let tc = proto_int64_js_1.protoInt64.enc(value);
252
+ (0, varint_js_1.varint64write)(tc.lo, tc.hi, this.buf);
253
+ return this;
254
+ }
255
+ /**
256
+ * Write a `sint64` value, a signed, zig-zag-encoded 64-bit varint.
257
+ */
258
+ sint64(value) {
259
+ let tc = proto_int64_js_1.protoInt64.enc(value),
260
+ // zigzag encode
261
+ sign = tc.hi >> 31, lo = (tc.lo << 1) ^ sign, hi = ((tc.hi << 1) | (tc.lo >>> 31)) ^ sign;
262
+ (0, varint_js_1.varint64write)(lo, hi, this.buf);
263
+ return this;
264
+ }
265
+ /**
266
+ * Write a `uint64` value, an unsigned 64-bit varint.
267
+ */
268
+ uint64(value) {
269
+ let tc = proto_int64_js_1.protoInt64.uEnc(value);
270
+ (0, varint_js_1.varint64write)(tc.lo, tc.hi, this.buf);
271
+ return this;
272
+ }
264
273
  }
265
274
  exports.BinaryWriter = BinaryWriter;
266
275
  class BinaryReader {
267
- constructor(buf, textDecoder) {
268
- this.varint64 = varint_js_1.varint64read; // dirty cast for `this`
269
- /**
270
- * Read a `uint32` field, an unsigned 32 bit varint.
271
- */
272
- this.uint32 = varint_js_1.varint32read; // dirty cast for `this` and access to protected `buf`
273
- this.buf = buf;
274
- this.len = buf.length;
275
- this.pos = 0;
276
- this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
277
- this.textDecoder = textDecoder ?? new TextDecoder();
278
- }
279
- /**
280
- * Reads a tag - field number and wire type.
281
- */
282
- tag() {
283
- let tag = this.uint32(),
284
- fieldNo = tag >>> 3,
285
- wireType = tag & 7;
286
- if (fieldNo <= 0 || wireType < 0 || wireType > 5)
287
- throw new Error(
288
- "illegal tag: field no " + fieldNo + " wire type " + wireType
289
- );
290
- return [fieldNo, wireType];
291
- }
292
- /**
293
- * Skip one element on the wire and return the skipped data.
294
- * Supports WireType.StartGroup since v2.0.0-alpha.23.
295
- */
296
- skip(wireType) {
297
- let start = this.pos;
298
- switch (wireType) {
299
- case WireType.Varint:
300
- while (this.buf[this.pos++] & 0x80) {
301
- // ignore
276
+ constructor(buf, textDecoder) {
277
+ this.varint64 = varint_js_1.varint64read; // dirty cast for `this`
278
+ /**
279
+ * Read a `uint32` field, an unsigned 32 bit varint.
280
+ */
281
+ this.uint32 = varint_js_1.varint32read; // dirty cast for `this` and access to protected `buf`
282
+ this.buf = buf;
283
+ this.len = buf.length;
284
+ this.pos = 0;
285
+ this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
286
+ this.textDecoder = textDecoder ?? new TextDecoder();
287
+ }
288
+ /**
289
+ * Reads a tag - field number and wire type.
290
+ */
291
+ tag() {
292
+ let tag = this.uint32(), fieldNo = tag >>> 3, wireType = tag & 7;
293
+ if (fieldNo <= 0 || wireType < 0 || wireType > 5)
294
+ throw new Error("illegal tag: field no " + fieldNo + " wire type " + wireType);
295
+ return [fieldNo, wireType];
296
+ }
297
+ /**
298
+ * Skip one element on the wire and return the skipped data.
299
+ * Supports WireType.StartGroup since v2.0.0-alpha.23.
300
+ */
301
+ skip(wireType) {
302
+ let start = this.pos;
303
+ switch (wireType) {
304
+ case WireType.Varint:
305
+ while (this.buf[this.pos++] & 0x80) {
306
+ // ignore
307
+ }
308
+ break;
309
+ // eslint-disable-next-line
310
+ // @ts-ignore TS7029: Fallthrough case in switch
311
+ case WireType.Bit64:
312
+ this.pos += 4;
313
+ // eslint-disable-next-line
314
+ // @ts-ignore TS7029: Fallthrough case in switch
315
+ case WireType.Bit32:
316
+ this.pos += 4;
317
+ break;
318
+ case WireType.LengthDelimited:
319
+ let len = this.uint32();
320
+ this.pos += len;
321
+ break;
322
+ case WireType.StartGroup:
323
+ // From descriptor.proto: Group type is deprecated, not supported in proto3.
324
+ // But we must still be able to parse and treat as unknown.
325
+ let t;
326
+ while ((t = this.tag()[1]) !== WireType.EndGroup) {
327
+ this.skip(t);
328
+ }
329
+ break;
330
+ default:
331
+ throw new Error("cant skip wire type " + wireType);
302
332
  }
303
- break;
304
- // eslint-disable-next-line
305
- // @ts-ignore TS7029: Fallthrough case in switch
306
- case WireType.Bit64:
307
- this.pos += 4;
308
- // eslint-disable-next-line
309
- // @ts-ignore TS7029: Fallthrough case in switch
310
- case WireType.Bit32:
311
- this.pos += 4;
312
- break;
313
- case WireType.LengthDelimited:
314
- let len = this.uint32();
333
+ this.assertBounds();
334
+ return this.buf.subarray(start, this.pos);
335
+ }
336
+ /**
337
+ * Throws error if position in byte array is out of range.
338
+ */
339
+ assertBounds() {
340
+ if (this.pos > this.len)
341
+ throw new RangeError("premature EOF");
342
+ }
343
+ /**
344
+ * Read a `int32` field, a signed 32 bit varint.
345
+ */
346
+ int32() {
347
+ return this.uint32() | 0;
348
+ }
349
+ /**
350
+ * Read a `sint32` field, a signed, zigzag-encoded 32-bit varint.
351
+ */
352
+ sint32() {
353
+ let zze = this.uint32();
354
+ // decode zigzag
355
+ return (zze >>> 1) ^ -(zze & 1);
356
+ }
357
+ /**
358
+ * Read a `int64` field, a signed 64-bit varint.
359
+ */
360
+ int64() {
361
+ return proto_int64_js_1.protoInt64.dec(...this.varint64());
362
+ }
363
+ /**
364
+ * Read a `uint64` field, an unsigned 64-bit varint.
365
+ */
366
+ uint64() {
367
+ return proto_int64_js_1.protoInt64.uDec(...this.varint64());
368
+ }
369
+ /**
370
+ * Read a `sint64` field, a signed, zig-zag-encoded 64-bit varint.
371
+ */
372
+ sint64() {
373
+ let [lo, hi] = this.varint64();
374
+ // decode zig zag
375
+ let s = -(lo & 1);
376
+ lo = ((lo >>> 1) | ((hi & 1) << 31)) ^ s;
377
+ hi = (hi >>> 1) ^ s;
378
+ return proto_int64_js_1.protoInt64.dec(lo, hi);
379
+ }
380
+ /**
381
+ * Read a `bool` field, a variant.
382
+ */
383
+ bool() {
384
+ let [lo, hi] = this.varint64();
385
+ return lo !== 0 || hi !== 0;
386
+ }
387
+ /**
388
+ * Read a `fixed32` field, an unsigned, fixed-length 32-bit integer.
389
+ */
390
+ fixed32() {
391
+ return this.view.getUint32((this.pos += 4) - 4, true);
392
+ }
393
+ /**
394
+ * Read a `sfixed32` field, a signed, fixed-length 32-bit integer.
395
+ */
396
+ sfixed32() {
397
+ return this.view.getInt32((this.pos += 4) - 4, true);
398
+ }
399
+ /**
400
+ * Read a `fixed64` field, an unsigned, fixed-length 64 bit integer.
401
+ */
402
+ fixed64() {
403
+ return proto_int64_js_1.protoInt64.uDec(this.sfixed32(), this.sfixed32());
404
+ }
405
+ /**
406
+ * Read a `fixed64` field, a signed, fixed-length 64-bit integer.
407
+ */
408
+ sfixed64() {
409
+ return proto_int64_js_1.protoInt64.dec(this.sfixed32(), this.sfixed32());
410
+ }
411
+ /**
412
+ * Read a `float` field, 32-bit floating point number.
413
+ */
414
+ float() {
415
+ return this.view.getFloat32((this.pos += 4) - 4, true);
416
+ }
417
+ /**
418
+ * Read a `double` field, a 64-bit floating point number.
419
+ */
420
+ double() {
421
+ return this.view.getFloat64((this.pos += 8) - 8, true);
422
+ }
423
+ /**
424
+ * Read a `bytes` field, length-delimited arbitrary data.
425
+ */
426
+ bytes() {
427
+ let len = this.uint32(), start = this.pos;
315
428
  this.pos += len;
316
- break;
317
- case WireType.StartGroup:
318
- // From descriptor.proto: Group type is deprecated, not supported in proto3.
319
- // But we must still be able to parse and treat as unknown.
320
- let t;
321
- while ((t = this.tag()[1]) !== WireType.EndGroup) {
322
- this.skip(t);
323
- }
324
- break;
325
- default:
326
- throw new Error("cant skip wire type " + wireType);
327
- }
328
- this.assertBounds();
329
- return this.buf.subarray(start, this.pos);
330
- }
331
- /**
332
- * Throws error if position in byte array is out of range.
333
- */
334
- assertBounds() {
335
- if (this.pos > this.len) throw new RangeError("premature EOF");
336
- }
337
- /**
338
- * Read a `int32` field, a signed 32 bit varint.
339
- */
340
- int32() {
341
- return this.uint32() | 0;
342
- }
343
- /**
344
- * Read a `sint32` field, a signed, zigzag-encoded 32-bit varint.
345
- */
346
- sint32() {
347
- let zze = this.uint32();
348
- // decode zigzag
349
- return (zze >>> 1) ^ -(zze & 1);
350
- }
351
- /**
352
- * Read a `int64` field, a signed 64-bit varint.
353
- */
354
- int64() {
355
- return proto_int64_js_1.protoInt64.dec(...this.varint64());
356
- }
357
- /**
358
- * Read a `uint64` field, an unsigned 64-bit varint.
359
- */
360
- uint64() {
361
- return proto_int64_js_1.protoInt64.uDec(...this.varint64());
362
- }
363
- /**
364
- * Read a `sint64` field, a signed, zig-zag-encoded 64-bit varint.
365
- */
366
- sint64() {
367
- let [lo, hi] = this.varint64();
368
- // decode zig zag
369
- let s = -(lo & 1);
370
- lo = ((lo >>> 1) | ((hi & 1) << 31)) ^ s;
371
- hi = (hi >>> 1) ^ s;
372
- return proto_int64_js_1.protoInt64.dec(lo, hi);
373
- }
374
- /**
375
- * Read a `bool` field, a variant.
376
- */
377
- bool() {
378
- let [lo, hi] = this.varint64();
379
- return lo !== 0 || hi !== 0;
380
- }
381
- /**
382
- * Read a `fixed32` field, an unsigned, fixed-length 32-bit integer.
383
- */
384
- fixed32() {
385
- return this.view.getUint32((this.pos += 4) - 4, true);
386
- }
387
- /**
388
- * Read a `sfixed32` field, a signed, fixed-length 32-bit integer.
389
- */
390
- sfixed32() {
391
- return this.view.getInt32((this.pos += 4) - 4, true);
392
- }
393
- /**
394
- * Read a `fixed64` field, an unsigned, fixed-length 64 bit integer.
395
- */
396
- fixed64() {
397
- return proto_int64_js_1.protoInt64.uDec(this.sfixed32(), this.sfixed32());
398
- }
399
- /**
400
- * Read a `fixed64` field, a signed, fixed-length 64-bit integer.
401
- */
402
- sfixed64() {
403
- return proto_int64_js_1.protoInt64.dec(this.sfixed32(), this.sfixed32());
404
- }
405
- /**
406
- * Read a `float` field, 32-bit floating point number.
407
- */
408
- float() {
409
- return this.view.getFloat32((this.pos += 4) - 4, true);
410
- }
411
- /**
412
- * Read a `double` field, a 64-bit floating point number.
413
- */
414
- double() {
415
- return this.view.getFloat64((this.pos += 8) - 8, true);
416
- }
417
- /**
418
- * Read a `bytes` field, length-delimited arbitrary data.
419
- */
420
- bytes() {
421
- let len = this.uint32(),
422
- start = this.pos;
423
- this.pos += len;
424
- this.assertBounds();
425
- return this.buf.subarray(start, start + len);
426
- }
427
- /**
428
- * Read a `string` field, length-delimited data converted to UTF-8 text.
429
- */
430
- string() {
431
- return this.textDecoder.decode(this.bytes());
432
- }
429
+ this.assertBounds();
430
+ return this.buf.subarray(start, start + len);
431
+ }
432
+ /**
433
+ * Read a `string` field, length-delimited data converted to UTF-8 text.
434
+ */
435
+ string() {
436
+ return this.textDecoder.decode(this.bytes());
437
+ }
433
438
  }
434
439
  exports.BinaryReader = BinaryReader;