diff --git a/src/file.ts b/src/file.ts index 598967152..79f417162 100644 --- a/src/file.ts +++ b/src/file.ts @@ -360,6 +360,7 @@ export interface CreateReadStreamOptions { validation?: 'md5' | 'crc32c' | false | true; start?: number; end?: number; + decompress?: boolean; } export interface SaveOptions extends CreateWriteStreamOptions {} @@ -1120,6 +1121,10 @@ class File extends ServiceObject { * NOTE: Byte ranges are inclusive; that is, `options.start = 0` and * `options.end = 999` represent the first 1000 bytes in a file or object. * NOTE: when specifying a byte range, data integrity is not available. + * @property {boolean} [decompress=true] Disable auto decompression of the + * received data. By default this option is set to `true`. + * Applicable in cases where the data was uploaded with + * `gzip: true` option. See {@link File#createWriteStream}. */ /** * Create a readable stream to read the contents of the remote file. It can be @@ -1192,6 +1197,7 @@ class File extends ServiceObject { * .pipe(fs.createWriteStream('/Users/stephen/logfile.txt')); */ createReadStream(options: CreateReadStreamOptions = {}): Readable { + options = Object.assign({decompress: true}, options); const rangeRequest = typeof options.start === 'number' || typeof options.end === 'number'; const tailRequest = options.end! < 0; @@ -1312,7 +1318,7 @@ class File extends ServiceObject { throughStreams.push(validateStream); } - if (isCompressed) { + if (isCompressed && options.decompress) { throughStreams.push(zlib.createGunzip()); } diff --git a/test/file.ts b/test/file.ts index 78447ec35..86651e82d 100644 --- a/test/file.ts +++ b/test/file.ts @@ -1203,6 +1203,17 @@ describe('File', () => { .resume(); }); + it('should not gunzip the response if "decompress: false" is passed', done => { + file + .createReadStream({decompress: false}) + .once('error', done) + .on('data', (data: {}) => { + assert.strictEqual(data, GZIPPED_DATA); + done(); + }) + .resume(); + }); + it('should emit errors from the gunzip stream', done => { const error = new Error('Error.'); const createGunzipStream = through();