create-torrent
Advanced tools
Comparing version 3.3.1 to 3.4.0
165
index.js
module.exports = createTorrent | ||
module.exports.announceList = [ | ||
[ 'udp://tracker.publicbt.com:80' ], | ||
[ 'udp://tracker.openbittorrent.com:80' ], | ||
[ 'udp://tracker.webtorrent.io:80' ], | ||
[ 'wss://tracker.webtorrent.io' ] // For WebRTC peers (see: WebTorrent.io) | ||
] | ||
module.exports.parseInput = parseInput | ||
var bencode = require('bencode') | ||
@@ -19,3 +28,3 @@ var BlockStream = require('block-stream') | ||
* Create a torrent. | ||
* @param {string|File|FileList|Blob|Buffer|Stream|Array.<File|Blob|Buffer|Steam>} input | ||
* @param {string|File|FileList|Buffer|Stream|Array.<File|Buffer|Stream>} input | ||
* @param {Object} opts | ||
@@ -39,7 +48,17 @@ * @param {string=} opts.name | ||
if (!opts) opts = {} | ||
var files | ||
parseInput(input, opts, function (err, files) { | ||
if (err) return cb(err) | ||
onFiles(files, opts, cb) | ||
}) | ||
} | ||
// TODO: support an array of paths | ||
// TODO: support an array of paths | ||
function parseInput (input, opts, cb) { | ||
if (typeof opts === 'function') { | ||
cb = opts | ||
opts = {} | ||
} | ||
if (!opts) opts = {} | ||
if (typeof FileList === 'function' && input instanceof FileList) | ||
if (isFileList(input)) | ||
input = Array.prototype.slice.call(input) | ||
@@ -50,20 +69,14 @@ | ||
var files | ||
if (Array.isArray(input) && input.length > 0) { | ||
opts.name = opts.name || input[0].name | ||
if (opts.name === undefined) | ||
throw new Error('Option \'name\' is required when input is not a file') | ||
throw new Error('missing option \'name\' and unable to infer it from input[0].name') | ||
// If there's just one file, allow the name to be set by `opts.name` | ||
if (input.length === 1 && !input[0].name) { | ||
input[0].name = opts.name | ||
} | ||
if (input.length === 1 && !input[0].name) input[0].name = opts.name | ||
files = input.map(function (item) { | ||
if (!item) return | ||
if (!item.name) | ||
throw new Error('Missing requied `name` property on input') | ||
var file = {} | ||
var file = { | ||
path: [ item.name || 'no-name' ] | ||
} | ||
if (isBlob(item)) { | ||
@@ -77,11 +90,16 @@ file.getStream = getBlobStream(item) | ||
if (!opts.pieceLength) | ||
throw new Error('Must specify `pieceLength` option if input is Stream') | ||
throw new Error('must specify `pieceLength` option if input is Stream') | ||
file.getStream = getStreamStream(item, file) | ||
file.length = 0 | ||
} else { | ||
throw new Error('Array must contain only File|Blob|Buffer|Stream objects') | ||
throw new Error('input must contain only File|Blob|Buffer|Stream objects') | ||
} | ||
if (!item.name) throw new Error('missing requied `name` property on input') | ||
file.path = [ item.name ] | ||
return file | ||
}) | ||
onFiles(files, opts, cb) | ||
process.nextTick(function () { | ||
cb(null, files) | ||
}) | ||
} else if (typeof input === 'string') { | ||
@@ -93,15 +111,12 @@ opts.name = opts.name || corePath.basename(input) | ||
if (Array.isArray(files)) { | ||
files = flatten(files) | ||
} else { | ||
files = [ files ] | ||
} | ||
if (Array.isArray(files)) files = flatten(files) | ||
else files = [ files ] | ||
var dirName = corePath.normalize(input) + corePath.sep | ||
files.forEach(function (file) { | ||
file.getStream = getFSStream(file.path) | ||
file.getStream = getFilePathStream(file.path) | ||
file.path = file.path.replace(dirName, '').split(corePath.sep) | ||
}) | ||
onFiles(files, opts, cb) | ||
cb(null, files) | ||
}) | ||
@@ -113,18 +128,2 @@ } else { | ||
createTorrent.announceList = [ | ||
[ 'udp://tracker.publicbt.com:80' ], | ||
[ 'udp://tracker.openbittorrent.com:80' ], | ||
[ 'udp://tracker.webtorrent.io:80' ], | ||
[ 'wss://tracker.webtorrent.io' ] // For WebRTC peers (see: WebTorrent.io) | ||
] | ||
function each (arr, fn, cb) { | ||
var tasks = arr.map(function (item) { | ||
return function (cb) { | ||
fn(item, cb) | ||
} | ||
}) | ||
parallel(tasks, cb) | ||
} | ||
function getFileInfo (path, cb) { | ||
@@ -148,9 +147,11 @@ cb = once(cb) | ||
} else if (err) { | ||
// there was an error | ||
// real error | ||
cb(err) | ||
} else { | ||
// this is a folder | ||
each(entries, function (entry, cb) { | ||
traversePath(fn, corePath.join(path, entry), cb) | ||
}, cb) | ||
parallel(entries.map(function (entry) { | ||
return function (cb) { | ||
traversePath(fn, corePath.join(path, entry), cb) | ||
} | ||
}), cb) | ||
} | ||
@@ -184,3 +185,3 @@ }) | ||
? opts.announceList | ||
: createTorrent.announceList // default | ||
: module.exports.announceList // default | ||
@@ -197,17 +198,13 @@ var torrent = { | ||
if (opts.comment !== undefined) { | ||
if (opts.comment !== undefined) | ||
torrent.info.comment = opts.comment | ||
} | ||
if (opts.createdBy !== undefined) { | ||
if (opts.createdBy !== undefined) | ||
torrent.info['created by'] = opts.createdBy | ||
} | ||
if (opts.private !== undefined) { | ||
if (opts.private !== undefined) | ||
torrent.info.private = Number(opts.private) | ||
} | ||
if (opts.urlList !== undefined) { | ||
if (opts.urlList !== undefined) | ||
torrent['url-list'] = opts.urlList | ||
} | ||
@@ -248,3 +245,3 @@ var singleFile = files.length === 1 | ||
/** | ||
* Check if `obj` is a W3C Blob object (which is the superclass of W3C File) | ||
* Check if `obj` is a W3C `Blob` object (which `File` inherits from) | ||
* @param {*} obj | ||
@@ -257,3 +254,12 @@ * @return {boolean} | ||
/** | ||
* Check if `obj` is a W3C `FileList` object | ||
* @param {*} obj | ||
* @return {boolean} | ||
*/ | ||
function isFileList (obj) { | ||
return typeof FileList === 'function' && obj instanceof FileList | ||
} | ||
/** | ||
@@ -268,12 +274,22 @@ * Check if `obj` is a node Readable stream | ||
function getBlobStream (data) { | ||
/** | ||
* Convert a `File` to a lazy readable stream. | ||
* @param {File|Blob} file | ||
* @return {function} | ||
*/ | ||
function getBlobStream (file) { | ||
return function () { | ||
return new FileReadStream(data) | ||
return new FileReadStream(file) | ||
} | ||
} | ||
function getBufferStream (data) { | ||
/** | ||
* Convert a `Buffer` to a lazy readable stream. | ||
* @param {Buffer} buffer | ||
* @return {function} | ||
*/ | ||
function getBufferStream (buffer) { | ||
return function () { | ||
var s = new stream.PassThrough() | ||
s.end(data) | ||
s.end(buffer) | ||
return s | ||
@@ -283,17 +299,32 @@ } | ||
function getFSStream (data) { | ||
/** | ||
* Convert a file path to a lazy readable stream. | ||
* @param {string} path | ||
* @return {function} | ||
*/ | ||
function getFilePathStream (path) { | ||
return function () { | ||
return fs.createReadStream(data) | ||
return fs.createReadStream(path) | ||
} | ||
} | ||
/** | ||
* Convert a readable stream to a lazy readable stream. Adds instrumentation to track | ||
* the number of bytes in the stream and set `file.length`. | ||
* | ||
* @param {Stream} stream | ||
* @param {Object} file | ||
* @return {function} | ||
*/ | ||
function getStreamStream (stream, file) { | ||
var counter = new Transform() | ||
counter._transform = function (buf, enc, done) { | ||
file.length += buf.length | ||
this.push(buf) | ||
done() | ||
return function () { | ||
var counter = new Transform() | ||
counter._transform = function (buf, enc, done) { | ||
file.length += buf.length | ||
this.push(buf) | ||
done() | ||
} | ||
stream.pipe(counter) | ||
return counter | ||
} | ||
stream.pipe(counter) | ||
return counter | ||
} |
{ | ||
"name": "create-torrent", | ||
"description": "Create .torrent files", | ||
"version": "3.3.1", | ||
"version": "3.4.0", | ||
"author": "Feross Aboukhadijeh <feross@feross.org> (http://feross.org/)", | ||
@@ -6,0 +6,0 @@ "bin": { |
@@ -47,7 +47,6 @@ # create-torrent [![travis](https://img.shields.io/travis/feross/create-torrent.svg?style=flat)](https://travis-ci.org/feross/create-torrent) [![npm](https://img.shields.io/npm/v/create-torrent.svg?style=flat)](https://npmjs.org/package/create-torrent) [![gittip](https://img.shields.io/gittip/feross.svg?style=flat)](https://www.gittip.com/feross/) | ||
- W3C [FileList](https://developer.mozilla.org/en-US/docs/Web/API/FileList) object (basically an array of `File` objects) | ||
- W3C [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) object | ||
- Node [Buffer](http://nodejs.org/api/buffer.html) object (works in [the browser](https://www.npmjs.org/package/buffer)) | ||
- Node [stream.Readable](http://nodejs.org/api/stream.html) object (must attach a `name` property on it (or set `opt.name`), and set `opt.pieceLength`) | ||
Or, an **array of `File`, `Blob`, `Buffer`, or `Stream` objects**. | ||
Or, an **array of `File`, `Buffer`, or `Stream` objects**. | ||
@@ -54,0 +53,0 @@ `opts` is optional and allows you to set special settings on the produced .torrent file. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
431288
667
90