How to use varint - 10 common examples

To help you get started, we’ve selected a few varint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github skale-me / node-parquet / lib / parquet.js View on Github external
function rleDecode(buf, offset, datalen, bitWidth) {
  var header = varint.decode(buf, offset);
  var count = header >>> 1;

  datalen -= varint.decode.bytes;
  if (header & 1) { // Bit packed
    console.log('read ' + (count * 8) + ' bit packed values');
  } else {          // RLE
    console.log('read ' + count + ' RLE values');
    console.log('remain ' + datalen + ' bytes');
  }
}
github heineiuo / rippledb / fixtures / memtable.js View on Github external
function createLookupKey (sequence, key, valueType) {
  const keySize = key.size
  const internalKeySize = keySize + 8
  const internalKeySizeBuf = Buffer.from(varint.encode(internalKeySize))
  const buf = Buffer.concat([
    internalKeySizeBuf,
    key.buffer,
    sequence.toFixedSizeBuffer(),
    Buffer.from(varint.encode(valueType.value))
  ])
  return new Slice(buf)
}
github heineiuo / rippledb / fixtures / memtable.js View on Github external
function createLookupKey (sequence, key, valueType) {
  const keySize = key.size
  const internalKeySize = keySize + 8
  const internalKeySizeBuf = Buffer.from(varint.encode(internalKeySize))
  const buf = Buffer.concat([
    internalKeySizeBuf,
    key.buffer,
    sequence.toFixedSizeBuffer(),
    Buffer.from(varint.encode(valueType.value))
  ])
  return new Slice(buf)
}
github creativechain / creativechain-universe / lib / trantor.js View on Github external
serialize() {
        let bufferHex = ContentData.serializeNumber(this.version);
        bufferHex += ContentData.serializeNumber(this.type);

        bufferHex += Buffer.from(varint.encode(this.txIds.length)).toString('hex');

        this.txIds.forEach(function (txId) {
            let buff = Buffer.from(txId, 'hex');
            if (buff.length !== 32) {
                throw 'Invalid txId: ' + txId;
            }

            bufferHex += buff.toString('hex');
        });

        return Buffer.from(bufferHex, 'hex');
    }
github ChainSafe / lodestar / packages / lodestar / src / network / reqResp.ts View on Github external
switch (method) {
      case Method.Hello:
        output = serialize(this.config.types.Hello, body);
        break;
      case Method.Goodbye:
        output = serialize(this.config.types.Goodbye, body);
        break;
      case Method.BeaconBlocksByRange:
        output = serialize(this.config.types.BeaconBlocksByRangeRequest, body);
        break;
      case Method.BeaconBlocksByRoot:
        output = serialize(this.config.types.BeaconBlocksByRootRequest, body);
        break;
    }
    return Buffer.concat([
      Buffer.from(varint.encode(output.length)),
      output,
    ]);
  }
  private encodeResponse(method: Method, body: ResponseBody): Buffer {
github mafintosh / protocol-buffers / decoder.js View on Github external
return function(obj, buf, offset) {
					var len = varint.decode(buf, offset);
					offset += varint.decode.bytesRead;
					var end = offset + len;
					obj[key] = [];
					while (offset < end) {
						var next = {};
						obj[key].push(next);
						var itemLen = varint.decode(buf, offset);
						offset += varint.decode.bytesRead;
						offset = dec(next, buf, offset, offset, offset+itemLen);
					}
					return offset;
				};
			};
github mafintosh / derived-key-storage / index.js View on Github external
nameStorage.read(0, st.size, (err, buf) => {
          if (err) return cb(err, null)
          let len = 0

          try {
            len = varint.decode(buf, 0)
          } catch (err) {
            return cb(null, null)
          }

          const offset = varint.decode.bytes
          if (offset + len !== buf.length) return cb(null, null)
          const name = buf.slice(offset)
          return cb(null, name)
        })
      })
github mafintosh / stream-channels / index.js View on Github external
function decodeVarint (buf, offset) {
  try {
    return varint.decode(buf, offset)
  } catch (err) {
    return -1
  }
}
github mafintosh / hypergraph / index.js View on Github external
function unpackArray (buf) {
  var offset = 0
  var arr = []
  while (offset < buf.length) {
    arr.push(varint.decode(buf, offset))
    offset += varint.decode.bytes
  }
  return arr
}

varint

protobuf-style varint bytes - use msb to create integer values of varying sizes

MIT
Latest version published 4 years ago

Package Health Score

71 / 100
Full package analysis

Popular varint functions