Skip to content

Commit 9ebface

Browse files
AVaksmanjkwlui
authored andcommitted
feat: add flag to allow disabling auto decompression by client (#850)
* feat: add flag to allow disabling auto decomression by client * refactor: rename returnCompressed default false to decompress default true * docs: fix * fix: docs * fix: test description
1 parent 302ad88 commit 9ebface

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/file.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ export interface CreateReadStreamOptions {
360360
validation?: 'md5' | 'crc32c' | false | true;
361361
start?: number;
362362
end?: number;
363+
decompress?: boolean;
363364
}
364365

365366
export interface SaveOptions extends CreateWriteStreamOptions {}
@@ -1120,6 +1121,10 @@ class File extends ServiceObject<File> {
11201121
* NOTE: Byte ranges are inclusive; that is, `options.start = 0` and
11211122
* `options.end = 999` represent the first 1000 bytes in a file or object.
11221123
* NOTE: when specifying a byte range, data integrity is not available.
1124+
* @property {boolean} [decompress=true] Disable auto decompression of the
1125+
* received data. By default this option is set to `true`.
1126+
* Applicable in cases where the data was uploaded with
1127+
* `gzip: true` option. See {@link File#createWriteStream}.
11231128
*/
11241129
/**
11251130
* Create a readable stream to read the contents of the remote file. It can be
@@ -1192,6 +1197,7 @@ class File extends ServiceObject<File> {
11921197
* .pipe(fs.createWriteStream('/Users/stephen/logfile.txt'));
11931198
*/
11941199
createReadStream(options: CreateReadStreamOptions = {}): Readable {
1200+
options = Object.assign({decompress: true}, options);
11951201
const rangeRequest =
11961202
typeof options.start === 'number' || typeof options.end === 'number';
11971203
const tailRequest = options.end! < 0;
@@ -1312,7 +1318,7 @@ class File extends ServiceObject<File> {
13121318
throughStreams.push(validateStream);
13131319
}
13141320

1315-
if (isCompressed) {
1321+
if (isCompressed && options.decompress) {
13161322
throughStreams.push(zlib.createGunzip());
13171323
}
13181324

test/file.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,17 @@ describe('File', () => {
12031203
.resume();
12041204
});
12051205

1206+
it('should not gunzip the response if "decompress: false" is passed', done => {
1207+
file
1208+
.createReadStream({decompress: false})
1209+
.once('error', done)
1210+
.on('data', (data: {}) => {
1211+
assert.strictEqual(data, GZIPPED_DATA);
1212+
done();
1213+
})
1214+
.resume();
1215+
});
1216+
12061217
it('should emit errors from the gunzip stream', done => {
12071218
const error = new Error('Error.');
12081219
const createGunzipStream = through();

0 commit comments

Comments
 (0)