Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade StandardJS #86

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
36 changes: 17 additions & 19 deletions git-remote-gittorrent → lib/git-remote-gittorrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

var Chalk = require('chalk')
var DHT = require('bittorrent-dht')
var exec = require('child_process').exec
var hat = require('hat')
var magnet = require('magnet-uri')
var prettyjson = require('prettyjson')
var spawn = require('child_process').spawn
var Swarm = require('bittorrent-swarm')
var ut_gittorrent = require('ut_gittorrent')
var utGittorrent = require('ut_gittorrent')
var WebTorrent = require('webtorrent')
var zeroFill = require('zero-fill')
var config = require('./config')
Expand All @@ -19,7 +18,7 @@ var git = require('./git')
// '0.16.1' -> '0016'
// '1.2.5' -> '0102'
//
var VERSION = require('./package.json').version
var VERSION = require('../package.json').version
.match(/([0-9]+)/g).slice(0, 2).map(zeroFill(2)).join('')

function die (error) {
Expand All @@ -28,15 +27,15 @@ function die (error) {
}

// Gotta enable color manually because stdout isn't a tty.
var chalk = new Chalk.constructor({enabled: true});
var chalk = new Chalk.constructor({ enabled: true })

var dht = new DHT({
bootstrap: config.dht.bootstrap
})

// After building a dictionary of references (sha's to branch names), responds
// to git's "list" and "fetch" commands.
function talk_to_git (refs) {
function talkToGit (refs) {
process.stdin.setEncoding('utf8')
var didFetch = false
process.stdin.on('readable', function () {
Expand All @@ -56,7 +55,7 @@ function talk_to_git (refs) {
}
// Format: "fetch sha branch"
line = line.split(/\s/)
get_infohash(line[1], line[2])
getInfoHash(line[1], line[2])
})
} else if (chunk && chunk !== '' && chunk !== '\n') {
console.warn('unhandled command: "' + chunk + '"')
Expand All @@ -67,7 +66,6 @@ function talk_to_git (refs) {
// If git already has all the refs it needs, we should exit now.
process.exit()
}
return
}
})
process.stdout.on('error', function () {
Expand All @@ -78,24 +76,24 @@ function talk_to_git (refs) {
var remotename = process.argv[2]
var url = process.argv[3]
var matches = url.match(/gittorrent:\/\/([a-f0-9]{40})\/(.*)/)
var refs = {} // Maps branch names to sha's.
var refs = {} // Maps branch names to sha's.
if (matches) {
var key = matches[1]
var reponame = matches[2]
if (remotename.search(/^gittorrent:\/\//) !== -1) {
remotename = key
}
dht.on('ready', function () {
var val = new Buffer(key, 'hex')
var val = Buffer.from(key, 'hex')
dht.get(val, function (err, res) {
if (err) {
return console.error(err)
}
var json = res.v.toString()
var repos = JSON.parse(json)
console.warn('\nMutable key ' + chalk.green(key) + ' returned:\n' +
prettyjson.render(repos, {keysColor: 'yellow', valuesColor: 'green'}) + '\n')
talk_to_git(repos.repositories[reponame])
prettyjson.render(repos, { keysColor: 'yellow', valuesColor: 'green' }) + '\n')
talkToGit(repos.repositories[reponame])
})
})
} else {
Expand All @@ -108,14 +106,14 @@ if (matches) {
die(err)
}
dht.on('ready', function () {
talk_to_git(refs)
talkToGit(refs)
})
})
}

var fetching = {} // Maps shas -> {got: <bool>, swarm, branches: [...]}
var todo = 0 // The number of sha's we have yet to fetch. We will not exit
// until this equals zero.
var fetching = {} // Maps shas -> {got: <bool>, swarm, branches: [...]}
var todo = 0 // The number of sha's we have yet to fetch. We will not exit
// until this equals zero.
dht.on('peer', function (addr, hash, from) {
var goal = fetching[hash]
if (!goal.peer) {
Expand All @@ -125,7 +123,7 @@ dht.on('peer', function (addr, hash, from) {
goal.swarm.addPeer(addr)
})

function get_infohash (sha, branch) {
function getInfoHash (sha, branch) {
branch = branch.replace(/^refs\/(heads\/)?/, '')
branch = branch.replace(/\/head$/, '')

Expand All @@ -139,19 +137,19 @@ function get_infohash (sha, branch) {
return
}

var info = {got: false, peer: false, swarm: null, branches: [branch]}
var info = { got: false, peer: false, swarm: null, branches: [branch] }
fetching[sha] = info

var magnetUri = 'magnet:?xt=urn:btih:' + sha
var parsed = magnet(magnetUri)
dht.lookup(parsed.infoHash)

var peerId = new Buffer('-WW' + VERSION + '-' + hat(48), 'utf8')
var peerId = Buffer.from('-WW' + VERSION + '-' + hat(48), 'utf8')
info.swarm = new Swarm(parsed.infoHash, peerId)
info.swarm.on('wire', function (wire, addr) {
console.warn('\nAdding swarm peer: ' + chalk.green(addr) + ' for ' +
chalk.green(parsed.infoHash))
wire.use(ut_gittorrent())
wire.use(utGittorrent())
wire.ut_gittorrent.on('handshake', function () {
wire.ut_gittorrent.ask(parsed.infoHash)
})
Expand Down
38 changes: 19 additions & 19 deletions git.js → lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var spawn = require('child_process').spawn

// Returns a process running `git ls-remote <url>` that calls `with_ref` on
// each parsed reference. The url may point to a local repository.
function ls (url, with_ref) {
function ls (url, withRef) {
var ls = spawn('git', ['ls-remote', url])
ls.stdout.on('data', function (lines) {
lines.toString().split('\n').forEach(function (line) {
Expand All @@ -18,7 +18,7 @@ function ls (url, with_ref) {
console.warn('[git ls-remote] expected a 40-byte sha: ' + sha + '\n')
console.warn('[git ls-remote] on line: ' + line.join('\t'))
}
with_ref(sha, branch)
withRef(sha, branch)
})
})
return ls
Expand All @@ -34,7 +34,7 @@ function pad4 (num) {

// Invokes `$ git-upload-pack --strict <dir>`, communicates haves and wants and
// emits 'ready' when stdout becomes a pack file stream.
function upload_pack (dir, want, have) {
function uploadPack (dir, want, have) {
// reference:
// https://github.com/git/git/blob/b594c975c7e865be23477989d7f36157ad437dc7/Documentation/technical/pack-protocol.txt#L346-L393
var upload = spawn('git-upload-pack', ['--strict', dir])
Expand All @@ -57,7 +57,7 @@ function upload_pack (dir, want, have) {
while (true) {
var line = getline()
if (line === null) {
return // to wait for more output
return // to wait for more output
}
if (!mode(line)) {
upload.stdout.removeAllListeners('readable')
Expand All @@ -67,56 +67,56 @@ function upload_pack (dir, want, have) {
}
})

var getline_len = null
// Extracts exactly one line from the stream. Uses `getline_len` in case the
var getLineLen = null
// Extracts exactly one line from the stream. Uses `getLineLen` in case the
// whole line could not be read.
function getline () {
// Format: '####line' where '####' represents the length of 'line' in hex.
if (!getline_len) {
getline_len = upload.stdout.read(4)
if (getline_len === null) {
if (!getLineLen) {
getLineLen = upload.stdout.read(4)
if (getLineLen === null) {
return null
}
getline_len = parseInt(getline_len, 16)
getLineLen = parseInt(getLineLen, 16)
}

if (getline_len === 0) {
if (getLineLen === 0) {
return ''
}

// Subtract by the four we just read, and the terminating newline.
var line = upload.stdout.read(getline_len - 4 - 1)
var line = upload.stdout.read(getLineLen - 4 - 1)
if (!line) {
return null
}
getline_len = null
upload.stdout.read(1) // And discard the newline.
getLineLen = null
upload.stdout.read(1) // And discard the newline.
return line.toString()
}

// First, the server lists the refs it has, but we already know from
// `git ls-remote`, so wait for it to signal the end.
function list (line) {
if (line === '') {
mode = have ? ack_objects_continue : wait_for_nak
mode = have ? ackObjectsContinue : waitForNak
}
return true
}

// If we only gave wants, git should respond with 'NAK', then the pack file.
function wait_for_nak (line) {
function waitForNak (line) {
return line !== 'NAK'
}

// With haves, we wait for 'ACK', but only if not ending in 'continue'.
function ack_objects_continue (line) {
function ackObjectsContinue (line) {
return !(line.search(/^ACK/) !== -1 && line.search(/continue$/) === -1)
}

// Writes one line to stdin so git-upload-pack can understand.
function writeln (line) {
if (line) {
var len = pad4(line.length + 4 + 1) // Add one for the newline.
var len = pad4(line.length + 4 + 1) // Add one for the newline.
upload.stdin.write(len + line + '\n')
} else {
upload.stdin.write('0000')
Expand All @@ -126,4 +126,4 @@ function upload_pack (dir, want, have) {
return upload
}

module.exports = {ls: ls, upload_pack: upload_pack}
module.exports = { ls: ls, upload_pack: uploadPack }
41 changes: 20 additions & 21 deletions gittorrentd → lib/gittorrentd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
var DHT = require('bittorrent-dht')
var EC = require('elliptic').ec
var ed25519 = new EC('ed25519')
var exec = require('child_process').exec
var glob = require('glob')
var fs = require('fs')
var hat = require('hat')
var net = require('net')
var utGittorrent = require('ut_gittorrent')
var utMetadata = require('ut_metadata')
var Protocol = require('bittorrent-protocol')
var spawn = require('child_process').spawn
var ut_gittorrent = require('ut_gittorrent')
var ut_metadata = require('ut_metadata')
var WebTorrent = require('webtorrent')
var zeroFill = require('zero-fill')
var config = require('./config')
Expand All @@ -22,7 +20,7 @@ var git = require('./git')
// '0.16.1' -> '0016'
// '1.2.5' -> '0102'
//
var VERSION = require('./package.json').version
var VERSION = require('../package.json').version
.match(/([0-9]+)/g).slice(0, 2).map(zeroFill(2)).join('')

function die (error) {
Expand All @@ -41,9 +39,9 @@ var userProfile = {
repositories: {}
}

var key = create_or_read_keyfile()
var key = createOrReadKeyFile()

function create_or_read_keyfile () {
function createOrReadKeyFile () {
if (!fs.existsSync(config.key)) {
var keypair = new EC('ed25519').genKeyPair()
fs.writeFileSync(config.key, JSON.stringify({
Expand All @@ -65,7 +63,7 @@ function create_or_read_keyfile () {
function bpad (n, buf) {
if (buf.length === n) return buf
if (buf.length < n) {
var b = new Buffer(n)
var b = Buffer.alloc(n)
buf.copy(b, n - buf.length)
for (var i = 0; i < n - buf.length; i++) b[i] = 0
return b
Expand All @@ -76,7 +74,7 @@ var head = ''

dht.on('ready', function () {
// Spider all */.git dirs and announce all refs.
var repos = glob.sync('*/{,.git/}git-daemon-export-ok', {strict: false})
var repos = glob.sync('*/{,.git/}git-daemon-export-ok', { strict: false })
var count = repos.length
repos.forEach(function (repo) {
console.log('in repo ' + repo)
Expand Down Expand Up @@ -108,7 +106,7 @@ dht.on('ready', function () {
ls.stdout.on('end', function () {
count--
if (count <= 0) {
publish_mutable_key()
publishMutableKey()
}
})
ls.on('exit', function (err) {
Expand All @@ -118,23 +116,24 @@ dht.on('ready', function () {
})
})

function publish_mutable_key () {
function publishMutableKey () {
var json = JSON.stringify(userProfile)
if (json.length > 950) {
console.error("Can't publish mutable key: doesn't fit in 950 bytes.")
return false
}
var value = new Buffer(json.length)
var value = Buffer.alloc(json.length)
value.write(json)
var sig = key.sign(value)
var opts = {
k: bpad(32, Buffer(key.getPublic().x.toArray())),
k: bpad(32, Buffer.from(key.getPublic().x.toArray())),
seq: 0,
v: value,
sig: Buffer.concat([
bpad(32, Buffer(sig.r.toArray())),
bpad(32, Buffer(sig.s.toArray()))
])}
bpad(32, Buffer.from(sig.r.toArray())),
bpad(32, Buffer.from(sig.s.toArray()))
])
}
console.log(json)
dht.put(opts, function (errors, hash) {
console.error('errors=', errors)
Expand All @@ -144,13 +143,13 @@ dht.on('ready', function () {

net.createServer(function (socket) {
var wire = new Protocol()
wire.use(ut_gittorrent())
wire.use(ut_metadata())
wire.use(utGittorrent())
wire.use(utMetadata())
socket.pipe(wire).pipe(socket)
wire.on('handshake', function (infoHash, peerId) {
console.log('Received handshake for ' + infoHash.toString('hex'))
var myPeerId = new Buffer('-WW' + VERSION + '-' + hat(48), 'utf8')
wire.handshake(new Buffer(infoHash), new Buffer(myPeerId))
var myPeerId = Buffer.from('-WW' + VERSION + '-' + hat(48), 'utf8')
wire.handshake(Buffer.from(infoHash), Buffer.from(myPeerId))
})
wire.ut_gittorrent.on('generatePack', function (sha) {
console.error('calling git pack-objects for ' + sha)
Expand All @@ -172,7 +171,7 @@ dht.on('ready', function () {
stream.on('close', function () {
console.error('Finished writing ' + filename)
var webtorrent = new WebTorrent({
dht: {bootstrap: config.dht.bootstrap},
dht: { bootstrap: config.dht.bootstrap },
tracker: false
})
webtorrent.seed(filename, function onTorrent (torrent) {
Expand Down
Loading