Skip to content

Commit c86dadd

Browse files
committed
fix(NODE-4831): check map value is not undefined
1 parent c4c560c commit c86dadd

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 !== undefined) {
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+
// Emit a message that matches the existing operation description.
301+
const msg = generateOpMsgBuffer(document);
302+
const msgHeader: MessageHeader = {
303+
length: msg.readInt32LE(0),
304+
requestId: 2,
305+
responseTo: 1,
306+
opCode: msg.readInt32LE(12)
307+
};
308+
const msgBody = msg.subarray(16);
309+
310+
const message = new BinMsg(msg, msgHeader, msgBody);
311+
connection.onMessage(message);
312+
});
313+
314+
it('does not error', function () {
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)