From 6fa5d60dfb9ebe39ae97d2b17931e4feda922d4c Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 28 Apr 2016 14:21:59 +0200 Subject: [PATCH] encode: preserve 'created by' and 'comment' keys Closes https://github.com/feross/create-torrent/issues/36 --- index.js | 14 +++++++++++--- test/encode.js | 8 ++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index a3aeb50..d9ca910 100644 --- a/index.js +++ b/index.js @@ -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) } diff --git a/test/encode.js b/test/encode.js index c367054..7b9362d 100644 --- a/test/encode.js +++ b/test/encode.js @@ -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() })