Skip to content

Commit

Permalink
Merge pull request #700 from stephenplusplus/spp--storage-tail-bytes
Browse files Browse the repository at this point in the history
storage: support tail bytes
  • Loading branch information
stephenplusplus committed Jul 1, 2015
2 parents c7bcdc8 + ab89f1c commit 0dacd2d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,25 @@ 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 || {};

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;
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 0dacd2d

Please sign in to comment.