@bufbuild/protobuf 0.0.1-alpha.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 (146) hide show
  1. package/README.md +6 -0
  2. package/dist/cjs/binary-encoding.js +434 -0
  3. package/dist/cjs/binary-format.js +2 -0
  4. package/dist/cjs/descriptor-registry.js +518 -0
  5. package/dist/cjs/descriptor-set.js +557 -0
  6. package/dist/cjs/enum.js +2 -0
  7. package/dist/cjs/field-list.js +2 -0
  8. package/dist/cjs/field.js +40 -0
  9. package/dist/cjs/google/protobuf/any_pb.js +280 -0
  10. package/dist/cjs/google/protobuf/api_pb.js +383 -0
  11. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +334 -0
  12. package/dist/cjs/google/protobuf/descriptor_pb.js +2311 -0
  13. package/dist/cjs/google/protobuf/duration_pb.js +201 -0
  14. package/dist/cjs/google/protobuf/empty_pb.js +72 -0
  15. package/dist/cjs/google/protobuf/field_mask_pb.js +347 -0
  16. package/dist/cjs/google/protobuf/source_context_pb.js +74 -0
  17. package/dist/cjs/google/protobuf/struct_pb.js +288 -0
  18. package/dist/cjs/google/protobuf/timestamp_pb.js +271 -0
  19. package/dist/cjs/google/protobuf/type_pb.js +613 -0
  20. package/dist/cjs/google/protobuf/wrappers_pb.js +658 -0
  21. package/dist/cjs/google/varint.js +280 -0
  22. package/dist/cjs/index.js +148 -0
  23. package/dist/cjs/json-format.js +2 -0
  24. package/dist/cjs/message-type.js +2 -0
  25. package/dist/cjs/message.js +98 -0
  26. package/dist/cjs/private/assert.js +52 -0
  27. package/dist/cjs/private/base64.js +118 -0
  28. package/dist/cjs/private/binary-format-common.js +253 -0
  29. package/dist/cjs/private/binary-format-proto2.js +120 -0
  30. package/dist/cjs/private/binary-format-proto3.js +102 -0
  31. package/dist/cjs/private/enum.js +70 -0
  32. package/dist/cjs/private/field-list.js +62 -0
  33. package/dist/cjs/private/field-wrapper.js +25 -0
  34. package/dist/cjs/private/field.js +34 -0
  35. package/dist/cjs/private/json-format-common.js +482 -0
  36. package/dist/cjs/private/json-format-proto2.js +100 -0
  37. package/dist/cjs/private/json-format-proto3.js +103 -0
  38. package/dist/cjs/private/message-type.js +37 -0
  39. package/dist/cjs/private/names.js +101 -0
  40. package/dist/cjs/private/options-map.js +2 -0
  41. package/dist/cjs/private/proto-runtime.js +24 -0
  42. package/dist/cjs/private/scalars.js +138 -0
  43. package/dist/cjs/private/util-common.js +229 -0
  44. package/dist/cjs/private/util.js +2 -0
  45. package/dist/cjs/proto-int64.js +121 -0
  46. package/dist/cjs/proto2.js +86 -0
  47. package/dist/cjs/proto3.js +99 -0
  48. package/dist/cjs/service-type.js +43 -0
  49. package/dist/cjs/type-registry.js +42 -0
  50. package/dist/esm/binary-encoding.js +434 -0
  51. package/dist/esm/binary-format.js +1 -0
  52. package/dist/esm/descriptor-registry.js +521 -0
  53. package/dist/esm/descriptor-set.js +517 -0
  54. package/dist/esm/enum.js +1 -0
  55. package/dist/esm/field-list.js +1 -0
  56. package/dist/esm/field.js +37 -0
  57. package/dist/esm/google/protobuf/any_pb.js +276 -0
  58. package/dist/esm/google/protobuf/api_pb.js +350 -0
  59. package/dist/esm/google/protobuf/compiler/plugin_pb.js +318 -0
  60. package/dist/esm/google/protobuf/descriptor_pb.js +2213 -0
  61. package/dist/esm/google/protobuf/duration_pb.js +197 -0
  62. package/dist/esm/google/protobuf/empty_pb.js +68 -0
  63. package/dist/esm/google/protobuf/field_mask_pb.js +343 -0
  64. package/dist/esm/google/protobuf/source_context_pb.js +70 -0
  65. package/dist/esm/google/protobuf/struct_pb.js +282 -0
  66. package/dist/esm/google/protobuf/timestamp_pb.js +267 -0
  67. package/dist/esm/google/protobuf/type_pb.js +569 -0
  68. package/dist/esm/google/protobuf/wrappers_pb.js +574 -0
  69. package/dist/esm/google/varint.js +265 -0
  70. package/dist/esm/index.js +24 -0
  71. package/dist/esm/json-format.js +1 -0
  72. package/dist/esm/message-type.js +1 -0
  73. package/dist/esm/message.js +94 -0
  74. package/dist/esm/private/assert.js +41 -0
  75. package/dist/esm/private/base64.js +113 -0
  76. package/dist/esm/private/binary-format-common.js +243 -0
  77. package/dist/esm/private/binary-format-proto2.js +87 -0
  78. package/dist/esm/private/binary-format-proto3.js +74 -0
  79. package/dist/esm/private/enum.js +64 -0
  80. package/dist/esm/private/field-list.js +58 -0
  81. package/dist/esm/private/field-wrapper.js +20 -0
  82. package/dist/esm/private/field.js +27 -0
  83. package/dist/esm/private/json-format-common.js +473 -0
  84. package/dist/esm/private/json-format-proto2.js +90 -0
  85. package/dist/esm/private/json-format-proto3.js +89 -0
  86. package/dist/esm/private/message-type.js +33 -0
  87. package/dist/esm/private/names.js +90 -0
  88. package/dist/esm/private/options-map.js +1 -0
  89. package/dist/esm/private/proto-runtime.js +15 -0
  90. package/dist/esm/private/scalars.js +129 -0
  91. package/dist/esm/private/util-common.js +207 -0
  92. package/dist/esm/private/util.js +1 -0
  93. package/dist/esm/proto-int64.js +118 -0
  94. package/dist/esm/proto2.js +77 -0
  95. package/dist/esm/proto3.js +90 -0
  96. package/dist/esm/service-type.js +37 -0
  97. package/dist/esm/type-registry.js +38 -0
  98. package/dist/types/binary-encoding.d.ts +421 -0
  99. package/dist/types/binary-format.d.ts +109 -0
  100. package/dist/types/descriptor-registry.d.ts +42 -0
  101. package/dist/types/descriptor-set.d.ts +171 -0
  102. package/dist/types/enum.d.ts +31 -0
  103. package/dist/types/field-list.d.ts +27 -0
  104. package/dist/types/field.d.ts +295 -0
  105. package/dist/types/google/protobuf/any_pb.d.ts +167 -0
  106. package/dist/types/google/protobuf/api_pb.d.ts +296 -0
  107. package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +294 -0
  108. package/dist/types/google/protobuf/descriptor_pb.d.ts +1959 -0
  109. package/dist/types/google/protobuf/duration_pb.d.ts +117 -0
  110. package/dist/types/google/protobuf/empty_pb.d.ts +44 -0
  111. package/dist/types/google/protobuf/field_mask_pb.d.ts +243 -0
  112. package/dist/types/google/protobuf/source_context_pb.d.ts +44 -0
  113. package/dist/types/google/protobuf/struct_pb.d.ts +205 -0
  114. package/dist/types/google/protobuf/timestamp_pb.d.ts +151 -0
  115. package/dist/types/google/protobuf/type_pb.d.ts +482 -0
  116. package/dist/types/google/protobuf/wrappers_pb.d.ts +379 -0
  117. package/dist/types/google/varint.d.ts +65 -0
  118. package/dist/types/index.d.ts +53 -0
  119. package/dist/types/json-format.d.ts +129 -0
  120. package/dist/types/message-type.d.ts +58 -0
  121. package/dist/types/message.d.ts +124 -0
  122. package/dist/types/private/assert.d.ts +19 -0
  123. package/dist/types/private/base64.d.ts +18 -0
  124. package/dist/types/private/binary-format-common.d.ts +38 -0
  125. package/dist/types/private/binary-format-proto2.d.ts +2 -0
  126. package/dist/types/private/binary-format-proto3.d.ts +2 -0
  127. package/dist/types/private/enum.d.ts +29 -0
  128. package/dist/types/private/field-list.d.ts +25 -0
  129. package/dist/types/private/field-wrapper.d.ts +27 -0
  130. package/dist/types/private/field.d.ts +15 -0
  131. package/dist/types/private/json-format-common.d.ts +30 -0
  132. package/dist/types/private/json-format-proto2.d.ts +2 -0
  133. package/dist/types/private/json-format-proto3.d.ts +2 -0
  134. package/dist/types/private/message-type.d.ts +15 -0
  135. package/dist/types/private/names.d.ts +19 -0
  136. package/dist/types/private/options-map.d.ts +7 -0
  137. package/dist/types/private/proto-runtime.d.ts +55 -0
  138. package/dist/types/private/scalars.d.ts +33 -0
  139. package/dist/types/private/util-common.d.ts +5 -0
  140. package/dist/types/private/util.d.ts +55 -0
  141. package/dist/types/proto-int64.d.ts +94 -0
  142. package/dist/types/proto2.d.ts +4 -0
  143. package/dist/types/proto3.d.ts +4 -0
  144. package/dist/types/service-type.d.ts +118 -0
  145. package/dist/types/type-registry.d.ts +37 -0
  146. package/package.json +31 -0
