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

Idempotent decode and encode #15

Closed
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ function encodeTorrentFile (parsed) {
info: parsed.info
}

torrent['announce-list'] = (parsed.announce || []).map(function (url) {
if (!torrent.announce) torrent.announce = url
url = new Buffer(url, 'utf8')
return [ url ]
})
if (parsed.announce.length > 0) {
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 || []

Expand Down
3 changes: 3 additions & 0 deletions test/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ var test = require('tape')

test('encode', function (t) {
var parsedTorrent = parseTorrentFile(fixtures.leaves.torrent)

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

console.log(buf.length, fixtures.leaves.torrent.length)
t.deepEqual(buf, fixtures.leaves.torrent)
t.deepEqual(doubleParsedTorrent, parsedTorrent)
t.end()
})
Expand Down