Skip to content

Commit 04964bd

Browse files
add regression test
1 parent 4d8745c commit 04964bd

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

lib/storage/file.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ File.prototype.createReadStream = function(options) {
259259
options = options || {};
260260

261261
var that = this;
262-
var throughStream = through();
262+
var rangeRequest =
263+
util.is(options.start, 'number') || util.is(options.end, 'number');
264+
var throughStream = streamEvents(through());
263265

264266
var validations = ['crc32c', 'md5'];
265267
var validation;
@@ -278,6 +280,10 @@ File.prototype.createReadStream = function(options) {
278280
validation = 'all';
279281
}
280282

283+
if (rangeRequest) {
284+
validation = false;
285+
}
286+
281287
var crc32c = validation === 'crc32c' || validation === 'all';
282288
var md5 = validation === 'md5' || validation === 'all';
283289

@@ -304,7 +310,7 @@ File.prototype.createReadStream = function(options) {
304310
uri: uri
305311
};
306312

307-
if (util.is(options.start, 'number') || util.is(options.end, 'number')) {
313+
if (rangeRequest) {
308314
reqOpts.headers = {
309315
Range: 'bytes=' + [options.start || '', options.end || ''].join('-')
310316
};
@@ -340,6 +346,13 @@ File.prototype.createReadStream = function(options) {
340346
})
341347

342348
.on('complete', function(res) {
349+
if (rangeRequest) {
350+
// Range requests can't receive data integrity checks.
351+
throughStream.emit('complete', res);
352+
throughStream.end();
353+
return;
354+
}
355+
343356
var failed = false;
344357
var crcFail = true;
345358
var md5Fail = true;
@@ -378,7 +391,7 @@ File.prototype.createReadStream = function(options) {
378391

379392
throughStream.emit('error', error);
380393
} else {
381-
throughStream.emit('complete');
394+
throughStream.emit('complete', res);
382395
}
383396

384397
throughStream.end();

regression/storage.js

+24
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,30 @@ describe('storage', function() {
336336
});
337337
});
338338

339+
it('should read a byte range from a file', function(done) {
340+
bucket.upload(files.big.path, function(err, file) {
341+
assert.ifError(err);
342+
343+
var fileSize = file.metadata.size;
344+
var byteRange = {
345+
start: Math.floor(fileSize * 1/3),
346+
end: Math.floor(fileSize * 2/3)
347+
};
348+
var expectedContentSize = byteRange.start + 1;
349+
350+
var sizeStreamed = 0;
351+
file.createReadStream(byteRange)
352+
.on('data', function (chunk) {
353+
sizeStreamed += chunk.length;
354+
})
355+
.on('error', done)
356+
.on('complete', function() {
357+
assert.equal(sizeStreamed, expectedContentSize);
358+
file.delete(done);
359+
});
360+
});
361+
});
362+
339363
describe('stream write', function() {
340364
it('should stream write, then remove file (3mb)', function(done) {
341365
var file = bucket.file('LargeFile');

0 commit comments

Comments
 (0)