mirror of
https://github.com/penpot/penpot.git
synced 2025-08-06 15:08:27 +02:00
♻️ Refactor uuid impl module.
Make it crossplatform (browser, nodejs, jvm).
This commit is contained in:
parent
bcb2609b18
commit
b8526c6e3b
2 changed files with 174 additions and 159 deletions
|
@ -14,7 +14,7 @@
|
|||
(:require [clj-uuid :as impl]
|
||||
[clojure.core :as c])
|
||||
:cljs
|
||||
(:require ["./uuid_impl.js" :as impl]
|
||||
(:require [uxbox.common.uuid-impl :as impl]
|
||||
[cljs.core :as c])))
|
||||
|
||||
(def zero #uuid "00000000-0000-0000-0000-000000000000")
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/*
|
||||
/**
|
||||
* 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
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
@ -10,32 +10,43 @@
|
|||
*/
|
||||
"use strict";
|
||||
|
||||
import cljs from "goog:cljs.core";
|
||||
goog.require("cljs.core");
|
||||
goog.provide("uxbox.common.uuid_impl");
|
||||
|
||||
const global = goog.global;
|
||||
const crypto = global.crypto;
|
||||
goog.scope(function() {
|
||||
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);
|
||||
return buf;
|
||||
};
|
||||
|
||||
// NOTE: NodeJS compatibility
|
||||
if (typeof require === "function") {
|
||||
global.crypto = require("crypto");
|
||||
}
|
||||
|
||||
if (global.crypto === undefined) {
|
||||
} else {
|
||||
// FALLBACK
|
||||
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++) {
|
||||
if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; }
|
||||
buf[i] = r >>> ((i & 0x03) << 3) & 0xff;
|
||||
}
|
||||
return buf;
|
||||
};
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
|
@ -88,11 +99,11 @@ function toHexString(buf) {
|
|||
|
||||
const buff = new Uint8Array(16);
|
||||
|
||||
export function v4() {
|
||||
function v4() {
|
||||
fill(buff);
|
||||
buff[6] = (buff[6] & 0x0f) | 0x40;
|
||||
buff[8] = (buff[8] & 0x3f) | 0x80;
|
||||
return cljs.uuid(toHexString(buff));
|
||||
return core.uuid(toHexString(buff));
|
||||
}
|
||||
|
||||
let initialized = false;
|
||||
|
@ -101,7 +112,7 @@ let clockseq;
|
|||
let lastms = 0;
|
||||
let lastns = 0;
|
||||
|
||||
export function v1() {
|
||||
function v1() {
|
||||
let cs = clockseq;
|
||||
|
||||
if (!initialized) {
|
||||
|
@ -179,5 +190,9 @@ export function v1() {
|
|||
buff[i + n] = node[n];
|
||||
}
|
||||
|
||||
return cljs.uuid(toHexString(buff));
|
||||
return core.uuid(toHexString(buff));
|
||||
}
|
||||
|
||||
self.v1 = v1;
|
||||
self.v4 = v4;
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue