From e21c8a23de7cfb14cc39644eff1193ce7e28bd04 Mon Sep 17 00:00:00 2001 From: ztimson Date: Thu, 3 May 2018 15:07:59 -0400 Subject: [PATCH] Init --- .gitignore | 45 +++++++++++++++++++++++++++++++++++++++++ README.md | 1 + index.ts | 1 + lib/index.d.ts | 1 + lib/index.js | 6 ++++++ lib/src/index.d.ts | 1 + lib/src/index.js | 6 ++++++ lib/src/webStorage.d.ts | 7 +++++++ lib/src/webStorage.js | 32 +++++++++++++++++++++++++++++ package-lock.json | 24 ++++++++++++++++++++++ package.json | 28 +++++++++++++++++++++++++ src/index.ts | 1 + src/webStorage.ts | 34 +++++++++++++++++++++++++++++++ tsconfig.json | 13 ++++++++++++ 14 files changed, 200 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 index.ts create mode 100644 lib/index.d.ts create mode 100644 lib/index.js create mode 100644 lib/src/index.d.ts create mode 100644 lib/src/index.js create mode 100644 lib/src/webStorage.d.ts create mode 100644 lib/src/webStorage.js create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/index.ts create mode 100644 src/webStorage.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ec4bbd --- /dev/null +++ b/.gitignore @@ -0,0 +1,45 @@ +# See http://help.github.com/ignore-files/ for more about ignoring files. + +# compiled output +/tmp +/out-tsc + +# dependencies +/node_modules + +# IDEs and editors +/.idea +.project +.classpath +.c9/ +*.launch +.settings/ +*.sublime-workspace + +# IDE - VSCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json + +# misc +/.sass-cache +/connect.lock +/coverage +/libpeerconnection.log +npm-debug.log +testem.log +/typings + +# e2e +/e2e/*.js +/e2e/*.map + +# System Files +.DS_Store +Thumbs.db + +/dist +dist.tgz +/.ng_pkg_build diff --git a/README.md b/README.md new file mode 100644 index 0000000..f26be45 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# WebStorage diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..f3c80e0 --- /dev/null +++ b/index.ts @@ -0,0 +1 @@ +export * from './src/index'; \ No newline at end of file diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..cba1843 --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1 @@ +export * from './src/index'; diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..f43f341 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,6 @@ +"use strict"; +function __export(m) { + for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; +} +Object.defineProperty(exports, "__esModule", { value: true }); +__export(require("./src/index")); diff --git a/lib/src/index.d.ts b/lib/src/index.d.ts new file mode 100644 index 0000000..0340438 --- /dev/null +++ b/lib/src/index.d.ts @@ -0,0 +1 @@ +export * from './webStorage'; diff --git a/lib/src/index.js b/lib/src/index.js new file mode 100644 index 0000000..bf61eab --- /dev/null +++ b/lib/src/index.js @@ -0,0 +1,6 @@ +"use strict"; +function __export(m) { + for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; +} +Object.defineProperty(exports, "__esModule", { value: true }); +__export(require("./webStorage")); diff --git a/lib/src/webStorage.d.ts b/lib/src/webStorage.d.ts new file mode 100644 index 0000000..895c367 --- /dev/null +++ b/lib/src/webStorage.d.ts @@ -0,0 +1,7 @@ +export interface WebStorageOptions { + fieldName?: string; + encryptionKey?: string; + defaultValue?: any; +} +export declare function LocalStorage(opts?: WebStorageOptions): (target: object, key: string) => void; +export declare function SessionStorage(opts?: WebStorageOptions): (target: object, key: string) => void; diff --git a/lib/src/webStorage.js b/lib/src/webStorage.js new file mode 100644 index 0000000..4b1925d --- /dev/null +++ b/lib/src/webStorage.js @@ -0,0 +1,32 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const crypto_js_1 = require("crypto-js"); +function LocalStorage(opts = {}) { + return storage(localStorage, opts); +} +exports.LocalStorage = LocalStorage; +function SessionStorage(opts = {}) { + return storage(sessionStorage, opts); +} +exports.SessionStorage = SessionStorage; +function storage(storageType, opts = {}) { + return function (target, key) { + if (!opts.fieldName) + opts.fieldName = key; + Object.defineProperty(target, key, { + get: function () { + let value = storageType.getItem(opts.fieldName); + if (!value && opts.defaultValue != null) + return opts.defaultValue; + if (value != null && opts.encryptionKey) + value = crypto_js_1.AES.decrypt(JSON.parse(value), opts.encryptionKey).toString(crypto_js_1.enc.Utf8); + return JSON.parse(value); + }, + set: function (value) { + if (value != null && opts.encryptionKey) + value = crypto_js_1.AES.encrypt(JSON.stringify(value), opts.encryptionKey).toString(); + storageType.setItem(opts.fieldName, JSON.stringify(value)); + } + }); + }; +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..b08f515 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,24 @@ +{ + "name": "WebStorage", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@types/crypto-js": { + "version": "3.1.39", + "resolved": "https://registry.npmjs.org/@types/crypto-js/-/crypto-js-3.1.39.tgz", + "integrity": "sha512-LNPf60hAYUnjqeUwWxFrJwjPwOFIwQNy/xdVRQMWnsly38M23lLjhG3osdAJQ74jOplIUCjOhibpqOos0l0FHA==" + }, + "crypto-js": { + "version": "3.1.9-1", + "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-3.1.9-1.tgz", + "integrity": "sha1-/aGedh/Ad+Af+/3G6f38WeiAbNg=" + }, + "typescript": { + "version": "2.8.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.8.3.tgz", + "integrity": "sha512-K7g15Bb6Ra4lKf7Iq2l/I5/En+hLIHmxWZGq3D4DIRNFxMNV6j2SHSvDOqs2tGd4UvD/fJvrwopzQXjLrT7Itw==", + "dev": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..668368f --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "webstorage-decorators", + "version": "1.0.0", + "description": "Decorators to sync variable to Local/Session storage", + "main": "./lib/index.js", + "typings": "./lib/index.d.ts", + "scripts": { + "prepare": "npm run build", + "build": "tsc" + }, + "files": [ + "lib" + ], + "keywords": [ + "LocalStorage", + "SessionStorage", + "WebStorage" + ], + "author": "Zak Timson", + "license": "ISC", + "dependencies": { + "@types/crypto-js": "^3.1.39", + "crypto-js": "^3.1.9-1" + }, + "devDependencies": { + "typescript": "^2.8.3" + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..77f64c3 --- /dev/null +++ b/src/index.ts @@ -0,0 +1 @@ +export * from './webStorage'; \ No newline at end of file diff --git a/src/webStorage.ts b/src/webStorage.ts new file mode 100644 index 0000000..6c2266d --- /dev/null +++ b/src/webStorage.ts @@ -0,0 +1,34 @@ +import {AES, enc} from 'crypto-js'; + +export interface WebStorageOptions { + fieldName?: string; + encryptionKey?: string; + defaultValue?: any; +} + +export function LocalStorage(opts: WebStorageOptions = {}) { + return storage(localStorage, opts); +} + +export function SessionStorage(opts: WebStorageOptions = {}) { + return storage(sessionStorage, opts); +} + +function storage(storageType: Storage, opts: WebStorageOptions = {}) { + return function(target: object, key: string) { + if(!opts.fieldName) opts.fieldName = key; + + Object.defineProperty(target, key, { + get: function() { + let value = storageType.getItem(opts.fieldName); + if(!value && opts.defaultValue != null) return opts.defaultValue; + if(value != null && opts.encryptionKey) value = AES.decrypt(JSON.parse(value), opts.encryptionKey).toString(enc.Utf8); + return JSON.parse(value); + }, + set: function(value) { + if(value != null && opts.encryptionKey) value = AES.encrypt(JSON.stringify(value), opts.encryptionKey).toString(); + storageType.setItem(opts.fieldName, JSON.stringify(value)); + } + }); + }; +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..f790c0a --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "ES2015", + "module": "commonjs", + "declaration": true, + "outDir": "lib", + "strict": true + }, + "include": [ + "index.ts", + "src/**/*" + ] +}