Skip to content

Commit

Permalink
Moving sharedID generation within sharedId module
Browse files Browse the repository at this point in the history
  • Loading branch information
skocheri committed Jun 15, 2020
1 parent 6cb4d98 commit 7b7847e
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 135 deletions.
130 changes: 127 additions & 3 deletions modules/sharedIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import * as utils from '../src/utils.js'
import {ajax} from '../src/ajax.js';
import {submodule} from '../src/hook.js';
import * as sharedIdGenerator from '../src/sharedIdGenerator.js';

const MODULE_NAME = 'sharedId';
const ID_SVC = 'https://id.sharedid.org/id';
const DEFAULT_24_HOURS = 86400;
const OPT_OUT_VALUE = '00000000000000000000000000';

const id = factory();
/**
* Constructs cookie value
* @param value
Expand Down Expand Up @@ -91,7 +90,7 @@ function idGenerationCallback(callback) {
callback(value);
},
error: function (statusText, responseBody) {
const value = constructCookieValue(sharedIdGenerator.id(), true);
const value = constructCookieValue(id(), true);
utils.logInfo('SharedId: Ulid Generated SharedId: ' + value.id);
callback(value);
}
Expand Down Expand Up @@ -152,6 +151,131 @@ function encodeId(value) {
return sharedId;
}

// These values should NEVER change. If
// they do, we're no longer making ulids!
const ENCODING = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; // Crockford's Base32
const ENCODING_LEN = ENCODING.length;
const TIME_MAX = Math.pow(2, 48) - 1;
const TIME_LEN = 10;
const RANDOM_LEN = 16;

/**
* the factory to generate unique identifier based on time and current pseudorandom number
* @param {string} the current pseudorandom number generator
* @returns {function(*=): *}
*/
function factory(currPrng) {
if (!currPrng) {
currPrng = detectPrng();
}
return function ulid(seedTime) {
if (isNaN(seedTime)) {
seedTime = Date.now();
}
return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN, currPrng);
};
}

/**
* creates and logs the error message
* @function
* @param {string} error message
* @returns {Error}
*/
function createError(message) {
utils.logError(message);
const err = new Error(message);
err.source = 'sharedId';
return err;
}

/**
* gets a a random charcter from generated pseudorandom number
* @param {string} the generated pseudorandom number
* @returns {string}
*/
function randomChar(prng) {
let rand = Math.floor(prng() * ENCODING_LEN);
if (rand === ENCODING_LEN) {
rand = ENCODING_LEN - 1;
}
return ENCODING.charAt(rand);
}

/**
* encodes the time based on the length
* @param now
* @param len
* @returns {string} encoded time.
*/
function encodeTime (now, len) {
if (isNaN(now)) {
throw new Error(now + ' must be a number');
}

if (Number.isInteger(now) === false) {
throw createError('time must be an integer');
}

if (now > TIME_MAX) {
throw createError('cannot encode time greater than ' + TIME_MAX);
}
if (now < 0) {
throw createError('time must be positive');
}

if (Number.isInteger(len) === false) {
throw createError('length must be an integer');
}
if (len < 0) {
throw createError('length must be positive');
}

let mod;
let str = '';
for (; len > 0; len--) {
mod = now % ENCODING_LEN;
str = ENCODING.charAt(mod) + str;
now = (now - mod) / ENCODING_LEN;
}
return str;
}

/**
* encodes random character
* @param len
* @param prng
* @returns {string}
*/
function encodeRandom (len, prng) {
let str = '';
for (; len > 0; len--) {
str = randomChar(prng) + str;
}
return str;
}

/**
* detects the pseudorandom number generator and generates the random number
* @function
* @param {string} error message
* @returns {string} a random number
*/
function detectPrng(root) {
if (!root) {
root = typeof window !== 'undefined' ? window : null;
}
const browserCrypto = root && (root.crypto || root.msCrypto);
if (browserCrypto) {
return () => {
const buffer = new Uint8Array(1);
browserCrypto.getRandomValues(buffer);
return buffer[0] / 0xff;
};
}
return () => Math.random();
}

/** @type {Submodule} */
export const sharedIdSubmodule = {
/**
Expand Down
132 changes: 0 additions & 132 deletions src/sharedIdGenerator.js

This file was deleted.

0 comments on commit 7b7847e

Please sign in to comment.