Skip to content

Commit 5558573

Browse files
addaleaxbaileympearson
andauthoredDec 4, 2024··
fix(NODE-6600): set object mode correctly for message chunking in SizedMessageTransform (#4345)
Co-authored-by: Bailey Pearson <bailey.pearson@mongodb.com>
1 parent 260e052 commit 5558573

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed
 

‎src/cmap/connection.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ export class SizedMessageTransform extends Transform {
769769
connection: Connection;
770770

771771
constructor({ connection }: { connection: Connection }) {
772-
super({ objectMode: false });
772+
super({ writableObjectMode: false, readableObjectMode: true });
773773
this.bufferPool = new BufferPool();
774774
this.connection = connection;
775775
}

‎test/unit/cmap/connection.test.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
MongoClientAuthProviders,
1212
MongoDBCollectionNamespace,
1313
MongoNetworkTimeoutError,
14-
ns
14+
ns,
15+
SizedMessageTransform
1516
} from '../../mongodb';
1617
import * as mock from '../../tools/mongodb-mock/index';
1718
import { getSymbolFrom } from '../../tools/utils';
@@ -323,4 +324,19 @@ describe('new Connection()', function () {
323324
});
324325
});
325326
});
327+
328+
describe('SizedMessageTransform', function () {
329+
it('parses chunks of wire messages', function () {
330+
const stream = new SizedMessageTransform({ connection: {} as any });
331+
// Message of length 4 + 4 = 8
332+
stream.write(Buffer.from([8, 0, 0, 0]));
333+
stream.write(Buffer.from([1, 2, 3, 4]));
334+
// Message of length 4 + 2 = 6, chunked differently
335+
stream.write(Buffer.from([6, 0, 0]));
336+
stream.write(Buffer.from([0, 5, 6]));
337+
expect(stream.read(1)).to.deep.equal(Buffer.from([8, 0, 0, 0, 1, 2, 3, 4]));
338+
expect(stream.read(1)).to.deep.equal(Buffer.from([6, 0, 0, 0, 5, 6]));
339+
expect(stream.read(1)).to.equal(null);
340+
});
341+
});
326342
});

0 commit comments

Comments
 (0)
Please sign in to comment.