Skip to content

Commit d7898f9

Browse files
fix(NODE-6042): Binary.toString output with respect to position (#663)
1 parent efab49a commit d7898f9

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

src/binary.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,15 @@ export class Binary extends BSONValue {
186186
}
187187

188188
toJSON(): string {
189-
return ByteUtils.toBase64(this.buffer);
189+
return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
190190
}
191191

192192
toString(encoding?: 'hex' | 'base64' | 'utf8' | 'utf-8'): string {
193-
if (encoding === 'hex') return ByteUtils.toHex(this.buffer);
194-
if (encoding === 'base64') return ByteUtils.toBase64(this.buffer);
193+
if (encoding === 'hex') return ByteUtils.toHex(this.buffer.subarray(0, this.position));
194+
if (encoding === 'base64') return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
195195
if (encoding === 'utf8' || encoding === 'utf-8')
196-
return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
197-
return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
196+
return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
197+
return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
198198
}
199199

200200
/** @internal */

test/node/binary.test.ts

+64
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,68 @@ describe('class Binary', () => {
144144
});
145145
});
146146
});
147+
148+
context('toString()', () => {
149+
context('when case is UTF8 (default)', () => {
150+
it('should respect position when converting to string', () => {
151+
const bin = new Binary();
152+
expect(bin.toString()).to.equal('');
153+
bin.put(1);
154+
expect(bin.toString()).to.equal('\u0001');
155+
});
156+
it('should remain same after round trip', () => {
157+
const bin = new BSON.Binary();
158+
const serializedBin = BSON.serialize({ bin });
159+
const roundTrippedBin = BSON.deserialize(serializedBin);
160+
expect(roundTrippedBin.bin.toString()).to.equal(bin.toString());
161+
});
162+
});
163+
164+
context('when case is hex', () => {
165+
it('should respect position when converting to string', () => {
166+
const bin = new Binary();
167+
expect(bin.toString('hex')).to.equal('');
168+
bin.put(1);
169+
expect(bin.toString('hex')).to.equal('01');
170+
});
171+
it('should remain same after round trip', () => {
172+
const bin = new BSON.Binary();
173+
const serializedBin = BSON.serialize({ bin });
174+
const roundTrippedBin = BSON.deserialize(serializedBin);
175+
expect(roundTrippedBin.bin.toString('hex')).to.equal(bin.toString('hex'));
176+
});
177+
});
178+
179+
context('when case is base64', () => {
180+
it('should respect position when converting to string', () => {
181+
const bin = new Binary();
182+
expect(bin.toString('base64')).to.equal('');
183+
bin.put(1);
184+
expect(bin.toString('base64')).to.equal('AQ==');
185+
});
186+
it('should remain same after round trip', () => {
187+
const bin = new BSON.Binary();
188+
const serializedBin = BSON.serialize({ bin });
189+
const roundTrippedBin = BSON.deserialize(serializedBin);
190+
expect(roundTrippedBin.bin.toString('base64')).to.equal(bin.toString());
191+
});
192+
});
193+
});
194+
195+
context('toJSON()', () => {
196+
it('should respect position when converting to JSON', () => {
197+
const bin = new Binary();
198+
expect(bin.toJSON()).to.equal('');
199+
bin.put(1);
200+
// toJSON uses base64
201+
expect(bin.toJSON()).to.equal('AQ==');
202+
});
203+
204+
it('should remain same after round trip', () => {
205+
const bin = new BSON.Binary();
206+
const serializedBin = BSON.serialize({ bin });
207+
const roundTrippedBin = BSON.deserialize(serializedBin);
208+
expect(roundTrippedBin.bin.toJSON()).to.equal(bin.toJSON());
209+
});
210+
});
147211
});

0 commit comments

Comments
 (0)