Skip to content

Commit ca6e0aa

Browse files
committed
fix(NODE-41439): only process when on last hello
1 parent 562b63a commit ca6e0aa

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/cmap/message_stream.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,28 @@ function processIncomingData(stream: MessageStream, callback: Callback<Buffer>)
168168
opCode: message.readInt32LE(12)
169169
};
170170

171+
const streamingProtocolHasAnotherHello = () => {
172+
if (stream.isStreamingProtocol) {
173+
// Can we read the next message size?
174+
if (buffer.length >= 4) {
175+
const sizeOfMessage = buffer.peek(4).readInt32LE();
176+
if (sizeOfMessage < buffer.length) {
177+
return true;
178+
}
179+
return false;
180+
}
181+
}
182+
return false;
183+
}
184+
171185
let ResponseType = messageHeader.opCode === OP_MSG ? BinMsg : Response;
172186
if (messageHeader.opCode !== OP_COMPRESSED) {
173187
const messageBody = message.slice(MESSAGE_HEADER_SIZE);
174188

175189
// If we are a monitoring message stream using the streaming protocol and
176190
// there is more in the buffer that can be read, skip processing since we
177191
// want the last hello command response that is in the buffer.
178-
if (stream.isStreamingProtocol && buffer.length >= 4) {
192+
if (streamingProtocolHasAnotherHello()) {
179193
processIncomingData(stream, callback);
180194
} else {
181195
stream.emit('message', new ResponseType(message, messageHeader, messageBody));
@@ -215,7 +229,7 @@ function processIncomingData(stream: MessageStream, callback: Callback<Buffer>)
215229
// If we are a monitoring message stream using the streaming protocol and
216230
// there is more in the buffer that can be read, skip processing since we
217231
// want the last hello command response that is in the buffer.
218-
if (stream.isStreamingProtocol && buffer.length >= 4) {
232+
if (streamingProtocolHasAnotherHello()) {
219233
processIncomingData(stream, callback);
220234
} else {
221235
stream.emit('message', new ResponseType(message, messageHeader, messageBody));

0 commit comments

Comments
 (0)