Skip to content

Instantly share code, notes, and snippets.

@creationix
Created August 1, 2013 20:02
Show Gist options
  • Save creationix/6134727 to your computer and use it in GitHub Desktop.
Save creationix/6134727 to your computer and use it in GitHub Desktop.
Git ls-remote as a mozilla app.
var socket = navigator.mozTCPSocket.open('github.com', 9418, {
binaryType: "arraybuffer"
});
socket.onopen = function () {
send("git-upload-pack /creationix/conquest.git\0host=github.com\0");
};
function send(string) {
var line;
if (string === null) {
line = toBuffer("0000");
}
else {
line = pktLine(string);
}
log("->", toString(line));
socket.send(line);
}
socket.ondata = function (evt) {
log("<-", toString(evt.data));
send(null);
};
socket.onerror = function (err) {
throw err;
};
function pktLine(line) {
var length = line.length + 4;
var array = new Uint8Array(length);
var header = "";
for (var i = 0; i < 4; i++) {
var val = length >> (3 - i) * 4 & 0xf;
array[i] = val + (val < 10 ? 0x30 : 0x57);
}
for (var i = 4; i < length; i++) {
array[i] = line.charCodeAt(i - 4);
}
return array.buffer;
}
function toString(buffer) {
return String.fromCharCode.apply(String, new Uint8Array(buffer));
}
function utf8Decode(string) {
return decodeURIComponent(escape(string));
}
function toBuffer(string) {
var length = string.length;
var array = new Uint8Array(length);
for (var i = 0; i < length; i++) {
array[i] = string.charCodeAt(i);
}
return array.buffer;
}
function utf8Encode(string) {
return unescape(encodeURIComponent(string));
}
function log(label, value) {
var p = document.createElement('p');
p.textContent = label + " " + value;
document.body.appendChild(p);
console.log(label, value);
}
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Git Browser</title>
</head>
<body><script src="app.js"></script></body>
</html>
{
"name": "Git-Browser",
"type": "privileged",
"description": "Browse Git Repos offline.",
"launch_path": "/index.html",
"permissions": {
"tcp-socket": {
"description": "Required for communicate with remote git servers"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment