Rename uuid related namespaces.

This commit is contained in:
Andrey Antukh 2016-06-22 00:08:08 +03:00
parent 4607d92c31
commit f81542a792
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
3 changed files with 12 additions and 13 deletions

View file

@ -8,19 +8,18 @@
"use strict"; "use strict";
goog.provide("uxbox.util.uuid.rng"); goog.provide("uxbox.util.rng_impl");
goog.require("cljs.core"); goog.require("cljs.core");
goog.scope(function() { goog.scope(function() {
const self = uxbox.util.rng_impl;
const global = goog.global; const global = goog.global;
const rng = uxbox.util.uuid.rng;
// Check if nodejs rng is available (high quality); // Check if nodejs rng is available (high quality);
if (cljs.core._STAR_target_STAR_ === "nodejs") { if (cljs.core._STAR_target_STAR_ === "nodejs") {
const crypto = require("crypto"); const crypto = require("crypto");
rng.getBytes = function(n) { self.getBytes = function(n) {
return crypto.randomBytes(n); return crypto.randomBytes(n);
}; };
} }
@ -28,7 +27,7 @@ goog.scope(function() {
// Check if whatwg rng is available (high quality); // Check if whatwg rng is available (high quality);
else if (global.crypto !== undefined && else if (global.crypto !== undefined &&
global.crypto.getRandomValues !== undefined) { global.crypto.getRandomValues !== undefined) {
rng.getBytes = function(n) { self.getBytes = function(n) {
const buf = new Uint8Array(16); const buf = new Uint8Array(16);
global.crypto.getRandomValues(buf); global.crypto.getRandomValues(buf);
return buf; return buf;
@ -38,7 +37,7 @@ goog.scope(function() {
// Switch Back to the Math.random (low quality); // Switch Back to the Math.random (low quality);
else { else {
console.warn("No high quality RNG available, switching back to Math.random."); console.warn("No high quality RNG available, switching back to Math.random.");
rng.getBytes = function(n) { self.getBytes = function(n) {
const buf = new Array(n); const buf = new Array(n);
for (let i = 0, r; i < n; i++) { for (let i = 0, r; i < n; i++) {
if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; } if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; }

View file

@ -13,7 +13,7 @@
If no high qualiry RNG, switches to the default Math based If no high qualiry RNG, switches to the default Math based
RNG with proper waring in the console." RNG with proper waring in the console."
(:require [uxbox.util.uuid.impl :as impl])) (:require [uxbox.util.uuid-impl :as impl]))
(defn v4 (defn v4
"Generate a v4 (random) UUID." "Generate a v4 (random) UUID."

View file

@ -7,14 +7,14 @@
*/ */
"use strict"; "use strict";
goog.provide("uxbox.util.uuid.impl"); goog.provide("uxbox.util.uuid_impl");
goog.require("uxbox.util.uuid.rng"); goog.require("uxbox.util.rng_impl");
goog.scope(function() { goog.scope(function() {
const impl = uxbox.util.uuid.impl; const self = uxbox.util.uuid_impl;
const rng = uxbox.util.uuid.rng; const rng = uxbox.util.rng_impl;
const hexMap = [];
const hexMap = [];
for (let i = 0; i < 256; i++) { for (let i = 0; i < 256; i++) {
hexMap[i] = (i + 0x100).toString(16).substr(1); hexMap[i] = (i + 0x100).toString(16).substr(1);
} }
@ -39,7 +39,7 @@ goog.scope(function() {
hexMap[buf[i++]]); hexMap[buf[i++]]);
} }
impl.v4 = function() { self.v4 = function() {
const buf = rng.getBytes(16); const buf = rng.getBytes(16);
buf[6] = (buf[6] & 0x0f) | 0x40; buf[6] = (buf[6] & 0x0f) | 0x40;
buf[8] = (buf[8] & 0x3f) | 0x80; buf[8] = (buf[8] & 0x3f) | 0x80;