Skip to content

Commit 21b729b

Browse files
fix(NODE-6165): useBigInt64 causes compareTopologyVersion to throw (#4109)
Co-authored-by: Bailey Pearson <[email protected]>
1 parent 9285c42 commit 21b729b

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

Diff for: src/sdam/server_description.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,19 @@ export function compareTopologyVersion(
258258
}
259259

260260
// TODO(NODE-2674): Preserve int64 sent from MongoDB
261-
const currentCounter = Long.isLong(currentTv.counter)
262-
? currentTv.counter
263-
: Long.fromNumber(currentTv.counter);
264-
const newCounter = Long.isLong(newTv.counter) ? newTv.counter : Long.fromNumber(newTv.counter);
261+
const currentCounter =
262+
typeof currentTv.counter === 'bigint'
263+
? Long.fromBigInt(currentTv.counter)
264+
: Long.isLong(currentTv.counter)
265+
? currentTv.counter
266+
: Long.fromNumber(currentTv.counter);
267+
268+
const newCounter =
269+
typeof newTv.counter === 'bigint'
270+
? Long.fromBigInt(newTv.counter)
271+
: Long.isLong(newTv.counter)
272+
? newTv.counter
273+
: Long.fromNumber(newTv.counter);
265274

266275
return currentCounter.compare(newCounter);
267276
}

Diff for: test/unit/sdam/server_description.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,22 @@ describe('ServerDescription', function () {
130130
currentTv: { processId: processIdZero, counter: Long.fromNumber(2) },
131131
newTv: { processId: processIdZero, counter: Long.fromNumber(2) },
132132
out: 0
133+
},
134+
{
135+
title: 'when process ids are equal and both counter values are zero bigints',
136+
// @ts-expect-error: Testing that the function handles bigints
137+
currentTv: { processId: processIdZero, counter: 0n },
138+
// @ts-expect-error: Testing that the function handles bigints
139+
newTv: { processId: processIdZero, counter: 0n },
140+
out: 0
141+
},
142+
{
143+
title: 'when process ids are equal and both counter values are non-zero bigints',
144+
// @ts-expect-error: Testing that the function handles bigints
145+
currentTv: { processId: processIdZero, counter: 2n },
146+
// @ts-expect-error: Testing that the function handles bigints
147+
newTv: { processId: processIdZero, counter: 2n },
148+
out: 0
133149
}
134150
];
135151
const compareTopologyVersionLessThanTests: CompareTopologyVersionTest[] = [
@@ -178,6 +194,12 @@ describe('ServerDescription', function () {
178194
currentTv: { processId: processIdZero, counter: Long.fromNumber(3) },
179195
newTv: { processId: processIdZero, counter: Long.fromNumber(2) },
180196
out: 1
197+
},
198+
{
199+
title: 'when processIds are equal but new counter is less than current (bigint)',
200+
currentTv: { processId: processIdZero, counter: 3n },
201+
newTv: { processId: processIdZero, counter: 2n },
202+
out: 1
181203
}
182204
];
183205

0 commit comments

Comments
 (0)