Skip to content

Commit 083b029

Browse files
committed
fix: Dont pass session to cloned cursor
1 parent 19ba74a commit 083b029

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const { ReadPreference } = require('../../src/read_preference');
1212
const { ServerType } = require('../../src/sdam/common');
1313
const { formatSort } = require('../../src/sort');
1414
const { FindCursor } = require('../../src/cursor/find_cursor');
15+
const kSession = Symbol('session');
1516

1617
describe('Cursor', function () {
1718
before(function () {
@@ -3755,6 +3756,65 @@ describe('Cursor', function () {
37553756
}
37563757
);
37573758

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

0 commit comments

Comments
 (0)