Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"use strict";
import * as bodec from 'bodec';
const PACK = bodec.fromRaw("PACK");
export function deframer(emit : (value? : any) => boolean) {
let state = 0;
let offset = 4;
let length = 0;
let data : Uint8Array;
let more = true;
return (item : Uint8Array) => {
// Forward the EOS marker
if (item === undefined) return emit();
// Once we're in pack mode, everything goes straight through
if (state === 3) return emit(item);
return (serviceName, onError) => {
onData = wrapHandler(onData, onError);
onDrain = wrapHandler(onDrain, onError);
const socket = connect(host, port, onError);
const inter = makeChannel();
inter.put = pktLine.deframer(inter.put);
socket.put = pktLine.framer(socket.put);
const greeting = bodec.fromRaw(serviceName + " " + path + "\0host=" + host + "\0");
socket.put(greeting);
// Pipe socket to inter with backpressure
socket.take(onData);
function onData(chunk) {
if (inter.put(chunk)) {
socket.take(onData);
}
else {
inter.drain(onDrain);
}
}
function onDrain() {
socket.take(onData);
}
return (item : string | Uint8Array | undefined | null) => {
if (item === undefined) return emit();
if (item === null) {
return emit(bodec.fromRaw("0000"));
}
if (typeof item === "string") {
item = bodec.fromUnicode(item);
}
return emit(bodec.join([frameHead(item.length + 4), item]));
};
}
function encodeTree(body : TreeBody) {
let tree = "";
if (Array.isArray(body)) throw new TypeError("Tree must be in object form");
const list = Object.keys(body).map(treeMap, body).sort(treeSort);
for (let i = 0, l = list.length; i < l; i++) {
const entry = list[i];
tree += entry.mode.toString(8) + " " + bodec.encodeUtf8(entry.name) +
"\0" + bodec.decodeHex(entry.hash);
}
return bodec.fromRaw(tree);
}
function normalizeBlob(body) {
var type = typeof body;
if (type === "string") {
return binary.fromRaw(body);
}
if (body && type === "object") {
if (body.constructor.name === "ArrayBuffer") body = new Uint8Array(body);
if (typeof body.length === "number") {
return body;//binary.toRaw(body);
}
}
throw new TypeError("Blob body must be raw string, ArrayBuffer or byte array");
}
function frame(obj : Frame) {
const type = obj.type;
const body = bodec.isBinary(obj.body) ? obj.body : (encoders as any)[type](obj.body) as Uint8Array;
return bodec.join([
bodec.fromRaw(type + " " + body.length + "\0"),
body
]);
}
function Parser(boundary, partHandler) {
this.boundary = bodec.fromRaw("\r\n--" + boundary);
this.lookBehind = bodec.create(this.boundary.length + 8);
this.boundaryChars = {};
var i = this.boundary.length;
while (i) this.boundaryChars[this.boundary[--i]] = true;
this.state = S.START;
this.index = null;
this.flags = 0;
if (typeof partHandler !== "function") throw new Error("multipart.Parser needs a part handler");
this.onPart = partHandler;
}
function frame(obj) {
var type = obj.type;
var body = obj.body;
if (!bodec.isBinary(body)) body = encoders[type](body);
return bodec.join([
bodec.fromRaw(type + " " + body.length + "\0"),
body
]);
}
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) {
async updateRef(ref, hash) {
const path = pathJoin(repo.rootPath, ref);
const lock = path + ".lock";
await this.fs.writeFile(lock, bodec.fromRaw(hash + "\n"));
return await this.fs.rename(lock, path);
}