♻️ Refactor uuid impl module.

Make it crossplatform (browser, nodejs, jvm).
This commit is contained in:
Andrey Antukh 2020-07-02 10:39:44 +02:00
parent bcb2609b18
commit b8526c6e3b
2 changed files with 174 additions and 159 deletions

View file

@ -14,7 +14,7 @@
(:require [clj-uuid :as impl] (:require [clj-uuid :as impl]
[clojure.core :as c]) [clojure.core :as c])
:cljs :cljs
(:require ["./uuid_impl.js" :as impl] (:require [uxbox.common.uuid-impl :as impl]
[cljs.core :as c]))) [cljs.core :as c])))
(def zero #uuid "00000000-0000-0000-0000-000000000000") (def zero #uuid "00000000-0000-0000-0000-000000000000")

View file

@ -1,4 +1,4 @@
/* /**
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
@ -10,32 +10,43 @@
*/ */
"use strict"; "use strict";
import cljs from "goog:cljs.core"; goog.require("cljs.core");
goog.provide("uxbox.common.uuid_impl");
const global = goog.global; goog.scope(function() {
const crypto = global.crypto; const core = cljs.core;
const self = uxbox.common.uuid_impl;
function fill(buf) { const fill = (() => {
if (typeof window === "object") {
return (buf) => {
window.crypto.getRandomValues(buf);
return buf;
};
} else if (typeof self === "object") {
return (buf) => {
self.crypto.getRandomValues(buf);
return buf;
};
} else if (typeof require === "function") {
const crypto = require("crypto");
return (buf) => {
crypto.getRandomValues(buf); crypto.getRandomValues(buf);
return buf; return buf;
}; };
} else {
// NOTE: NodeJS compatibility // FALLBACK
if (typeof require === "function") {
global.crypto = require("crypto");
}
if (global.crypto === undefined) {
console.warn("No high quality RNG available, switching back to Math.random."); console.warn("No high quality RNG available, switching back to Math.random.");
fill = function(buf) { return (buf) => {
for (let i = 0, r; i < buf.length; i++) { for (let i = 0, r; i < buf.length; i++) {
if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; } if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; }
buf[i] = r >>> ((i & 0x03) << 3) & 0xff; buf[i] = r >>> ((i & 0x03) << 3) & 0xff;
} }
return buf; return buf;
};
} }
} })();
/* /*
* The MIT License (MIT) * The MIT License (MIT)
@ -88,11 +99,11 @@ function toHexString(buf) {
const buff = new Uint8Array(16); const buff = new Uint8Array(16);
export function v4() { function v4() {
fill(buff); fill(buff);
buff[6] = (buff[6] & 0x0f) | 0x40; buff[6] = (buff[6] & 0x0f) | 0x40;
buff[8] = (buff[8] & 0x3f) | 0x80; buff[8] = (buff[8] & 0x3f) | 0x80;
return cljs.uuid(toHexString(buff)); return core.uuid(toHexString(buff));
} }
let initialized = false; let initialized = false;
@ -101,7 +112,7 @@ let clockseq;
let lastms = 0; let lastms = 0;
let lastns = 0; let lastns = 0;
export function v1() { function v1() {
let cs = clockseq; let cs = clockseq;
if (!initialized) { if (!initialized) {
@ -179,5 +190,9 @@ export function v1() {
buff[i + n] = node[n]; buff[i + n] = node[n];
} }
return cljs.uuid(toHexString(buff)); return core.uuid(toHexString(buff));
} }
self.v1 = v1;
self.v4 = v4;
});