|
| 1 | +function createError(message) { |
| 2 | + var err = new Error(message); |
| 3 | + err.source = "ulid"; |
| 4 | + return err; |
| 5 | +} |
| 6 | +// These values should NEVER change. If |
| 7 | +// they do, we're no longer making ulids! |
| 8 | +var ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // Crockford's Base32 |
| 9 | +var ENCODING_LEN = ENCODING.length; |
| 10 | +var TIME_MAX = Math.pow(2, 48) - 1; |
| 11 | +var TIME_LEN = 10; |
| 12 | +var RANDOM_LEN = 16; |
| 13 | +function replaceCharAt(str, index, char) { |
| 14 | + if (index > str.length - 1) { |
| 15 | + return str; |
| 16 | + } |
| 17 | + return str.substr(0, index) + char + str.substr(index + 1); |
| 18 | +} |
| 19 | +function incrementBase32(str) { |
| 20 | + var done = undefined; |
| 21 | + var index = str.length; |
| 22 | + var char = void 0; |
| 23 | + var charIndex = void 0; |
| 24 | + var maxCharIndex = ENCODING_LEN - 1; |
| 25 | + while (!done && index-- >= 0) { |
| 26 | + char = str[index]; |
| 27 | + charIndex = ENCODING.indexOf(char); |
| 28 | + if (charIndex === -1) { |
| 29 | + throw createError("incorrectly encoded string"); |
| 30 | + } |
| 31 | + if (charIndex === maxCharIndex) { |
| 32 | + str = replaceCharAt(str, index, ENCODING[0]); |
| 33 | + continue; |
| 34 | + } |
| 35 | + done = replaceCharAt(str, index, ENCODING[charIndex + 1]); |
| 36 | + } |
| 37 | + if (typeof done === "string") { |
| 38 | + return done; |
| 39 | + } |
| 40 | + throw createError("cannot increment this string"); |
| 41 | +} |
| 42 | +function randomChar(prng) { |
| 43 | + var rand = Math.floor(prng() * ENCODING_LEN); |
| 44 | + if (rand === ENCODING_LEN) { |
| 45 | + rand = ENCODING_LEN - 1; |
| 46 | + } |
| 47 | + return ENCODING.charAt(rand); |
| 48 | +} |
| 49 | +function encodeTime(now, len) { |
| 50 | + if (isNaN(now)) { |
| 51 | + throw new Error(now + " must be a number"); |
| 52 | + } |
| 53 | + if (now > TIME_MAX) { |
| 54 | + throw createError("cannot encode time greater than " + TIME_MAX); |
| 55 | + } |
| 56 | + if (now < 0) { |
| 57 | + throw createError("time must be positive"); |
| 58 | + } |
| 59 | + if (Number.isInteger(now) === false) { |
| 60 | + throw createError("time must be an integer"); |
| 61 | + } |
| 62 | + var mod = void 0; |
| 63 | + var str = ""; |
| 64 | + for (; len > 0; len--) { |
| 65 | + mod = now % ENCODING_LEN; |
| 66 | + str = ENCODING.charAt(mod) + str; |
| 67 | + now = (now - mod) / ENCODING_LEN; |
| 68 | + } |
| 69 | + return str; |
| 70 | +} |
| 71 | +function encodeRandom(len, prng) { |
| 72 | + var str = ""; |
| 73 | + for (; len > 0; len--) { |
| 74 | + str = randomChar(prng) + str; |
| 75 | + } |
| 76 | + return str; |
| 77 | +} |
| 78 | +function decodeTime(id) { |
| 79 | + if (id.length !== TIME_LEN + RANDOM_LEN) { |
| 80 | + throw createError("malformed ulid"); |
| 81 | + } |
| 82 | + var time = id.substr(0, TIME_LEN).split("").reverse().reduce(function (carry, char, index) { |
| 83 | + var encodingIndex = ENCODING.indexOf(char); |
| 84 | + if (encodingIndex === -1) { |
| 85 | + throw createError("invalid character found: " + char); |
| 86 | + } |
| 87 | + return carry += encodingIndex * Math.pow(ENCODING_LEN, index); |
| 88 | + }, 0); |
| 89 | + if (time > TIME_MAX) { |
| 90 | + throw createError("malformed ulid, timestamp too large"); |
| 91 | + } |
| 92 | + return time; |
| 93 | +} |
| 94 | +function detectPrng() { |
| 95 | + var allowInsecure = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; |
| 96 | + var root = arguments[1]; |
| 97 | + |
| 98 | + if (!root) { |
| 99 | + root = typeof window !== "undefined" ? window : null; |
| 100 | + } |
| 101 | + var browserCrypto = root && (root.crypto || root.msCrypto); |
| 102 | + if (browserCrypto) { |
| 103 | + try { |
| 104 | + return function () { |
| 105 | + return browserCrypto.getRandomValues(new Uint8Array(1))[0] / 0xff; |
| 106 | + }; |
| 107 | + } catch (e) {} |
| 108 | + } else { |
| 109 | + try { |
| 110 | + var nodeCrypto = require("crypto"); |
| 111 | + return function () { |
| 112 | + return nodeCrypto.randomBytes(1).readUInt8() / 0xff; |
| 113 | + }; |
| 114 | + } catch (e) {} |
| 115 | + } |
| 116 | + if (allowInsecure) { |
| 117 | + try { |
| 118 | + console.error("secure crypto unusable, falling back to insecure Math.random()!"); |
| 119 | + } catch (e) {} |
| 120 | + return function () { |
| 121 | + return Math.random(); |
| 122 | + }; |
| 123 | + } |
| 124 | + throw createError("secure crypto unusable, insecure Math.random not allowedW"); |
| 125 | +} |
| 126 | +function factory(currPrng) { |
| 127 | + if (!currPrng) { |
| 128 | + currPrng = detectPrng(); |
| 129 | + } |
| 130 | + return function ulid(seedTime) { |
| 131 | + if (isNaN(seedTime)) { |
| 132 | + seedTime = Date.now(); |
| 133 | + } |
| 134 | + return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN, currPrng); |
| 135 | + }; |
| 136 | +} |
| 137 | +function monotonicFactory(currPrng) { |
| 138 | + if (!currPrng) { |
| 139 | + currPrng = detectPrng(); |
| 140 | + } |
| 141 | + var lastTime = 0; |
| 142 | + var lastRandom = void 0; |
| 143 | + return function ulid(seedTime) { |
| 144 | + if (isNaN(seedTime)) { |
| 145 | + seedTime = Date.now(); |
| 146 | + } |
| 147 | + if (seedTime <= lastTime) { |
| 148 | + var incrementedRandom = lastRandom = incrementBase32(lastRandom); |
| 149 | + return encodeTime(lastTime, TIME_LEN) + incrementedRandom; |
| 150 | + } |
| 151 | + lastTime = seedTime; |
| 152 | + var newRandom = lastRandom = encodeRandom(RANDOM_LEN, currPrng); |
| 153 | + return encodeTime(seedTime, TIME_LEN) + newRandom; |
| 154 | + }; |
| 155 | +} |
| 156 | +var ulid = factory(); |
| 157 | + |
| 158 | +export { replaceCharAt, incrementBase32, randomChar, encodeTime, encodeRandom, decodeTime, detectPrng, factory, monotonicFactory, ulid }; |
0 commit comments