diff options
Diffstat (limited to 'lib/file.js')
-rw-r--r-- | lib/file.js | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/lib/file.js b/lib/file.js index 44e8c6b..5149047 100644 --- a/lib/file.js +++ b/lib/file.js @@ -7,6 +7,7 @@ var FileStream = require('./file-stream') var inherits = require('inherits') var mime = require('./mime.json') var path = require('path') +var stream = require('stream') inherits(File, EventEmitter) @@ -67,14 +68,22 @@ File.prototype.deselect = function () { */ File.prototype.createReadStream = function (opts) { var self = this - var stream = new FileStream(self, opts) - self._torrent.select(stream._startPiece, stream._endPiece, true, function () { - stream._notify() + if (this.length === 0) { + var empty = new stream.PassThrough() + process.nextTick(function () { + empty.end() + }) + return empty + } + + var fileStream = new FileStream(self, opts) + self._torrent.select(fileStream._startPiece, fileStream._endPiece, true, function () { + fileStream._notify() }) - eos(stream, function () { - self._torrent.deselect(stream._startPiece, stream._endPiece, true) + eos(fileStream, function () { + self._torrent.deselect(fileStream._startPiece, fileStream._endPiece, true) }) - return stream + return fileStream } /** |