How to use the varint.encode function in varint

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 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 / hypercore / lib / protocol.js View on Github external
function encode (enc, type, msg) {
  var channel = 0
  var len = enc.encodingLength(msg) + 1 + 1
  var buf = new Buffer(len + varint.encodingLength(len))
  var offset = 0

  varint.encode(len, buf, offset)
  offset += varint.encode.bytes
  buf[offset++] = channel
  buf[offset++] = type
  enc.encode(msg, buf, offset)

  return buf
}
github mafintosh / hypergraph / index.js View on Github external
function packArray (arr) {
  var size = 0
  var offset = 0
  for (var i = 0; i < arr.length; i++) {
    size += varint.encodingLength(arr[i])
  }
  var buf = new Buffer(size)
  for (var j = 0; j < arr.length; j++) {
    varint.encode(arr[j], buf, offset)
    offset += varint.encode.bytes
  }
  return buf
}
github mafintosh / stream-channels / index.js View on Github external
OutgoingChannel.prototype.preallocate = function (length) {
  var payload = this.idLength + length
  var buf = new Buffer(varint.encodingLength(payload) + payload)
  var offset = 0

  varint.encode(payload, buf, offset)
  offset += varint.encode.bytes

  varint.encode(this.id, buf, offset)
  offset += varint.encode.bytes

  return buf
}
github digidem / hyperdb-osm / lib / utils.js View on Github external
function versionFromKeySeq (key, seq) {
  return d64.encode(
    Buffer.concat([
      key,
      toBuffer(varint.encode(seq))
    ])
  )
}
github digidem / hyperdb-osm / lib / geo-index.js View on Github external
function buffer36ToVersion (buf) {
  var key = buf.slice(0, 32)
  var seq = buf.readUInt32LE(32)
  var seqOut = toBuffer(varint.encode(seq))
  var res = Buffer.concat([key, seqOut])
  return d64.encode(res)
}

varint

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

MIT
Latest version published 4 years ago

Package Health Score

68 / 100
Full package analysis

Popular varint functions