From a05559ef0abfa3f6e8a5c6963f209da45b1711c6 Mon Sep 17 00:00:00 2001 From: Neil Francis Ramirez Hipona Date: Wed, 27 Sep 2023 23:34:59 +0800 Subject: [PATCH 1/5] Update package settings typescript --- package.json | 3 ++- tsconfig.json | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f2cf399..80d68a3 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "Web utility storage manager for handling data encryption, save and persist, update and data purge in your local and session storage", "main": "lib/index.js", "type": "commonjs", + "types": "lib/index.d.ts", "directories": { "lib": "lib" }, @@ -11,7 +12,7 @@ "test": "jest", "test:build": "rm -rf lib && npm run build:js && jest", "test:silent-build": "rm -rf lib && npm run build:js && jest --verbose false", - "build": "rm -rf lib && npm run build:js", + "build": "rm -rf lib && npm run build:js && npm run build:types", "build:types": "tsc --emitDeclarationOnly", "build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline", "publish": "npm run build && npm publish" diff --git a/tsconfig.json b/tsconfig.json index a4035d3..627c72d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "noEmit": true, + "noEmit": false, "allowJs": true, "alwaysStrict": true, "module": "ESNext", From 8c5a454a6ae7b7e93288a110a09155d6416c23fb Mon Sep 17 00:00:00 2001 From: Neil Francis Ramirez Hipona Date: Wed, 27 Sep 2023 23:35:41 +0800 Subject: [PATCH 2/5] Update lib --- lib/class/cryptor.d.ts | 11 +++ lib/class/encoded-storage.d.ts | 12 +++ lib/class/encrypted-storage.d.ts | 15 ++++ lib/class/interface.d.ts | 143 +++++++++++++++++++++++++++++++ lib/class/storage.d.ts | 23 +++++ lib/index.d.ts | 4 + package-lock.json | 4 +- 7 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 lib/class/cryptor.d.ts create mode 100644 lib/class/encoded-storage.d.ts create mode 100644 lib/class/encrypted-storage.d.ts create mode 100644 lib/class/interface.d.ts create mode 100644 lib/class/storage.d.ts create mode 100644 lib/index.d.ts diff --git a/lib/class/cryptor.d.ts b/lib/class/cryptor.d.ts new file mode 100644 index 0000000..540ac96 --- /dev/null +++ b/lib/class/cryptor.d.ts @@ -0,0 +1,11 @@ +import { CryptorOption, KeyOption, ReturnOption, CryptorModel, VectorIV } from './interface'; +export declare const CryptorDefaults: CryptorOption; +export declare class Cryptor implements CryptorModel { + #private; + constructor(options?: CryptorOption, ivHex?: VectorIV); + get settings(): CryptorOption; + get key(): KeyOption; + get ivHex(): KeyOption; + encrypt(subject: string): ReturnOption; + decrypt(encrypted: string): ReturnOption; +} diff --git a/lib/class/encoded-storage.d.ts b/lib/class/encoded-storage.d.ts new file mode 100644 index 0000000..2976725 --- /dev/null +++ b/lib/class/encoded-storage.d.ts @@ -0,0 +1,12 @@ +import { KeyPath, StorageValue, Storage } from './interface'; +import { WebStore } from "./storage"; +export declare class EncodedWebStore extends WebStore { + #private; + /** + * + * @param storage Storage interface to be used and initialized. + */ + constructor(storage: Storage, delimiter?: string); + getItem(key: KeyPath): StorageValue; + setItem(key: KeyPath, value: StorageValue): boolean | Error; +} diff --git a/lib/class/encrypted-storage.d.ts b/lib/class/encrypted-storage.d.ts new file mode 100644 index 0000000..49959c8 --- /dev/null +++ b/lib/class/encrypted-storage.d.ts @@ -0,0 +1,15 @@ +import { KeyPath, StorageValue, Storage, EncryptedWebStorage } from './interface'; +import { WebStore } from "./storage"; +import { Cryptor } from "./cryptor"; +export declare class EncryptedWebStore extends WebStore implements EncryptedWebStorage { + #private; + /** + * + * @param storage Storage interface to be used and initialized. + */ + constructor(storage: Storage, cryptor: Cryptor, delimiter?: string); + getItem(key: KeyPath): StorageValue; + setItem(key: KeyPath, value: StorageValue): boolean | Error; + getEncryptedRawItem(key: string): any; + setEncryptedRawItem(key: string, value: any): boolean | Error; +} diff --git a/lib/class/interface.d.ts b/lib/class/interface.d.ts new file mode 100644 index 0000000..a867781 --- /dev/null +++ b/lib/class/interface.d.ts @@ -0,0 +1,143 @@ +/// +export type KeyPath = string; +export type StorageValue = any; +export type StorageItem = { + key: KeyPath; + value: StorageValue; +}; +/** + * Attribute compare will work for a collection of items where values match or will replace a value of the matched key for data objects. + */ +export type AttributeCompare = { + name: string; + value: string | number; +}; +export interface Storage { + /** + * keypath delimeter. defaults to '.'. + */ + delimiter: string; + /** + * Returns an integer representing the number of data items stored in the Storage object. + */ + get length(): number; + /** + * Returns an integer representing the number of data items stored in the Storage object. + * @param n When passed a number n, this method will return the name of the nth key in the storage. + */ + key(n: number): number; + /** + * When passed a key name, will return that key's value. + * @param {KeyPath} key key name. + */ + getItem(key: KeyPath): StorageValue; + /** + * When passed a key name and value, will add that key to the storage, or update that key's value if it already exists. + * @param {KeyPath} key A string containing the name of the key you want to create/update. + * @param {StorageValue} value A string containing the value you want to give the key you are creating/updating. + */ + setItem(key: KeyPath, value: StorageValue): boolean | Error; + /** + * When passed a key name, will remove that key from the storage. + * @param {KeyPath} key A string containing the name of the key you want to remove. + */ + removeItem(key: KeyPath): void; + /** + * When invoked, will empty all keys out of the storage. + */ + clear(): void; +} +export interface WebStorage extends Storage { + /** + * Add multiple entries of key value pairs to the storage. + * @param {StorageItem[]} items Items to add individually in the storage. + */ + setMultipleItems(items: StorageItem[]): boolean | Error; + /** + * Remove multiple entries found in the specified keypaths. + * Will only work on top level keypaths and will not utilize an `AttributeCompare`. + * Use `removeItemInItem` to utilize an `AttributeCompare`. + * @param {KeyPath[]} keys + */ + removeMultipleItems(keys: KeyPath[]): void; + /** + * Returns multiple entries found in the specified keypaths. + * Will only work on top level keypaths and will not utilize an `AttributeCompare`. + * Use `getItemInItem` to utilize an `AttributeCompare`. + * @param {KeyPath[]} keys + */ + getMultipleItems(keys: KeyPath[]): StorageValue[]; + /** + * Append item to an existing item on the storage. Works for object and array type data. + * @param {KeyPath} key keypath of the data you want to append to. + * @param {StorageValue} value data value you want to append to. + */ + appendItemInItem(key: KeyPath, value: StorageValue): boolean | Error; + /** + * Updates an item in the specified keypath. + * @param {KeyPath} key keypath of the data. + * @param {AttributeCompare} attrCompare data key attribute to be updated. + */ + updateItemInItem(key: KeyPath, attrCompare: AttributeCompare | null, newValue: StorageValue): boolean | Error; + /** + * Removes an item in the specified keypath. + * @param {KeyPath} key keypath of the data. + * @param {AttributeCompare} attrCompare data key attribute to be updated. + */ + removeItemInItem(key: KeyPath, attrCompare?: AttributeCompare): boolean | Error; + /** + * Returns data found in the specified keypath. + * @param {KeyPath} key keypath of the data. + * @param {AttributeCompare} attrCompare data key attribute to be updated. + */ + getItemInItem(key: KeyPath, attrCompare?: AttributeCompare): StorageValue; +} +export interface EncryptedWebStorage extends Storage { + /** + * When passed a key name, will return that key's value. + * @param {KeyPath} key key name. + */ + getEncryptedRawItem(key: KeyPath): StorageValue; + /** + * When passed a key name and value, will add that key to the storage, or update that key's value if it already exists. + * @param {KeyPath} key A string containing the name of the key you want to create/update. + * @param {StorageValue} value A string containing the value you want to give the key you are creating/updating. + */ + setEncryptedRawItem(key: KeyPath, value: StorageValue): boolean | Error; +} +/** + * Cryptor interface + */ +export interface CryptorOption { + salt: string | Buffer; + keyLength: number; + algorithm: string; + password: string | Buffer; + byteLength: number; +} +export type KeyOption = string | null; +export type ReturnOption = string | null; +export type VectorIV = string | null; +export interface CryptorModel { + /** + * Returns the cryptor's configs + */ + get settings(): CryptorOption; + /** + * Returns the cryptor's generated encryption key + */ + get key(): KeyOption; + /** + * Returns the cryptor's initialization vector. + * NOTE: This is important for decryption. Make sure you store it somewhere for reuse. + */ + get ivHex(): KeyOption; + /** + * Encrypt the data and save to storage. + */ + encrypt(subject: string): ReturnOption; + /** + * Returns the decrypted data from storage. + */ + decrypt(encrypted: string): ReturnOption; +} diff --git a/lib/class/storage.d.ts b/lib/class/storage.d.ts new file mode 100644 index 0000000..d3c1632 --- /dev/null +++ b/lib/class/storage.d.ts @@ -0,0 +1,23 @@ +import { AttributeCompare, KeyPath, Storage, StorageItem, StorageValue, WebStorage } from './interface'; +export declare class WebStore implements WebStorage { + #private; + delimiter: string; + /** + * + * @param storage Storage interface to be used and initialized. + */ + constructor(storage: Storage, delimiter?: string); + get length(): number; + key(n: number): number; + getItem(key: KeyPath): StorageValue; + setItem(key: KeyPath, value: StorageValue): boolean | Error; + removeItem(key: KeyPath): void; + clear(): void; + setMultipleItems(items: StorageItem[]): boolean | Error; + removeMultipleItems(keys: KeyPath[]): void; + getMultipleItems(keys: KeyPath[]): StorageValue[]; + appendItemInItem(key: KeyPath, value: any): boolean | Error; + updateItemInItem(key: KeyPath, attrCompare: AttributeCompare | null, newValue: StorageValue): boolean | Error; + removeItemInItem(key: KeyPath, attrCompare?: AttributeCompare): boolean | Error; + getItemInItem(key: KeyPath, attrCompare?: AttributeCompare): StorageValue; +} diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..bcecf8a --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,4 @@ +export interface Options { + delimiter?: string; + isEncoded: boolean; +} diff --git a/package-lock.json b/package-lock.json index e99067a..46a71bb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "web-storage-manager", - "version": "4.0.3", + "version": "4.0.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "web-storage-manager", - "version": "4.0.3", + "version": "4.0.4", "license": "MIT", "devDependencies": { "@babel/cli": "^7.22.5", From 5e485a6675e6295e6a0ce6066856b5474ffd9169 Mon Sep 17 00:00:00 2001 From: Neil Francis Ramirez Hipona Date: Wed, 27 Sep 2023 23:36:15 +0800 Subject: [PATCH 3/5] Update package version to 4.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 80d68a3..d74279f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "web-storage-manager", - "version": "4.0.4", + "version": "4.1.0", "description": "Web utility storage manager for handling data encryption, save and persist, update and data purge in your local and session storage", "main": "lib/index.js", "type": "commonjs", From 27ebb7503d732e31d5cb57cc2fd864667222256a Mon Sep 17 00:00:00 2001 From: Neil Francis Ramirez Hipona Date: Wed, 27 Sep 2023 23:53:19 +0800 Subject: [PATCH 4/5] Update scripts --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d74279f..d936237 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ }, "scripts": { "test": "jest", - "test:build": "rm -rf lib && npm run build:js && jest", + "test:build": "rm -rf lib && npm run build && jest", "test:silent-build": "rm -rf lib && npm run build:js && jest --verbose false", "build": "rm -rf lib && npm run build:js && npm run build:types", "build:types": "tsc --emitDeclarationOnly", From 0d24068c2382e17cdf5f1af84a72f7e5b1cee0ef Mon Sep 17 00:00:00 2001 From: Neil Francis Ramirez Hipona Date: Wed, 27 Sep 2023 23:53:49 +0800 Subject: [PATCH 5/5] Update test run scripts --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d936237..93e8e6e 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "scripts": { "test": "jest", "test:build": "rm -rf lib && npm run build && jest", - "test:silent-build": "rm -rf lib && npm run build:js && jest --verbose false", + "test:silent-build": "rm -rf lib && npm run build && jest --verbose false", "build": "rm -rf lib && npm run build:js && npm run build:types", "build:types": "tsc --emitDeclarationOnly", "build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline",