@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
@@ -43,34 +43,34 @@
43
43
  * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L175
44
44
  */
45
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];
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
+ }
54
55
  }
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];
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
+ }
71
72
  }
72
- }
73
- throw new Error("invalid varint");
73
+ throw new Error("invalid varint");
74
74
  }
75
75
  /**
76
76
  * Write a 64 bit varint, given as two JS numbers, to the given bytes array.
@@ -80,31 +80,31 @@ export function varint64read() {
80
80
  * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/writer.js#L344
81
81
  */
82
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;
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;
90
97
  }
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;
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
+ }
105
106
  }
106
- }
107
- bytes.push((hi >>> 31) & 0x01);
107
+ bytes.push((hi >>> 31) & 0x01);
108
108
  }
109
109
  // constants for binary math
110
110
  const TWO_PWR_32_DBL = (1 << 16) * (1 << 16);
@@ -119,31 +119,32 @@ const TWO_PWR_32_DBL = (1 << 16) * (1 << 16);
119
119
  * Copyright 2008 Google Inc.
120
120
  */
121
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;
122
+ // Check for minus sign.
123
+ let minus = dec[0] == "-";
124
+ if (minus)
125
+ dec = dec.slice(1);
126
+ // Work 6 decimal digits at a time, acting like we're converting base 1e6
127
+ // digits to binary. This is safe to do with floating point math because
128
+ // Number.isSafeInteger(ALL_32_BITS * 1e6) == true.
129
+ const base = 1e6;
130
+ let lowBits = 0;
131
+ let highBits = 0;
132
+ function add1e6digit(begin, end) {
133
+ // Note: Number('') is 0.
134
+ const digit1e6 = Number(dec.slice(begin, end));
135
+ highBits *= base;
136
+ lowBits = lowBits * base + digit1e6;
137
+ // Carry bits from lowBits to
138
+ if (lowBits >= TWO_PWR_32_DBL) {
139
+ highBits = highBits + ((lowBits / TWO_PWR_32_DBL) | 0);
140
+ lowBits = lowBits % TWO_PWR_32_DBL;
141
+ }
140
142
  }
141
- }
142
- add1e6digit(-24, -18);
143
- add1e6digit(-18, -12);
144
- add1e6digit(-12, -6);
145
- add1e6digit(-6);
146
- return [minus, lowBits, highBits];
143
+ add1e6digit(-24, -18);
144
+ add1e6digit(-18, -12);
145
+ add1e6digit(-12, -6);
146
+ add1e6digit(-6);
147
+ return [minus, lowBits, highBits];
147
148
  }
148
149
  /**
149
150
  * Format 64 bit integer value (as two JS numbers) to decimal string.
@@ -151,54 +152,52 @@ export function int64fromString(dec) {
151
152
  * Copyright 2008 Google Inc.
152
153
  */
153
154
  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;
155
+ // Skip the expensive conversion if the number is small enough to use the
156
+ // built-in conversions.
157
+ if (bitsHigh <= 0x1fffff) {
158
+ return "" + (TWO_PWR_32_DBL * bitsHigh + bitsLow);
192
159
  }
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
- );
160
+ // What this code is doing is essentially converting the input number from
161
+ // base-2 to base-1e7, which allows us to represent the 64-bit range with
162
+ // only 3 (very large) digits. Those digits are then trivial to convert to
163
+ // a base-10 string.
164
+ // The magic numbers used here are -
165
+ // 2^24 = 16777216 = (1,6777216) in base-1e7.
166
+ // 2^48 = 281474976710656 = (2,8147497,6710656) in base-1e7.
167
+ // Split 32:32 representation into 16:24:24 representation so our
168
+ // intermediate digits don't overflow.
169
+ let low = bitsLow & 0xffffff;
170
+ let mid = (((bitsLow >>> 24) | (bitsHigh << 8)) >>> 0) & 0xffffff;
171
+ let high = (bitsHigh >> 16) & 0xffff;
172
+ // Assemble our three base-1e7 digits, ignoring carries. The maximum
173
+ // value in a digit at this step is representable as a 48-bit integer, which
174
+ // can be stored in a 64-bit floating point number.
175
+ let digitA = low + mid * 6777216 + high * 6710656;
176
+ let digitB = mid + high * 8147497;
177
+ let digitC = high * 2;
178
+ // Apply carries from A to B and from B to C.
179
+ let base = 10000000;
180
+ if (digitA >= base) {
181
+ digitB += Math.floor(digitA / base);
182
+ digitA %= base;
183
+ }
184
+ if (digitB >= base) {
185
+ digitC += Math.floor(digitB / base);
186
+ digitB %= base;
187
+ }
188
+ // Convert base-1e7 digits to base-10, with optional leading zeroes.
189
+ function decimalFrom1e7(digit1e7, needLeadingZeros) {
190
+ let partial = digit1e7 ? String(digit1e7) : "";
191
+ if (needLeadingZeros) {
192
+ return "0000000".slice(partial.length) + partial;
193
+ }
194
+ return partial;
195
+ }
196
+ return (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));
202
201
  }
