How to use the bodec.slice 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
function $body(byte : number, i : number, chunk : Uint8Array) : $State {
    if (inf.write(byte)) return $body;
    const buf = inf.flush();
    if (buf.length !== length) throw new Error("Length mismatch, expected " + length + " got " + buf.length);
    inf.recycle();
    if (buf.length) {
      parts.push(buf);
    }
    emitObject();
    // If this was all the objects, start calculating the sha1sum
    if (--num) return $header;
    sha1sum.update(bodec.slice(chunk, 0, i + 1));
    return $checksum;
  }
github creationix / wheaty / wheaty.js View on Github external
if (modes.isFile(meta.mode)) {
    var body = yield meta.repo.loadAs("blob", meta.hash);

    if (meta.mode === modes.exec) {
      // #! but not #!/
      if (body[0] === 0x23 && body[1] === 0x21 && body[2] !== 0x2f) {
        var i = 2;
        var language = "";
        while (i < body.length && body[i] !== 0x0d && body[i] !== 0x0a) {
          language += String.fromCharCode(body[i++]);
        }
        var runtime = runtimes[language];
        if (!runtime) {
          throw new Error("Invalid runtime specified: " + JSON.stringify(language));
        }
        body = bodec.slice(body, i);
        if (runtime.constructor === Function) {
          return runtime(pathToEntry, url, body);
        }
        return yield* runtime(pathToEntry, url, body);
      }
    }

    // Render static files.
    return [200, {
      "ETag": '"' + meta.hash + '"',
      "Content-Type": getMime(getPathname(url)),
    }, body];
  }
}
github creationix / wheaty / server.js View on Github external
if (modes.isFile(meta.mode)) {
    var body = yield repo.loadAs("blob", meta.hash);

    if (meta.mode === modes.exec) {
      // #! but not #!/
      if (body[0] === 0x23 && body[1] === 0x21 && body[2] !== 0x2f) {
        var i = 2;
        var language = "";
        while (i < body.length && body[i] !== 0x0d && body[i] !== 0x0a) {
          language += String.fromCharCode(body[i++]);
        }
        var runtime = runtimes[language];
        if (!runtime) {
          throw new Error("Invalid runtime specified: " + JSON.stringify(language));
        }
        body = bodec.slice(body, i);
        if (runtime.constructor === Function) {
          return runtime(load, url, body);
        }
        return yield* runtime(load, url, body);
      }
    }

    // Render static files.
    return [200, {
      "ETag": '"' + meta.hash + '"',
      "Content-Type": getMime(url),
    }, body];
  }
}
github es-git / es-git / ts / lib / pkt-line.ts View on Github external
state = 1;
          }
          else {
            state = -1;
            throw new SyntaxError("Invalid length: " + length);
          }
        }
      }
      else if (state === 1) {
        data[offset++] = byte;
        if (offset === length) {
          offset = 4;
          state = 0;
          length = 0;
          if (data[0] === 1) {
            more = emit(bodec.slice(data, 1));
          }
          else if (data[0] === 2) {
            more = emit({progress: bodec.toUnicode(data, 1)});
          }
          else if (data[0] === 3) {
            more = emit({error: bodec.toUnicode(data, 1)});
          }
          else {
            more = emit(bodec.toUnicode(data).trim());
          }
        }
      }
      else if (state === 2) {
        if (offset < 4 && byte === PACK[offset++]) {
          continue;
        }
github es-git / es-git / ts / mixins / fs-db.ts View on Github external
const crc = readUint32(buffer, crcOffset + i * 4);
    let offset = readUint32(buffer, lengthOffset + i * 4);
    if (offset & 0x80000000) {
      offset = largeOffset + (offset &0x7fffffff) * 8;
      checkOffset = Math.max(checkOffset, offset + 8);
      offset = readUint64(buffer, offset);
    }
    indexes[i] = {
      hash: hash,
      offset: offset,
      crc: crc
    };
  }
  const packChecksum = bodec.toHex(bodec.slice(buffer, checkOffset, checkOffset + 20));
  const checksum = bodec.toHex(bodec.slice(buffer, checkOffset + 20, checkOffset + 40));
  if (sha1(bodec.slice(buffer, 0, checkOffset + 20)) !== checksum) {
    throw new Error("Checksum mistmatch");
  }

  const byHash = {};
  indexes.sort((a, b) => a.offset - b.offset);
  indexes.forEach(data => {
    byHash[data.hash] = {
      offset: data.offset,
      crc: data.crc,
    };
  });
  const offsets = indexes.map(entry => entry.offset).sort((a, b) => a - b);

  return {
    offsets: offsets,
    byHash: byHash,
github mjackson / mach / build / npm / multipart / Parser.js View on Github external
Parser.prototype.onPartData = function (chunk, start, end) {
  this._stream.write(bodec.slice(chunk, start, end));
};
github es-git / es-git / ts / lib / pack-codec.ts View on Github external
}
  size = size >>> 0;
  let ref;
  if (type === "ref-delta") {
    ref = bodec.toHex(bodec.slice(chunk, offset, offset += 20));
  }
  else if (type === "ofs-delta") {
    byte = chunk[offset++];
    ref = byte & 0x7f;
    while (byte & 0x80) {
      byte = chunk[offset++];
      ref = ((ref + 1) << 7) | (byte & 0x7f);
    }
  }

  const body = inflate(bodec.slice(chunk, offset));
  if (body.length !== size) {
    throw new Error("Size mismatch");
  }
  const result : any = {
    type: type,
    body: body
  };
  if (typeof ref !== "undefined") {
    result.ref = ref;
  }
  return result;
}