@d3vtool/secid
Version:
Generate a fast, secure, and compact unique identifier (ID) for use in modern applications, providing small, URL friendly, and collision resistant values.
69 lines (68 loc) • 3.72 kB
JavaScript
;
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _a, _SecId_usageCounter, _SecId_urlSafeAlphabets, _SecId_getRandomBytes, _SecId_generateRadInt;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecId = void 0;
class SecId {
static generate(length = 23, alphabets = __classPrivateFieldGet(this, _a, "f", _SecId_urlSafeAlphabets)) {
var _b;
__classPrivateFieldSet(this, _a, (_b = __classPrivateFieldGet(this, _a, "f", _SecId_usageCounter), ++_b), "f", _SecId_usageCounter);
let id = "";
const randomBytes = __classPrivateFieldGet(this, _a, "m", _SecId_getRandomBytes).call(this, length);
const randomInt = __classPrivateFieldGet(this, _a, "m", _SecId_generateRadInt).call(this, 0, alphabets.length);
let index = 0;
const alphabetLength = alphabets.length - 1;
for (let i = 0; i < length; ++i) {
if (randomBytes[i] >= alphabetLength) {
index = (randomBytes[i] % alphabetLength);
index = (index > randomInt) ? index - randomInt :
(__classPrivateFieldGet(this, _a, "f", _SecId_usageCounter) + index < (alphabetLength - index)) ?
(__classPrivateFieldGet(this, _a, "f", _SecId_usageCounter) + index) : index;
}
else {
index = randomBytes[i];
}
id += alphabets[index];
}
return id;
}
}
exports.SecId = SecId;
_a = SecId, _SecId_getRandomBytes = function _SecId_getRandomBytes(byteCount = 16) {
const isNodeEnv = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
//@ts-ignore
const isBrowserEnv = typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues;
if (isNodeEnv) {
const { randomBytes } = require('crypto');
return randomBytes(byteCount);
}
else if (isBrowserEnv) {
const bytes = new Uint8Array(byteCount);
//@ts-ignore
return window.crypto.getRandomValues(bytes);
}
else {
throw new Error("Error: Unkown environment.");
}
}, _SecId_generateRadInt = function _SecId_generateRadInt(from, to) {
const ans = to - from;
if (ans < 0) {
throw new Error("Error Ending range value cannot be smaller then starting value.");
}
else if (ans === 0) {
throw new Error("Error Ending range value cannot be equal to starting value.");
}
return Math.floor(Math.random() * (to - from) + 1) + from;
};
_SecId_usageCounter = { value: 0 };
_SecId_urlSafeAlphabets = { value: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-" };