react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / node-uuid / uuid.js
81145 views// uuid.js1//2// Copyright (c) 2010-2012 Robert Kieffer3// MIT License - http://opensource.org/licenses/mit-license.php45(function() {6var _global = this;78// Unique ID creation requires a high quality random # generator. We feature9// detect to determine the best RNG source, normalizing to a function that10// returns 128-bits of randomness, since that's what's usually required11var _rng;1213// Node.js crypto-based RNG - http://nodejs.org/docs/v0.6.2/api/crypto.html14//15// Moderately fast, high quality16if (typeof(_global.require) == 'function') {17try {18var _rb = _global.require('crypto').randomBytes;19_rng = _rb && function() {return _rb(16);};20} catch(e) {}21}2223if (!_rng && _global.crypto && crypto.getRandomValues) {24// WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto25//26// Moderately fast, high quality27var _rnds8 = new Uint8Array(16);28_rng = function whatwgRNG() {29crypto.getRandomValues(_rnds8);30return _rnds8;31};32}3334if (!_rng) {35// Math.random()-based (RNG)36//37// If all else fails, use Math.random(). It's fast, but is of unspecified38// quality.39var _rnds = new Array(16);40_rng = function() {41for (var i = 0, r; i < 16; i++) {42if ((i & 0x03) === 0) r = Math.random() * 0x100000000;43_rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;44}4546return _rnds;47};48}4950// Buffer class to use51var BufferClass = typeof(_global.Buffer) == 'function' ? _global.Buffer : Array;5253// Maps for number <-> hex string conversion54var _byteToHex = [];55var _hexToByte = {};56for (var i = 0; i < 256; i++) {57_byteToHex[i] = (i + 0x100).toString(16).substr(1);58_hexToByte[_byteToHex[i]] = i;59}6061// **`parse()` - Parse a UUID into it's component bytes**62function parse(s, buf, offset) {63var i = (buf && offset) || 0, ii = 0;6465buf = buf || [];66s.toLowerCase().replace(/[0-9a-f]{2}/g, function(oct) {67if (ii < 16) { // Don't overflow!68buf[i + ii++] = _hexToByte[oct];69}70});7172// Zero out remaining bytes if string was short73while (ii < 16) {74buf[i + ii++] = 0;75}7677return buf;78}7980// **`unparse()` - Convert UUID byte array (ala parse()) into a string**81function unparse(buf, offset) {82var i = offset || 0, bth = _byteToHex;83return bth[buf[i++]] + bth[buf[i++]] +84bth[buf[i++]] + bth[buf[i++]] + '-' +85bth[buf[i++]] + bth[buf[i++]] + '-' +86bth[buf[i++]] + bth[buf[i++]] + '-' +87bth[buf[i++]] + bth[buf[i++]] + '-' +88bth[buf[i++]] + bth[buf[i++]] +89bth[buf[i++]] + bth[buf[i++]] +90bth[buf[i++]] + bth[buf[i++]];91}9293// **`v1()` - Generate time-based UUID**94//95// Inspired by https://github.com/LiosK/UUID.js96// and http://docs.python.org/library/uuid.html9798// random #'s we need to init node and clockseq99var _seedBytes = _rng();100101// Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)102var _nodeId = [103_seedBytes[0] | 0x01,104_seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]105];106107// Per 4.2.2, randomize (14 bit) clockseq108var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;109110// Previous uuid creation time111var _lastMSecs = 0, _lastNSecs = 0;112113// See https://github.com/broofa/node-uuid for API details114function v1(options, buf, offset) {115var i = buf && offset || 0;116var b = buf || [];117118options = options || {};119120var clockseq = options.clockseq != null ? options.clockseq : _clockseq;121122// UUID timestamps are 100 nano-second units since the Gregorian epoch,123// (1582-10-15 00:00). JSNumbers aren't precise enough for this, so124// time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'125// (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.126var msecs = options.msecs != null ? options.msecs : new Date().getTime();127128// Per 4.2.1.2, use count of uuid's generated during the current clock129// cycle to simulate higher resolution clock130var nsecs = options.nsecs != null ? options.nsecs : _lastNSecs + 1;131132// Time since last uuid creation (in msecs)133var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;134135// Per 4.2.1.2, Bump clockseq on clock regression136if (dt < 0 && options.clockseq == null) {137clockseq = clockseq + 1 & 0x3fff;138}139140// Reset nsecs if clock regresses (new clockseq) or we've moved onto a new141// time interval142if ((dt < 0 || msecs > _lastMSecs) && options.nsecs == null) {143nsecs = 0;144}145146// Per 4.2.1.2 Throw error if too many uuids are requested147if (nsecs >= 10000) {148throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');149}150151_lastMSecs = msecs;152_lastNSecs = nsecs;153_clockseq = clockseq;154155// Per 4.1.4 - Convert from unix epoch to Gregorian epoch156msecs += 12219292800000;157158// `time_low`159var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;160b[i++] = tl >>> 24 & 0xff;161b[i++] = tl >>> 16 & 0xff;162b[i++] = tl >>> 8 & 0xff;163b[i++] = tl & 0xff;164165// `time_mid`166var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;167b[i++] = tmh >>> 8 & 0xff;168b[i++] = tmh & 0xff;169170// `time_high_and_version`171b[i++] = tmh >>> 24 & 0xf | 0x10; // include version172b[i++] = tmh >>> 16 & 0xff;173174// `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)175b[i++] = clockseq >>> 8 | 0x80;176177// `clock_seq_low`178b[i++] = clockseq & 0xff;179180// `node`181var node = options.node || _nodeId;182for (var n = 0; n < 6; n++) {183b[i + n] = node[n];184}185186return buf ? buf : unparse(b);187}188189// **`v4()` - Generate random UUID**190191// See https://github.com/broofa/node-uuid for API details192function v4(options, buf, offset) {193// Deprecated - 'format' argument, as supported in v1.2194var i = buf && offset || 0;195196if (typeof(options) == 'string') {197buf = options == 'binary' ? new BufferClass(16) : null;198options = null;199}200options = options || {};201202var rnds = options.random || (options.rng || _rng)();203204// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`205rnds[6] = (rnds[6] & 0x0f) | 0x40;206rnds[8] = (rnds[8] & 0x3f) | 0x80;207208// Copy bytes to buffer, if provided209if (buf) {210for (var ii = 0; ii < 16; ii++) {211buf[i + ii] = rnds[ii];212}213}214215return buf || unparse(rnds);216}217218// Export public API219var uuid = v4;220uuid.v1 = v1;221uuid.v4 = v4;222uuid.parse = parse;223uuid.unparse = unparse;224uuid.BufferClass = BufferClass;225226if (typeof(module) != 'undefined' && module.exports) {227// Publish as node.js module228module.exports = uuid;229} else if (typeof define === 'function' && define.amd) {230// Publish as AMD module231define(function() {return uuid;});232233234} else {235// Publish as global (in browsers)236var _previousRoot = _global.uuid;237238// **`noConflict()` - (browser only) to reset global 'uuid' var**239uuid.noConflict = function() {240_global.uuid = _previousRoot;241return uuid;242};243244_global.uuid = uuid;245}246}).call(this);247248249