From 21b729b983e2ddbe1dd9cff31f996825a45ec4e0 Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Wed, 22 May 2024 16:17:22 -0400 Subject: [PATCH] fix(NODE-6165): useBigInt64 causes compareTopologyVersion to throw (#4109) Co-authored-by: Bailey Pearson --- src/sdam/server_description.ts | 17 +++++++++++++---- test/unit/sdam/server_description.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/sdam/server_description.ts b/src/sdam/server_description.ts index 5068931b6a..c95f7cc905 100644 --- a/src/sdam/server_description.ts +++ b/src/sdam/server_description.ts @@ -258,10 +258,19 @@ export function compareTopologyVersion( } // TODO(NODE-2674): Preserve int64 sent from MongoDB - const currentCounter = Long.isLong(currentTv.counter) - ? currentTv.counter - : Long.fromNumber(currentTv.counter); - const newCounter = Long.isLong(newTv.counter) ? newTv.counter : Long.fromNumber(newTv.counter); + const currentCounter = + typeof currentTv.counter === 'bigint' + ? Long.fromBigInt(currentTv.counter) + : Long.isLong(currentTv.counter) + ? currentTv.counter + : Long.fromNumber(currentTv.counter); + + const newCounter = + typeof newTv.counter === 'bigint' + ? Long.fromBigInt(newTv.counter) + : Long.isLong(newTv.counter) + ? newTv.counter + : Long.fromNumber(newTv.counter); return currentCounter.compare(newCounter); } diff --git a/test/unit/sdam/server_description.test.ts b/test/unit/sdam/server_description.test.ts index e91a0863bd..14c3ae0dbd 100644 --- a/test/unit/sdam/server_description.test.ts +++ b/test/unit/sdam/server_description.test.ts @@ -130,6 +130,22 @@ describe('ServerDescription', function () { currentTv: { processId: processIdZero, counter: Long.fromNumber(2) }, newTv: { processId: processIdZero, counter: Long.fromNumber(2) }, out: 0 + }, + { + title: 'when process ids are equal and both counter values are zero bigints', + // @ts-expect-error: Testing that the function handles bigints + currentTv: { processId: processIdZero, counter: 0n }, + // @ts-expect-error: Testing that the function handles bigints + newTv: { processId: processIdZero, counter: 0n }, + out: 0 + }, + { + title: 'when process ids are equal and both counter values are non-zero bigints', + // @ts-expect-error: Testing that the function handles bigints + currentTv: { processId: processIdZero, counter: 2n }, + // @ts-expect-error: Testing that the function handles bigints + newTv: { processId: processIdZero, counter: 2n }, + out: 0 } ]; const compareTopologyVersionLessThanTests: CompareTopologyVersionTest[] = [ @@ -178,6 +194,12 @@ describe('ServerDescription', function () { currentTv: { processId: processIdZero, counter: Long.fromNumber(3) }, newTv: { processId: processIdZero, counter: Long.fromNumber(2) }, out: 1 + }, + { + title: 'when processIds are equal but new counter is less than current (bigint)', + currentTv: { processId: processIdZero, counter: 3n }, + newTv: { processId: processIdZero, counter: 2n }, + out: 1 } ];