Debian Codegraph @

node/@bufbuild/protobuf/dist/commonjs/wire/binary-encoding.js

raw
1 "use strict";
2 // Copyright 2021-2026 Buf Technologies, Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 Object.defineProperty(exports, "__esModule", { value: true });
16 exports.BinaryReader = exports.BinaryWriter = exports.INT32_MIN = exports.INT32_MAX = exports.UINT32_MAX = exports.FLOAT32_MIN = exports.FLOAT32_MAX = exports.WireType = void 0;
17 const varint_js_1 = require("./varint.js");
18 const proto_int64_js_1 = require("../proto-int64.js");
19 const text_encoding_js_1 = require("./text-encoding.js");
20 /**
21 * Protobuf binary format wire types.
22 *
23 * A wire type provides just enough information to find the length of the
24 * following value.
25 *
26 * See https://developers.google.com/protocol-buffers/docs/encoding#structure
27 */
28 var WireType;
29 (function (WireType) {
30 /**
31 * Used for int32, int64, uint32, uint64, sint32, sint64, bool, enum
32 */
33 WireType[WireType["Varint"] = 0] = "Varint";
34 /**
35 * Used for fixed64, sfixed64, double.
36 * Always 8 bytes with little-endian byte order.
37 */
38 WireType[WireType["Bit64"] = 1] = "Bit64";
39 /**
40 * Used for string, bytes, embedded messages, packed repeated fields
41 *
42 * Only repeated numeric types (types which use the varint, 32-bit,
43 * or 64-bit wire types) can be packed. In proto3, such fields are
44 * packed by default.
45 */
46 WireType[WireType["LengthDelimited"] = 2] = "LengthDelimited";
47 /**
48 * Start of a tag-delimited aggregate, such as a proto2 group, or a message
49 * in editions with message_encoding = DELIMITED.
50 */
51 WireType[WireType["StartGroup"] = 3] = "StartGroup";
52 /**
53 * End of a tag-delimited aggregate.
54 */
55 WireType[WireType["EndGroup"] = 4] = "EndGroup";
56 /**
57 * Used for fixed32, sfixed32, float.
58 * Always 4 bytes with little-endian byte order.
59 */
60 WireType[WireType["Bit32"] = 5] = "Bit32";
61 })(WireType || (exports.WireType = WireType = {}));
62 /**
63 * Maximum value for a 32-bit floating point value (Protobuf FLOAT).
64 */
65 exports.FLOAT32_MAX = 3.4028234663852886e38;
66 /**
67 * Minimum value for a 32-bit floating point value (Protobuf FLOAT).
68 */
69 exports.FLOAT32_MIN = -3.4028234663852886e38;
70 /**
71 * Maximum value for an unsigned 32-bit integer (Protobuf UINT32, FIXED32).
72 */
73 exports.UINT32_MAX = 0xffffffff;
74 /**
75 * Maximum value for a signed 32-bit integer (Protobuf INT32, SFIXED32, SINT32).
76 */
77 exports.INT32_MAX = 0x7fffffff;
78 /**
79 * Minimum value for a signed 32-bit integer (Protobuf INT32, SFIXED32, SINT32).
80 */
81 exports.INT32_MIN = -0x80000000;
82 class BinaryWriter {
83 constructor(encodeUtf8) {
84 /**
85 * Previous fork positions (the write position at the time
86 * `fork()` was called).
87 */
88 this.stackPos = [];
89 this.encodeUtf8Into = encodeUtf8
90 ? (0, text_encoding_js_1.emulateEncodeInto)(encodeUtf8)
91 : (0, text_encoding_js_1.getTextEncoding)().encodeUtf8Into;
92 this.buffer = EMPTY_BUFFER;
93 this.viewCache = EMPTY_VIEW;
94 this.pos = 0;
95 }
96 ensureCapacity(size) {
97 const required = this.pos + size;
98 if (required > this.buffer.length) {
99 let newLen = this.buffer.length || INITIAL_SIZE;
100 while (newLen < required)
101 newLen *= 2;
102 const newBuf = new Uint8Array(newLen);
103 if (this.pos > 0)
104 newBuf.set(this.buffer);
105 this.buffer = newBuf;
106 }
107 }
108 /**
109 * The DataView over `buffer`, rebuilt only if the buffer has grown since it
110 * was last used.
111 */
112 view() {
113 const bytes = this.buffer;
114 const view = this.viewCache;
115 // Since ensureCapacity() only ever replaces the buffer with a strictly larger one,
116 // equal lengths mean the view is still current. This is faster than comparing
117 // buffers directly.
118 if (view.byteLength === bytes.byteLength)
119 return view;
120 const newView = new DataView(bytes.buffer);
121 this.viewCache = newView;
122 return newView;
123 }
124 /**
125 * Return all bytes written and reset this writer.
126 */
127 finish() {
128 const result = this.buffer.slice(0, this.pos);
129 this.pos = 0;
130 this.stackPos = [];
131 return result;
132 }
133 /**
134 * Start a new fork for length-delimited data like a message
135 * or a packed repeated field.
136 *
137 * Must be joined later with `join()`.
138 */
139 fork() {
140 this.stackPos.push(this.pos);
141 // Reserve room for the length prefix. Payloads under 128 bytes, fairly
142 // common, will need no copy in join().
143 this.ensureCapacity(DEFAULT_LEN_PREFIX_SIZE);
144 this.buffer[this.pos++] = 0;
145 return this;
146 }
147 /**
148 * Join the last fork. Write its length and bytes, then
149 * return to the previous state.
150 */
151 join() {
152 const forkPos = this.stackPos.pop();
153 if (forkPos === undefined)
154 throw new Error("invalid state, fork stack empty");
155 // fork() presumed the payload would fit the prefix it reserved. If it
156 // doesn't, we need to shift the bytes we just wrote.
157 const len = this.pos - forkPos - DEFAULT_LEN_PREFIX_SIZE;
158 const lenPrefixSize = varint32Size(len);
159 if (lenPrefixSize > DEFAULT_LEN_PREFIX_SIZE) {
160 // Widening pushes the payload past the end of the buffer, so grow first:
161 // copyWithin clamps to the buffer instead of throwing, so a short buffer
162 // would silently drop the tail of the payload.
163 this.ensureCapacity(lenPrefixSize - DEFAULT_LEN_PREFIX_SIZE);
164 this.buffer.copyWithin(forkPos + lenPrefixSize, forkPos + DEFAULT_LEN_PREFIX_SIZE, this.pos);
165 }
166 this.pos = forkPos;
167 this.uint32(len);
168 this.pos += len;
169 return this;
170 }
171 /**
172 * Writes a tag (field number and wire type).
173 *
174 * Equivalent to `uint32( (fieldNo << 3 | type) >>> 0 )`.
175 *
176 * Generated code should compute the tag ahead of time and call `uint32()`.
177 */
178 tag(fieldNo, type) {
179 return this.uint32(((fieldNo << 3) | type) >>> 0);
180 }
181 /**
182 * Write a chunk of raw bytes.
183 */
184 raw(chunk) {
185 this.ensureCapacity(chunk.length);
186 this.buffer.set(chunk, this.pos);
187 this.pos += chunk.length;
188 return this;
189 }
190 /**
191 * Write a `uint32` value, an unsigned 32 bit varint.
192 */
193 uint32(value) {
194 assertUInt32(value);
195 // uint32 varints are at most 5 bytes; reserve once and avoid per-byte
196 // capacity checks.
197 this.ensureCapacity(5);
198 if (value < 0x80) {
199 this.buffer[this.pos++] = value;
200 return this;
201 }
202 while (value > 0x7f) {
203 this.buffer[this.pos++] = (value & 0x7f) | 0x80;
204 value >>>= 7;
205 }
206 this.buffer[this.pos++] = value;
207 return this;
208 }
209 /**
210 * Write a `int32` value, a signed 32 bit varint.
211 */
212 int32(value) {
213 assertInt32(value);
214 if (value >= 0) {
215 return this.uint32(value);
216 }
217 // Negative: sign-extend to 64 bits, encodes to 10 bytes.
218 this.ensureCapacity(10);
219 for (let i = 0; i < 9; i++) {
220 this.buffer[this.pos++] = (value & 0x7f) | 0x80;
221 value >>= 7;
222 }
223 this.buffer[this.pos++] = 1;
224 return this;
225 }
226 /**
227 * Write a `bool` value, a varint.
228 */
229 bool(value) {
230 this.ensureCapacity(1);
231 this.buffer[this.pos++] = value ? 1 : 0;
232 return this;
233 }
234 /**
235 * Write a `bytes` value, length-delimited arbitrary data.
236 */
237 bytes(value) {
238 this.uint32(value.byteLength);
239 return this.raw(value);
240 }
241 /**
242 * Write a `string` value, length-delimited data converted to UTF-8 text.
243 */
244 string(value) {
245 // TextEncoder.encode() coerces its argument to string, but encodeInto()
246 // rejects non-strings.
247 if (typeof value !== "string") {
248 value = String(value);
249 }
250 const len = value.length;
251 // Fast path for ASCII.
252 if (len <= ASCII_MAX_LENGTH) {
253 this.ensureCapacity(len + 1);
254 const ascii = this.buffer;
255 let pos = this.pos;
256 ascii[pos++] = len;
257 let i = 0;
258 for (; i < len; i++) {
259 const code = value.charCodeAt(i);
260 if (code > 0x7f)
261 break;
262 ascii[pos++] = code;
263 }
264 if (i == len) {
265 this.pos = pos;
266 return this;
267 }
268 }
269 // encodeUtf8Into needs the full-length buffer upfront. The length prefix
270 // can be upto 5 bytes, and a UTF-16 code unit takes at most 3 UTF-8 bytes.
271 this.ensureCapacity(len * 3 + 5);
272 // The length prefix goes first, but the byte length is only known after
273 // encoding. We guess the final varint size here (assuming most text is
274 // ASCII) and then encode.
275 const lenPrefixSizeGuess = varint32Size(len);
276 const buf = this.buffer;
277 const start = this.pos;
278 const { written } = this.encodeUtf8Into(value, buf.subarray(start + lenPrefixSizeGuess));
279 // If our guess was incorrect, we need to shift the bytes we just wrote.
280 const lenPrefixSize = varint32Size(written);
281 if (lenPrefixSize != lenPrefixSizeGuess) {
282 buf.copyWithin(start + lenPrefixSize, start + lenPrefixSizeGuess, start + lenPrefixSizeGuess + written);
283 }
284 // Write the lenPrefix and advance the pos.
285 this.uint32(written);
286 this.pos += written;
287 return this;
288 }
289 /**
290 * Write a `float` value, 32-bit floating point number.
291 */
292 float(value) {
293 assertFloat32(value);
294 this.ensureCapacity(4);
295 this.view().setFloat32(this.pos, value, true);
296 this.pos += 4;
297 return this;
298 }
299 /**
300 * Write a `double` value, a 64-bit floating point number.
301 */
302 double(value) {
303 this.ensureCapacity(8);
304 this.view().setFloat64(this.pos, value, true);
305 this.pos += 8;
306 return this;
307 }
308 /**
309 * Write a `fixed32` value, an unsigned, fixed-length 32-bit integer.
310 */
311 fixed32(value) {
312 assertUInt32(value);
313 this.ensureCapacity(4);
314 this.view().setUint32(this.pos, value, true);
315 this.pos += 4;
316 return this;
317 }
318 /**
319 * Write a `sfixed32` value, a signed, fixed-length 32-bit integer.
320 */
321 sfixed32(value) {
322 assertInt32(value);
323 this.ensureCapacity(4);
324 this.view().setInt32(this.pos, value, true);
325 this.pos += 4;
326 return this;
327 }
328 /**
329 * Write a `sint32` value, a signed, zigzag-encoded 32-bit varint.
330 */
331 sint32(value) {
332 assertInt32(value);
333 // zigzag encode then emit as uint32 varint
334 return this.uint32(((value << 1) ^ (value >> 31)) >>> 0);
335 }
336 /**
337 * Write a `sfixed64` value, a signed, fixed-length 64-bit integer.
338 */
339 sfixed64(value) {
340 const tc = proto_int64_js_1.protoInt64.enc(value);
341 this.ensureCapacity(8);
342 const view = this.view();
343 view.setInt32(this.pos, tc.lo, true);
344 view.setInt32(this.pos + 4, tc.hi, true);
345 this.pos += 8;
346 return this;
347 }
348 /**
349 * Write a `fixed64` value, an unsigned, fixed-length 64 bit integer.
350 */
351 fixed64(value) {
352 const tc = proto_int64_js_1.protoInt64.uEnc(value);
353 this.ensureCapacity(8);
354 const view = this.view();
355 view.setInt32(this.pos, tc.lo, true);
356 view.setInt32(this.pos + 4, tc.hi, true);
357 this.pos += 8;
358 return this;
359 }
360 /**
361 * Write a `int64` value, a signed 64-bit varint.
362 */
363 int64(value) {
364 const tc = proto_int64_js_1.protoInt64.enc(value);
365 return this.writeVarint64(tc.lo, tc.hi);
366 }
367 /**
368 * Write a `sint64` value, a signed, zig-zag-encoded 64-bit varint.
369 */
370 sint64(value) {
371 const tc = proto_int64_js_1.protoInt64.enc(value),
372 // zigzag encode
373 sign = tc.hi >> 31, lo = (tc.lo << 1) ^ sign, hi = ((tc.hi << 1) | (tc.lo >>> 31)) ^ sign;
374 return this.writeVarint64(lo, hi);
375 }
376 /**
377 * Write a `uint64` value, an unsigned 64-bit varint.
378 */
379 uint64(value) {
380 const tc = proto_int64_js_1.protoInt64.uEnc(value);
381 return this.writeVarint64(tc.lo, tc.hi);
382 }
383 /**
384 * Write a 64-bit varint directly into the buffer. Accepts the value as
385 * split low/high 32-bit words.
386 *
387 * Ported from varint64write() to avoid the intermediate number[] buffer.
388 * See https://github.com/protocolbuffers/protobuf/blob/8a71927d74a4ce34efe2d8769fda198f52d20d12/js/experimental/runtime/kernel/writer.js#L344
389 */
390 writeVarint64(lo, hi) {
391 // Worst case: 10 bytes.
392 this.ensureCapacity(10);
393 const buf = this.buffer;
394 let pos = this.pos;
395 for (let i = 0; i < 28; i = i + 7) {
396 const shift = lo >>> i;
397 const hasNext = !(shift >>> 7 == 0 && hi == 0);
398 buf[pos++] = (hasNext ? shift | 0x80 : shift) & 0xff;
399 if (!hasNext) {
400 this.pos = pos;
401 return this;
402 }
403 }
404 const splitBits = ((lo >>> 28) & 0x0f) | ((hi & 0x07) << 4);
405 const hasMoreBits = !(hi >> 3 == 0);
406 buf[pos++] = (hasMoreBits ? splitBits | 0x80 : splitBits) & 0xff;
407 if (!hasMoreBits) {
408 this.pos = pos;
409 return this;
410 }
411 for (let i = 3; i < 31; i = i + 7) {
412 const shift = hi >>> i;
413 const hasNext = !(shift >>> 7 == 0);
414 buf[pos++] = (hasNext ? shift | 0x80 : shift) & 0xff;
415 if (!hasNext) {
416 this.pos = pos;
417 return this;
418 }
419 }
420 buf[pos++] = (hi >>> 31) & 0x01;
421 this.pos = pos;
422 return this;
423 }
424 }
425 exports.BinaryWriter = BinaryWriter;
426 /**
427 * Capacity of the buffer allocated by the first write..
428 */
429 const INITIAL_SIZE = 128;
430 /**
431 * Bytes `fork()` reserves for the length prefix, betting that the payload will
432 * be under 128 bytes. `join()` fills them in, and widens them if the bet was
433 * wrong.
434 */
435 const DEFAULT_LEN_PREFIX_SIZE = 1;
436 /**
437 * Shared empty buffer used as the initial value before the first write.
438 * Avoids allocating and zeroing `INITIAL_SIZE` bytes per BinaryWriter when a
439 * writer is only used for a tiny message (or not used at all).
440 */
441 const EMPTY_BUFFER = new Uint8Array(0);
442 /**
443 * Shared empty view, paired with `EMPTY_BUFFER`. Never written to: any
444 * fixed-width write first grows the buffer, which replaces this view.
445 */
446 const EMPTY_VIEW = new DataView(EMPTY_BUFFER.buffer);
447 /**
448 * Longest string on the ASCII fast paths. Must stay below 0x80, so
449 * that the writer's length prefix always fits a single varint byte.
450 */
451 const ASCII_MAX_LENGTH = 32;
452 /**
453 * Number of bytes needed to encode `value` as an unsigned 32-bit varint.
454 */
455 function varint32Size(value) {
456 if (value < 0x80)
457 return 1;
458 if (value < 0x4000)
459 return 2;
460 if (value < 0x200000)
461 return 3;
462 if (value < 0x10000000)
463 return 4;
464 return 5;
465 }
466 class BinaryReader {
467 constructor(buf, decodeUtf8 = (0, text_encoding_js_1.getTextEncoding)().decodeUtf8) {
468 this.decodeUtf8 = decodeUtf8;
469 this.varint64Lo = 0;
470 this.varint64Hi = 0;
471 this.varint64 = varint_js_1.varint64read; // dirty cast for `this`
472 /**
473 * Read a `uint32` field, an unsigned 32 bit varint.
474 */
475 this.uint32 = varint_js_1.varint32read;
476 this.buf = buf;
477 this.len = buf.length;
478 this.pos = 0;
479 this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
480 }
481 /**
482 * Reads a tag - field number and wire type. Tags are uint32 varints; values
483 * that do not fit in uint32 are rejected.
484 */
485 tag() {
486 const start = this.pos;
487 const tag = this.uint32();
488 const bytesRead = this.pos - start;
489 if (bytesRead > 5 || (bytesRead == 5 && this.buf[this.pos - 1] > 0x0f)) {
490 throw new Error("illegal tag: varint overflows uint32");
491 }
492 const fieldNo = tag >>> 3;
493 const wireType = tag & 7;
494 if (fieldNo <= 0 || wireType > 5) {
495 throw new Error("illegal tag: field no " + fieldNo + " wire type " + wireType);
496 }
497 return [fieldNo, wireType];
498 }
499 /**
500 * Skip one element and return the skipped data.
501 *
502 * When skipping StartGroup, provide the tags field number to check for
503 * matching field number in the EndGroup tag. Recursion into nested groups
504 * is guarded by the `recursionLimit` argument: When the limit is reached,
505 * this method throws.
506 */
507 skip(wireType, fieldNo, recursionLimit = 100) {
508 let start = this.pos;
509 switch (wireType) {
510 case WireType.Varint:
511 while (this.buf[this.pos++] & 0x80) {
512 // ignore
513 }
514 break;
515 // @ts-ignore TS7029: Fallthrough case in switch -- ignore instead of expect-error for compiler settings without noFallthroughCasesInSwitch: true
516 case WireType.Bit64:
517 this.pos += 4;
518 case WireType.Bit32:
519 this.pos += 4;
520 break;
521 case WireType.LengthDelimited:
522 let len = this.uint32();
523 this.pos += len;
524 break;
525 case WireType.StartGroup:
526 if (recursionLimit <= 0) {
527 throw new Error("maximum recursion depth reached");
528 }
529 for (;;) {
530 const [fn, wt] = this.tag();
531 if (wt === WireType.EndGroup) {
532 if (fieldNo !== undefined && fn !== fieldNo) {
533 throw new Error("invalid end group tag");
534 }
535 break;
536 }
537 this.skip(wt, fn, recursionLimit - 1);
538 }
539 break;
540 default:
541 throw new Error("cant skip wire type " + wireType);
542 }
543 this.assertBounds();
544 return this.buf.subarray(start, this.pos);
545 }
546 /**
547 * Throws error if position in byte array is out of range.
548 */
549 assertBounds() {
550 if (this.pos > this.len)
551 throw new RangeError("premature EOF");
552 }
553 /**
554 * Read a `int32` field, a signed 32 bit varint.
555 */
556 int32() {
557 return this.uint32() | 0;
558 }
559 /**
560 * Read a `sint32` field, a signed, zigzag-encoded 32-bit varint.
561 */
562 sint32() {
563 let zze = this.uint32();
564 // decode zigzag
565 return (zze >>> 1) ^ -(zze & 1);
566 }
567 /**
568 * Read a `int64` field, a signed 64-bit varint.
569 */
570 int64() {
571 this.varint64();
572 return proto_int64_js_1.protoInt64.dec(this.varint64Lo, this.varint64Hi);
573 }
574 /**
575 * Read a `uint64` field, an unsigned 64-bit varint.
576 */
577 uint64() {
578 this.varint64();
579 return proto_int64_js_1.protoInt64.uDec(this.varint64Lo, this.varint64Hi);
580 }
581 /**
582 * Read a `sint64` field, a signed, zig-zag-encoded 64-bit varint.
583 */
584 sint64() {
585 this.varint64();
586 let lo = this.varint64Lo;
587 let hi = this.varint64Hi;
588 // decode zig zag
589 let s = -(lo & 1);
590 lo = ((lo >>> 1) | ((hi & 1) << 31)) ^ s;
591 hi = (hi >>> 1) ^ s;
592 return proto_int64_js_1.protoInt64.dec(lo, hi);
593 }
594 /**
595 * Read a `bool` field, a variant.
596 */
597 bool() {
598 // Fast path: most bools are 0x0 or 0x1.
599 const b = this.buf[this.pos];
600 if (b < 0x80) {
601 this.pos++;
602 return b !== 0;
603 }
604 this.varint64();
605 return this.varint64Lo !== 0 || this.varint64Hi !== 0;
606 }
607 /**
608 * Read a `fixed32` field, an unsigned, fixed-length 32-bit integer.
609 */
610 fixed32() {
611 // biome-ignore lint/suspicious/noAssignInExpressions: no
612 return this.view.getUint32((this.pos += 4) - 4, true);
613 }
614 /**
615 * Read a `sfixed32` field, a signed, fixed-length 32-bit integer.
616 */
617 sfixed32() {
618 // biome-ignore lint/suspicious/noAssignInExpressions: no
619 return this.view.getInt32((this.pos += 4) - 4, true);
620 }
621 /**
622 * Read a `fixed64` field, an unsigned, fixed-length 64 bit integer.
623 */
624 fixed64() {
625 return proto_int64_js_1.protoInt64.uDec(this.sfixed32(), this.sfixed32());
626 }
627 /**
628 * Read a `fixed64` field, a signed, fixed-length 64-bit integer.
629 */
630 sfixed64() {
631 return proto_int64_js_1.protoInt64.dec(this.sfixed32(), this.sfixed32());
632 }
633 /**
634 * Read a `float` field, 32-bit floating point number.
635 */
636 float() {
637 // biome-ignore lint/suspicious/noAssignInExpressions: no
638 return this.view.getFloat32((this.pos += 4) - 4, true);
639 }
640 /**
641 * Read a `double` field, a 64-bit floating point number.
642 */
643 double() {
644 // biome-ignore lint/suspicious/noAssignInExpressions: no
645 return this.view.getFloat64((this.pos += 8) - 8, true);
646 }
647 /**
648 * Read a `bytes` field, length-delimited arbitrary data.
649 */
650 bytes() {
651 let len = this.uint32(), start = this.pos;
652 this.pos += len;
653 this.assertBounds();
654 return this.buf.subarray(start, start + len);
655 }
656 /**
657 * Read a `string` field, length-delimited data converted to UTF-8 text. If
658 * `strict` is true, throw on invalid UTF-8 instead of substituting U+FFFD.
659 */
660 string(strict) {
661 const bytes = this.bytes();
662 const len = bytes.length;
663 // Fast path for ASCII.
664 if (len <= ASCII_MAX_LENGTH) {
665 const codes = new Array(len);
666 for (let i = 0; i < len; i++) {
667 const byte = bytes[i];
668 if (byte > 0x7f) {
669 return this.decodeUtf8(bytes, strict);
670 }
671 codes[i] = byte;
672 }
673 return String.fromCharCode.apply(String, codes);
674 }
675 return this.decodeUtf8(bytes, strict);
676 }
677 }
678 exports.BinaryReader = BinaryReader;
679 /**
680 * Assert a valid signed protobuf 32-bit integer as a number or string.
681 */
682 function assertInt32(arg) {
683 if (typeof arg == "string") {
684 arg = Number(arg);
685 }
686 else if (typeof arg != "number") {
687 throw new Error("invalid int32: " + typeof arg);
688 }
689 if (!Number.isInteger(arg) ||
690 arg > exports.INT32_MAX ||
691 arg < exports.INT32_MIN)
692 throw new Error("invalid int32: " + arg);
693 }
694 /**
695 * Assert a valid unsigned protobuf 32-bit integer as a number or string.
696 */
697 function assertUInt32(arg) {
698 if (typeof arg == "string") {
699 arg = Number(arg);
700 }
701 else if (typeof arg != "number") {
702 throw new Error("invalid uint32: " + typeof arg);
703 }
704 if (!Number.isInteger(arg) ||
705 arg > exports.UINT32_MAX ||
706 arg < 0)
707 throw new Error("invalid uint32: " + arg);
708 }
709 /**
710 * Assert a valid protobuf float value as a number or string.
711 */
712 function assertFloat32(arg) {
713 if (typeof arg == "string") {
714 const o = arg;
715 arg = Number(arg);
716 if (Number.isNaN(arg) && o !== "NaN") {
717 throw new Error("invalid float32: " + o);
718 }
719 }
720 else if (typeof arg != "number") {
721 throw new Error("invalid float32: " + typeof arg);
722 }
723 if (Number.isFinite(arg) &&
724 (arg > exports.FLOAT32_MAX || arg < exports.FLOAT32_MIN))
725 throw new Error("invalid float32: " + arg);
726 }