Skip to content

Commit 72e5236

Browse files
PR changes
1 parent 06bf665 commit 72e5236

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/binary.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,7 @@ Object.defineProperty(Binary.prototype, '_bsontype', { value: 'Binary' });
298298
export type UUIDExtended = {
299299
$uuid: string;
300300
};
301-
const BYTE_LENGTH = 16;
302-
303-
const kId = Symbol('id');
301+
const UUID_BYTE_LENGTH = 16;
304302

305303
/**
306304
* A class representation of the BSON UUID type.
@@ -309,8 +307,6 @@ const kId = Symbol('id');
309307
export class UUID extends Binary {
310308
static cacheHexString: boolean;
311309

312-
/** UUID Bytes @internal */
313-
private [kId]!: Buffer;
314310
/** UUID hexString cache @internal */
315311
private __id?: string;
316312

@@ -325,9 +321,9 @@ export class UUID extends Binary {
325321
if (input == null) {
326322
bytes = UUID.generate();
327323
} else if (input instanceof UUID) {
328-
bytes = Buffer.from(input[kId]);
324+
bytes = Buffer.from(input.buffer);
329325
hexStr = input.__id;
330-
} else if (ArrayBuffer.isView(input) && input.byteLength === BYTE_LENGTH) {
326+
} else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
331327
bytes = ensureBuffer(input);
332328
} else if (typeof input === 'string') {
333329
bytes = uuidHexStringToBuffer(input);
@@ -337,7 +333,7 @@ export class UUID extends Binary {
337333
);
338334
}
339335
super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
340-
this[kId] = bytes;
336+
this.buffer = bytes;
341337
this.__id = hexStr;
342338
}
343339

@@ -346,11 +342,11 @@ export class UUID extends Binary {
346342
* @readonly
347343
*/
348344
get id(): Buffer {
349-
return this[kId];
345+
return this.buffer;
350346
}
351347

352348
set id(value: Buffer) {
353-
this[kId] = value;
349+
this.buffer = value;
354350

355351
if (UUID.cacheHexString) {
356352
this.__id = bufferToUuidHexString(value);
@@ -426,7 +422,7 @@ export class UUID extends Binary {
426422
* Generates a populated buffer containing a v4 uuid
427423
*/
428424
static generate(): Buffer {
429-
const bytes = randomBytes(BYTE_LENGTH);
425+
const bytes = randomBytes(UUID_BYTE_LENGTH);
430426

431427
// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
432428
// Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
@@ -455,7 +451,7 @@ export class UUID extends Binary {
455451

456452
if (isUint8Array(input)) {
457453
// check for length & uuid version (https://tools.ietf.org/html/rfc4122#section-4.1.3)
458-
if (input.length !== BYTE_LENGTH) {
454+
if (input.length !== UUID_BYTE_LENGTH) {
459455
return false;
460456
}
461457

test/node/uuid_tests.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const { inspect } = require('util');
66
const { validate: uuidStringValidate, version: uuidStringVersion } = require('uuid');
77
const BSON = require('../register-bson');
88
const BSONTypeError = BSON.BSONTypeError;
9+
const BSON_DATA_BINARY = BSON.BSON_DATA_BINARY;
10+
const BSON_BINARY_SUBTYPE_UUID_NEW = BSON.BSON_BINARY_SUBTYPE_UUID_NEW;
911

1012
// Test values
1113
const UPPERCASE_DASH_SEPARATED_UUID_STRING = 'AAAAAAAA-AAAA-4AAA-AAAA-AAAAAAAAAAAA';
@@ -170,5 +172,23 @@ describe('UUID', () => {
170172
const plainUUIDSerialization = BSON.serialize({ uuid: exampleUUID });
171173
expect(plainUUIDSerialization).to.deep.equal(toBinarySerialization);
172174
});
175+
176+
it('should have a valid UUID _bsontype with Object input without error', () => {
177+
const output = BSON.serialize({ uuid: new BSON.UUID() });
178+
expect(output[4]).to.equal(BSON_DATA_BINARY);
179+
expect(output[14]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
180+
});
181+
182+
it('should have a valid UUID _bsontype with Map input without error', () => {
183+
const output = BSON.serialize(new Map([['uuid', new BSON.UUID()]]));
184+
expect(output[4]).to.equal(BSON_DATA_BINARY);
185+
expect(output[14]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
186+
});
187+
188+
it('should have as a valid UUID _bsontype with Array input without error', () => {
189+
const output = BSON.serialize({ a: [new BSON.UUID()] });
190+
expect(output[11]).to.equal(BSON_DATA_BINARY);
191+
expect(output[18]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
192+
});
173193
});
174194
});

0 commit comments

Comments
 (0)