New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@studyportals/data-storage

Package Overview
Dependencies
Maintainers
19
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@studyportals/data-storage - npm Package Compare versions

Comparing version 1.2.2 to 1.3.0

.npmignore

358

dist/classes/DataStorage.js

@@ -0,206 +1,158 @@

"use strict";
/**
* Create a browser independent storage solution.
*/
let DataStorage = {
enabled: null,
/**
* Check if the localStorage is available.
*
* This function checks if the localStorage is available and sets the
* enabled parameter to true if it is available, or false when it's not.
*
* @deprecated According to @stefanklokgieters the check for browser support
* might be removed completely since its [widely
* adopted](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage)
* while according to @braaibander it might be still valid for Cognito Modes
* in some browsers. Changing or removing this method would be a breaking
* change since it is already exposed. For the moment it's a good reason
* just to leave it as it is. We should re-think those choices as soon as we
* draft v2.x.x.
*
* @return void
*/
init() {
if (typeof localStorage !== 'undefined') {
try {
/**
* Test if we are able to set and remove values.
*
* In some browsers (shame on you Safari!) the localStorage is
* available, but under certain conditions (like inPrivate, shame on
* you Safari) errors are thrown as soon as you are trying to add
* something to the localStorage (again, shame on you Safari!).
* These calls will trigger exceptions in that case.
*/
localStorage.setItem('test', 'dummy value');
localStorage.removeItem('test');
this.enabled = true;
}
catch (error) {
this.enabled = false;
}
}
},
/**
* Store a value into localStorage if available, otherwise store in cookie.
*
* @param {String} $key - LocalStorage key (name).
* @param {String} $value - LocalStorage key values.
* @param {Number} [$ttl] - LocalStorage custom expiry date in seconds.
*
* @returns {boolean}
*/
store($key, $value, $ttl) {
if (isNaN($ttl)) {
// Expiry date of 30 minutes by default.
$ttl = 1800;
}
if (!this.enabled) {
document.cookie = `${$key}=${$value};expires=${$ttl / 86400}`;
}
else {
// Manually insert an expiry date
let $time = new Date();
$time = $time.setSeconds($time.getSeconds() + $ttl);
let $container = {
'expires': $time,
'data': $value
};
localStorage[$key] = JSON.stringify($container);
}
return true;
},
/**
* Retrieve a value from localStorage if available, otherwise get from cookie.
*
* @param {String} $key
* @return {String}
*/
retrieve($key) {
// Get the required cookie.
function getCookie(name) {
let pattern = RegExp(name + "=.[^;]*");
let matched = document.cookie.match(pattern);
if (matched) {
let cookie = matched[0].split('=');
return cookie[1];
}
return '';
}
// If there is no localStorage support use cookies as a fallback.
if (!this.enabled) {
return getCookie($key);
}
else {
let $decoded = localStorage[$key];
try {
$decoded = JSON.parse($decoded);
}
catch (error) {
$decoded = {};
}
if (!$decoded || !$decoded.hasOwnProperty('expires') || !$decoded.hasOwnProperty('data')) {
return '';
}
let $expiryDate = parseInt($decoded.expires, 10);
if (isNaN($expiryDate)) {
$expiryDate = 0;
}
// Check if the data is expired
if (new Date() >= new Date($expiryDate)) {
localStorage[$key] = null;
return '';
}
return $decoded.data;
}
},
/**
* Remove a key from the data source.
*
* @param {String} $key
* @return void
*/
remove($key) {
// Clear the specific localStorage or cookie entry.
if (!this.enabled) {
document.cookie = `${$key}=;expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}
else {
delete localStorage[$key];
}
},
/**
* Updates an object in local storage
* or creates the object if it does not exist
*
* TODO: BUG: This method will simply overwrite the previous stored value if
* it is not an object already
*
* TODO: BUG: This method expects an $TTL as third argument even though it
* not listed as an argument. A TTL of `0` is send to the store when no $TTL
* provided resulting in invalidating the key immediately.
*
* @param {String} $key
* @param {Object} $value
* @param {Number} $ttl
* @return {*|String}
*/
update($key, $value, $ttl = null) {
let $old = this.retrieve($key);
if (typeof $old !== 'object') {
$old = {};
}
$old = Object.assign({}, $old, $value);
this.store($key, $old, $ttl);
return $old;
}
};
DataStorage.init();
// Export the module.
module.exports = DataStorage;
Object.defineProperty(exports, "__esModule", { value: true });
var DataStorage = /** @class */ (function () {
function DataStorage() {
this.enabled = false;
}
/**
* Check if the localStorage is available.
*
* This function checks if the localStorage is available and sets the
* enabled parameter to true if it is available, or false when it's not.
*
* @deprecated According to @stefanklokgieters the check for browser support
* might be removed completely since its [widely
* adopted](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage)
* while according to @braaibander it might be still valid for Cognito Modes
* in some browsers. Changing or removing this method would be a breaking
* change since it is already exposed. For the moment it's a good reason
* just to leave it as it is. We should re-think those choices as soon as we
* draft v2.x.x.
*
* @return void
*/
DataStorage.prototype.init = function () {
try {
if (typeof localStorage === 'undefined') {
this.enabled = false;
return;
}
/**
* Test if we are able to set and remove values.
*
* In some browsers (shame on you Safari!) the localStorage is
* available, but under certain conditions (like inPrivate, shame on
* you Safari) errors are thrown as soon as you are trying to add
* something to the localStorage (again, shame on you Safari!).
* These calls will trigger exceptions in that case.
*/
localStorage.setItem('test', 'dummy value');
localStorage.removeItem('test');
this.enabled = true;
}
catch (error) {
this.enabled = false;
}
};
/**
* Store a value into localStorage if available, otherwise store in cookie.
*
* @param key - LocalStorage key (name).
* @param value - LocalStorage key values.
* @param ttl - LocalStorage custom expiry date in seconds. 30 minutes by default.
*/
DataStorage.prototype.store = function (key, value, ttl) {
if (ttl === void 0) { ttl = 1800; }
if (!this.enabled) {
document.cookie = key + "=" + value + ";expires=" + ttl / 86400;
}
else {
// Manually insert an expiry date
var time = new Date();
var expiresAt = time.setSeconds(time.getSeconds() + ttl);
var container = {
'expires': expiresAt,
'data': value
};
localStorage[key] = JSON.stringify(container);
}
return true;
};
/**
* Retrieve a value from localStorage if available, otherwise get from cookie.
* @param key
*/
DataStorage.prototype.retrieve = function (key) {
// If there is no localStorage support use cookies as a fallback.
if (!this.enabled) {
return this.getCookie(key);
}
else {
var decoded = localStorage[key];
try {
decoded = JSON.parse(decoded);
}
catch (error) {
decoded = {};
}
if (!decoded || !decoded.hasOwnProperty('expires') || !decoded.hasOwnProperty('data')) {
return '';
}
var expiryDate = parseInt(decoded.expires, 10);
if (isNaN(expiryDate)) {
expiryDate = 0;
}
// Check if the data is expired
if (new Date() >= new Date(expiryDate)) {
localStorage[key] = null;
return '';
}
return decoded.data;
}
};
/**
* Remove a key from the data source.
*
* @param {String} key
* @return void
*/
DataStorage.prototype.remove = function (key) {
// Clear the specific localStorage or cookie entry.
if (!this.enabled) {
document.cookie = key + "=;expires=Thu, 01 Jan 1970 00:00:01 GMT;";
}
else {
delete localStorage[key];
}
};
/**
* Updates an object in local storage
* or creates the object if it does not exist
*
* TODO: BUG: This method will simply overwrite the previous stored value if
* it is not an object already
*
* TODO: BUG: This method expects an $TTL as third argument even though it
* not listed as an argument. A TTL of `0` is send to the store when no $TTL
* provided resulting in invalidating the key immediately.
*
* @param {String} key
* @param {Object} value
* @param {Number} ttl
* @return {*|String}
*/
DataStorage.prototype.update = function (key, value, ttl) {
var old = this.retrieve(key);
if (typeof old !== 'object') {
old = {};
}
old = Object.assign({}, old, value);
this.store(key, old, ttl);
return old;
};
DataStorage.prototype.getCookie = function (name) {
var pattern = RegExp(name + "=.[^;]*");
var matched = document.cookie.match(pattern);
if (matched) {
var cookie = matched[0].split('=');
return cookie[1];
}
return '';
};
return DataStorage;
}());
exports.DataStorage = DataStorage;
;

@@ -1,1 +0,7 @@

module.exports.DataStorage = require('./classes/DataStorage');
"use strict";
var DataStorage_1 = require("./classes/DataStorage");
var dataStorage = new DataStorage_1.DataStorage();
dataStorage.init();
module.exports = {
DataStorage: dataStorage
};
{
"name": "@studyportals/data-storage",
"version": "1.2.2",
"version": "1.3.0",
"description": "Create a browser independent local storage solution.",

@@ -19,10 +19,11 @@ "main": "dist/index.js",

"test:mocha": "mocha",
"test": "npm run test:mocha && npm run test:cypress"
"test": "tsc && npm run test:mocha && npm run test:cypress"
},
"devDependencies": {
"chai": "^4.2.0",
"cypress": "^3.1.0",
"cypress": "^3.1.4",
"mocha": "^5.2.0",
"semver": "^5.5.1"
"semver": "^5.5.1",
"typescript": "^3.2.2"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc