Skip to content

Commit

Permalink
Moved code around in storage.js
Browse files Browse the repository at this point in the history
  • Loading branch information
saikumarrs committed Sep 7, 2021
1 parent a10dab6 commit 5eaa91f
Showing 1 changed file with 90 additions and 125 deletions.
215 changes: 90 additions & 125 deletions utils/storage/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,72 @@ const defaults = {
page_storage_init_referrer: "rl_page_init_referrer",
page_storage_init_referring_domain: "rl_page_init_referring_domain",
prefix: "RudderEncrypt:",
key: "Rudder"
key: "Rudder",
};

/**
* Json stringify the given value
* @param {*} value
*/
function stringify(value) {
return JSON.stringify(value);
}

/**
* JSON parse the value
* @param {*} value
*/
function parse(value) {
// if not parsable, return as is without json parse
try {
return value ? JSON.parse(value) : null;
} catch (e) {
logger.error(e);
return value || null;
}
}

/**
* trim using regex for browser polyfill
* @param {*} value
*/
function trim(value) {
return value.replace(/^\s+|\s+$/gm, "");
}

/**
* decrypt value
* @param {*} value
*/
function decryptValue(value) {
if (!value || (typeof value === "string" && trim(value) === "")) {
return value;
}
if (value.substring(0, defaults.prefix.length) === defaults.prefix) {
return AES.decrypt(
value.substring(defaults.prefix.length),
defaults.key
).toString(Utf8);
}
return value;
}

/**
* AES encrypt value with constant prefix
* @param {*} value
*/
function encryptValue(value) {
if (trim(value) === "") {
return value;
}
const prefixedVal = `${defaults.prefix}${AES.encrypt(
value,
defaults.key
).toString()}`;

return prefixedVal;
}

/**
* An object that handles persisting key-val from Analytics
*/
Expand All @@ -41,235 +104,136 @@ class Storage {
}

/**
* Json stringify the given value
* @param {*} value
*/
stringify(value) {
return JSON.stringify(value);
}

/**
* JSON parse the value
* @param {*} value
*/
parse(value) {
// if not parseable, return as is without json parse
try {
return value ? JSON.parse(value) : null;
} catch (e) {
logger.error(e);
return value || null;
}
}

/**
* trim using regex for browser polyfill
* @param {*} value
*/
trim(value) {
return value.replace(/^\s+|\s+$/gm, "");
}

/**
* AES encrypt value with constant prefix
* @param {*} value
*/
encryptValue(value) {
if (this.trim(value) == "") {
return value;
}
const prefixedVal = `${defaults.prefix}${AES.encrypt(
value,
defaults.key
).toString()}`;

return prefixedVal;
}

/**
* decrypt value
*
* @param {*} key
* @param {*} value
*/
decryptValue(value) {
if (!value || (typeof value === "string" && this.trim(value) == "")) {
return value;
}
if (value.substring(0, defaults.prefix.length) == defaults.prefix) {
return AES.decrypt(
value.substring(defaults.prefix.length),
defaults.key
).toString(Utf8);
}
return value;
setItem(key, value) {
this.storage.set(key, encryptValue(stringify(value)));
}

/**
*
* @param {*} key
* @param {*} value
*/
setItem(key, value) {
this.storage.set(key, this.encryptValue(this.stringify(value)));
setStringItem(key, value) {
if (typeof value !== "string") {
logger.error(`[Storage] ${key} should be string`);
return;
}
this.setItem(key, value);
}

/**
*
* @param {*} value
*/
setUserId(value) {
if (typeof value !== "string") {
logger.error("[Storage] setUserId:: userId should be string");
return;
}
this.storage.set(
defaults.user_storage_key,
this.encryptValue(this.stringify(value))
);
this.setStringItem(defaults.user_storage_key, value);
}

/**
*
* @param {*} value
*/
setUserTraits(value) {
this.storage.set(
defaults.user_storage_trait,
this.encryptValue(this.stringify(value))
);
this.setItem(defaults.user_storage_trait, value);
}

/**
*
* @param {*} value
*/
setGroupId(value) {
if (typeof value !== "string") {
logger.error("[Storage] setGroupId:: groupId should be string");
return;
}
this.storage.set(
defaults.group_storage_key,
this.encryptValue(this.stringify(value))
);
this.setStringItem(defaults.group_storage_key, value);
}

/**
*
* @param {*} value
*/
setGroupTraits(value) {
this.storage.set(
defaults.group_storage_trait,
this.encryptValue(this.stringify(value))
);
this.setItem(defaults.group_storage_trait, value);
}

/**
*
* @param {*} value
*/
setAnonymousId(value) {
if (typeof value !== "string") {
logger.error("[Storage] setAnonymousId:: anonymousId should be string");
return;
}
this.storage.set(
defaults.user_storage_anonymousId,
this.encryptValue(this.stringify(value))
);
this.setStringItem(defaults.user_storage_anonymousId, value);
}

/**
* @param {*} value
*/
setInitialReferrer(value) {
this.storage.set(
defaults.page_storage_init_referrer,
this.encryptValue(this.stringify(value))
);
this.setItem(defaults.page_storage_init_referrer, value);
}

/**
* @param {*} value
*/
setInitialReferringDomain(value) {
this.storage.set(
defaults.page_storage_init_referring_domain,
this.encryptValue(this.stringify(value))
);
this.setItem(defaults.page_storage_init_referring_domain, value);
}

/**
*
* @param {*} key
*/
getItem(key) {
return this.parse(this.decryptValue(this.storage.get(key)));
return parse(decryptValue(this.storage.get(key)));
}

/**
* get the stored userId
*/
getUserId() {
return this.parse(
this.decryptValue(this.storage.get(defaults.user_storage_key))
);
return this.getItem(defaults.user_storage_key);
}

/**
* get the stored user traits
*/
getUserTraits() {
return this.parse(
this.decryptValue(this.storage.get(defaults.user_storage_trait))
);
return this.getItem(defaults.user_storage_trait);
}

/**
* get the stored userId
*/
getGroupId() {
return this.parse(
this.decryptValue(this.storage.get(defaults.group_storage_key))
);
return this.getItem(defaults.group_storage_key);
}

/**
* get the stored user traits
*/
getGroupTraits() {
return this.parse(
this.decryptValue(this.storage.get(defaults.group_storage_trait))
);
return this.getItem(defaults.group_storage_trait);
}

/**
* get stored anonymous id
*/
getAnonymousId() {
return this.parse(
this.decryptValue(this.storage.get(defaults.user_storage_anonymousId))
);
return this.getItem(defaults.user_storage_anonymousId);
}

/**
* get stored initial referrer
*/
getInitialReferrer() {
return this.parse(
this.decryptValue(this.storage.get(defaults.page_storage_init_referrer))
);
return this.getItem(defaults.page_storage_init_referrer);
}

/**
* get stored initial referring domain
*/
getInitialReferringDomain() {
return this.parse(
this.decryptValue(
this.storage.get(defaults.page_storage_init_referring_domain)
)
);
return this.getItem(defaults.page_storage_init_referring_domain);
}

/**
Expand All @@ -294,4 +258,5 @@ class Storage {
}
}

// eslint-disable-next-line import/prefer-default-export
export { Storage };

0 comments on commit 5eaa91f

Please sign in to comment.