From 4607d92c31ed127665e2516be5e65d0df5674f4e Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 21 Jun 2016 19:50:09 +0300 Subject: [PATCH] Move vendor's uuid module into uxbox.util. --- src/uxbox/common/workers.cljs | 2 +- src/uxbox/main/data/colors.cljs | 2 +- src/uxbox/main/data/dashboard.cljs | 2 +- src/uxbox/main/data/images.cljs | 2 +- src/uxbox/main/data/shapes.cljs | 2 +- src/uxbox/main/data/workspace.cljs | 2 +- src/uxbox/main/state/shapes.cljs | 2 +- .../core.cljs => src/uxbox/util/uuid.cljs | 6 +++--- {vendor => src/uxbox/util}/uuid/impl.js | 9 +++++---- {vendor => src/uxbox/util}/uuid/rng.js | 16 +++++++++------ test/uxbox/tests/shapes_state.cljs | 20 +++++++++---------- 11 files changed, 35 insertions(+), 30 deletions(-) rename vendor/uuid/core.cljs => src/uxbox/util/uuid.cljs (86%) rename {vendor => src/uxbox/util}/uuid/impl.js (86%) rename {vendor => src/uxbox/util}/uuid/rng.js (81%) diff --git a/src/uxbox/common/workers.cljs b/src/uxbox/common/workers.cljs index f0c42bdef..aec230cad 100644 --- a/src/uxbox/common/workers.cljs +++ b/src/uxbox/common/workers.cljs @@ -7,7 +7,7 @@ (ns uxbox.common.workers "A lightweight layer on top of webworkers api." (:require [beicon.core :as rx] - [uuid.core :as uuid] + [uxbox.util.uuid :as uuid] [uxbox.util.transit :as t])) ;; --- Implementation diff --git a/src/uxbox/main/data/colors.cljs b/src/uxbox/main/data/colors.cljs index 98df354fb..716e94489 100644 --- a/src/uxbox/main/data/colors.cljs +++ b/src/uxbox/main/data/colors.cljs @@ -8,7 +8,7 @@ (ns uxbox.main.data.colors (:require [clojure.set :as set] [beicon.core :as rx] - [uuid.core :as uuid] + [uxbox.util.uuid :as uuid] [uxbox.main.state :as st] [uxbox.common.rstore :as rs] [uxbox.main.state.colors :as stc] diff --git a/src/uxbox/main/data/dashboard.cljs b/src/uxbox/main/data/dashboard.cljs index bf2f1e524..d17b98e61 100644 --- a/src/uxbox/main/data/dashboard.cljs +++ b/src/uxbox/main/data/dashboard.cljs @@ -7,7 +7,7 @@ (ns uxbox.main.data.dashboard (:require [beicon.core :as rx] - [uuid.core :as uuid] + [uxbox.util.uuid :as uuid] [uxbox.common.rstore :as rs] [uxbox.common.router :as r] [uxbox.main.state :as st] diff --git a/src/uxbox/main/data/images.cljs b/src/uxbox/main/data/images.cljs index 26c6ea186..e8d49cc5a 100644 --- a/src/uxbox/main/data/images.cljs +++ b/src/uxbox/main/data/images.cljs @@ -8,7 +8,7 @@ (ns uxbox.main.data.images (:require [cuerdas.core :as str] [beicon.core :as rx] - [uuid.core :as uuid] + [uxbox.util.uuid :as uuid] [uxbox.common.rstore :as rs] [uxbox.main.state :as st] [uxbox.common.repo :as rp])) diff --git a/src/uxbox/main/data/shapes.cljs b/src/uxbox/main/data/shapes.cljs index 1d987aa11..e0f674b17 100644 --- a/src/uxbox/main/data/shapes.cljs +++ b/src/uxbox/main/data/shapes.cljs @@ -7,7 +7,7 @@ (ns uxbox.main.data.shapes (:require [beicon.core :as rx] - [uuid.core :as uuid] + [uxbox.util.uuid :as uuid] [uxbox.main.constants :as c] [uxbox.common.rstore :as rs] [uxbox.common.router :as r] diff --git a/src/uxbox/main/data/workspace.cljs b/src/uxbox/main/data/workspace.cljs index fbe86ad65..1c8d6c12e 100644 --- a/src/uxbox/main/data/workspace.cljs +++ b/src/uxbox/main/data/workspace.cljs @@ -7,7 +7,7 @@ (ns uxbox.main.data.workspace (:require [beicon.core :as rx] - [uuid.core :as uuid] + [uxbox.util.uuid :as uuid] [uxbox.main.constants :as c] [uxbox.common.rstore :as rs] [uxbox.common.schema :as sc] diff --git a/src/uxbox/main/state/shapes.cljs b/src/uxbox/main/state/shapes.cljs index aea15f126..77f5ba7d0 100644 --- a/src/uxbox/main/state/shapes.cljs +++ b/src/uxbox/main/state/shapes.cljs @@ -1,6 +1,6 @@ (ns uxbox.main.state.shapes "A collection of functions for manage shapes insinde the state." - (:require [uuid.core :as uuid] + (:require [uxbox.util.uuid :as uuid] [uxbox.util.data :refer (index-of)] [uxbox.common.geom :as geom])) diff --git a/vendor/uuid/core.cljs b/src/uxbox/util/uuid.cljs similarity index 86% rename from vendor/uuid/core.cljs rename to src/uxbox/util/uuid.cljs index d81b5df17..00fea32c9 100644 --- a/vendor/uuid/core.cljs +++ b/src/uxbox/util/uuid.cljs @@ -4,7 +4,7 @@ ;; ;; Copyright (c) 2016 Andrey Antukh -(ns uuid.core +(ns uxbox.util.uuid "Provides a UUID v4 uuid generation. In difference with builtin `random-uuid` function this @@ -13,7 +13,7 @@ If no high qualiry RNG, switches to the default Math based RNG with proper waring in the console." - (:require [uuid.impl :as impl])) + (:require [uxbox.util.uuid.impl :as impl])) (defn v4 "Generate a v4 (random) UUID." @@ -21,5 +21,5 @@ (uuid (impl/v4))) (def random - "Alias for `uuid.core/v4`." + "Alias for `uxbox.util.uuid/v4`." v4) diff --git a/vendor/uuid/impl.js b/src/uxbox/util/uuid/impl.js similarity index 86% rename from vendor/uuid/impl.js rename to src/uxbox/util/uuid/impl.js index 07ad0bc03..a0806aaf9 100644 --- a/vendor/uuid/impl.js +++ b/src/uxbox/util/uuid/impl.js @@ -7,11 +7,12 @@ */ "use strict"; -goog.provide("uuid.impl"); -goog.require("uuid.rng"); +goog.provide("uxbox.util.uuid.impl"); +goog.require("uxbox.util.uuid.rng"); goog.scope(function() { - const rng = uuid.rng; + const impl = uxbox.util.uuid.impl; + const rng = uxbox.util.uuid.rng; const hexMap = []; for (let i = 0; i < 256; i++) { @@ -38,7 +39,7 @@ goog.scope(function() { hexMap[buf[i++]]); } - uuid.impl.v4 = function() { + impl.v4 = function() { const buf = rng.getBytes(16); buf[6] = (buf[6] & 0x0f) | 0x40; buf[8] = (buf[8] & 0x3f) | 0x80; diff --git a/vendor/uuid/rng.js b/src/uxbox/util/uuid/rng.js similarity index 81% rename from vendor/uuid/rng.js rename to src/uxbox/util/uuid/rng.js index 8cf6dfd75..995c44eb5 100644 --- a/vendor/uuid/rng.js +++ b/src/uxbox/util/uuid/rng.js @@ -8,35 +8,39 @@ "use strict"; -goog.provide("uuid.rng"); +goog.provide("uxbox.util.uuid.rng"); goog.require("cljs.core"); goog.scope(function() { const global = goog.global; + const rng = uxbox.util.uuid.rng; + // Check if nodejs rng is available (high quality); if (cljs.core._STAR_target_STAR_ === "nodejs") { const crypto = require("crypto"); - uuid.rng.getBytes = function(n) { + rng.getBytes = function(n) { return crypto.randomBytes(n); }; } + // Check if whatwg rng is available (high quality); else if (global.crypto !== undefined && global.crypto.getRandomValues !== undefined) { - uuid.rng.getBytes = function(n) { + rng.getBytes = function(n) { const buf = new Uint8Array(16); global.crypto.getRandomValues(buf); return buf; }; } + // Switch Back to the Math.random (low quality); else { console.warn("No high quality RNG available, switching back to Math.random."); - uuid.rng.getBytes = function(n) { - const buf = new Array(16); - for (let i = 0, r; i < 16; i++) { + rng.getBytes = function(n) { + const buf = new Array(n); + for (let i = 0, r; i < n; i++) { if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; } buf[i] = r >>> ((i & 0x03) << 3) & 0xff; } diff --git a/test/uxbox/tests/shapes_state.cljs b/test/uxbox/tests/shapes_state.cljs index 4a1fe702d..c9fa4e1fd 100644 --- a/test/uxbox/tests/shapes_state.cljs +++ b/test/uxbox/tests/shapes_state.cljs @@ -1,7 +1,7 @@ (ns uxbox.tests.shapes-state (:require [cljs.test :as t :include-macros true] [cljs.pprint :refer (pprint)] - [uuid.core :as uuid] + [uxbox.util.uuid :as uuid] [uxbox.main.state.shapes :as sh])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -25,7 +25,7 @@ (assoc-in [:pages-by-id 1 :shapes] [2 1]) (assoc-in [:shapes-by-id 2] {:id 2 :page 1}))] - (with-redefs [uuid.core/random (constantly 2)] + (with-redefs [uxbox.util.uuid/random (constantly 2)] (let [result (sh/duplicate-shapes initial [1])] ;; (pprint expected) ;; (pprint result) @@ -44,7 +44,7 @@ (assoc-in [:shapes-by-id 1 :items] [5 4 2 3]) (assoc-in [:shapes-by-id 4] {:id 4 :name "3" :page 1 :group 1}) (assoc-in [:shapes-by-id 5] {:id 5 :name "2" :page 1 :group 1}))] - (with-redefs [uuid.core/random (constantly-inc 4)] + (with-redefs [uxbox.util.uuid/random (constantly-inc 4)] (let [result (sh/duplicate-shapes initial [2 3])] ;; (pprint expected) ;; (pprint result) @@ -65,7 +65,7 @@ (assoc-in [:pages-by-id 1 :shapes] [6 5 1 4]) (assoc-in [:shapes-by-id 5] {:id 5 :name "4" :page 1}) (assoc-in [:shapes-by-id 6] {:id 6 :name "3" :page 1}))] - (with-redefs [uuid.core/random (constantly-inc 5)] + (with-redefs [uxbox.util.uuid/random (constantly-inc 5)] (let [result (sh/duplicate-shapes initial [3 4])] ;; (pprint expected) ;; (pprint result) @@ -86,7 +86,7 @@ :type :group :items [4]}) (assoc-in [:shapes-by-id 4] {:id 4 :page 1 :group 3}))] - (with-redefs [uuid.core/random (constantly-inc 3)] + (with-redefs [uxbox.util.uuid/random (constantly-inc 3)] (let [result (sh/duplicate-shapes initial [1])] ;; (pprint expected) ;; (pprint result) @@ -324,7 +324,7 @@ (assoc-in [:shapes-by-id 2 :group] 4) (assoc-in [:shapes-by-id 4] {:type :group :name "Group 10" :items [2] :id 4 :page 1}))] - (with-redefs [uuid.core/random (constantly 4) + (with-redefs [uxbox.util.uuid/random (constantly 4) cljs.core/rand-int (constantly 10)] (let [result (sh/group-shapes initial [2] 1)] (t/is (= result expected)))))) @@ -345,7 +345,7 @@ (assoc-in [:shapes-by-id 3 :group] 4) (assoc-in [:shapes-by-id 4] {:type :group :name "Group 10" :items [2 3] :id 4 :page 1}))] - (with-redefs [uuid.core/random (constantly 4) + (with-redefs [uxbox.util.uuid/random (constantly 4) cljs.core/rand-int (constantly 10)] (let [result (sh/group-shapes initial [2 3] 1)] (t/is (= result expected)))))) @@ -366,7 +366,7 @@ (assoc-in [:shapes-by-id 3 :group] 4) (assoc-in [:shapes-by-id 4] {:type :group :name "Group 10" :items [2 3] :id 4 :page 1}))] - (with-redefs [uuid.core/random (constantly 4) + (with-redefs [uxbox.util.uuid/random (constantly 4) cljs.core/rand-int (constantly 10)] (let [result (sh/group-shapes initial [2 3] 1)] (t/is (= result expected)))))) @@ -387,7 +387,7 @@ (assoc-in [:shapes-by-id 3 :items] [4]) (assoc-in [:shapes-by-id 4] {:type :group :name "Group 10" :items [2] :id 4 :page 1 :group 3}))] - (with-redefs [uuid.core/random (constantly 4) + (with-redefs [uxbox.util.uuid/random (constantly 4) cljs.core/rand-int (constantly 10)] (let [result (sh/group-shapes initial [2] 1)] (t/is (= result expected)))))) @@ -410,7 +410,7 @@ :items [1 2] :id 5 :page 1}) (update-in [:shapes-by-id] dissoc 3) (update-in [:shapes-by-id] dissoc 4))] - (with-redefs [uuid.core/random (constantly 5) + (with-redefs [uxbox.util.uuid/random (constantly 5) cljs.core/rand-int (constantly 10)] (let [result (sh/group-shapes initial [1 2] 1)] (t/is (= result expected))))))