PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/cordova-plugin-disable-bitcode/node_modules/node-uuid/uuid.js

https://gitlab.com/blocknotary/IonicInterviews
JavaScript | 249 lines | 143 code | 49 blank | 57 comment | 48 complexity | dbb38875ae7680ea775a0a35c0441767 MD5 | raw file
  1. // node-uuid/uuid.js
  2. //
  3. // Copyright (c) 2010 Robert Kieffer
  4. // Dual licensed under the MIT and GPL licenses.
  5. // Documentation and details at https://github.com/broofa/node-uuid
  6. (function() {
  7. var _global = this;
  8. // Unique ID creation requires a high quality random # generator, but
  9. // Math.random() does not guarantee "cryptographic quality". So we feature
  10. // detect for more robust APIs, normalizing each method to return 128-bits
  11. // (16 bytes) of random data.
  12. var mathRNG, nodeRNG, whatwgRNG;
  13. // Math.random()-based RNG. All platforms, very fast, unknown quality
  14. var _rndBytes = new Array(16);
  15. mathRNG = function() {
  16. var r, b = _rndBytes, i = 0;
  17. for (var i = 0, r; i < 16; i++) {
  18. if ((i & 0x03) == 0) r = Math.random() * 0x100000000;
  19. b[i] = r >>> ((i & 0x03) << 3) & 0xff;
  20. }
  21. return b;
  22. }
  23. // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
  24. // WebKit only (currently), moderately fast, high quality
  25. if (_global.crypto && crypto.getRandomValues) {
  26. var _rnds = new Uint32Array(4);
  27. whatwgRNG = function() {
  28. crypto.getRandomValues(_rnds);
  29. for (var c = 0 ; c < 16; c++) {
  30. _rndBytes[c] = _rnds[c >> 2] >>> ((c & 0x03) * 8) & 0xff;
  31. }
  32. return _rndBytes;
  33. }
  34. }
  35. // Node.js crypto-based RNG - http://nodejs.org/docs/v0.6.2/api/crypto.html
  36. // Node.js only, moderately fast, high quality
  37. try {
  38. var _rb = require('crypto').randomBytes;
  39. nodeRNG = _rb && function() {
  40. return _rb(16);
  41. };
  42. } catch (e) {}
  43. // Select RNG with best quality
  44. var _rng = nodeRNG || whatwgRNG || mathRNG;
  45. // Buffer class to use
  46. var BufferClass = typeof(Buffer) == 'function' ? Buffer : Array;
  47. // Maps for number <-> hex string conversion
  48. var _byteToHex = [];
  49. var _hexToByte = {};
  50. for (var i = 0; i < 256; i++) {
  51. _byteToHex[i] = (i + 0x100).toString(16).substr(1);
  52. _hexToByte[_byteToHex[i]] = i;
  53. }
  54. // **`parse()` - Parse a UUID into it's component bytes**
  55. function parse(s, buf, offset) {
  56. var i = (buf && offset) || 0, ii = 0;
  57. buf = buf || [];
  58. s.toLowerCase().replace(/[0-9a-f]{2}/g, function(byte) {
  59. if (ii < 16) { // Don't overflow!
  60. buf[i + ii++] = _hexToByte[byte];
  61. }
  62. });
  63. // Zero out remaining bytes if string was short
  64. while (ii < 16) {
  65. buf[i + ii++] = 0;
  66. }
  67. return buf;
  68. }
  69. // **`unparse()` - Convert UUID byte array (ala parse()) into a string**
  70. function unparse(buf, offset) {
  71. var i = offset || 0, bth = _byteToHex;
  72. return bth[buf[i++]] + bth[buf[i++]] +
  73. bth[buf[i++]] + bth[buf[i++]] + '-' +
  74. bth[buf[i++]] + bth[buf[i++]] + '-' +
  75. bth[buf[i++]] + bth[buf[i++]] + '-' +
  76. bth[buf[i++]] + bth[buf[i++]] + '-' +
  77. bth[buf[i++]] + bth[buf[i++]] +
  78. bth[buf[i++]] + bth[buf[i++]] +
  79. bth[buf[i++]] + bth[buf[i++]];
  80. }
  81. // **`v1()` - Generate time-based UUID**
  82. //
  83. // Inspired by https://github.com/LiosK/UUID.js
  84. // and http://docs.python.org/library/uuid.html
  85. // random #'s we need to init node and clockseq
  86. var _seedBytes = _rng();
  87. // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
  88. var _nodeId = [
  89. _seedBytes[0] | 0x01,
  90. _seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]
  91. ];
  92. // Per 4.2.2, randomize (14 bit) clockseq
  93. var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;
  94. // Previous uuid creation time
  95. var _lastMSecs = 0, _lastNSecs = 0;
  96. // See https://github.com/broofa/node-uuid for API details
  97. function v1(options, buf, offset) {
  98. var i = buf && offset || 0;
  99. var b = buf || [];
  100. options = options || {};
  101. var clockseq = options.clockseq != null ? options.clockseq : _clockseq;
  102. // UUID timestamps are 100 nano-second units since the Gregorian epoch,
  103. // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
  104. // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
  105. // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
  106. var msecs = options.msecs != null ? options.msecs : new Date().getTime();
  107. // Per 4.2.1.2, use count of uuid's generated during the current clock
  108. // cycle to simulate higher resolution clock
  109. var nsecs = options.nsecs != null ? options.nsecs : _lastNSecs + 1;
  110. // Time since last uuid creation (in msecs)
  111. var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
  112. // Per 4.2.1.2, Bump clockseq on clock regression
  113. if (dt < 0 && options.clockseq == null) {
  114. clockseq = clockseq + 1 & 0x3fff;
  115. }
  116. // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
  117. // time interval
  118. if ((dt < 0 || msecs > _lastMSecs) && options.nsecs == null) {
  119. nsecs = 0;
  120. }
  121. // Per 4.2.1.2 Throw error if too many uuids are requested
  122. if (nsecs >= 10000) {
  123. throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');
  124. }
  125. _lastMSecs = msecs;
  126. _lastNSecs = nsecs;
  127. _clockseq = clockseq;
  128. // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
  129. msecs += 12219292800000;
  130. // `time_low`
  131. var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
  132. b[i++] = tl >>> 24 & 0xff;
  133. b[i++] = tl >>> 16 & 0xff;
  134. b[i++] = tl >>> 8 & 0xff;
  135. b[i++] = tl & 0xff;
  136. // `time_mid`
  137. var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
  138. b[i++] = tmh >>> 8 & 0xff;
  139. b[i++] = tmh & 0xff;
  140. // `time_high_and_version`
  141. b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
  142. b[i++] = tmh >>> 16 & 0xff;
  143. // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
  144. b[i++] = clockseq >>> 8 | 0x80;
  145. // `clock_seq_low`
  146. b[i++] = clockseq & 0xff;
  147. // `node`
  148. var node = options.node || _nodeId;
  149. for (var n = 0; n < 6; n++) {
  150. b[i + n] = node[n];
  151. }
  152. return buf ? buf : unparse(b);
  153. }
  154. // **`v4()` - Generate random UUID**
  155. // See https://github.com/broofa/node-uuid for API details
  156. function v4(options, buf, offset) {
  157. // Deprecated - 'format' argument, as supported in v1.2
  158. var i = buf && offset || 0;
  159. if (typeof(options) == 'string') {
  160. buf = options == 'binary' ? new BufferClass(16) : null;
  161. options = null;
  162. }
  163. options = options || {};
  164. var rnds = options.random || (options.rng || _rng)();
  165. // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
  166. rnds[6] = (rnds[6] & 0x0f) | 0x40;
  167. rnds[8] = (rnds[8] & 0x3f) | 0x80;
  168. // Copy bytes to buffer, if provided
  169. if (buf) {
  170. for (var ii = 0; ii < 16; ii++) {
  171. buf[i + ii] = rnds[ii];
  172. }
  173. }
  174. return buf || unparse(rnds);
  175. }
  176. // Export public API
  177. var uuid = v4;
  178. uuid.v1 = v1;
  179. uuid.v4 = v4;
  180. uuid.parse = parse;
  181. uuid.unparse = unparse;
  182. uuid.BufferClass = BufferClass;
  183. // Export RNG options
  184. uuid.mathRNG = mathRNG;
  185. uuid.nodeRNG = nodeRNG;
  186. uuid.whatwgRNG = whatwgRNG;
  187. if (typeof(module) != 'undefined') {
  188. // Play nice with node.js
  189. module.exports = uuid;
  190. } else {
  191. // Play nice with browsers
  192. var _previousRoot = _global.uuid;
  193. // **`noConflict()` - (browser only) to reset global 'uuid' var**
  194. uuid.noConflict = function() {
  195. _global.uuid = _previousRoot;
  196. return uuid;
  197. }
  198. _global.uuid = uuid;
  199. }
  200. }());