diff --git a/README.md b/README.md index c408610..566beca 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,6 @@ export class MyCustomClass { | Options | Description | |---------|-------------| | 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) | ## Caveats diff --git a/package.json b/package.json index 51f2488..7fa0a61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webstorage-decorators", - "version": "3.2.6", + "version": "4.0.0", "description": "Decorators to sync class properties to Local/Session storage", "repository": "https://github.com/ztimson/WebstorageDecorators", "main": "./lib/index.js", @@ -21,10 +21,7 @@ ], "author": "Zak Timson", "license": "ISC", - "dependencies": { - "@types/crypto-js": "^4.0.1", - "crypto-js": "^4.0.0" - }, + "dependencies": {}, "devDependencies": { "@types/jest": "^26.0.15", "jest": "^26.6.0", diff --git a/src/webstorage.ts b/src/webstorage.ts index 0c5d77a..db7d07c 100644 --- a/src/webstorage.ts +++ b/src/webstorage.ts @@ -1,13 +1,9 @@ -import * as crypto from 'crypto-js'; - /** * Options to be used with WebStorage decorators */ export interface WebStorageOptions { /** Default value to provide if storage is empty */ default?: any; - /** Key to prevent plain text storage **/ - encryptWith?: string; /** Key to save under */ key?: string; } @@ -69,9 +65,9 @@ function fromStorage(storage: Storage, opts: WebStorageOptions) { return temp; } storedVal = JSON.parse(storedVal); - if(opts.encryptWith != null) storedVal = JSON.parse(crypto.AES.decrypt(storedVal, opts.encryptWith).toString(crypto.enc.Utf8)); 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); } @@ -89,13 +85,12 @@ function decoratorBuilder(storage: Storage, opts: WebStorageOptions) { let field = fromStorage(storage, opts); Object.defineProperty(target, key, { 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; }, set: function(value?) { field = value; if(value == null) storage.removeItem(opts.key); - if(opts.encryptWith != null) value = crypto.AES.encrypt(JSON.stringify(value), opts.encryptWith).toString(); storage.setItem(opts.key, JSON.stringify(value)); } }); diff --git a/tests/webstorage.spec.ts b/tests/webstorage.spec.ts index 6ff7ecf..ca47794 100644 --- a/tests/webstorage.spec.ts +++ b/tests/webstorage.spec.ts @@ -1,7 +1,6 @@ import {LocalStorage, SessionStorage} from "../src"; const CUSTOM_KEY = '_MY_KEY' -const ENCRYPTION_KEY = 'abc123'; class TestType { constructor(public first: string, public last: string) { } @@ -12,16 +11,14 @@ class TestStorage { @LocalStorage() localStorage: any; @LocalStorage({a: true, b: 'test', c: 3.14}) defaultedLocalStorage: any; @LocalStorage(null, {key: CUSTOM_KEY}) customLocalStorage: any; - @LocalStorage(null, {encryptWith: ENCRYPTION_KEY}) encryptedLocalStorage: any; @LocalStorage(new TestType('John', 'Smith')) objectLocalStorage!: TestType; @SessionStorage() sessionStorage: any; @SessionStorage({a: true, b: 'test', c: 3.14}) defaultedSessionStorage: any; @SessionStorage(null, {key: CUSTOM_KEY}) customSessionStorage: any; - @SessionStorage(null, {encryptWith: ENCRYPTION_KEY}) encryptedSessionStorage: any; @SessionStorage(new TestType('John', 'Smith')) objectSessionStorage!: TestType; } -describe('Webstorage Decorators', () => { +describe('WebStorage Decorators', () => { let testComponent: TestStorage; beforeEach(() => { localStorage.clear(); @@ -67,12 +64,6 @@ describe('Webstorage Decorators', () => { expect(localStorage.getItem(CUSTOM_KEY)).toBe(JSON.stringify(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', () => { testComponent.localStorage = [1]; testComponent.localStorage.push(2); @@ -128,12 +119,6 @@ describe('Webstorage Decorators', () => { expect(sessionStorage.getItem(CUSTOM_KEY)).toBe(JSON.stringify(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', () => { testComponent.sessionStorage = [1]; testComponent.sessionStorage.push(2);