203
202
  /**
204
203
  * Write a 32 bit varint, signed or unsigned. Same as `varint64write(0, value, bytes)`
@@ -208,20 +207,21 @@ export function int64toString(bitsLow, bitsHigh) {
208
207
  * See https://github.com/protocolbuffers/protobuf/blob/1b18833f4f2a2f681f4e4a25cdf3b0a43115ec26/js/binary/encoder.js#L144
209
208
  */
210
209
  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;
210
+ if (value >= 0) {
211
+ // write value as varint 32
212
+ while (value > 0x7f) {
213
+ bytes.push((value & 0x7f) | 0x80);
214
+ value = value >>> 7;
215
+ }
216
+ bytes.push(value);
216
217
  }
217
- bytes.push(value);
218
- } else {
219
- for (let i = 0; i < 9; i++) {
220
- bytes.push((value & 127) | 128);
221
- value = value >> 7;
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);
222
224
  }
223
- bytes.push(1);
224
- }
225
225
  }
226
226
  /**
227
227
  * Read an unsigned 32 bit varint.
@@ -229,37 +229,38 @@ export function varint32write(value, bytes) {
229
229
  * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/buffer_decoder.js#L220
230
230
  */
231
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++)
232
+ let b = this.buf[this.pos++];
233
+ let result = b & 0x7f;
234
+ if ((b & 0x80) == 0) {
235
+ this.assertBounds();
236
+ return result;
237
+ }
260
238
  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;
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)
262
+ throw new Error("invalid varint");
263
+ this.assertBounds();
264
+ // Result can have 32 bits, convert it to unsigned
265
+ return result >>> 0;
265
266
  }
package/dist/esm/index.js CHANGED
@@ -1,7 +1,20 @@
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.
1
14
  export { proto3 } from "./proto3.js";
2
15
  export { proto2 } from "./proto2.js";
3
16
  export { protoInt64 } from "./proto-int64.js";
4
- export { Message } from "./message.js";
17
+ export { Message, } from "./message.js";
5
18
  export { ScalarType } from "./field.js";
6
19
  export { MethodKind, MethodIdempotency } from "./service-type.js";
7
20
  export { TypeRegistry } from "./type-registry.js";
@@ -1 +1,14 @@
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.
1
14
  export {};
@@ -1 +1,14 @@
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.
1
14
  export {};
@@ -1,3 +1,16 @@
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.
1
14
  /**
2
15
  * Message is the base class of every message, generated, or created at
3
16
  * runtime.
@@ -6,89 +19,80 @@
6
19
  * run time, use proto3.makeMessageType().
7
20
  */
8
21
  export class Message {
9
- /**
10
- * Compare with a message of the same type.
11
- */
12
- equals(other) {
13
- return this.getType().runtime.util.equals(this.getType(), this, other);
14
- }
15
- /**
16
- * Create a deep copy.
17
- */
18
- clone() {
19
- // return this.getType().runtime.util.clone(this);
20
- return this.getType().runtime.util.clone(this);
21
- }
22
- /**
23
- * Parse from binary data, merging fields.
24
- *
25
- * Repeated fields are appended. Map entries are added, overwriting
26
- * existing keys.
27
- *
28
- * If a message field is already present, it will be merged with the
29
- * new data.
30
- */
31
- fromBinary(bytes, options) {
32
- const type = this.getType(),
33
- format = type.runtime.bin,
34
- opt = format.makeReadOptions(options);
35
- format.readMessage(this, opt.readerFactory(bytes), bytes.byteLength, opt);
36
- return this;
37
- }
38
- /**
39
- * Parse a message from a JSON value.
40
- */
41
- fromJson(jsonValue, options) {
42
- const type = this.getType(),
43
- format = type.runtime.json,
44
- opt = format.makeReadOptions(options);
45
- format.readMessage(type, jsonValue, opt, this);
46
- return this;
47
- }
48
- /**
49
- * Parse a message from a JSON string.
50
- */
51
- fromJsonString(jsonString, options) {
52
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- assigning to JsonValue is safe here
53
- return this.fromJson(JSON.parse(jsonString), options);
54
- }
55
- /**
56
- * Serialize the message to binary data.
57
- */
58
- toBinary(options) {
59
- const type = this.getType(),
60
- bin = type.runtime.bin,
61
- opt = bin.makeWriteOptions(options),
62
- writer = opt.writerFactory();
63
- bin.writeMessage(this, writer, opt);
64
- return writer.finish();
65
- }
66
- /**
67
- * Serialize the message to a JSON value, a JavaScript value that can be
68
- * passed to JSON.stringify().
69
- */
70
- toJson(options) {
71
- const type = this.getType(),
72
- json = type.runtime.json,
73
- opt = json.makeWriteOptions(options);
74
- return json.writeMessage(this, opt);
75
- }
76
- /**
77
- * Serialize the message to a JSON string.
78
- */
79
- toJsonString(options) {
80
- const value = this.toJson(options);
81
- return JSON.stringify(value, null, options?.prettySpaces ?? 0);
82
- }
83
- /**
84
- * Retrieve the MessageType of this message - a singleton that represents
85
- * the protobuf message declaration and provides metadata for reflection-
86
- * based operations.
87
- */
88
- getType() {
89
- // Any class that extends Message _must_ provide a complete static
90
- // implementation of MessageType.
91
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return
92
- return Object.getPrototypeOf(this).constructor;
93
- }
22
+ /**
23
+ * Compare with a message of the same type.
24
+ */
25
+ equals(other) {
26
+ return this.getType().runtime.util.equals(this.getType(), this, other);
27
+ }
28
+ /**
29
+ * Create a deep copy.
30
+ */
31
+ clone() {
32
+ // return this.getType().runtime.util.clone(this);
33
+ return this.getType().runtime.util.clone(this);
34
+ }
35
+ /**
36
+ * Parse from binary data, merging fields.
37
+ *
38
+ * Repeated fields are appended. Map entries are added, overwriting
39
+ * existing keys.
40
+ *
41
+ * If a message field is already present, it will be merged with the
42
+ * new data.
43
+ */
44
+ fromBinary(bytes, options) {
45
+ const type = this.getType(), format = type.runtime.bin, opt = format.makeReadOptions(options);
46
+ format.readMessage(this, opt.readerFactory(bytes), bytes.byteLength, opt);
47
+ return this;
48
+ }
49
+ /**
50
+ * Parse a message from a JSON value.
51
+ */
52
+ fromJson(jsonValue, options) {
53
+ const type = this.getType(), format = type.runtime.json, opt = format.makeReadOptions(options);
54
+ format.readMessage(type, jsonValue, opt, this);
55
+ return this;
56
+ }
57
+ /**
58
+ * Parse a message from a JSON string.
59
+ */
60
+ fromJsonString(jsonString, options) {
61
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- assigning to JsonValue is safe here
62
+ return this.fromJson(JSON.parse(jsonString), options);
63
+ }
64
+ /**
65
+ * Serialize the message to binary data.
66
+ */
67
+ toBinary(options) {
68
+ const type = this.getType(), bin = type.runtime.bin, opt = bin.makeWriteOptions(options), writer = opt.writerFactory();
69
+ bin.writeMessage(this, writer, opt);
70
+ return writer.finish();
71
+ }
72
+ /**
73
+ * Serialize the message to a JSON value, a JavaScript value that can be
74
+ * passed to JSON.stringify().
75
+ */
76
+ toJson(options) {
77
+ const type = this.getType(), json = type.runtime.json, opt = json.makeWriteOptions(options);
78
+ return json.writeMessage(this, opt);
79
+ }
80
+ /**
81
+ * Serialize the message to a JSON string.
82
+ */
83
+ toJsonString(options) {
84
+ const value = this.toJson(options);
85
+ return JSON.stringify(value, null, options?.prettySpaces ?? 0);
86
+ }
87
+ /**
88
+ * Retrieve the MessageType of this message - a singleton that represents
89
+ * the protobuf message declaration and provides metadata for reflection-
90
+ * based operations.
91
+ */
92
+ getType() {
93
+ // Any class that extends Message _must_ provide a complete static
94
+ // implementation of MessageType.
95
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return
96
+ return Object.getPrototypeOf(this).constructor;
97
+ }
94
98
  }