From eedc66f8fc82a714963e452162dae406705d4bf7 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Tue, 22 Nov 2022 19:24:33 +0100 Subject: [PATCH 1/2] fix(NODE-4831): check map value is not undefined --- src/cmap/connection.ts | 3 ++- test/unit/cmap/connection.test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/cmap/connection.ts b/src/cmap/connection.ts index 7aa913fbe40..265ea7a1c0d 100644 --- a/src/cmap/connection.ts +++ b/src/cmap/connection.ts @@ -384,7 +384,8 @@ export class Connection extends TypedEventEmitter { } else { // Get the first orphaned operation description. const entry = this[kQueue].entries().next(); - if (entry) { + /* eslint no-restricted-syntax: 0 */ + if (entry.value !== undefined) { 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 a9b95bcea2c..b6ab033a118 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(/undefined is not iterable/); + }); + }); + context('when more than one operation description is in the queue', function () { let spyOne; let spyTwo; From 48244b6bc88ff0defd79eef31320f6696df7549d Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Tue, 22 Nov 2022 20:47:35 +0100 Subject: [PATCH 2/2] fix(NODE-4831): check for null --- src/cmap/connection.ts | 3 +-- test/unit/cmap/connection.test.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cmap/connection.ts b/src/cmap/connection.ts index 265ea7a1c0d..85f60008174 100644 --- a/src/cmap/connection.ts +++ b/src/cmap/connection.ts @@ -384,8 +384,7 @@ export class Connection extends TypedEventEmitter { } else { // Get the first orphaned operation description. const entry = this[kQueue].entries().next(); - /* eslint no-restricted-syntax: 0 */ - if (entry.value !== undefined) { + 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 b6ab033a118..5c8d872bb84 100644 --- a/test/unit/cmap/connection.test.ts +++ b/test/unit/cmap/connection.test.ts @@ -311,7 +311,7 @@ describe('new Connection()', function () { const message = new BinMsg(msg, msgHeader, msgBody); expect(() => { connection.onMessage(message); - }).to.not.throw(/undefined is not iterable/); + }).to.not.throw(); }); });