Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import bodec from 'bodec';
// This is a small sample packfile with couple offset deltas
// pack-5851ce932ec42973b51d631afe25da247c3dc49a.pack
export default bodec.fromBase64('UEFDSwAAAAIAAAAQnQ54nJ3MWwoCMQxA0f+uIhtQ0nYeKYgobsENZNoEC/OQMTK6e2cN/l4411YRYCo5kseITVLpSmAfOVLSnFJB6kJqSukDuSevMhu0moed9CmrKjKFwpIxtT7TINh2vSqReHX8tseywr1OcOPXJuMIJ6vTJa/CVpe5fo55mc7gY2p86LFBOGCH6PY6VTP5x7prKfAVA54Xe+yLWTbQOor7AZUCSPmRDnicnctRCgIhEADQf08xFyjGUVeFiKIrdAEdZ0lYd8OM7fh1hn4fvNFFQEi8JCcuCoWSmakwY8xoHGMxkdgimZjVM3VZB8wUPMUJLWrRPml0IdspuJl1JHJBSijGRlLpPR5bh3ttcEuvXZYFTqO2C3dJo25r/Rx5a2fQJlpNHgnhgBOi+mmrY8g/V11LgVV2mOsi6guDiEL9mA94nJ3PTWrDMBBA4b1OMRdosDT6hRIKvkIuIMkjd6htGXVCkts3Z+j2wbd4MohA+5Cai874uiQXQmuIjagsAWMp3rWS0WCM6syDDgGbDCXEmhz5Zl00iayv2mpyHk2xVLVZlhJUvst3H3DjHeb8+6Btg0/h/asOysL94Oel9v0KGpPVxjtE+Jj8NKl33VmE/mPV3M8XrO8x4WOFkusPSIc+eOUjb9B4I/UHHmNMM5QOeJydy1sKwjAQRuH3rGI2oGQmlzYgIrgDcQNp8hcDTSsxostXt+B5/OD0BpAzMJmzJJs4J5Fh5OiCsB3nMFvoOAakkaHusWHtpJm1y9YYb4KXSawgR/GY9MQ+8OB/TZhVfPbb1uhaKp3j44VloUMv9ZQaYi/bWt77tNUjsQmWxTttaae91uqrtfSOf151wRorqN9Ac1mgPgYNRBeSDnicncvdCcIwEADg90xxCyiXn6YGRBRXcIG75IKBppX2xI6vM/j6waerCGAozFyCiA2Jx+QJh5Rd8l5cHUiSdcVTzeZFq8wKY5TkamYsIWO1Xkau8VRdHNhF5BLJsUWqht76XFZ4tA532j4yTXDW1q95FdK2zG0/5qVfwPoUrIshWThgRDQ/7U1V/rnmVgpsSxdQ2dV8AbwRRT6TC3icnczNCQIxEEDhe6qYBpT8JwuyCHvybgOTmOBAsoE4ouUrluD1wfd4lgLexpqjL9G5kG6YUtY56ohqccbVaEzQoaLXAp98HxOu1GHDx6u0Biemfs6zINPY6X3Mo6+gzGKV9jYEOEgvpfjWTszlHysuOzFhg+03ER9fQDcKqQl4nDM0MDAzMVFIL0pNLcnMS9crqShhEHwQ5TRdT6bE+tY/8blzjRyr9lYcMoSoy60kVmVeajlYifjVm28/SzW0d12ZKCB++trFC8ZKOxBKjMBqauylWlkm6kbyCrH0Gp01vHQ9NnMNAFftOrq1AXic80jNyclXCM8vyknhckxJUSjOz03lAgBQjAcOPXicS8zLL8lILVJIy8xJ5QIAI9cEvLEBeJyrTC1RSMzLL8lILVJIy8xJ5QIAOsAGLmWAPnicm8lYOqEUAAX6AhVkEHicKw2aEAQABEABqqoCeJwzNDAwMzFRyK1ML0pNLcnMS9crqShhEHwQ5TRdT6bE+tY/8blzjRyr9lYcAgAxUhBDqAJ4nDM0MDAzMVFIL0pNLcnMS9crqShhEHwQ5TRdT6bE+tY/8blzjRyr9lYcAgAPuQ9dqAJ4nDM0MDAzMVFIL0pNLcnMS9crqShhCK3dYPty+oksL6Y+ub1WMq+Voh9ZAAAZvA8xPHic80jNyclXCM8vyknhAgAcMgQnuZAj3ZpSLQckQi9VfpQYWt+hefM=');
}
for (var path in recording.paths) {
var actual = (yield repo.pathToEntry(root, path)).hash;
var expected = recording.paths[path];
if (actual !== expected) {
console.log("change in " + path + " from " + expected + " to " + actual);
break outer;
}
}
return [304, recording.headers];
}
}
if (result === false) result = (yield* execute(root, code, url));
if (result) {
var headers = result[1];
if (bodec.isBinary(result[2])) {
// Auto deflate text files if request accepts it.
if (/\b(?:text|javascript)\b/.test(headers["Content-Type"]) &&
/\bgzip\b/.test(req.headers["accept-encoding"])) {
result[2] = yield function (callback) {
zlib.gzip(result[2], callback);
};
headers["Content-Encoding"] = "gzip";
}
// Auto-add Content-Length header for response bodies.
if (!headers["Content-Length"]) {
headers["Content-Length"] = result[2].length;
}
}
}
"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);
function flush(stream) {
if (!stream._chunks)
return;
var chunk;
while (chunk = stream._chunks.shift()) {
stream.size -= chunk.length;
if (stream.encoding) {
stream.emit('data', bodec.toString(chunk, stream.encoding));
} else {
stream.emit('data', chunk);
}
// If the stream was paused in a data event handler, break.
if (stream.paused)
break;
}
if (stream.ended) {
if (!stream.paused) {
stream._chunks = null;
stream.emit('end');
}
} else if (stream._wasFull && !stream.full) {
stream._wasFull = false;
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) {
hash: ruleEntry.hash
});
}
if (!entry) {
// If you get here, the entry didn't match
// In raw-mode this is a no-find.
if (!bake) break;
// In bake mode, look for rule that may serve this path.
return searchRules();
}
if (bake && (entry.mode === modes.sym)) {
if (!check("blob", entry.hash)) return;
var blob = storage.get(entry.hash);
var link = binary.toUnicode(blob);
var rest = parts.slice(index + 1).join("/");
var linkPath = pathJoin(partial, link, rest);
return resolvePath(linkPath, bake, callback);
}
// We're good, move on!
mode = entry.mode;
hash = entry.hash;
partial = newPath;
if (mode === modes.commit) root = partial;
index++;
} else {
break;
}
}
if (meta.mode === modes.sym) {
var target = bodec.toUnicode(yield repo.loadAs("blob", meta.hash));
target = pathJoin(base, subPath, '..', target, subRest);
return yield* pathToEntry(target);
}
// Check for .gitmodules file
meta = yield repo.pathToEntry(root, ".gitmodules");
if (!(meta && modes.isFile(meta.mode))) {
throw new Error("Missing .gitmodules file");
}
// Load and parse the .gitmodules file.
// TODO: cache this in memory by path and hash
var config = configCodec.decode(bodec.toUnicode(yield repo.loadAs("blob", meta.hash)));
config = config.submodule[subPath];
if (!config) {
throw new Error("Missing .gitmodules entry for " + subPath);
}
// Iterate the search loop with the new repo and path.
ref = config.ref || "refs/heads/master";
repo = yield* getRepo(config.url, ref);
base = subPath;
path = subRest;
}
};
}
function* render(pathToEntry, url, runtimes) {
// Strip of query string from url to get pathname.
var pathname = getPathname(url);
var meta = yield* pathToEntry(pathname);
if (!meta) return;
var repo = meta.repo;
// Send redirects for symlinks
if (meta.mode === modes.sym) {
var target = yield repo.loadAs("blob", meta.hash);
target = bodec.toUnicode(target);
if (target[0] !== "/") target = pathJoin(url, "..", target);
return [302, {Location: target}];
}
// Special rules for tree requests.
if (meta.mode === modes.tree) {
// Make sure requests for trees end in trailing slashes.
if (pathname[pathname.length - 1] !== "/") {
return [301, { Location: pathname + "/" }];
}
// Load the actual tree listing, this should be cached by mem-cache.
var tree = yield repo.loadAs("tree", meta.hash);
// Look for a index file
if (tree["index.html"] && modes.isFile(tree["index.html"].mode)) {
meta = tree["index.html"];
meta.repo = repo;
function respond(code, headers, body) {
// Log the request
notify(item.method + " " + pathname + " " + code);
if (typeof body === "string") body = binary.fromUnicode(body);
var contentType, contentLength;
headers.forEach(function (pair) {
var key = pair[0].toLowerCase();
if (key === "content-type") contentType = pair[1];
else if (key === "content-length") contentLength = pair[1];
});
if (!contentType) headers.push(["Content-Type", "text/plain"]);
if (!contentLength) headers.push(["Content-Length", body.length]);
encode({
code: code,
headers: headers
});
encode(body);
encode();
}
}
return [301, { Location: url + "/" }];
}
// Load the actual tree listing, this should be cached by mem-cache.
var tree = yield repo.loadAs("tree", meta.hash);
// Look for a index file
if (tree["index.html"] && modes.isFile(tree["index.html"].mode)) {
meta = tree["index.html"];
url = pathJoin(url, "index.html");
// Fall through down to static file handler.
}
// Otherwise render a index file
else {
return [200, {
"ETag": '"' + meta.hash + '-html"',
"Content-Type": "text/html",
}, bodec.fromUnicode(formatTree(tree))];
}
}
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) {