Skip to content

Commit 836810f

Browse files
authored
NODE-3008: Dont pass session to cloned cursor (#2725)
fix: Dont pass session to cloned cursor
1 parent e362d1d commit 836810f

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

src/cursor/aggregation_cursor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AggregateOperation, AggregateOptions } from '../operations/aggregate';
22
import { AbstractCursor, assertUninitialized } from './abstract_cursor';
33
import { executeOperation, ExecutionResult } from '../operations/execute_operation';
4+
import { mergeOptions } from '../utils';
45
import type { Document } from '../bson';
56
import type { Sort } from '../sort';
67
import type { Topology } from '../sdam/topology';
@@ -52,9 +53,9 @@ export class AggregationCursor extends AbstractCursor {
5253
}
5354

5455
clone(): AggregationCursor {
56+
const clonedOptions = mergeOptions({}, this[kOptions]);
5557
return new AggregationCursor(this[kParent], this.topology, this.namespace, this[kPipeline], {
56-
...this[kOptions],
57-
...this.cursorOptions
58+
...clonedOptions
5859
});
5960
}
6061

src/cursor/find_cursor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ExplainVerbosityLike } from '../explain';
44
import { CountOperation, CountOptions } from '../operations/count';
55
import { executeOperation, ExecutionResult } from '../operations/execute_operation';
66
import { FindOperation, FindOptions } from '../operations/find';
7+
import { mergeOptions } from '../utils';
78
import type { Hint } from '../operations/operation';
89
import type { CollationOptions } from '../operations/command';
910
import type { Topology } from '../sdam/topology';
@@ -52,9 +53,9 @@ export class FindCursor extends AbstractCursor {
5253
}
5354

5455
clone(): FindCursor {
56+
const clonedOptions = mergeOptions({}, this[kBuiltOptions]);
5557
return new FindCursor(this.topology, this.namespace, this[kFilter], {
56-
...this[kBuiltOptions],
57-
...this.cursorOptions
58+
...clonedOptions
5859
});
5960
}
6061

test/functional/cursor.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3755,6 +3755,65 @@ describe('Cursor', function () {
37553755
}
37563756
);
37573757

3758+
describe('#clone', function () {
3759+
let client;
3760+
let db;
3761+
let collection;
3762+
3763+
beforeEach(function () {
3764+
client = this.configuration.newClient({ w: 1 });
3765+
3766+
return client.connect().then(client => {
3767+
db = client.db(this.configuration.db);
3768+
collection = db.collection('test_coll');
3769+
});
3770+
});
3771+
3772+
afterEach(function () {
3773+
return client.close();
3774+
});
3775+
3776+
context('when executing on a find cursor', function () {
3777+
it('removes the existing session from the cloned cursor', function () {
3778+
const docs = [{ name: 'test1' }, { name: 'test2' }];
3779+
return collection.insertMany(docs).then(() => {
3780+
const cursor = collection.find({}, { batchSize: 1 });
3781+
return cursor
3782+
.next()
3783+
.then(doc => {
3784+
expect(doc).to.exist;
3785+
const clonedCursor = cursor.clone();
3786+
expect(clonedCursor.cursorOptions.session).to.not.exist;
3787+
expect(clonedCursor.session).to.not.exist;
3788+
})
3789+
.finally(() => {
3790+
return cursor.close();
3791+
});
3792+
});
3793+
});
3794+
});
3795+
3796+
context('when executing on an aggregation cursor', function () {
3797+
it('removes the existing session from the cloned cursor', function () {
3798+
const docs = [{ name: 'test1' }, { name: 'test2' }];
3799+
return collection.insertMany(docs).then(() => {
3800+
const cursor = collection.aggregate([{ $match: {} }], { batchSize: 1 });
3801+
return cursor
3802+
.next()
3803+
.then(doc => {
3804+
expect(doc).to.exist;
3805+
const clonedCursor = cursor.clone();
3806+
expect(clonedCursor.cursorOptions.session).to.not.exist;
3807+
expect(clonedCursor.session).to.not.exist;
3808+
})
3809+
.finally(() => {
3810+
return cursor.close();
3811+
});
3812+
});
3813+
});
3814+
});
3815+
});
3816+
37583817
it('should return a promise when no callback supplied to forEach method', function () {
37593818
const configuration = this.configuration;
37603819
const client = configuration.newClient({ w: 1 }, { maxPoolSize: 1 });

0 commit comments

Comments
 (0)