Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 8a9f259

Browse files
authored
fix: 🐛 fix the issue 2667, do not mutate object given to options … (#2668)
* fix: 🐛 fix the issue 2667, do not mutate object given to options argument in file download method * test: 🧪 add the test for issue 2667, and fix another faulty test * style: 🎨 fix the lint error in test
1 parent e0ac235 commit 8a9f259

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

src/file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,7 @@ class File extends ServiceObject<File, FileMetadata> {
23092309
cb = optionsOrCallback as DownloadCallback;
23102310
options = {};
23112311
} else {
2312-
options = optionsOrCallback as DownloadOptions;
2312+
options = Object.assign({}, optionsOrCallback);
23132313
}
23142314

23152315
let called = false;

test/file.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,11 +2596,26 @@ describe('File', () => {
25962596
file.download({}, assert.ifError);
25972597
});
25982598

2599+
it('should not mutate options object after use', done => {
2600+
const optionsObject = {destination: './unknown.jpg'};
2601+
fileReadStream._read = () => {
2602+
assert.strictEqual(optionsObject.destination, './unknown.jpg');
2603+
assert.deepStrictEqual(optionsObject, {destination: './unknown.jpg'});
2604+
done();
2605+
};
2606+
file.download(optionsObject, assert.ifError);
2607+
});
2608+
25992609
it('should pass the provided options to createReadStream', done => {
2600-
const readOptions = {start: 100, end: 200};
2610+
const readOptions = {start: 100, end: 200, destination: './unknown.jpg'};
26012611

26022612
file.createReadStream = (options: {}) => {
2603-
assert.deepStrictEqual(options, readOptions);
2613+
assert.deepStrictEqual(options, {start: 100, end: 200});
2614+
assert.deepStrictEqual(readOptions, {
2615+
start: 100,
2616+
end: 200,
2617+
destination: './unknown.jpg',
2618+
});
26042619
done();
26052620
return fileReadStream;
26062621
};

0 commit comments

Comments
 (0)