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

feat: add maxPieceLength option #267

Merged
merged 2 commits into from
Jan 4, 2025
Merged
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 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 = 4 MiB)
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 = 4 * 1024 * 1024
}

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)
})
})
Loading