Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js readUIntBE arbitrary size restriction?

Tags:

node.js

buffer

Background

I am reading buffers using the Node.js buffer native API. This API has two functions called readUIntBE and readUIntLE for Big Endian and Little Endian respectively.

https://nodejs.org/api/buffer.html#buffer_buf_readuintbe_offset_bytelength_noassert

Problem

By reading the docs, I stumbled upon the following lines:

  • byteLength Number of bytes to read. Must satisfy: 0 < byteLength <= 6.

If I understand correctly, this means that I can only read 6 bytes at a time using this function, which makes it useless for my use case, as I need to read a timestamp comprised of 8 bytes.

Questions

  1. Is this a documentation typo?
  2. If not, what is the reason for such an arbitrary limitation?
  3. How do I read 8 bytes in a row ( or how do I read sequences greater than 6 bytes? )
like image 538
Flame_Phoenix Avatar asked Nov 29 '25 14:11

Flame_Phoenix


1 Answers

Answer

After asking in the official Node.js repo, I got the following response from one of the members:

  1. No it is not a typo
  2. The byteLength corresponds to e.g. 8bit, 16bit, 24bit, 32bit, 40bit and 48bit. More is not possible since JS numbers are only safe up to Number.MAX_SAFE_INTEGER.
  3. If you want to read 8 bytes, you can read multiple entries by adding the offset.

Source: https://github.com/nodejs/node/issues/20249#issuecomment-383899009

like image 157
Flame_Phoenix Avatar answered Dec 02 '25 05:12

Flame_Phoenix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!