Last active
December 15, 2015 16:38
-
-
Save chrisdickinson/5290339 to your computer and use it in GitHub Desktop.
based on the newly released `git-list-pack` stream; this is an example of cloning repos from the browser. use `browserify client.js > bundle.js` to compile.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var shoe = require('shoe') | |
, fetch = require('git-fetch-pack') | |
, unpack = require('git-list-pack') | |
, through = require('through') | |
, Buffer = require('buffer').Buffer | |
// make a websocket connection to the | |
// server. | |
var conn = shoe('/git') | |
, client | |
// automatically clone plate. | |
client = fetch( | |
'git://github.com/chrisdickinson/plate.git' | |
, want | |
) | |
// we get a callback for each ref. say `true` | |
// if we want the ref, `false` if we don't. | |
function want(ref, ready) { | |
return ready(/heads/.test(ref.name)) | |
} | |
client | |
.pipe(conn) | |
.pipe(parse()) | |
.pipe(client) | |
// `pack` is a separate stream that | |
// emits binary packfile data. | |
client.pack.pipe(unpack()).on('data', function(obj) { | |
console.log(obj.offset, obj.num) | |
}) | |
// parse turns our serialized objects | |
// back into js objects. | |
function parse() { | |
return through(function(data) { | |
// turn it back into a JS object + buffer data. | |
data = JSON.parse(data) | |
if(data.data !== null) { | |
data.data = new Buffer(data.data, 'base64') | |
} | |
this.queue(data) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<h1>test git</h1> | |
<script src="/bundle.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net') | |
, fs = require('fs') | |
var unpack = require('git-list-pack') | |
, concat = require('concat-stream') | |
, transport = require('git-transport-protocol') | |
, through = require('through') | |
var http = require('http') | |
, ecstatic = require('ecstatic')(__dirname) | |
, shoe = require('shoe') | |
, server | |
, sock | |
// serve up files. | |
server = http.createServer(ecstatic) | |
server.listen(9999) | |
// do websockets. install at `/git`. | |
sock = shoe(connect) | |
sock.install(server, '/git') | |
function connect(conn) { | |
// this runs whenever we get a new websocket connection. | |
var tcp = net.connect({host: 'github.com', port: 9418}) | |
conn | |
.pipe(transport(tcp)) | |
.pipe(translate()) | |
.pipe(conn) | |
} | |
function translate() { | |
return through(function(data) { | |
// if there's data, base64 so we don't have any | |
// silly unicode issues. then queue | |
// up the stringified object. | |
data.data = data.data ? data.data.toString('base64') : null | |
this.queue(JSON.stringify(data)) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've created a repo while working with this stuff as @maks suggested. I've added a little bit stuff to the template and a write function to write stuff into the html. You can find it here: dignifiedquire/js-git-demo.
@chrisdickinson I've also tried to use git-objectify-pack with it to get real objects, but it fails with this stack trace: