Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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");
}
}
]);
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() {
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");
}
}
]);
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);
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
};
}
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;
}
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) {
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;
}
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) {
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;
}