create-torrent
Advanced tools
Comparing version 3.12.0 to 3.13.0
114
index.js
@@ -23,2 +23,3 @@ /*global Blob, FileList */ | ||
var fs = require('fs') | ||
var isFile = require('is-file') | ||
var MultiStream = require('multistream') | ||
@@ -51,4 +52,5 @@ var once = require('once') | ||
if (!opts) opts = {} | ||
parseInput(input, opts, function (err, files) { | ||
parseInput(input, opts, function (err, files, singleFileTorrent) { | ||
if (err) return cb(err) | ||
opts.singleFileTorrent = singleFileTorrent | ||
onFiles(files, opts, cb) | ||
@@ -85,41 +87,57 @@ }) | ||
parallel(input.map(function (item) { | ||
return function (cb) { | ||
var file = {} | ||
var isSingleFileTorrent = (input.length === 1) | ||
if (isBlob(item)) { | ||
file.getStream = getBlobStream(item) | ||
file.length = item.size | ||
} else if (Buffer.isBuffer(item)) { | ||
file.getStream = getBufferStream(item) | ||
file.length = item.length | ||
} else if (isReadable(item)) { | ||
if (!opts.pieceLength) { | ||
throw new Error('must specify `pieceLength` option if input is Stream') | ||
if (input.length === 1 && typeof input[0] === 'string') { | ||
// If there's a single path, verify it's a file before deciding this is a single | ||
// file torrent | ||
isFile(input[0], function (err, pathIsFile) { | ||
if (err) return cb(err) | ||
isSingleFileTorrent = pathIsFile | ||
processInput() | ||
}) | ||
} else { | ||
processInput() | ||
} | ||
function processInput () { | ||
parallel(input.map(function (item) { | ||
return function (cb) { | ||
var file = {} | ||
if (isBlob(item)) { | ||
file.getStream = getBlobStream(item) | ||
file.length = item.size | ||
} else if (Buffer.isBuffer(item)) { | ||
file.getStream = getBufferStream(item) | ||
file.length = item.length | ||
} else if (isReadable(item)) { | ||
if (!opts.pieceLength) { | ||
throw new Error('must specify `pieceLength` option if input is Stream') | ||
} | ||
file.getStream = getStreamStream(item, file) | ||
file.length = 0 | ||
} else if (typeof item === 'string') { | ||
if (typeof fs.readdir !== 'function') { | ||
throw new Error('filesystem paths do not work in the browser') | ||
} | ||
var keepRoot = numPaths > 1 || isSingleFileTorrent | ||
getFiles(item, keepRoot, cb) | ||
return // early return! | ||
} else { | ||
throw new Error('invalid input type') | ||
} | ||
file.getStream = getStreamStream(item, file) | ||
file.length = 0 | ||
} else if (typeof item === 'string') { | ||
if (typeof fs.readdir !== 'function') { | ||
throw new Error('filesystem paths do not work in the browser') | ||
} | ||
var keepRoot = numPaths > 1 | ||
getFiles(item, keepRoot, cb) | ||
return // early return! | ||
} else { | ||
throw new Error('invalid input type') | ||
if (!item.name) throw new Error('missing requied `name` property on input') | ||
file.path = item.name.split(corePath.sep) | ||
cb(null, file) | ||
} | ||
if (!item.name) throw new Error('missing requied `name` property on input') | ||
file.path = item.name.split(corePath.sep) | ||
cb(null, file) | ||
} | ||
}), function (err, files) { | ||
if (err) return cb(err) | ||
files = flatten(files) | ||
cb(null, files) | ||
}) | ||
}), function (err, files) { | ||
if (err) return cb(err) | ||
files = flatten(files) | ||
cb(null, files, isSingleFileTorrent) | ||
}) | ||
} | ||
} | ||
function getFiles (path, keepRoot, cb) { | ||
traversePath(getFileInfo, path, function (err, files) { | ||
traversePath(path, getFileInfo, function (err, files) { | ||
if (err) return cb(err) | ||
@@ -130,14 +148,12 @@ | ||
var dirName = corePath.normalize(path) | ||
if (keepRoot || files.length === 1) { | ||
dirName = dirName.slice(0, dirName.lastIndexOf(corePath.sep) + 1) | ||
} else { | ||
if (dirName[dirName.length - 1] !== corePath.sep) dirName += corePath.sep | ||
path = corePath.normalize(path) | ||
if (keepRoot) { | ||
path = path.slice(0, path.lastIndexOf(corePath.sep) + 1) | ||
} | ||
if (path[path.length - 1] !== corePath.sep) path += corePath.sep | ||
files.forEach(function (file) { | ||
file.getStream = getFilePathStream(file.path) | ||
file.path = file.path.replace(dirName, '').split(corePath.sep) | ||
file.path = file.path.replace(path, '').split(corePath.sep) | ||
}) | ||
cb(null, files) | ||
@@ -159,3 +175,3 @@ }) | ||
function traversePath (fn, path, cb) { | ||
function traversePath (path, fn, cb) { | ||
fs.readdir(path, function (err, entries) { | ||
@@ -172,3 +188,3 @@ if (err && err.code === 'ENOTDIR') { | ||
return function (cb) { | ||
traversePath(fn, corePath.join(path, entry), cb) | ||
traversePath(corePath.join(path, entry), fn, cb) | ||
} | ||
@@ -283,5 +299,5 @@ }), cb) | ||
if (opts.comment !== undefined) torrent.info.comment = opts.comment | ||
if (opts.comment !== undefined) torrent.comment = opts.comment | ||
if (opts.createdBy !== undefined) torrent.info['created by'] = opts.createdBy | ||
if (opts.createdBy !== undefined) torrent['created by'] = opts.createdBy | ||
@@ -298,4 +314,2 @@ if (opts.private !== undefined) torrent.info.private = Number(opts.private) | ||
var singleFile = files.length === 1 | ||
var pieceLength = opts.pieceLength || calcPieceLength(files.reduce(sumLength, 0)) | ||
@@ -312,6 +326,6 @@ torrent.info['piece length'] = pieceLength | ||
if (!singleFile) { | ||
if (opts.singleFileTorrent) { | ||
torrent.info.length = torrentLength | ||
} else { | ||
torrent.info.files = files | ||
} else { | ||
torrent.info.length = torrentLength | ||
} | ||
@@ -318,0 +332,0 @@ |
{ | ||
"name": "create-torrent", | ||
"description": "Create .torrent files", | ||
"version": "3.12.0", | ||
"version": "3.13.0", | ||
"author": "Feross Aboukhadijeh <feross@feross.org> (http://feross.org/)", | ||
@@ -18,2 +18,3 @@ "bin": { | ||
"flatten": "0.0.1", | ||
"is-file": "^1.0.0", | ||
"minimist": "^1.1.0", | ||
@@ -20,0 +21,0 @@ "multistream": "^1.0.0", |
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
441602
900
12
+ Addedis-file@^1.0.0
+ Addedis-file@1.0.0(transitive)