How to use the bodec.toRaw 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 / test / test-zlib.js View on Github external
function testStream() {
    const done = false;
    const chunks = [];
    const deflated = deflate(bin);
    const inf = inflateStream();

    for (let i = 0, l = deflated.length; i < l; ++i) {
      inf.write(deflated[i]);
    }
    const inflated = inf.flush();
    if (bodec.toRaw(bin) !== bodec.toRaw(inflated)) {
      console.log([bin.length, inflated.length]);
      throw new Error("Problem with roundtrip");
    }
  }
]);
github es-git / es-git / test / test-zlib.js View on Github external
function testRoundTrip() {
    const deflated = deflate(bin);
    if (!bodec.isBinary(deflated)) {
      throw new Error("deflate output should be native binary");
    }
    const inflated = inflate(deflated);
    if (!bodec.isBinary(inflated)) {
      throw new Error("inflate output should be native binary");
    }
    if (bodec.toRaw(bin) !== bodec.toRaw(inflated)) {
      console.log([bin, inflated]);
      throw new Error("Problem with roundtrip");
    }
  },
  function testStream() {
github es-git / es-git / test / test-object-codec.js View on Github external
function testBinaryBlob() {
    const blob = bodec.create(256);
    for (let i = 0; i < 256; i++) { blob[i] = i; }
    const bin = codec.frame({type:"blob",body:blob});
    const obj = codec.deframe(bin, true);
    if (bodec.toRaw(blob) !== bodec.toRaw(obj.body)) {
      throw new Error("Problem decoding binary blob");
    }
  }
]);
github es-git / es-git / ts / lib / object-codec.ts View on Github external
let i = 0;
  let start;
  let key : keyof CommitBody | 'parent';
  const parents : string[] = [];
  const commit : any = {
    tree: "",
    parents: parents,
    author: undefined,
    committer: undefined,
    message: ""
  };
  while (body[i] !== 0x0a) {
    start = i;
    i = indexOf(body, 0x20, start);
    if (i < 0) throw new SyntaxError("Missing space");
    key = bodec.toRaw(body, start, i++) as any;
    start = i;
    i = indexOf(body, 0x0a, start);
    if (i < 0) throw new SyntaxError("Missing linefeed");
    let value = bodec.toUnicode(body, start, i++);
    if (key === "parent") {
      parents.push(value);
    }
    else {
      if (key === "author" || key === "committer") {
        commit[key] = decodePerson(value);
      }
      commit[key] = value;
    }
  }
  i++;
  commit.message = bodec.toUnicode(body, i, body.length);
github digidem / react-mapfilter / app / js / lib / backbone-github.js View on Github external
function deframe(buffer, decode) {
  var space = indexOf(buffer, 0x20);
  if (space < 0) throw new Error("Invalid git object buffer");
  var nil = indexOf(buffer, 0x00, space);
  if (nil < 0) throw new Error("Invalid git object buffer");
  var body = bodec.slice(buffer, nil + 1);
  var size = parseDec(buffer, space + 1, nil);
  if (size !== body.length) throw new Error("Invalid body length.");
  var type = bodec.toRaw(buffer, 0, space);
  return {
    type: type,
    body: decode ? decoders[type](body) : body
  };
}
github es-git / es-git / ts / lib / object-codec.ts View on Github external
function decodeTag(body : Uint8Array) {
  let i = 0;
  let start;
  let key;
  const tag : any = {};
  while (body[i] !== 0x0a) {
    start = i;
    i = indexOf(body, 0x20, start);
    if (i < 0) throw new SyntaxError("Missing space");
    key = bodec.toRaw(body, start, i++);
    start = i;
    i = indexOf(body, 0x0a, start);
    if (i < 0) throw new SyntaxError("Missing linefeed");
    let value : any = bodec.toUnicode(body, start, i++);
    if (key === "tagger") value = decodePerson(value);
    tag[key] = value;
  }
  i++;
  tag.message = bodec.toUnicode(body, i, body.length);
  return tag as TagBody;
}
github creationix / tedit / src / backends / encrypt-repo.js View on Github external
encrypt: function (plain) {
      var iv = forge.random.getBytesSync(16);
      var cipher = forge.cipher.createCipher('AES-CBC', key);
      cipher.start({iv: iv});
      var raw = bodec.toRaw(plain);
      cipher.update(forge.util.createBuffer(raw));
      cipher.finish();
      var encrypted = cipher.output.bytes();
      return bodec.fromRaw(iv + encrypted);
    },
    decrypt: function (encrypted) {
github digidem / react-mapfilter / app / js / lib / backbone-github.js View on Github external
function decodeTag(body) {
  var i = 0;
  var start;
  var key;
  var tag = {};
  while (body[i] !== 0x0a) {
    start = i;
    i = indexOf(body, 0x20, start);
    if (i < 0) throw new SyntaxError("Missing space");
    key = bodec.toRaw(body, start, i++);
    start = i;
    i = indexOf(body, 0x0a, start);
    if (i < 0) throw new SyntaxError("Missing linefeed");
    var value = bodec.toUnicode(body, start, i++);
    if (key === "tagger") value = decodePerson(value);
    tag[key] = value;
  }
  i++;
  tag.message = bodec.toUnicode(body, i, body.length);
  return tag;
}
github creationix / tedit / src / backends / encrypt-repo.js View on Github external
decrypt: function (encrypted) {
      var decipher = forge.cipher.createDecipher('AES-CBC', key);
      var iv = bodec.toRaw(encrypted, 0, 16);
      encrypted = bodec.toRaw(encrypted, 16);
      decipher.start({iv: iv});
      decipher.update(forge.util.createBuffer(encrypted));
      decipher.finish();
      return bodec.fromRaw(decipher.output.bytes());
    },
    getRootTree: function (callback) {
github es-git / es-git / ts / lib / object-codec.ts View on Github external
function deframe(buffer : Uint8Array, decode? : boolean) : Frame {
  const space = indexOf(buffer, 0x20);
  if (space < 0) throw new Error("Invalid git object buffer");
  const nil = indexOf(buffer, 0x00, space);
  if (nil < 0) throw new Error("Invalid git object buffer");
  const body = bodec.slice(buffer, nil + 1);
  const size = parseDec(buffer, space + 1, nil);
  if (size !== body.length) throw new Error("Invalid body length.");
  const type = bodec.toRaw(buffer, 0, space);
  return {
    type: type,
    body: decode ? (decoders as any)[type](body) : body
  } as any;
}