diff --git a/lib/storage/file.js b/lib/storage/file.js index fe3502589f6..4e9557190e8 100644 --- a/lib/storage/file.js +++ b/lib/storage/file.js @@ -396,6 +396,17 @@ File.prototype.move = function(destination, callback) { * }) * .pipe(fs.createWriteStream('/Users/stephen/logfile.txt')) * .on('error', function(err) {}); + * + * //- + * // To read a tail byte range, specify only `options.end` as a negative + * // number. + * //- + * var logFile = myBucket.file('access_log'); + * logFile.createReadStream({ + * end: -100 + * }) + * .pipe(fs.createWriteStream('/Users/stephen/logfile.txt')) + * .on('error', function(err) {}); */ File.prototype.createReadStream = function(options) { options = options || {}; @@ -403,6 +414,7 @@ File.prototype.createReadStream = function(options) { var that = this; var rangeRequest = util.is(options.start, 'number') || util.is(options.end, 'number'); + var tailRequest = options.end < 0; var throughStream = streamEvents(through()); var requestStream; @@ -477,6 +489,12 @@ File.prototype.createReadStream = function(options) { }; } + if (tailRequest) { + reqOpts.headers = { + Range: 'bytes=' + options.end + }; + } + that.bucket.storage.makeAuthorizedRequest_(reqOpts, { onAuthorized: function(err, authorizedReqOpts) { if (err) { diff --git a/test/storage/file.js b/test/storage/file.js index df5deb8d311..e38b8a6bc97 100644 --- a/test/storage/file.js +++ b/test/storage/file.js @@ -801,6 +801,22 @@ describe('File', function() { readStream.destroy = done; }); }); + + describe('tail requests', function() { + it('should make a request for the tail bytes', function(done) { + var endOffset = -10; + + request_Override = function(opts) { + setImmediate(function () { + assert.equal(opts.headers.Range, 'bytes=' + endOffset); + done(); + }); + return duplexify(); + }; + + file.createReadStream({ end: endOffset }); + }); + }); }); describe('createWriteStream', function() {