Skip to content

Commit 5fbd387

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

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/cmap/connection.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ 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+
/* eslint no-restricted-syntax: 0 */
388+
if (entry.value !== undefined) {
388389
const [requestId, orphaned]: [number, OperationDescription] = entry.value;
389390
// If the orphaned operation description exists then set it.
390391
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(/undefined is not iterable/);
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)