Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

uuid-mongodb

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

uuid-mongodb - npm Package Compare versions

Comparing version 0.9.1 to 1.0.0

assets/uuid-mongodb.png

55

lib/index.js

@@ -5,2 +5,10 @@ const { Binary } = require('mongodb');

const Err = {
InvalidUUID: new Error(`Invalid UUID.`),
UnexpectedUUIDType: new Error(
'Unexpected UUID type. UUID must be a string or a MongoDB Binary (SUBTYPE_UUID).'
),
UnsupportedUUIDVersion: new Error('Unsupported uuid version.'),
InvalidHexString: new Error('Invalid hex string.'),
};
module.exports = {

@@ -16,8 +24,10 @@ v1() {

from(uuid) {
if (typeof uuid === 'string') return generateUUID(uuid);
else if (uuid instanceof Binary)
if (typeof uuid === 'string') {
if (!validateUuid(uuid)) throw Err.InvalidUUID;
return generateUUID(uuid);
} else if (uuid instanceof Binary) {
if (uuid.length() !== 36) throw Err.InvalidUUID;
return stringify(uuid.read(0, uuid.length()));
throw new Error(
'Unexpected UUID type. UUID must be a string or a MongoDB Binary (SUBTYPE_UUID)'
);
throw Err.UnexpectedUUIDType;
}
},

@@ -27,4 +37,3 @@ };

function generateUUID(uuid = undefined, v = 1) {
if (v <= 0 || v > 4 || v === 2 || v === 3)
throw Error('unsupported uuid version');
if (v <= 0 || v > 4 || v === 2 || v === 3) throw Err.UnsupportedUUIDVersion;

@@ -34,17 +43,27 @@ let uuidv = uuidv1;

let muuid;
const apply = mu => {
// add to string method
mu.toString = function() {
return stringify(this.buffer);
}.bind(mu);
return mu;
};
if (uuid) {
if (!validateUuid(uuid)) throw Err.InvalidUUID;
const normalized = normalize(uuid);
if (normalized === false) throw new Error('Invalid hex string');
muuid = new Binary(Buffer.from(normalized, 'hex'), Binary.SUBTYPE_UUID);
if (!normalized) throw Err.InvalidHexString;
return apply(
new Binary(Buffer.from(normalized, 'hex'), Binary.SUBTYPE_UUID)
);
} else {
const uuid = uuidv(null, new Buffer(16));
muuid = Binary(uuid, Binary.SUBTYPE_UUID);
return apply(Binary(uuid, Binary.SUBTYPE_UUID));
}
}
muuid.toString = function() {
return stringify(this.buffer);
}.bind(muuid);
return muuid;
function validateUuid(string) {
return !!/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.exec(
string
);
}

@@ -55,5 +74,5 @@

const stripped = string.replace(/-/g, '');
const stripped = string.replace(/-/g, '').toLowerCase();
if (stripped.length !== 32 || !stripped.match(/^[a-fA-F0-9]+$/)) return false;
if (!stripped.match(/^[a-f0-9]{32}$/)) return false;

@@ -60,0 +79,0 @@ return stripped;

{
"name": "uuid-mongodb",
"version": "0.9.1",
"version": "1.0.0",
"description": "",
"main": "lib/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha test/**/*.js "
},

@@ -28,3 +28,7 @@ "repository": {

"mongodb": "^3.1.1"
},
"devDependencies": {
"mocha": "^5.2.0",
"mongodb": "^3.1.1"
}
}
# uuid-mongodb
Generates and parses [BSON UUIDs](https://docs.mongodb.com/manual/reference/method/UUID/) for use with MongoDB.
Generates and parses [BSON UUIDs](https://docs.mongodb.com/manual/reference/method/UUID/) for use with MongoDB. BSON UUIDs provide better performance than their string counterparts.
<p align="center">
<img src="https://raw.githubusercontent.com/cdimascio/uuid-mongodb/master/assets/uuid-mongodb.png?raw=truef"/>
</p>
Inspired by [@srcagency's](https://github.com/srcagency) [mongo-uuid](https://github.com/srcagency/mongo-uuid)

@@ -27,11 +31,28 @@

const mUUID2 = MUUID.from('393967e0-8de1-11e8-9eb6-529269fb1459')
const mUUID2 = MUUID.from(/** MongoDb Binary of SUBTYPE_UUID */)
# Create a binary UUID from a MongoDb Binary
# This is useful to get MUUIDs helpful toString() method
const mUUID3 = MUUID.from(/** MongoDb Binary of SUBTYPE_UUID */)
```
## Example
### Query
```javascript
const uuid = MUUID.from('393967e0-8de1-11e8-9eb6-529269fb1459');
return collection
.then(c =>
c.findOne({
_id: uuid,
})
)
```
## Notes
Currently support UUID v1 and v4
Currently supports UUID v1 and v4
## License
MIT
[MIT](./LICENSE)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc