Skip to content

Commit

Permalink
Merge pull request #610 from robertdimarco/rd-emit-response-event
Browse files Browse the repository at this point in the history
Emit response event from file#createReadStream()
  • Loading branch information
ryanseys committed May 20, 2015
2 parents aa71a71 + 9112d53 commit 0ba8b88
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
5 changes: 5 additions & 0 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@ File.prototype.move = function(destination, callback) {
* image.createReadStream()
* .pipe(fs.createWriteStream('/Users/stephen/Photos/image.png'))
* .on('error', function(err) {})
* .on('response', function(response) {
* // Server connected and responded with the specified status and headers.
* })
* .on('complete', function() {
* // The file is fully downloaded.
* });
Expand Down Expand Up @@ -464,6 +467,8 @@ File.prototype.createReadStream = function(options) {
request(authorizedReqOpts)
.on('error', done)

.on('response', throughStream.emit.bind(throughStream, 'response'))

.on('data', function(chunk) {
if (crc32c) {
localCrcHash = crc.calculate(chunk, localCrcHash);
Expand Down
59 changes: 37 additions & 22 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,30 @@ describe('File', function() {
});

describe('createReadStream', function() {

function getFakeRequest(data, fakeResponse) {
function FakeRequest(req) {
if (!(this instanceof FakeRequest)) {
return new FakeRequest(req);
}

var that = this;

stream.Readable.call(this);
this._read = function() {
this.push(data);
this.push(null);
};

setImmediate(function() {
that.emit('response', fakeResponse);
that.emit('complete', fakeResponse);
});
}
nodeutil.inherits(FakeRequest, stream.Readable);
return FakeRequest;
}

it('should create an authorized request', function(done) {
var expectedPath = util.format('https://{b}.storage.googleapis.com/{o}', {
b: file.bucket.name,
Expand Down Expand Up @@ -398,6 +422,19 @@ describe('File', function() {
});
});

it('should emit response event from request', function(done) {
var response = {
headers: { 'x-goog-hash': 'md5=fakefakefake' }
};
request_Override = getFakeRequest('body', response);

file.createReadStream({ validation: false })
.on('response', function(res) {
assert.deepEqual(response, res);
done();
});
});

it('should get readable stream from request', function(done) {
var fakeRequest = { a: 'b', c: 'd' };

Expand Down Expand Up @@ -442,28 +479,6 @@ describe('File', function() {
}
};

function getFakeRequest(data, fakeResponse) {
function FakeRequest(req) {
if (!(this instanceof FakeRequest)) {
return new FakeRequest(req);
}

var that = this;

stream.Readable.call(this);
this._read = function() {
this.push(data);
this.push(null);
};

setImmediate(function() {
that.emit('complete', fakeResponse);
});
}
nodeutil.inherits(FakeRequest, stream.Readable);
return FakeRequest;
}

beforeEach(function() {
file.metadata.mediaLink = 'http://uri';

Expand Down

0 comments on commit 0ba8b88

Please sign in to comment.