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