From 9795cdb19f8e652b4dc4badd4fe8e6d1a7b837a6 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Tue, 22 Nov 2022 23:04:29 +0100 Subject: [PATCH] fix(NODE-4831): check map value is not undefined (#3477) --- src/cmap/connection.ts | 2 +- test/unit/cmap/connection.test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/cmap/connection.ts b/src/cmap/connection.ts index 7aa913fbe4..85f6000817 100644 --- a/src/cmap/connection.ts +++ b/src/cmap/connection.ts @@ -384,7 +384,7 @@ export class Connection extends TypedEventEmitter { } else { // Get the first orphaned operation description. const entry = this[kQueue].entries().next(); - if (entry) { + if (entry.value != null) { const [requestId, orphaned]: [number, OperationDescription] = entry.value; // If the orphaned operation description exists then set it. operationDescription = orphaned; diff --git a/test/unit/cmap/connection.test.ts b/test/unit/cmap/connection.test.ts index a9b95bcea2..5c8d872bb8 100644 --- a/test/unit/cmap/connection.test.ts +++ b/test/unit/cmap/connection.test.ts @@ -287,6 +287,34 @@ describe('new Connection()', function () { }); }); + context('when no operation description is in the queue', function () { + const document = { ok: 1 }; + + beforeEach(function () { + // @ts-expect-error: driverSocket does not fully satisfy the stream type, but that's okay + connection = sinon.spy(new Connection(driverSocket, connectionOptionsDefaults)); + connection.isMonitoringConnection = true; + const queueSymbol = getSymbolFrom(connection, 'queue'); + queue = connection[queueSymbol]; + }); + + it('does not error', function () { + const msg = generateOpMsgBuffer(document); + const msgHeader: MessageHeader = { + length: msg.readInt32LE(0), + requestId: 2, + responseTo: 1, + opCode: msg.readInt32LE(12) + }; + const msgBody = msg.subarray(16); + + const message = new BinMsg(msg, msgHeader, msgBody); + expect(() => { + connection.onMessage(message); + }).to.not.throw(); + }); + }); + context('when more than one operation description is in the queue', function () { let spyOne; let spyTwo;