@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
@@ -1,280 +0,0 @@
1
- "use strict";
2
- // Copyright 2008 Google Inc. All rights reserved.
3
- //
4
- // Redistribution and use in source and binary forms, with or without
5
- // modification, are permitted provided that the following conditions are
6
- // met:
7
- //
8
- // * Redistributions of source code must retain the above copyright
9
- // notice, this list of conditions and the following disclaimer.
10
- // * Redistributions in binary form must reproduce the above
11
- // copyright notice, this list of conditions and the following disclaimer
12
- // in the documentation and/or other materials provided with the
13
- // distribution.
14
- // * Neither the name of Google Inc. nor the names of its
15
- // contributors may be used to endorse or promote products derived from
16
- // this software without specific prior written permission.
17
- //
18
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
- // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
- // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
- // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
- // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
- //
30
- // Code generated by the Protocol Buffer compiler is owned by the owner
31
- // of the input file used when generating it. This code is not
32
- // standalone and requires a support library to be linked with it. This
33
- // support library is itself covered by the above license.
34
- Object.defineProperty(exports, "__esModule", { value: true });
35
- exports.varint32read =
36
- exports.varint32write =
37
- exports.int64toString =
38
- exports.int64fromString =
39
- exports.varint64write =
40
- exports.varint64read =
41
- void 0;
42
- /* eslint-disable prefer-const,@typescript-eslint/restrict-plus-operands */
43
- /**
44
- * Read a 64 bit varint as two JS numbers.
45
- *
46
- * Returns tuple:
47
- * [0]: low bits
48
- * [1]: high bits
49
- *
50
- * Copyright 2008 Google Inc. All rights reserved.
51
- *
52
- * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L175
53
- */
54
- function varint64read() {
55
- let lowBits = 0;
56
- let highBits = 0;
57
- for (let shift = 0; shift < 28; shift += 7) {
58
- let b = this.buf[this.pos++];
59
- lowBits |= (b & 0x7f) << shift;
60
- if ((b & 0x80) == 0) {
61
- this.assertBounds();
62
- return [lowBits, highBits];
63
- }
64
- }
65
- let middleByte = this.buf[this.pos++];
66
- // last four bits of the first 32 bit number
67
- lowBits |= (middleByte & 0x0f) << 28;
68
- // 3 upper bits are part of the next 32 bit number
69
- highBits = (middleByte & 0x70) >> 4;
70
- if ((middleByte & 0x80) == 0) {
71
- this.assertBounds();
72
- return [lowBits, highBits];
73
- }
74
- for (let shift = 3; shift <= 31; shift += 7) {
75
- let b = this.buf[this.pos++];
76
- highBits |= (b & 0x7f) << shift;
77
- if ((b & 0x80) == 0) {
78
- this.assertBounds();
79
- return [lowBits, highBits];
80
- }
81
- }
82
- throw new Error("invalid varint");
83
- }
84
- exports.varint64read = varint64read;
85
- /**
86
- * Write a 64 bit varint, given as two JS numbers, to the given bytes array.
87
- *
88
- * Copyright 2008 Google Inc. All rights reserved.
89
- *
90
- * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/writer.js#L344
91
- */
92
- function varint64write(lo, hi, bytes) {
93
- for (let i = 0; i < 28; i = i + 7) {
94
- const shift = lo >>> i;
95
- const hasNext = !(shift >>> 7 == 0 && hi == 0);
96
- const byte = (hasNext ? shift | 0x80 : shift) & 0xff;
97
- bytes.push(byte);
98
- if (!hasNext) {
99
- return;
100
- }
101
- }
102
- const splitBits = ((lo >>> 28) & 0x0f) | ((hi & 0x07) << 4);
103
- const hasMoreBits = !(hi >> 3 == 0);
104
- bytes.push((hasMoreBits ? splitBits | 0x80 : splitBits) & 0xff);
105
- if (!hasMoreBits) {
106
- return;
107
- }
108
- for (let i = 3; i < 31; i = i + 7) {
109
- const shift = hi >>> i;
110
- const hasNext = !(shift >>> 7 == 0);
111
- const byte = (hasNext ? shift | 0x80 : shift) & 0xff;
112
- bytes.push(byte);
113
- if (!hasNext) {
114
- return;
115
- }
116
- }
117
- bytes.push((hi >>> 31) & 0x01);
118
- }
119
- exports.varint64write = varint64write;
120
- // constants for binary math
121
- const TWO_PWR_32_DBL = (1 << 16) * (1 << 16);
122
- /**
123
- * Parse decimal string of 64 bit integer value as two JS numbers.
124
- *
125
- * Returns tuple:
126
- * [0]: minus sign?
127
- * [1]: low bits
128
- * [2]: high bits
129
- *
130
- * Copyright 2008 Google Inc.
131
- */
132
- function int64fromString(dec) {
133
- // Check for minus sign.
134
- let minus = dec[0] == "-";
135
- if (minus) dec = dec.slice(1);
136
- // Work 6 decimal digits at a time, acting like we're converting base 1e6
137
- // digits to binary. This is safe to do with floating point math because
138
- // Number.isSafeInteger(ALL_32_BITS * 1e6) == true.
139
- const base = 1e6;
140
- let lowBits = 0;
141
- let highBits = 0;
142
- function add1e6digit(begin, end) {
143
- // Note: Number('') is 0.
144
- const digit1e6 = Number(dec.slice(begin, end));
145
- highBits *= base;
146
- lowBits = lowBits * base + digit1e6;
147
- // Carry bits from lowBits to
148
- if (lowBits >= TWO_PWR_32_DBL) {
149
- highBits = highBits + ((lowBits / TWO_PWR_32_DBL) | 0);
150
- lowBits = lowBits % TWO_PWR_32_DBL;
151
- }
152
- }
153
- add1e6digit(-24, -18);
154
- add1e6digit(-18, -12);
155
- add1e6digit(-12, -6);
156
- add1e6digit(-6);
157
- return [minus, lowBits, highBits];
158
- }
159
- exports.int64fromString = int64fromString;
160
- /**
161
- * Format 64 bit integer value (as two JS numbers) to decimal string.
162
- *
163
- * Copyright 2008 Google Inc.
164
- */
165
- function int64toString(bitsLow, bitsHigh) {
166
- // Skip the expensive conversion if the number is small enough to use the
167
- // built-in conversions.
168
- if (bitsHigh <= 0x1fffff) {
169
- return "" + (TWO_PWR_32_DBL * bitsHigh + bitsLow);
170
- }
171
- // What this code is doing is essentially converting the input number from
172
- // base-2 to base-1e7, which allows us to represent the 64-bit range with
173
- // only 3 (very large) digits. Those digits are then trivial to convert to
174
- // a base-10 string.
175
- // The magic numbers used here are -
176
- // 2^24 = 16777216 = (1,6777216) in base-1e7.
177
- // 2^48 = 281474976710656 = (2,8147497,6710656) in base-1e7.
178
- // Split 32:32 representation into 16:24:24 representation so our
179
- // intermediate digits don't overflow.
180
- let low = bitsLow & 0xffffff;
181
- let mid = (((bitsLow >>> 24) | (bitsHigh << 8)) >>> 0) & 0xffffff;
182
- let high = (bitsHigh >> 16) & 0xffff;
183
- // Assemble our three base-1e7 digits, ignoring carries. The maximum
184
- // value in a digit at this step is representable as a 48-bit integer, which
185
- // can be stored in a 64-bit floating point number.
186
- let digitA = low + mid * 6777216 + high * 6710656;
187
- let digitB = mid + high * 8147497;
188
- let digitC = high * 2;
189
- // Apply carries from A to B and from B to C.
190
- let base = 10000000;
191
- if (digitA >= base) {
192
- digitB += Math.floor(digitA / base);
193
- digitA %= base;
194
- }
195
- if (digitB >= base) {
196
- digitC += Math.floor(digitB / base);
197
- digitB %= base;
198
- }
199
- // Convert base-1e7 digits to base-10, with optional leading zeroes.
200
- function decimalFrom1e7(digit1e7, needLeadingZeros) {
201
- let partial = digit1e7 ? String(digit1e7) : "";
202
- if (needLeadingZeros) {
203
- return "0000000".slice(partial.length) + partial;
204
- }
205
- return partial;
206
- }
207
- return (
208
- decimalFrom1e7(digitC, /*needLeadingZeros=*/ 0) +
209
- decimalFrom1e7(digitB, /*needLeadingZeros=*/ digitC) +
210
- // If the final 1e7 digit didn't need leading zeros, we would have
211
- // returned via the trivial code path at the top.
212
- decimalFrom1e7(digitA, /*needLeadingZeros=*/ 1)
213
- );
214
- }
215
- exports.int64toString = int64toString;
216
- /**
217
- * Write a 32 bit varint, signed or unsigned. Same as `varint64write(0, value, bytes)`
218
- *
219
- * Copyright 2008 Google Inc. All rights reserved.
220
- *
221
- * See https://github.com/protocolbuffers/protobuf/blob/1b18833f4f2a2f681f4e4a25cdf3b0a43115ec26/js/binary/encoder.js#L144
222
- */
223
- function varint32write(value, bytes) {
224
- if (value >= 0) {
225
- // write value as varint 32
226
- while (value > 0x7f) {
227
- bytes.push((value & 0x7f) | 0x80);
228
- value = value >>> 7;
229
- }
230
- bytes.push(value);
231
- } else {
232
- for (let i = 0; i < 9; i++) {
233
- bytes.push((value & 127) | 128);
234
- value = value >> 7;
235
- }
236
- bytes.push(1);
237
- }
238
- }
239
- exports.varint32write = varint32write;
240
- /**
241
- * Read an unsigned 32 bit varint.
242
- *
243
- * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L220
244
- */
245
- function varint32read() {
246
- let b = this.buf[this.pos++];
247
- let result = b & 0x7f;
248
- if ((b & 0x80) == 0) {
249
- this.assertBounds();
250
- return result;
251
- }
252
- b = this.buf[this.pos++];
253
- result |= (b & 0x7f) << 7;
254
- if ((b & 0x80) == 0) {
255
- this.assertBounds();
256
- return result;
257
- }
258
- b = this.buf[this.pos++];
259
- result |= (b & 0x7f) << 14;
260
- if ((b & 0x80) == 0) {
261
- this.assertBounds();
262
- return result;
263
- }
264
- b = this.buf[this.pos++];
265
- result |= (b & 0x7f) << 21;
266
- if ((b & 0x80) == 0) {
267
- this.assertBounds();
268
- return result;
269
- }
270
- // Extract only last 4 bits
271
- b = this.buf[this.pos++];
272
- result |= (b & 0x0f) << 28;
273
- for (let readBytes = 5; (b & 0x80) !== 0 && readBytes < 10; readBytes++)
274
- b = this.buf[this.pos++];
275
- if ((b & 0x80) != 0) throw new Error("invalid varint");
276
- this.assertBounds();
277
- // Result can have 32 bits, convert it to unsigned
278
- return result >>> 0;
279
- }
280
- exports.varint32read = varint32read;
@@ -1,265 +0,0 @@
1
- // Copyright 2008 Google Inc. All rights reserved.
2
- //
3
- // Redistribution and use in source and binary forms, with or without
4
- // modification, are permitted provided that the following conditions are
5
- // met:
6
- //
7
- // * Redistributions of source code must retain the above copyright
8
- // notice, this list of conditions and the following disclaimer.
9
- // * Redistributions in binary form must reproduce the above
10
- // copyright notice, this list of conditions and the following disclaimer
11
- // in the documentation and/or other materials provided with the
12
- // distribution.
13
- // * Neither the name of Google Inc. nor the names of its
14
- // contributors may be used to endorse or promote products derived from
15
- // this software without specific prior written permission.
16
- //
17
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
- // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
- // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
- // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
- // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
- //
29
- // Code generated by the Protocol Buffer compiler is owned by the owner
30
- // of the input file used when generating it. This code is not
31
- // standalone and requires a support library to be linked with it. This
32
- // support library is itself covered by the above license.
33
- /* eslint-disable prefer-const,@typescript-eslint/restrict-plus-operands */
34
- /**
35
- * Read a 64 bit varint as two JS numbers.
36
- *
37
- * Returns tuple:
38
- * [0]: low bits
39
- * [1]: high bits
40
- *
41
- * Copyright 2008 Google Inc. All rights reserved.
42
- *
43
- * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L175
44
- */
45
- export function varint64read() {
46
- let lowBits = 0;
47
- let highBits = 0;
48
- for (let shift = 0; shift < 28; shift += 7) {
49
- let b = this.buf[this.pos++];
50
- lowBits |= (b & 0x7f) << shift;
51
- if ((b & 0x80) == 0) {
52
- this.assertBounds();
53
- return [lowBits, highBits];
54
- }
55
- }
56
- let middleByte = this.buf[this.pos++];
57
- // last four bits of the first 32 bit number
58
- lowBits |= (middleByte & 0x0f) << 28;
59
- // 3 upper bits are part of the next 32 bit number
60
- highBits = (middleByte & 0x70) >> 4;
61
- if ((middleByte & 0x80) == 0) {
62
- this.assertBounds();
63
- return [lowBits, highBits];
64
- }
65
- for (let shift = 3; shift <= 31; shift += 7) {
66
- let b = this.buf[this.pos++];
67
- highBits |= (b & 0x7f) << shift;
68
- if ((b & 0x80) == 0) {
69
- this.assertBounds();
70
- return [lowBits, highBits];
71
- }
72
- }
73
- throw new Error("invalid varint");
74
- }
75
- /**
76
- * Write a 64 bit varint, given as two JS numbers, to the given bytes array.
77
- *
78
- * Copyright 2008 Google Inc. All rights reserved.
79
- *
80
- * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/writer.js#L344
81
- */
82
- export function varint64write(lo, hi, bytes) {
83
- for (let i = 0; i < 28; i = i + 7) {
84
- const shift = lo >>> i;
85
- const hasNext = !(shift >>> 7 == 0 && hi == 0);
86
- const byte = (hasNext ? shift | 0x80 : shift) & 0xff;
87
- bytes.push(byte);
88
- if (!hasNext) {
89
- return;
90
- }
91
- }
92
- const splitBits = ((lo >>> 28) & 0x0f) | ((hi & 0x07) << 4);
93
- const hasMoreBits = !(hi >> 3 == 0);
94
- bytes.push((hasMoreBits ? splitBits | 0x80 : splitBits) & 0xff);
95
- if (!hasMoreBits) {
96
- return;
97
- }
98
- for (let i = 3; i < 31; i = i + 7) {
99
- const shift = hi >>> i;
100
- const hasNext = !(shift >>> 7 == 0);
101
- const byte = (hasNext ? shift | 0x80 : shift) & 0xff;
102
- bytes.push(byte);
103
- if (!hasNext) {
104
- return;
105
- }
106
- }
107
- bytes.push((hi >>> 31) & 0x01);
108
- }
109
- // constants for binary math
110
- const TWO_PWR_32_DBL = (1 << 16) * (1 << 16);
111
- /**
112
- * Parse decimal string of 64 bit integer value as two JS numbers.
113
- *
114
- * Returns tuple:
115
- * [0]: minus sign?
116
- * [1]: low bits
117
- * [2]: high bits
118
- *
119
- * Copyright 2008 Google Inc.
120
- */
121
- export function int64fromString(dec) {
122
- // Check for minus sign.
123
- let minus = dec[0] == "-";
124
- if (minus) dec = dec.slice(1);
125
- // Work 6 decimal digits at a time, acting like we're converting base 1e6
126
- // digits to binary. This is safe to do with floating point math because
127
- // Number.isSafeInteger(ALL_32_BITS * 1e6) == true.
128
- const base = 1e6;
129
- let lowBits = 0;
130
- let highBits = 0;
131
- function add1e6digit(begin, end) {
132
- // Note: Number('') is 0.
133
- const digit1e6 = Number(dec.slice(begin, end));
134
- highBits *= base;
135
- lowBits = lowBits * base + digit1e6;
136
- // Carry bits from lowBits to
137
- if (lowBits >= TWO_PWR_32_DBL) {
138
- highBits = highBits + ((lowBits / TWO_PWR_32_DBL) | 0);
139
- lowBits = lowBits % TWO_PWR_32_DBL;
140
- }
141
- }
142
- add1e6digit(-24, -18);
143
- add1e6digit(-18, -12);
144
- add1e6digit(-12, -6);
145
- add1e6digit(-6);
146
- return [minus, lowBits, highBits];
147
- }
148
- /**
149
- * Format 64 bit integer value (as two JS numbers) to decimal string.
150
- *
151
- * Copyright 2008 Google Inc.
152
- */
153
- export function int64toString(bitsLow, bitsHigh) {
154
- // Skip the expensive conversion if the number is small enough to use the
155
- // built-in conversions.
156
- if (bitsHigh <= 0x1fffff) {
157
- return "" + (TWO_PWR_32_DBL * bitsHigh + bitsLow);
158
- }
159
- // What this code is doing is essentially converting the input number from
160
- // base-2 to base-1e7, which allows us to represent the 64-bit range with
161
- // only 3 (very large) digits. Those digits are then trivial to convert to
162
- // a base-10 string.
163
- // The magic numbers used here are -
164
- // 2^24 = 16777216 = (1,6777216) in base-1e7.
165
- // 2^48 = 281474976710656 = (2,8147497,6710656) in base-1e7.
166
- // Split 32:32 representation into 16:24:24 representation so our
167
- // intermediate digits don't overflow.
168
- let low = bitsLow & 0xffffff;
169
- let mid = (((bitsLow >>> 24) | (bitsHigh << 8)) >>> 0) & 0xffffff;
170
- let high = (bitsHigh >> 16) & 0xffff;
171
- // Assemble our three base-1e7 digits, ignoring carries. The maximum
172
- // value in a digit at this step is representable as a 48-bit integer, which
173
- // can be stored in a 64-bit floating point number.
174
- let digitA = low + mid * 6777216 + high * 6710656;
175
- let digitB = mid + high * 8147497;
176
- let digitC = high * 2;
177
- // Apply carries from A to B and from B to C.
178
- let base = 10000000;
179
- if (digitA >= base) {
180
- digitB += Math.floor(digitA / base);
181
- digitA %= base;
182
- }
183
- if (digitB >= base) {
184
- digitC += Math.floor(digitB / base);
185
- digitB %= base;
186
- }
187
- // Convert base-1e7 digits to base-10, with optional leading zeroes.
188
- function decimalFrom1e7(digit1e7, needLeadingZeros) {
189
- let partial = digit1e7 ? String(digit1e7) : "";
190
- if (needLeadingZeros) {
191
- return "0000000".slice(partial.length) + partial;
192
- }
193
- return partial;
194
- }
195
- return (
196
- decimalFrom1e7(digitC, /*needLeadingZeros=*/ 0) +
197
- decimalFrom1e7(digitB, /*needLeadingZeros=*/ digitC) +
198
- // If the final 1e7 digit didn't need leading zeros, we would have
199
- // returned via the trivial code path at the top.
200
- decimalFrom1e7(digitA, /*needLeadingZeros=*/ 1)
201
- );
202
- }
203
- /**
204
- * Write a 32 bit varint, signed or unsigned. Same as `varint64write(0, value, bytes)`
205
- *
206
- * Copyright 2008 Google Inc. All rights reserved.
207
- *
208
- * See https://github.com/protocolbuffers/protobuf/blob/1b18833f4f2a2f681f4e4a25cdf3b0a43115ec26/js/binary/encoder.js#L144
209
- */
210
- export function varint32write(value, bytes) {
211
- if (value >= 0) {
212
- // write value as varint 32
213
- while (value > 0x7f) {
214
- bytes.push((value & 0x7f) | 0x80);
215
- value = value >>> 7;
216
- }
217
- bytes.push(value);
218
- } else {
219
- for (let i = 0; i < 9; i++) {
220
- bytes.push((value & 127) | 128);
221
- value = value >> 7;
222
- }
223
- bytes.push(1);
224
- }
225
- }
226
- /**
227
- * Read an unsigned 32 bit varint.
228
- *
229
- * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L220
230
- */
231
- export function varint32read() {
232
- let b = this.buf[this.pos++];
233
- let result = b & 0x7f;
234
- if ((b & 0x80) == 0) {
235
- this.assertBounds();
236
- return result;
237
- }
238
- b = this.buf[this.pos++];
239
- result |= (b & 0x7f) << 7;
240
- if ((b & 0x80) == 0) {
241
- this.assertBounds();
242
- return result;
243
- }
244
- b = this.buf[this.pos++];
245
- result |= (b & 0x7f) << 14;
246
- if ((b & 0x80) == 0) {
247
- this.assertBounds();
248
- return result;
249
- }
250
- b = this.buf[this.pos++];
251
- result |= (b & 0x7f) << 21;
252
- if ((b & 0x80) == 0) {
253
- this.assertBounds();
254
- return result;
255
- }
256
- // Extract only last 4 bits
257
- b = this.buf[this.pos++];
258
- result |= (b & 0x0f) << 28;
259
- for (let readBytes = 5; (b & 0x80) !== 0 && readBytes < 10; readBytes++)
260
- b = this.buf[this.pos++];
261
- if ((b & 0x80) != 0) throw new Error("invalid varint");
262
- this.assertBounds();
263
- // Result can have 32 bits, convert it to unsigned
264
- return result >>> 0;
265
- }