Skip to content

Commit 9795cdb

Browse files
authored
fix(NODE-4831): check map value is not undefined (#3477)
1 parent ff375e9 commit 9795cdb

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/cmap/connection.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
384384
} else {
385385
// Get the first orphaned operation description.
386386
const entry = this[kQueue].entries().next();
387-
if (entry) {
387+
if (entry.value != null) {
388388
const [requestId, orphaned]: [number, OperationDescription] = entry.value;
389389
// If the orphaned operation description exists then set it.
390390
operationDescription = orphaned;

test/unit/cmap/connection.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,34 @@ describe('new Connection()', function () {
287287
});
288288
});
289289

290+
context('when no operation description is in the queue', function () {
291+
const document = { ok: 1 };
292+
293+
beforeEach(function () {
294+
// @ts-expect-error: driverSocket does not fully satisfy the stream type, but that's okay
295+
connection = sinon.spy(new Connection(driverSocket, connectionOptionsDefaults));
296+
connection.isMonitoringConnection = true;
297+
const queueSymbol = getSymbolFrom(connection, 'queue');
298+
queue = connection[queueSymbol];
299+
});
300+
301+
it('does not error', function () {
302+
const msg = generateOpMsgBuffer(document);
303+
const msgHeader: MessageHeader = {
304+
length: msg.readInt32LE(0),
305+
requestId: 2,
306+
responseTo: 1,
307+
opCode: msg.readInt32LE(12)
308+
};
309+
const msgBody = msg.subarray(16);
310+
311+
const message = new BinMsg(msg, msgHeader, msgBody);
312+
expect(() => {
313+
connection.onMessage(message);
314+
}).to.not.throw();
315+
});
316+
});
317+
290318
context('when more than one operation description is in the queue', function () {
291319
let spyOne;
292320
let spyTwo;

0 commit comments

Comments
 (0)