How to use the bodec.fromHex function in bodec

To help you get started, we’ve selected a few bodec 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 es-git / es-git / ts / lib / pack-codec.ts View on Github external
left = item.num;
      write(packHeader(item.num));
    }
    else if (typeof item.type === "string" && bodec.isBinary(item.body)) {
      // The header must be sent before items.
      if (typeof left !== "number") throw new Error("Headers not sent yet");

      // Make sure we haven't sent all the items already
      if (!left) throw new Error("All items already sent");

      // Send the item in packstream format
      write(packFrame(item));

      // Send the checksum after the last item
      if (!--left) {
        emit(bodec.fromHex(sha1sum.digest()));
      }
    }
    else {
      throw new Error("Invalid item");
    }
  };
  function write(chunk : Uint8Array) {
github es-git / es-git / ts / lib / pack-codec.ts View on Github external
if (typeof item.ref === "number") {
    // write BIG_ENDIAN_MODIFIED_BASE_128_NUMBER
    let offset = item.ref;
    // Calculate how many digits we need in base 128 and move the pointer
    i += Math.floor(Math.log(offset) / Math.log(0x80)) + 1;
    // Write the last digit
    head[i] = offset & 0x7f;
    // Then write the rest
    while (offset >>= 7) {
      head[--i] = 0x80 | (--offset & 0x7f);
    }
  }

  const parts = [bodec.fromArray(head)];
  if (typeof item.ref === "string") {
    parts.push(bodec.fromHex(item.ref));
  }
  parts.push(deflate(item.body));
  return bodec.join(parts);
}
github creationix / tedit / src / data / jon-parser.js View on Github external
var offset = 0, length = jon.length;
  var num = patterns.length;
  var tokens = [];
  while (offset < length) {
    var start = offset;
    var sub = jon.substring(offset);
    var token, value;
    for (var i = 0; i < num; i++) {
      var pattern = patterns[i];
      var match = sub.match(pattern[0]);
      if (!match) continue;
      value = match[0];
      offset += value.length;
      token = pattern[1];
      if (token) {
        value = token === "BUFFER" ? binary.fromHex(match[1].replace(/\s/g, '')) :
                token === "STRING" ? JSON.parse(value) :
                token === "CONSTANT" ? JSON.parse(value) :
                token === "FORM" ? new Form(value) :
                token === "IDENT" ? value :
                token === "SYMBOL" ? new Symbol(value) :
                token === "HEX" ? parseInt(match[2], 16) * (match[1] ? -1 : 1) :
                token === "INTEGER" ? parseInt(value, 10) :
                token === "ZERO" ? 0 : undefined;
        if (token === "HEX" || token === "ZERO" || token === "INTEGER" ||
            token === "BUFFER" || token === "FORM" || token === "SYMBOL") {
          token = "VALUE";
        }
      }
      break;
    }
    if (i === num) {