vault-storage
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -1,1 +0,1 @@ | ||
var l={get(r,e){return typeof r[e]=="function"?r[e].bind(r):e in r?r[e]:r.getItem(e)},set(r,e,t){return r.setItem(e,t)},deleteProperty(r,e){return r.removeItem(e)}},c=l;var i="readonly",a="readwrite",d="store",o=class{dbName="vault";db=null;constructor(e,t=!1){if(this.dbName=e||this.dbName,!t)return new Proxy(this,c)}async setItem(e,t){return this.do(a,n=>n.put({key:e,value:t}))}async getItem(e){return this.do(i,t=>t.get(e)).then(t=>t?.value??null)}async removeItem(e){return this.do(a,t=>t.delete(e))}async clear(){return this.do(a,e=>e.clear())}async keys(){return this.do(i,e=>e.getAllKeys())}async length(){return this.do(i,e=>e.count())}async init(){return new Promise((e,t)=>{let n=indexedDB.open(this.dbName,1);n.onupgradeneeded=s=>{s.target.result.createObjectStore(d,{keyPath:"key"})},n.onsuccess=()=>{this.db=n.result,e()},n.onerror=s=>t(s)})}async do(e,t){this.db||await this.init();let n=this.db.transaction(d,e),s=t(n.objectStore(d));return new Promise((u,y)=>{s.onsuccess=()=>u(e===i?s.result:void 0),s.onerror=()=>y(s.error)})}};var m=new o,g=m;export{g as default}; | ||
var y={get(r,e){return typeof r[e]=="function"?r[e].bind(r):e in r?r[e]:r.getItem(e)},set(r,e,t){return r.setItem(e,t)},deleteProperty(r,e){return r.removeItem(e)}},d=y;var a="readonly",i="readwrite",c="store",o=class{storageName="vault-storage";db=null;constructor(e,t=!1){if(this.storageName=e||this.storageName,!t)return new Proxy(this,d)}async setItem(e,t){return this.do(i,n=>n.put({key:e,value:t}))}async getItem(e){return this.do(a,t=>t.get(e)).then(t=>t?.value??null)}async removeItem(e){return this.do(i,t=>t.delete(e))}async clear(){return this.do(i,e=>e.clear())}async keys(){return this.do(a,e=>e.getAllKeys())}async length(){return this.do(a,e=>e.count())}async init(){return new Promise((e,t)=>{let n=indexedDB.open(this.storageName,1);n.onupgradeneeded=s=>{s.target.result.createObjectStore(c,{keyPath:"key"})},n.onsuccess=()=>{this.db=n.result,e()},n.onerror=s=>t(s)})}async do(e,t){this.db||await this.init();let n=this.db.transaction(c,e),s=t(n.objectStore(c));return new Promise((u,l)=>{s.onsuccess=()=>u(e===a?s.result:void 0),s.onerror=()=>l(s.error)})}};var m=new o,b=m;export{b as default}; |
{ | ||
"name": "vault-storage", | ||
"description": "Vault, a micro yet robust browser storage library", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"author": "ManiarTech®️ - Mohamed Aamir Maniar", | ||
@@ -40,14 +40,16 @@ "license": "MIT", | ||
"./vault": "./dist/vault.js", | ||
"./secured-vault": "./dist/secured-vault.js", | ||
"./types": "./types/index.d.ts", | ||
"./types/vault": "./types/vault.d.ts", | ||
"./types/secured-vault": "./types/secured-vault.d.ts" | ||
"./secured-vault": "./dist/secured-vault.js" | ||
}, | ||
"files": [ | ||
"dist", | ||
"types" | ||
], | ||
"scripts": { | ||
"test": "karma start karma.conf.js", | ||
"clean": "rimraf dist types", | ||
"clean": "node clean.js", | ||
"build:types": "tsc", | ||
"build:vault": "node ./build.js && npm run build:types", | ||
"build": "npm run clean && npm run build:vault && npm run build:types", | ||
"watch": "node ./build.js --watch --debug" | ||
"watch": "node ./build.js --watch --debug", | ||
"prepack": "npm run build" | ||
}, | ||
@@ -66,3 +68,2 @@ "devDependencies": { | ||
"karma-typescript": "^5.5.4", | ||
"rimraf": "^5.0.5", | ||
"ts-node": "^10.9.2", | ||
@@ -69,0 +70,0 @@ "typescript": "^5.3.3" |
@@ -36,3 +36,6 @@ # vault | ||
First, import `vault-storage` in your project: | ||
First, import the `vault` from `vault-storage`. The `vault` is a default instance | ||
of the `Vault` storage class and hence does not need any special initialization | ||
or setup!!! The `vault` provides a ready to use instance similar to localStorage | ||
and sessionStorage. You can start using it right away without any setup. | ||
@@ -45,6 +48,4 @@ ```javascript | ||
By default, the `vault` does not need any special initialization or setup!!! | ||
In this way, it behaves similar to the local and session storages, It uses | ||
default database and store names. | ||
> **Just start using it!** | ||
@@ -64,4 +65,6 @@ | ||
You can also use a custom database name and store name. This is useful when you | ||
want to use multiple databases or stores. | ||
You can also create a custom storage. This is useful when you want to use | ||
multiple storages for different purposes. All the custom storage also share the | ||
same API as the default vault storage and other built-in storages like | ||
localStorage and sessionStorage. | ||
@@ -72,12 +75,17 @@ ```javascript | ||
const myStorage = new Vault("my-storage") | ||
myStorage.setItem("key", "value") | ||
console.log("key", await myStorage.getItem("key")) | ||
const appStorage = new Vault("app-storage") | ||
appStorage.setItem("key", "value") | ||
console.log("key", await appStorage.getItem("key")) | ||
const userStorage = new Vault("user-storage") | ||
userStorage.setItem("key", "value") | ||
``` | ||
### Custom Secure Database | ||
### Secure Storage | ||
Secured databases are useful when you want to store sensitive data. It provides | ||
similar API to the `vault` but it encrypts the data before storing it in the | ||
database. It uses browser's native crypto API to encrypt the data. | ||
Secured storages are useful when you want to store sensitive data. It shares | ||
the same API but it encrypts the data before storing it in the | ||
storage. It uses browser's native crypto API to encrypt the data. The secured | ||
storage can be created using a fixed credentials or dynamic credentials (credentials | ||
that are generated based on the key). | ||
@@ -87,20 +95,22 @@ ```javascript | ||
// Secured storage using fixed password and salt. | ||
const securedStorage1 = new SecuredVault("secured-storage", { | ||
password: "my-password", | ||
salt: "my-salt", | ||
// Secured storage using fixed credentials (password and salt). | ||
const authStorage = new SecuredVault("secured-storage", { | ||
password: "SADF@#$W$ERWESD", | ||
salt: "SDF@#$%SERWESD", | ||
}); | ||
// Secured storage using dynamic password and salt. | ||
const securedStorage2 = new SecuredVault("secured-storage", (key) => { | ||
const password = key.startsWith("key1") ? "my-password1" : "my-password2"; | ||
const salt = key.startsWith("key1") ? "my-salt1" : "my-salt2"; | ||
authStorage.token = "my-token" | ||
console.log("token", await authStorage.token) | ||
// Secured storage using dynamic credentials. | ||
const securedStorage = new SecuredVault("secured-storage", (key) => { | ||
const password = key === "token" ? "ASF@#$%QER()SDF" : "SXDFW#$%@#SDF"; | ||
const salt = key.startsWith("key1") ? "xxx@xxxxxxxxxx" : "yyy@yyyyyyyyyy"; | ||
return { password, salt }; | ||
}); | ||
// Secured storage using promise based password and salt. | ||
const securedStorage3 = new SecuredVault("secured-storage", async (key) => { | ||
// Secured storage using promise based dynamic credentials. | ||
const sensitiveStorage = new SecuredVault("secured-storage", async (key) => { | ||
return new Promise(async (resolve) => { | ||
const res = await fetch("/get-key") | ||
const { password, salt } = generatePasswordFromKey(res.key) | ||
const { password, salt } = await fetchOrGenerateCredentialsFor(key) | ||
resolve({ password, salt }) | ||
@@ -110,12 +120,14 @@ }); | ||
// Once the secured valued is setup, usage is similar to the regular vault storage. | ||
// Just start using it! | ||
// Set the values. It stores the encrypted Uint8Array in the database | ||
// Set the values. It stores the encrypted Uint8Array in the storage | ||
// against the key. If you want to immediately use the value, then | ||
// you must use await while setting the value. | ||
await securedStorage1.setItem("key1", "value1"); | ||
await authStorage.setItem("token", "eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZSIsImV4cCI6MTcwNzA2NzgwMywiaWF0IjoxNzA3MDY3ODAzfQ.XmPqTUN3KJeEArX58xVfHIQGGtm291p9ZamBvrflCMo") | ||
// Get the values. Remember to use await! As it's asynchronous. | ||
const value1 = await securedStorage1.key1; // "value1" | ||
const token = await authStorage.token; // Decrypted token from the authStorage | ||
// and returns the original token. | ||
``` | ||
@@ -190,7 +202,7 @@ | ||
- `setItem(key: string, value: any)`: Store data in the database. | ||
- `getItem(key: string)`: Retrieve data from the database. | ||
- `removeItem(key: string)`: Remove data from the database. | ||
- `clear()`: Clear all data from the database. | ||
- `length()`: Get the count of entries in the database. | ||
- `setItem(key: string, value: any)`: Store data in the storage. | ||
- `getItem(key: string)`: Retrieve data from the storage. | ||
- `removeItem(key: string)`: Remove data from the storage. | ||
- `clear()`: Clear all data from the storage. | ||
- `length()`: Get the count of entries in the storage. | ||
@@ -197,0 +209,0 @@ ## Comparing Vault with LocalStorage |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
13
248
0
21463
11
163