Skip to content

Commit

Permalink
feat: add maxPieceLength option to address #266
Browse files Browse the repository at this point in the history
  • Loading branch information
SilentBot1 committed Jan 4, 2025
1 parent f342a69 commit 2bed028
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Or, an **array of `string`, `File`, `Buffer`, or `stream.Readable` objects**.
filterJunkFiles: Boolean, // remove hidden and other junk files? (default = true)
private: Boolean, // is this a private .torrent? (default = false)
pieceLength: Number, // force a custom piece length (number of bytes)
maxPieceLength: Number, // force a maximum piece length for auto piece length selection, does not affect pieceLength option (default = Infinity)
announceList: [[String]], // custom trackers (array of arrays of strings) (see [bep12](http://www.bittorrent.org/beps/bep_0012.html))
urlList: [String], // web seed urls (see [bep19](http://www.bittorrent.org/beps/bep_0019.html))
info: Object, // add non-standard info dict entries, e.g. info.source, a convention for cross-seeding
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const announceList = [
* @param {string=} opts.createdBy
* @param {boolean|number=} opts.private
* @param {number=} opts.pieceLength
* @param {number=} opts.maxPieceLength
* @param {Array.<Array.<string>>=} opts.announceList
* @param {Array.<string>=} opts.urlList
* @param {Object=} opts.info
Expand Down Expand Up @@ -151,6 +152,10 @@ function _parseInput (input, opts, cb) {
opts.name = `Unnamed Torrent ${Date.now()}`
}

if (!opts.maxPieceLength) {
opts.maxPieceLength = Infinity
}

const numPaths = input.reduce((sum, item) => sum + Number(typeof item === 'string'), 0)

let isSingleFileTorrent = (input.length === 1)
Expand Down Expand Up @@ -300,7 +305,7 @@ function onFiles (files, opts, cb) {
if (opts.urlList !== undefined) torrent['url-list'] = opts.urlList

const estimatedTorrentLength = files.reduce(sumLength, 0)
const pieceLength = opts.pieceLength || calcPieceLength(estimatedTorrentLength)
const pieceLength = opts.pieceLength || Math.min(calcPieceLength(estimatedTorrentLength), opts.maxPieceLength)
torrent.info['piece length'] = pieceLength

getPieceList(
Expand Down
54 changes: 54 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,57 @@ test('implicit torrent name from file names with slashes in them', t => {
t.equal(parsedTorrent.files[1].path, path.join('My Cool Folder', 'My Cool File 2'))
})
})

test('verify torrent length with maxPieceLength set', t => {
t.plan(8)

const buf1 = Buffer.from('buf1')
buf1.name = 'My Cool Folder/My Cool File 1'

const buf2 = Buffer.from('buf2')
buf2.name = 'My Cool Folder/My Cool File 2'

createTorrent([buf1, buf2], { maxPieceLength: 10 }, async (err, torrent) => {
t.error(err)
const parsedTorrent = await parseTorrent(torrent)

t.equal(parsedTorrent.name, 'My Cool Folder')

t.equal(parsedTorrent.files.length, 2)

t.equal(parsedTorrent.files[0].name, 'My Cool File 1')
t.equal(parsedTorrent.files[0].path, path.join('My Cool Folder', 'My Cool File 1'))

t.equal(parsedTorrent.files[1].name, 'My Cool File 2')
t.equal(parsedTorrent.files[1].path, path.join('My Cool Folder', 'My Cool File 2'))

t.equal(parsedTorrent.pieceLength, 10)
})
})

test('verify maxPieceLength is ignored when pieceLength is manually set', t => {
t.plan(8)

const buf1 = Buffer.from('buf1')
buf1.name = 'My Cool Folder/My Cool File 1'

const buf2 = Buffer.from('buf2')
buf2.name = 'My Cool Folder/My Cool File 2'

createTorrent([buf1, buf2], { pieceLength: 1024, maxPieceLength: 10 }, async (err, torrent) => {
t.error(err)
const parsedTorrent = await parseTorrent(torrent)

t.equal(parsedTorrent.name, 'My Cool Folder')

t.equal(parsedTorrent.files.length, 2)

t.equal(parsedTorrent.files[0].name, 'My Cool File 1')
t.equal(parsedTorrent.files[0].path, path.join('My Cool Folder', 'My Cool File 1'))

t.equal(parsedTorrent.files[1].name, 'My Cool File 2')
t.equal(parsedTorrent.files[1].path, path.join('My Cool Folder', 'My Cool File 2'))

t.equal(parsedTorrent.pieceLength, 1024)
})
})

0 comments on commit 2bed028

Please sign in to comment.