Skip to content

Commit 5ac1376

Browse files
revisions from 6.x transfer
1 parent 44ad321 commit 5ac1376

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

src/binary.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,15 @@ export class Binary extends BSONValue {
217217
}
218218

219219
toJSON(): string {
220-
return ByteUtils.toBase64(this.buffer);
220+
return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
221221
}
222222

223223
toString(encoding?: 'hex' | 'base64' | 'utf8' | 'utf-8'): string {
224-
if (encoding === 'hex') return ByteUtils.toHex(this.buffer);
225-
if (encoding === 'base64') return ByteUtils.toBase64(this.buffer);
224+
if (encoding === 'hex') return ByteUtils.toHex(this.buffer.subarray(0, this.position));
225+
if (encoding === 'base64') return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
226226
if (encoding === 'utf8' || encoding === 'utf-8')
227-
return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
228-
return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
227+
return ByteUtils.toUTF8(this.buffer, 0, this.position);
228+
return ByteUtils.toUTF8(this.buffer, 0, this.position);
229229
}
230230

231231
/** @internal */

test/node/binary.test.ts

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

0 commit comments

Comments
 (0)