Skip to content
This repository has been archived by the owner on Apr 13, 2018. It is now read-only.

Commit

Permalink
encode: preserve 'created by' and 'comment' keys
Browse files Browse the repository at this point in the history
  • Loading branch information
feross committed Apr 28, 2016
1 parent 600ac91 commit 6fa5d60
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,26 @@ function encodeTorrentFile (parsed) {
info: parsed.info
}

torrent['announce-list'] = parsed.announce.map(function (url) {
torrent['announce-list'] = (parsed.announce || []).map(function (url) {
if (!torrent.announce) torrent.announce = url
url = new Buffer(url, 'utf8')
return [ url ]
})

torrent['url-list'] = parsed.urlList || []

if (parsed.created) {
torrent['creation date'] = (parsed.created.getTime() / 1000) | 0
}
if (parsed.urlList) {
torrent['url-list'] = parsed.urlList

if (parsed.createdBy) {
torrent['created by'] = parsed.createdBy
}

if (parsed.comment) {
torrent.comment = parsed.comment
}

return bencode.encode(torrent)
}

Expand Down
8 changes: 2 additions & 6 deletions test/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@ test('encode', function (t) {
var buf = parseTorrentFile.encode(parsedTorrent)
var doubleParsedTorrent = parseTorrentFile(buf)

t.deepEqual(doubleParsedTorrent.infoBuffer, parsedTorrent.infoBuffer)
t.equal(doubleParsedTorrent.created.getDate(), parsedTorrent.created.getDate())
t.deepEqual(doubleParsedTorrent.announce, parsedTorrent.announce)

t.deepEqual(doubleParsedTorrent, parsedTorrent)
t.end()
})

test('encode w/ comment field', function (t) {
var parsedTorrent = parseTorrentFile(fixtures.leaves.torrent)
parsedTorrent.comment = 'hi there!'

var buf = parseTorrentFile.encode(parsedTorrent)
var doubleParsedTorrent = parseTorrentFile(buf)

t.equal(doubleParsedTorrent.comment, parsedTorrent.comment)
t.deepEqual(doubleParsedTorrent, parsedTorrent)
t.end()
})

0 comments on commit 6fa5d60

Please sign in to comment.