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