Comparing version 0.3.0 to 0.5.0
@@ -1,15 +0,17 @@ | ||
var gulp = global.gulp = require('gulp'), | ||
plugins = global.plugins = require("gulp-load-plugins")( { scope: ['devDependencies'] } );; | ||
'use strict' | ||
gulp.task('eslint', function() { | ||
let gulp = require('gulp'), | ||
plugins = require('gulp-load-plugins')( { scope: ['devDependencies'] } ) | ||
gulp.task('lint', function () { | ||
return gulp.src( 'vindication.js' ) | ||
.pipe( global.plugins.eslint() ) | ||
.pipe( global.plugins.eslint.format() ) | ||
.pipe( global.plugins.eslint.failOnError() ); | ||
}); | ||
.pipe( plugins.eslint() ) | ||
.pipe( plugins.eslint.format() ) | ||
.pipe( plugins.eslint.failOnError() ) | ||
}) | ||
gulp.task( 'mocha', function(callback) { | ||
return gulp.src( './test/*.js' ).pipe( global.plugins.mocha({reporter: 'nyan'}) ); | ||
} ); | ||
gulp.task( 'mocha', function (callback) { | ||
return gulp.src( './test/*.mocha.js' ).pipe( plugins.mocha({reporter: 'nyan'}) ) | ||
} ) | ||
gulp.task( 'default', [ 'eslint', 'mocha' ] ); | ||
gulp.task( 'default', [ 'lint', 'mocha' ] ) |
@@ -1,60 +0,59 @@ | ||
var os = require('os'); | ||
var crypto = require('crypto'); | ||
'use strict' | ||
var RANDOM_MAX_LENGTH = 64; | ||
var os = require('os') | ||
var crypto = require('crypto') | ||
var toString = Object.prototype.toString; | ||
function isString(obj) { | ||
return "[object String]" === toString.call(obj); | ||
var RANDOM_MAX_LENGTH = 64 | ||
var toString = Object.prototype.toString | ||
function isString (obj) { | ||
return toString.call(obj) === '[object String]' | ||
} | ||
function isNumber(obj) { | ||
return (toString.call(obj) === "[object " + Number + "]") || !isNaN(obj); | ||
function isNumber (obj) { | ||
return (toString.call(obj) === '[object ' + Number + ']') || !isNaN(obj) | ||
} | ||
function isObject(obj) { | ||
return obj === Object(obj); | ||
} | ||
function createConfig(length){ | ||
if( length<4 || length > 256 ) | ||
throw new Error('Length must be a number between 4 and 256.'); | ||
function createConfig (length) { | ||
if ( length < 4 || length > 256 ) | ||
throw new Error('Length must be a number between 4 and 256.') | ||
var config = { | ||
processLength : length >= 8 ? 2 : 0, | ||
processLength: length >= 8 ? 2 : 0, | ||
networkLength: length >= 8 ? 2 : 0, | ||
timeLength : length < 16 ? 2 : (length >= 36 ? 8 : 4) | ||
}; | ||
timeLength: length < 16 ? 2 : (length >= 36 ? 8 : 4) | ||
} | ||
config.randomLength = length - config.processLength - config.networkLength - 2*config.timeLength; | ||
config.randomLength = length - config.processLength - config.networkLength - 2 * config.timeLength | ||
return config; | ||
return config | ||
} | ||
function getNetworkAddress(networkInterfaces){ | ||
function getNetworkAddress (networkInterfaces) { | ||
for (var i in networkInterfaces) { | ||
for (var v in i) { | ||
if (networkInterfaces[i][v] && networkInterfaces[i][v].address && !networkInterfaces[i][v].internal) { | ||
return networkInterfaces[i][v].address; | ||
return networkInterfaces[i][v].address | ||
} | ||
} | ||
} | ||
return os.hostname(); | ||
return os.hostname() | ||
} | ||
var Clerobee = function Clerobee( length ) { | ||
this.config = createConfig( length || 16 ); | ||
}; | ||
var Clerobee = function Clerobee ( length ) { | ||
this.config = createConfig( length || 16 ) | ||
} | ||
var ClerobeeProto = Clerobee.prototype; | ||
var ClerobeeProto = Clerobee.prototype | ||
ClerobeeProto.padHex = function(hex, digits) { | ||
return hex.length >= digits ? hex.substring(0, digits) : this.getRandom(digits - hex.length) + hex; | ||
}; | ||
ClerobeeProto.derive = function( config, masterID ) { | ||
var lengthP = config.timeLength + config.processLength + config.networkLength; | ||
var lengthR = lengthP + config.randomLength; | ||
ClerobeeProto.padHex = function (hex, digits) { | ||
return hex.length >= digits ? hex.substring(0, digits) : this.getRandom(digits - hex.length) + hex | ||
} | ||
ClerobeeProto.derive = function ( config, masterID ) { | ||
var lengthP = config.timeLength + config.processLength + config.networkLength | ||
var lengthR = lengthP + config.randomLength | ||
var masterRandom = masterID.substring( lengthP, lengthR ); | ||
var masterNano = masterID.substring( lengthR, lengthR+config.timeLength ); | ||
var masterRandom = masterID.substring( lengthP, lengthR ) | ||
var masterNano = masterID.substring( lengthR, lengthR + config.timeLength ) | ||
return this.getRandom(config.randomLength, masterRandom, masterNano ); | ||
}; | ||
ClerobeeProto.innerGenerate = function( config, randomGenerator ) { | ||
return this.getRandom(config.randomLength, masterRandom, masterNano ) | ||
} | ||
ClerobeeProto.innerGenerate = function ( config, randomGenerator ) { | ||
var ts = this.getTimestamp( config.timeLength ), | ||
@@ -64,111 +63,112 @@ pid = this.getProcessId( config.processLength ), | ||
rid = randomGenerator( ), | ||
nnId = this.getNanoTime( config.timeLength ); | ||
nnId = this.getNanoTime( config.timeLength ) | ||
return ts + pid + nid + rid + nnId; | ||
}; | ||
ClerobeeProto.getSliceRandom = function( config, uuid ){ | ||
var lengthP = config.timeLength + config.processLength + config.networkLength; | ||
var lengthR = lengthP + config.randomLength; | ||
return ts + pid + nid + rid + nnId | ||
} | ||
ClerobeeProto.getSliceRandom = function ( config, uuid ) { | ||
var lengthP = config.timeLength + config.processLength + config.networkLength | ||
var lengthR = lengthP + config.randomLength | ||
return uuid.substring( lengthP, lengthR ); | ||
}; | ||
ClerobeeProto.cryptify = function( string, length ){ | ||
return uuid.substring( lengthP, lengthR ) | ||
} | ||
ClerobeeProto.cryptify = function ( string, length ) { | ||
return crypto.createHash('md5') | ||
.update( string, 'utf8') | ||
.digest('hex') | ||
.slice(-length ); | ||
}; | ||
ClerobeeProto.generateByLength = function( length ) { | ||
var self = this; | ||
var config = length ? createConfig( length ) : this.config; | ||
return this.innerGenerate.bind( self, config, function( ){ return self.getRandom( config.randomLength ); } )(); | ||
}; | ||
ClerobeeProto.generateByMasterID = function( masterID ) { | ||
var self = this; | ||
var config = createConfig( masterID.length ); | ||
return this.innerGenerate.bind( self, config, function( ){ return self.derive( config, masterID ); } )(); | ||
}; | ||
ClerobeeProto.generateBySource = function( reference, length ) { | ||
var self = this; | ||
var config = length ? createConfig( length ) : this.config; | ||
var refString = this.cryptify( JSON.stringify( reference ), config.randomLength ); | ||
return this.innerGenerate.bind( self, config, function( ){ return self.padHex(refString, config.randomLength); } )(); | ||
}; | ||
ClerobeeProto.generate = function( basis, extra ) { | ||
return !basis || isNumber(basis) ? this.generateByLength(basis) : ( isString(basis) ? this.generateByMasterID(basis) : this.generateBySource(basis, extra) ); | ||
}; | ||
ClerobeeProto.isSourced = function( reference, length, uuid ) { | ||
if( !reference || !length || !uuid ) | ||
return false; | ||
.slice(-length ) | ||
} | ||
ClerobeeProto.generateByLength = function ( length ) { | ||
var self = this | ||
var config = length ? createConfig( length ) : this.config | ||
return this.innerGenerate.bind( self, config, function ( ) { return self.getRandom( config.randomLength ) } )() | ||
} | ||
ClerobeeProto.generateByMasterID = function ( masterID ) { | ||
var self = this | ||
var config = createConfig( masterID.length ) | ||
return this.innerGenerate.bind( self, config, function ( ) { return self.derive( config, masterID ) } )() | ||
} | ||
ClerobeeProto.generateBySource = function ( reference, length ) { | ||
var self = this | ||
var config = length ? createConfig( length ) : this.config | ||
var refString = this.cryptify( JSON.stringify( reference ), config.randomLength ) | ||
return this.innerGenerate.bind( self, config, function ( ) { return self.padHex(refString, config.randomLength) } )() | ||
} | ||
ClerobeeProto.generate = function ( basis, extra ) { | ||
return !basis || isNumber(basis) ? this.generateByLength(basis) : ( isString(basis) ? this.generateByMasterID(basis) : this.generateBySource(basis, extra) ) | ||
} | ||
ClerobeeProto.isSourced = function ( reference, length, uuid ) { | ||
if ( !reference || !length || !uuid ) | ||
return false | ||
var config = createConfig( length ); | ||
var config = createConfig( length ) | ||
var refString = this.cryptify( JSON.stringify( reference ), config.randomLength ); | ||
var refString = this.cryptify( JSON.stringify( reference ), config.randomLength ) | ||
var refRandom = this.getSliceRandom(config, uuid); | ||
var refRandom = this.getSliceRandom(config, uuid) | ||
return refRandom.substring(refRandom.length - refString.length) === refString; | ||
}; | ||
ClerobeeProto.isDerived = function( masterID, slaveID ) { | ||
if( !masterID || !slaveID || masterID.length !== slaveID.length ) | ||
return false; | ||
return refRandom.substring(refRandom.length - refString.length) === refString | ||
} | ||
ClerobeeProto.isDerived = function ( masterID, slaveID ) { | ||
if ( !masterID || !slaveID || masterID.length !== slaveID.length ) | ||
return false | ||
var config = createConfig( masterID.length ); | ||
var config = createConfig( masterID.length ) | ||
var slaveRandom = this.getSliceRandom(config, slaveID); | ||
var slaveRandom = this.getSliceRandom(config, slaveID) | ||
return this.derive( config, masterID ) === slaveRandom; | ||
}; | ||
ClerobeeProto.getRandom = function( randomLength, masterRandom, masterNano) { | ||
var self = this; | ||
randomLength = randomLength || this.config.randomLength; | ||
return this.derive( config, masterID ) === slaveRandom | ||
} | ||
ClerobeeProto.getRandom = function ( randomLength, masterRandom, masterNano) { | ||
var self = this | ||
randomLength = randomLength || this.config.randomLength | ||
var random = ''; | ||
for( var i = 0; i<randomLength; i+= RANDOM_MAX_LENGTH ){ | ||
var length = Math.min( randomLength-i, RANDOM_MAX_LENGTH ); | ||
var random = '' | ||
for ( var i = 0; i < randomLength; i += RANDOM_MAX_LENGTH ) { | ||
var length = Math.min( randomLength - i, RANDOM_MAX_LENGTH ) | ||
if( masterRandom && masterNano ){ | ||
var sliceR = masterRandom.substring(i, i+length); | ||
if ( masterRandom && masterNano ) { | ||
var sliceR = masterRandom.substring(i, i + length) | ||
random += self.padHex( Number( parseInt(sliceR, 36) + parseInt(masterNano, 36) ).toString(36), length ); | ||
random += self.padHex( Number( parseInt(sliceR, 36) + parseInt(masterNano, 36) ).toString(36), length ) | ||
} | ||
else{ | ||
random += this.toBase36String( Math.floor( Math.random() * 1e200 ), length ); | ||
else { | ||
random += this.toBase36String( Math.floor( Math.random() * 1e200 ), length ) | ||
} | ||
} | ||
return random; | ||
}; | ||
ClerobeeProto.getProcessId = function( processLength ) { | ||
processLength = processLength || this.config.processLength; | ||
return random | ||
} | ||
ClerobeeProto.getProcessId = function ( processLength ) { | ||
processLength = processLength || this.config.processLength | ||
var pid = (process && process.pid) ? process.pid : Date.now(); | ||
var pid = (process && process.pid) ? process.pid : Date.now() | ||
return this.toBase36String( pid, processLength ); | ||
}; | ||
ClerobeeProto.getNetworkId = function( networkLength ) { | ||
networkLength = networkLength || this.config.networkLength; | ||
return this.toBase36String( pid, processLength ) | ||
} | ||
ClerobeeProto.getNetworkId = function ( networkLength ) { | ||
networkLength = networkLength || this.config.networkLength | ||
return this.cryptify( getNetworkAddress( os.networkInterfaces() ), networkLength ); | ||
}; | ||
ClerobeeProto.getTimestamp = function( timeLength ) { | ||
timeLength = timeLength || this.config-timeLength; | ||
return this.cryptify( getNetworkAddress( os.networkInterfaces() ), networkLength ) | ||
} | ||
ClerobeeProto.getTimestamp = function ( timeLength ) { | ||
timeLength = timeLength || this.config - timeLength | ||
return this.toBase36String( Date.now(), timeLength ); | ||
}; | ||
ClerobeeProto.getNanoTime = function( timeLength ) { | ||
timeLength = timeLength || this.config.timeLength; | ||
return this.toBase36String( Date.now(), timeLength ) | ||
} | ||
ClerobeeProto.getNanoTime = function ( timeLength ) { | ||
timeLength = timeLength || this.config.timeLength | ||
var time = (process && process.hrtime) ? process.hrtime() : [1, Date.now()]; | ||
var nano = time[0] * 1e9 + time[1]; | ||
var time = (process && process.hrtime) ? process.hrtime() : [1, Date.now()] | ||
var nano = time[0] * 1e9 + time[1] | ||
// (nano + '').split('').reverse().join('') | ||
return this.toBase36String( nano, timeLength ); | ||
}; | ||
ClerobeeProto.toBase36String = function(value, length) { | ||
value = value || 0; length = length || 4; | ||
return this.toBase36String( nano, timeLength ) | ||
} | ||
ClerobeeProto.toBase36String = function (value, length) { | ||
value = value || 0 | ||
length = length || 4 | ||
return this.padHex( Number( value ).toString(36), length); | ||
}; | ||
return this.padHex( Number( value ).toString(36), length) | ||
} | ||
module.exports = exports = Clerobee; | ||
module.exports = exports = Clerobee |
{ | ||
"name": "clerobee", | ||
"version": "0.3.0", | ||
"version": "0.5.0", | ||
"description": "A featureful dependency-free UID generator", | ||
@@ -34,12 +34,11 @@ "keywords": [ | ||
"devDependencies": { | ||
"chai": "latest", | ||
"gulp": "latest", | ||
"gulp-eslint": "~1", | ||
"gulp-load-plugins": "latest", | ||
"gulp-mocha": "latest" | ||
"chai": "^3.5.0", | ||
"gulp": "^3.9.1", | ||
"gulp-eslint": "^2.0.0", | ||
"gulp-load-plugins": "^1.2.0", | ||
"gulp-mocha": "^2.2.0" | ||
}, | ||
"engines": { | ||
"node": ">= 0.10.0" | ||
}, | ||
"_id": "clerobee@0.3.0" | ||
"node": ">= 4.0.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
310
15510
1