@@ -0,0 +1,265 @@
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
+ }
@@ -0,0 +1,24 @@
1
+ export { proto3 } from "./proto3.js";
2
+ export { proto2 } from "./proto2.js";
3
+ export { protoInt64 } from "./proto-int64.js";
4
+ export { Message } from "./message.js";
5
+ export { ScalarType } from "./field.js";
6
+ export { MethodKind, MethodIdempotency } from "./service-type.js";
7
+ export { TypeRegistry } from "./type-registry.js";
8
+ export { DescriptorRegistry } from "./descriptor-registry.js";
9
+ export { DescriptorSet } from "./descriptor-set.js";
10
+ export { WireType, BinaryWriter, BinaryReader } from "./binary-encoding.js";
11
+ export {} from "./json-format.js";
12
+ // ideally, we would export these types with sub-path exports:
13
+ export * from "./google/protobuf/compiler/plugin_pb.js";
14
+ export * from "./google/protobuf/api_pb.js";
15
+ export * from "./google/protobuf/any_pb.js";
16
+ export * from "./google/protobuf/descriptor_pb.js";
17
+ export * from "./google/protobuf/duration_pb.js";
18
+ export * from "./google/protobuf/empty_pb.js";
19
+ export * from "./google/protobuf/field_mask_pb.js";
20
+ export * from "./google/protobuf/source_context_pb.js";
21
+ export * from "./google/protobuf/struct_pb.js";
22
+ export * from "./google/protobuf/timestamp_pb.js";
23
+ export * from "./google/protobuf/type_pb.js";
24
+ export * from "./google/protobuf/wrappers_pb.js";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Message is the base class of every message, generated, or created at
3
+ * runtime.
4
+ *
5
+ * It is _not_ safe to extend this class. If you want to create a message at
6
+ * run time, use proto3.makeMessageType().
7
+ */
8
+ 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
+ }
94
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Assert that condition is truthy or throw error (with message)
3
+ */
4
+ export function assert(condition, msg) {
5
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- we want the implicit conversion to boolean
6
+ if (!condition) {
7
+ throw new Error(msg);
8
+ }
9
+ }
10
+ const FLOAT32_MAX = 3.4028234663852886e38,
11
+ FLOAT32_MIN = -3.4028234663852886e38,
12
+ UINT32_MAX = 0xffffffff,
13
+ INT32_MAX = 0x7fffffff,
14
+ INT32_MIN = -0x80000000;
15
+ /**
16
+ * Assert a valid signed protobuf 32-bit integer.
17
+ */
18
+ export function assertInt32(arg) {
19
+ if (typeof arg !== "number") throw new Error("invalid int 32: " + typeof arg);
20
+ if (!Number.isInteger(arg) || arg > INT32_MAX || arg < INT32_MIN)
21
+ throw new Error("invalid int 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
22
+ }
23
+ /**
24
+ * Assert a valid unsigned protobuf 32-bit integer.
25
+ */
26
+ export function assertUInt32(arg) {
27
+ if (typeof arg !== "number")
28
+ throw new Error("invalid uint 32: " + typeof arg);
29
+ if (!Number.isInteger(arg) || arg > UINT32_MAX || arg < 0)
30
+ throw new Error("invalid uint 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
31
+ }
32
+ /**
33
+ * Assert a valid protobuf float value.
34
+ */
35
+ export function assertFloat32(arg) {
36
+ if (typeof arg !== "number")
37
+ throw new Error("invalid float 32: " + typeof arg);
38
+ if (!Number.isFinite(arg)) return;
39
+ if (arg > FLOAT32_MAX || arg < FLOAT32_MIN)
40
+ throw new Error("invalid float 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
41
+ }
@@ -0,0 +1,113 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unnecessary-condition, prefer-const */
2
+ // lookup table from base64 character to byte
3
+ let encTable =
4
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
5
+ // lookup table from base64 character *code* to byte because lookup by number is fast
6
+ let decTable = [];
7
+ for (let i = 0; i < encTable.length; i++)
8
+ decTable[encTable[i].charCodeAt(0)] = i;
9
+ // support base64url variants
10
+ decTable["-".charCodeAt(0)] = encTable.indexOf("+");
11
+ decTable["_".charCodeAt(0)] = encTable.indexOf("/");
12
+ /**
13
+ * Decodes a base64 string to a byte array.
14
+ *
15
+ * - ignores white-space, including line breaks and tabs
16
+ * - allows inner padding (can decode concatenated base64 strings)
17
+ * - does not require padding
18
+ * - understands base64url encoding:
19
+ * "-" instead of "+",
20
+ * "_" instead of "/",
21
+ * no padding
22
+ */
23
+ export function base64decode(base64Str) {
24
+ // estimate byte size, not accounting for inner padding and whitespace
25
+ let es = (base64Str.length * 3) / 4;
26
+ // if (es % 3 !== 0)
27
+ // throw new Error("invalid base64 string");
28
+ if (base64Str[base64Str.length - 2] == "=") es -= 2;
29
+ else if (base64Str[base64Str.length - 1] == "=") es -= 1;
30
+ let bytes = new Uint8Array(es),
31
+ bytePos = 0, // position in byte array
32
+ groupPos = 0, // position in base64 group
33
+ b, // current byte
34
+ p = 0; // previous byte
35
+ for (let i = 0; i < base64Str.length; i++) {
36
+ b = decTable[base64Str.charCodeAt(i)];
37
+ if (b === undefined) {
38
+ switch (base64Str[i]) {
39
+ // @ts-ignore TS7029: Fallthrough case in switch
40
+ case "=":
41
+ groupPos = 0; // reset state when padding found
42
+ // @ts-ignore TS7029: Fallthrough case in switch
43
+ case "\n":
44
+ case "\r":
45
+ case "\t":
46
+ case " ":
47
+ continue; // skip white-space, and padding
48
+ default:
49
+ throw Error("invalid base64 string.");
50
+ }
51
+ }
52
+ switch (groupPos) {
53
+ case 0:
54
+ p = b;
55
+ groupPos = 1;
56
+ break;
57
+ case 1:
58
+ bytes[bytePos++] = (p << 2) | ((b & 48) >> 4);
59
+ p = b;
60
+ groupPos = 2;
61
+ break;
62
+ case 2:
63
+ bytes[bytePos++] = ((p & 15) << 4) | ((b & 60) >> 2);
64
+ p = b;
65
+ groupPos = 3;
66
+ break;
67
+ case 3:
68
+ bytes[bytePos++] = ((p & 3) << 6) | b;
69
+ groupPos = 0;
70
+ break;
71
+ }
72
+ }
73
+ if (groupPos == 1) throw Error("invalid base64 string.");
74
+ return bytes.subarray(0, bytePos);
75
+ }
76
+ /**
77
+ * Encodes a byte array to a base64 string.
78
+ * Adds padding at the end.
79
+ * Does not insert newlines.
80
+ */
81
+ export function base64encode(bytes) {
82
+ let base64 = "",
83
+ groupPos = 0, // position in base64 group
84
+ b, // current byte
85
+ p = 0; // carry over from previous byte
86
+ for (let i = 0; i < bytes.length; i++) {
87
+ b = bytes[i];
88
+ switch (groupPos) {
89
+ case 0:
90
+ base64 += encTable[b >> 2];
91
+ p = (b & 3) << 4;
92
+ groupPos = 1;
93
+ break;
94
+ case 1:
95
+ base64 += encTable[p | (b >> 4)];
96
+ p = (b & 15) << 2;
97
+ groupPos = 2;
98
+ break;
99
+ case 2:
100
+ base64 += encTable[p | (b >> 6)];
101
+ base64 += encTable[b & 63];
102
+ groupPos = 0;
103
+ break;
104
+ }
105
+ }
106
+ // padding required?
107
+ if (groupPos) {
108
+ base64 += encTable[p];
109
+ base64 += "=";
110
+ if (groupPos == 1) base64 += "=";
111
+ }
112
+ return base64;
113
+ }