Removed crypto-js

This commit is contained in:
ztimson 2021-05-15 19:57:12 -04:00
parent f27014af14
commit 132f23a028
4 changed files with 6 additions and 30 deletions

View File

@ -36,7 +36,6 @@ export class MyCustomClass {
| Options | Description | | Options | Description |
|---------|-------------| |---------|-------------|
| default | Default value, same as decorator's first argument | | default | Default value, same as decorator's first argument |
| encryptWith | Secret key to encrypt stored values with |
| key | Key to reference value inside local/session storage (Defaults to the property name) | | key | Key to reference value inside local/session storage (Defaults to the property name) |
## Caveats ## Caveats

View File

@ -1,6 +1,6 @@
{ {
"name": "webstorage-decorators", "name": "webstorage-decorators",
"version": "3.2.6", "version": "4.0.0",
"description": "Decorators to sync class properties to Local/Session storage", "description": "Decorators to sync class properties to Local/Session storage",
"repository": "https://github.com/ztimson/WebstorageDecorators", "repository": "https://github.com/ztimson/WebstorageDecorators",
"main": "./lib/index.js", "main": "./lib/index.js",
@ -21,10 +21,7 @@
], ],
"author": "Zak Timson", "author": "Zak Timson",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {},
"@types/crypto-js": "^4.0.1",
"crypto-js": "^4.0.0"
},
"devDependencies": { "devDependencies": {
"@types/jest": "^26.0.15", "@types/jest": "^26.0.15",
"jest": "^26.6.0", "jest": "^26.6.0",

View File

@ -1,13 +1,9 @@
import * as crypto from 'crypto-js';
/** /**
* Options to be used with WebStorage decorators * Options to be used with WebStorage decorators
*/ */
export interface WebStorageOptions { export interface WebStorageOptions {
/** Default value to provide if storage is empty */ /** Default value to provide if storage is empty */
default?: any; default?: any;
/** Key to prevent plain text storage **/
encryptWith?: string;
/** Key to save under */ /** Key to save under */
key?: string; key?: string;
} }
@ -69,9 +65,9 @@ function fromStorage(storage: Storage, opts: WebStorageOptions) {
return temp; return temp;
} }
storedVal = JSON.parse(<string>storedVal); storedVal = JSON.parse(<string>storedVal);
if(opts.encryptWith != null) storedVal = JSON.parse(crypto.AES.decrypt(<string>storedVal, opts.encryptWith).toString(crypto.enc.Utf8));
if(typeof storedVal != 'object' || !Array.isArray(storedVal)) return storedVal; if(typeof storedVal != 'object' || !Array.isArray(storedVal)) return storedVal;
if(opts.default != null && opts.default.constructor != null) return Object.assign(new opts.default.constructor(), opts.default, storedVal); if(opts.default != null && opts.default.constructor != null)
return Object.assign(new opts.default.constructor(), opts.default, storedVal);
return Object.assign({}, opts.default, storedVal); return Object.assign({}, opts.default, storedVal);
} }
@ -89,13 +85,12 @@ function decoratorBuilder(storage: Storage, opts: WebStorageOptions) {
let field = fromStorage(storage, opts); let field = fromStorage(storage, opts);
Object.defineProperty(target, key, { Object.defineProperty(target, key, {
get: function() { get: function() {
if(field != fromStorage(storage, {key: opts.key, encryptWith: opts.encryptWith})) target[key] = field; if(field != fromStorage(storage, {key: opts.key})) target[key] = field;
return field; return field;
}, },
set: function(value?) { set: function(value?) {
field = value; field = value;
if(value == null) storage.removeItem(<string>opts.key); if(value == null) storage.removeItem(<string>opts.key);
if(opts.encryptWith != null) value = <any>crypto.AES.encrypt(JSON.stringify(value), opts.encryptWith).toString();
storage.setItem(<string>opts.key, JSON.stringify(value)); storage.setItem(<string>opts.key, JSON.stringify(value));
} }
}); });

View File

@ -1,7 +1,6 @@
import {LocalStorage, SessionStorage} from "../src"; import {LocalStorage, SessionStorage} from "../src";
const CUSTOM_KEY = '_MY_KEY' const CUSTOM_KEY = '_MY_KEY'
const ENCRYPTION_KEY = 'abc123';
class TestType { class TestType {
constructor(public first: string, public last: string) { } constructor(public first: string, public last: string) { }
@ -12,16 +11,14 @@ class TestStorage {
@LocalStorage() localStorage: any; @LocalStorage() localStorage: any;
@LocalStorage({a: true, b: 'test', c: 3.14}) defaultedLocalStorage: any; @LocalStorage({a: true, b: 'test', c: 3.14}) defaultedLocalStorage: any;
@LocalStorage(null, {key: CUSTOM_KEY}) customLocalStorage: any; @LocalStorage(null, {key: CUSTOM_KEY}) customLocalStorage: any;
@LocalStorage(null, {encryptWith: ENCRYPTION_KEY}) encryptedLocalStorage: any;
@LocalStorage(new TestType('John', 'Smith')) objectLocalStorage!: TestType; @LocalStorage(new TestType('John', 'Smith')) objectLocalStorage!: TestType;
@SessionStorage() sessionStorage: any; @SessionStorage() sessionStorage: any;
@SessionStorage({a: true, b: 'test', c: 3.14}) defaultedSessionStorage: any; @SessionStorage({a: true, b: 'test', c: 3.14}) defaultedSessionStorage: any;
@SessionStorage(null, {key: CUSTOM_KEY}) customSessionStorage: any; @SessionStorage(null, {key: CUSTOM_KEY}) customSessionStorage: any;
@SessionStorage(null, {encryptWith: ENCRYPTION_KEY}) encryptedSessionStorage: any;
@SessionStorage(new TestType('John', 'Smith')) objectSessionStorage!: TestType; @SessionStorage(new TestType('John', 'Smith')) objectSessionStorage!: TestType;
} }
describe('Webstorage Decorators', () => { describe('WebStorage Decorators', () => {
let testComponent: TestStorage; let testComponent: TestStorage;
beforeEach(() => { beforeEach(() => {
localStorage.clear(); localStorage.clear();
@ -67,12 +64,6 @@ describe('Webstorage Decorators', () => {
expect(localStorage.getItem(CUSTOM_KEY)).toBe(JSON.stringify(testValue)); expect(localStorage.getItem(CUSTOM_KEY)).toBe(JSON.stringify(testValue));
expect(testComponent.customLocalStorage).toBe(testValue); expect(testComponent.customLocalStorage).toBe(testValue);
}); });
test('Encrypted', () => {
const testValue = Math.random();
testComponent.encryptedLocalStorage = testValue;
expect(localStorage.getItem('encryptedLocalStorage')).not.toBe(JSON.stringify(testValue));
expect(testComponent.encryptedLocalStorage).toBe(testValue);
});
test('Impure Functions', () => { test('Impure Functions', () => {
testComponent.localStorage = [1]; testComponent.localStorage = [1];
testComponent.localStorage.push(2); testComponent.localStorage.push(2);
@ -128,12 +119,6 @@ describe('Webstorage Decorators', () => {
expect(sessionStorage.getItem(CUSTOM_KEY)).toBe(JSON.stringify(testValue)); expect(sessionStorage.getItem(CUSTOM_KEY)).toBe(JSON.stringify(testValue));
expect(testComponent.customSessionStorage).toBe(testValue); expect(testComponent.customSessionStorage).toBe(testValue);
}); });
test('Encrypted', () => {
const testValue = Math.random();
testComponent.encryptedSessionStorage = testValue;
expect(sessionStorage.getItem('encryptedSessionStorage')).not.toBe(JSON.stringify(testValue));
expect(testComponent.encryptedSessionStorage).toBe(testValue);
});
test('Impure Functions', () => { test('Impure Functions', () => {
testComponent.sessionStorage = [1]; testComponent.sessionStorage = [1];
testComponent.sessionStorage.push(2); testComponent.sessionStorage.push(2);