Skip to content

Commit

Permalink
fix(NODE-6600): set object mode correctly for message chunking in Siz…
Browse files Browse the repository at this point in the history
…edMessageTransform (#4345)

Co-authored-by: Bailey Pearson <[email protected]>
  • Loading branch information
addaleax and baileympearson authored Dec 4, 2024
1 parent 260e052 commit 5558573
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/cmap/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ export class SizedMessageTransform extends Transform {
connection: Connection;

constructor({ connection }: { connection: Connection }) {
super({ objectMode: false });
super({ writableObjectMode: false, readableObjectMode: true });
this.bufferPool = new BufferPool();
this.connection = connection;
}
Expand Down
18 changes: 17 additions & 1 deletion test/unit/cmap/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
MongoClientAuthProviders,
MongoDBCollectionNamespace,
MongoNetworkTimeoutError,
ns
ns,
SizedMessageTransform
} from '../../mongodb';
import * as mock from '../../tools/mongodb-mock/index';
import { getSymbolFrom } from '../../tools/utils';
Expand Down Expand Up @@ -323,4 +324,19 @@ describe('new Connection()', function () {
});
});
});

describe('SizedMessageTransform', function () {
it('parses chunks of wire messages', function () {
const stream = new SizedMessageTransform({ connection: {} as any });
// Message of length 4 + 4 = 8
stream.write(Buffer.from([8, 0, 0, 0]));
stream.write(Buffer.from([1, 2, 3, 4]));
// Message of length 4 + 2 = 6, chunked differently
stream.write(Buffer.from([6, 0, 0]));
stream.write(Buffer.from([0, 5, 6]));
expect(stream.read(1)).to.deep.equal(Buffer.from([8, 0, 0, 0, 1, 2, 3, 4]));
expect(stream.read(1)).to.deep.equal(Buffer.from([6, 0, 0, 0, 5, 6]));
expect(stream.read(1)).to.equal(null);
});
});
});

0 comments on commit 5558573

Please sign in to comment.