bson
Advanced tools
Comparing version 4.5.3 to 4.5.4
@@ -25,3 +25,3 @@ { | ||
], | ||
"version": "4.5.3" | ||
"version": "4.5.4" | ||
} |
@@ -866,5 +866,5 @@ import { Buffer } from 'buffer'; | ||
* | ||
* @param id - Can be a 24 character hex string, 12 byte binary Buffer, or a number. | ||
* @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number. | ||
*/ | ||
constructor(id?: string | Buffer | number | ObjectIdLike | ObjectId); | ||
constructor(inputId?: string | Buffer | number | ObjectIdLike | ObjectId); | ||
/* | ||
@@ -871,0 +871,0 @@ * The ObjectId bytes |
@@ -20,3 +20,3 @@ "use strict"; | ||
} | ||
this.value = +value; | ||
this.value = +value | 0; | ||
} | ||
@@ -23,0 +23,0 @@ /** |
@@ -21,46 +21,53 @@ "use strict"; | ||
* | ||
* @param id - Can be a 24 character hex string, 12 byte binary Buffer, or a number. | ||
* @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number. | ||
*/ | ||
function ObjectId(id) { | ||
function ObjectId(inputId) { | ||
if (!(this instanceof ObjectId)) | ||
return new ObjectId(id); | ||
// Duck-typing to support ObjectId from different npm packages | ||
if (id instanceof ObjectId) { | ||
this[kId] = id.id; | ||
this.__id = id.__id; | ||
} | ||
if (typeof id === 'object' && id && 'id' in id) { | ||
if ('toHexString' in id && typeof id.toHexString === 'function') { | ||
this[kId] = buffer_1.Buffer.from(id.toHexString(), 'hex'); | ||
return new ObjectId(inputId); | ||
// workingId is set based on type of input and whether valid id exists for the input | ||
var workingId; | ||
if (typeof inputId === 'object' && inputId && 'id' in inputId) { | ||
if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) { | ||
throw new error_1.BSONTypeError('Argument passed in must have an id that is of type string or Buffer'); | ||
} | ||
if ('toHexString' in inputId && typeof inputId.toHexString === 'function') { | ||
workingId = buffer_1.Buffer.from(inputId.toHexString(), 'hex'); | ||
} | ||
else { | ||
this[kId] = typeof id.id === 'string' ? buffer_1.Buffer.from(id.id) : id.id; | ||
workingId = inputId.id; | ||
} | ||
} | ||
// The most common use case (blank id, new objectId instance) | ||
if (id == null || typeof id === 'number') { | ||
else { | ||
workingId = inputId; | ||
} | ||
// the following cases use workingId to construct an ObjectId | ||
if (workingId == null || typeof workingId === 'number') { | ||
// The most common use case (blank id, new objectId instance) | ||
// Generate a new id | ||
this[kId] = ObjectId.generate(typeof id === 'number' ? id : undefined); | ||
// If we are caching the hex string | ||
if (ObjectId.cacheHexString) { | ||
this.__id = this.id.toString('hex'); | ||
} | ||
this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined); | ||
} | ||
if (ArrayBuffer.isView(id) && id.byteLength === 12) { | ||
this[kId] = ensure_buffer_1.ensureBuffer(id); | ||
else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) { | ||
this[kId] = ensure_buffer_1.ensureBuffer(workingId); | ||
} | ||
if (typeof id === 'string') { | ||
if (id.length === 12) { | ||
var bytes = buffer_1.Buffer.from(id); | ||
else if (typeof workingId === 'string') { | ||
if (workingId.length === 12) { | ||
var bytes = buffer_1.Buffer.from(workingId); | ||
if (bytes.byteLength === 12) { | ||
this[kId] = bytes; | ||
} | ||
else { | ||
throw new error_1.BSONTypeError('Argument passed in must be a string of 12 bytes'); | ||
} | ||
} | ||
else if (id.length === 24 && checkForHexRegExp.test(id)) { | ||
this[kId] = buffer_1.Buffer.from(id, 'hex'); | ||
else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) { | ||
this[kId] = buffer_1.Buffer.from(workingId, 'hex'); | ||
} | ||
else { | ||
throw new error_1.BSONTypeError('Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters'); | ||
throw new error_1.BSONTypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters'); | ||
} | ||
} | ||
else { | ||
throw new error_1.BSONTypeError('Argument passed in does not match the accepted types'); | ||
} | ||
// If we are caching the hex string | ||
if (ObjectId.cacheHexString) { | ||
@@ -129,3 +136,3 @@ this.__id = this.id.toString('hex'); | ||
if ('number' !== typeof time) { | ||
time = ~~(Date.now() / 1000); | ||
time = Math.floor(Date.now() / 1000); | ||
} | ||
@@ -285,3 +292,3 @@ var inc = ObjectId.getInc(); | ||
/** @internal */ | ||
ObjectId.index = ~~(Math.random() * 0xffffff); | ||
ObjectId.index = Math.floor(Math.random() * 0xffffff); | ||
return ObjectId; | ||
@@ -288,0 +295,0 @@ }()); |
@@ -18,3 +18,3 @@ { | ||
"types": "bson.d.ts", | ||
"version": "4.5.3", | ||
"version": "4.5.4", | ||
"author": { | ||
@@ -21,0 +21,0 @@ "name": "The MongoDB NodeJS Team", |
@@ -28,3 +28,3 @@ import type { EJSONOptions } from './extended_json'; | ||
this.value = +value; | ||
this.value = +value | 0; | ||
} | ||
@@ -31,0 +31,0 @@ |
@@ -34,3 +34,3 @@ import { Buffer } from 'buffer'; | ||
/** @internal */ | ||
static index = ~~(Math.random() * 0xffffff); | ||
static index = Math.floor(Math.random() * 0xffffff); | ||
@@ -47,50 +47,50 @@ static cacheHexString: boolean; | ||
* | ||
* @param id - Can be a 24 character hex string, 12 byte binary Buffer, or a number. | ||
* @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number. | ||
*/ | ||
constructor(id?: string | Buffer | number | ObjectIdLike | ObjectId) { | ||
if (!(this instanceof ObjectId)) return new ObjectId(id); | ||
constructor(inputId?: string | Buffer | number | ObjectIdLike | ObjectId) { | ||
if (!(this instanceof ObjectId)) return new ObjectId(inputId); | ||
// Duck-typing to support ObjectId from different npm packages | ||
if (id instanceof ObjectId) { | ||
this[kId] = id.id; | ||
this.__id = id.__id; | ||
} | ||
if (typeof id === 'object' && id && 'id' in id) { | ||
if ('toHexString' in id && typeof id.toHexString === 'function') { | ||
this[kId] = Buffer.from(id.toHexString(), 'hex'); | ||
// workingId is set based on type of input and whether valid id exists for the input | ||
let workingId; | ||
if (typeof inputId === 'object' && inputId && 'id' in inputId) { | ||
if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) { | ||
throw new BSONTypeError( | ||
'Argument passed in must have an id that is of type string or Buffer' | ||
); | ||
} | ||
if ('toHexString' in inputId && typeof inputId.toHexString === 'function') { | ||
workingId = Buffer.from(inputId.toHexString(), 'hex'); | ||
} else { | ||
this[kId] = typeof id.id === 'string' ? Buffer.from(id.id) : id.id; | ||
workingId = inputId.id; | ||
} | ||
} else { | ||
workingId = inputId; | ||
} | ||
// The most common use case (blank id, new objectId instance) | ||
if (id == null || typeof id === 'number') { | ||
// the following cases use workingId to construct an ObjectId | ||
if (workingId == null || typeof workingId === 'number') { | ||
// The most common use case (blank id, new objectId instance) | ||
// Generate a new id | ||
this[kId] = ObjectId.generate(typeof id === 'number' ? id : undefined); | ||
// If we are caching the hex string | ||
if (ObjectId.cacheHexString) { | ||
this.__id = this.id.toString('hex'); | ||
} | ||
} | ||
if (ArrayBuffer.isView(id) && id.byteLength === 12) { | ||
this[kId] = ensureBuffer(id); | ||
} | ||
if (typeof id === 'string') { | ||
if (id.length === 12) { | ||
const bytes = Buffer.from(id); | ||
this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined); | ||
} else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) { | ||
this[kId] = ensureBuffer(workingId); | ||
} else if (typeof workingId === 'string') { | ||
if (workingId.length === 12) { | ||
const bytes = Buffer.from(workingId); | ||
if (bytes.byteLength === 12) { | ||
this[kId] = bytes; | ||
} else { | ||
throw new BSONTypeError('Argument passed in must be a string of 12 bytes'); | ||
} | ||
} else if (id.length === 24 && checkForHexRegExp.test(id)) { | ||
this[kId] = Buffer.from(id, 'hex'); | ||
} else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) { | ||
this[kId] = Buffer.from(workingId, 'hex'); | ||
} else { | ||
throw new BSONTypeError( | ||
'Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters' | ||
'Argument passed in must be a string of 12 bytes or a string of 24 hex characters' | ||
); | ||
} | ||
} else { | ||
throw new BSONTypeError('Argument passed in does not match the accepted types'); | ||
} | ||
// If we are caching the hex string | ||
if (ObjectId.cacheHexString) { | ||
@@ -161,3 +161,3 @@ this.__id = this.id.toString('hex'); | ||
if ('number' !== typeof time) { | ||
time = ~~(Date.now() / 1000); | ||
time = Math.floor(Date.now() / 1000); | ||
} | ||
@@ -164,0 +164,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2861419
39261