Skip to content

Commit

Permalink
fix(NODE-4831): check map value is not undefined (#3477)
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Nov 22, 2022
1 parent ff375e9 commit 9795cdb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cmap/connection.ts
Expand Up @@ -384,7 +384,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
} 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;
Expand Down
28 changes: 28 additions & 0 deletions test/unit/cmap/connection.test.ts
Expand Up @@ -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;
Expand Down

0 comments on commit 9795cdb

Please sign in to comment.