Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vault-storage

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vault-storage - npm Package Compare versions

Comparing version 1.2.2 to 1.2.3

dist/index.js.map

124

dist/index.js

@@ -1,1 +0,123 @@

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 o="readonly",i="readwrite",u="store",a=class{storageName="vault-storage";db=null;constructor(e,t=!1){if(this.storageName=e||this.storageName,!t)return new Proxy(this,c)}async setItem(e,t,s=null){return this.do(i,n=>n.put({key:e,value:t,meta:s}))}async getItem(e){return this.do(o,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(o,e=>e.getAllKeys())}async length(){return this.do(o,e=>e.count())}async init(){return new Promise((e,t)=>{let s=indexedDB.open(this.storageName,1);s.onupgradeneeded=n=>{n.target.result.createObjectStore(u,{keyPath:"key"})},s.onsuccess=()=>{this.db=s.result,e()},s.onerror=n=>t(n)})}async getItemMeta(e){return this.do(o,t=>t.get(e)).then(t=>t?.meta??null)}async do(e,t){this.db||await this.init();let s=this.db.transaction(u,e),n=t(s.objectStore(u));return new Promise((d,y)=>{n.onsuccess=()=>d(e===o?n.result:void 0),n.onerror=()=>y(n.error)})}};var m=new a,b=m;export{b as default};
// src/proxy-handler.ts
var proxyHandler = {
get(target, key) {
return typeof target[key] === "function" ? target[key].bind(target) : key in target ? target[key] : target.getItem(key);
},
set(target, key, value) {
return target.setItem(key, value);
},
deleteProperty(target, key) {
return target.removeItem(key);
}
};
var proxy_handler_default = proxyHandler;
// src/vault.ts
var r = "readonly";
var rw = "readwrite";
var s = "store";
var Vault = class {
storageName = "vault-storage";
db = null;
/**
* Creates new custom instance of custom Vault Storage.
* @param {string} [storageName] - The name of the storage.
* @param {boolean} [isParent=false] - A flag to indicate if this instance
* is a parent. This property should be ignored by the user unless they are
* extending the Vault class.
*/
constructor(storageName, isParent = false) {
this.storageName = storageName || this.storageName;
if (!isParent)
return new Proxy(this, proxy_handler_default);
}
/**
* Set an item in the database with additional metadata.
* @param {string} key - The key of the item.
* @param {any} value - The value of the item.
* @param {any} meta - The metadata for the item (e.g., ttl, expiration) or
* any other data that should be stored alongside the value.
* @returns {Promise<void>}
*/
async setItem(key, value, meta = null) {
return this.do(rw, (s2) => s2.put({ key, value, meta }));
}
/**
* Get an item from the database.
* @param {string} key - The key of the item.
* @returns {Promise<any>} - The value of the item.
*/
async getItem(key) {
return this.do(r, (s2) => s2.get(key)).then((r2) => r2?.value ?? null);
}
/**
* Remove an item from the database.
* @param {string} key - The key of the item.
* @returns {Promise<void>}
*/
async removeItem(key) {
return this.do(rw, (s2) => s2.delete(key));
}
/**
* Clear the database.
* @returns {Promise<void>}
*/
async clear() {
return this.do(rw, (s2) => s2.clear());
}
/**
* Get all keys in the database.
* @returns {Promise<string[]>} - An array of keys.
*/
async keys() {
return this.do(r, (s2) => s2.getAllKeys());
}
/**
* Get the number of items in the database.
* @returns {Promise<number>} - The number of items.
*/
async length() {
return this.do(r, (s2) => s2.count());
}
// Initialize the database and return a promise.
async init() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.storageName, 1);
request.onupgradeneeded = (e) => {
e.target.result.createObjectStore(s, { keyPath: "key" });
};
request.onsuccess = () => {
this.db = request.result;
resolve();
};
request.onerror = (e) => reject(e);
});
}
/**
* Get an item's metadata from the database.
* @param {string} key - The key of the item.
* @returns {Promise<any>} - The metadata of the item.
*/
async getItemMeta(key) {
return this.do(r, (s2) => s2.get(key)).then((r2) => r2?.meta ?? null);
}
// Execute a transaction and return a promise.
async do(operationType, operation) {
if (!this.db)
await this.init();
const transaction = this.db.transaction(s, operationType);
const request = operation(transaction.objectStore(s));
return new Promise((resolve, reject) => {
request.onsuccess = () => resolve(operationType === r ? request.result : void 0);
request.onerror = () => reject(request.error);
});
}
};
// src/index.ts
var vault = new Vault();
var src_default = vault;
export {
src_default as default
};
//# sourceMappingURL=index.js.map

@@ -1,1 +0,17 @@

var t={get(n,e){return typeof n[e]=="function"?n[e].bind(n):e in n?n[e]:n.getItem(e)},set(n,e,r){return n.setItem(e,r)},deleteProperty(n,e){return n.removeItem(e)}},o=t;export{o as default};
// src/proxy-handler.ts
var proxyHandler = {
get(target, key) {
return typeof target[key] === "function" ? target[key].bind(target) : key in target ? target[key] : target.getItem(key);
},
set(target, key, value) {
return target.setItem(key, value);
},
deleteProperty(target, key) {
return target.removeItem(key);
}
};
var proxy_handler_default = proxyHandler;
export {
proxy_handler_default as default
};
//# sourceMappingURL=proxy-handler.js.map

@@ -1,1 +0,254 @@

var m={get(s,e){return typeof s[e]=="function"?s[e].bind(s):e in s?s[e]:s.getItem(e)},set(s,e,t){return s.setItem(e,t)},deleteProperty(s,e){return s.removeItem(e)}},c=m;var i="readonly",y="readwrite",d="store",a=class{storageName="vault-storage";db=null;constructor(e,t=!1){if(this.storageName=e||this.storageName,!t)return new Proxy(this,c)}async setItem(e,t,r=null){return this.do(y,n=>n.put({key:e,value:t,meta:r}))}async getItem(e){return this.do(i,t=>t.get(e)).then(t=>t?.value??null)}async removeItem(e){return this.do(y,t=>t.delete(e))}async clear(){return this.do(y,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 r=indexedDB.open(this.storageName,1);r.onupgradeneeded=n=>{n.target.result.createObjectStore(d,{keyPath:"key"})},r.onsuccess=()=>{this.db=r.result,e()},r.onerror=n=>t(n)})}async getItemMeta(e){return this.do(i,t=>t.get(e)).then(t=>t?.meta??null)}async do(e,t){this.db||await this.init();let r=this.db.transaction(d,e),n=t(r.objectStore(d));return new Promise((o,g)=>{n.onsuccess=()=>o(e===i?n.result:void 0),n.onerror=()=>g(n.error)})}};var u=class extends a{encConfig;keyCache=new Map;constructor(e,t){return super(e,!0),this.encConfig=t,new Proxy(this,c)}async setItem(e,t,r=null){if(this.encConfig===null||t===null||t===void 0)return super.setItem(e,t,r);let n=await this.getKey(e),o=await f(n,typeof t=="string"?t:JSON.stringify(t));return super.setItem(e,o,r)}async getItem(e){let t=await super.getItem(e);return t===null?null:this.encConfig===null?t:w(await this.getKey(e),t)}getKey=async e=>{if(this.keyCache.has(e))return this.keyCache.get(e);if(typeof this.encConfig=="function"){let r=await this.encConfig(e);return await p(r.password,await l(r.salt))}let t=await p(this.encConfig.password,await l(this.encConfig.salt));return this.keyCache.set(e,t),t}};async function l(s){let t=new TextEncoder().encode(s),r=await crypto.subtle.digest("SHA-256",t);return new Uint8Array(r)}async function p(s,e){let t=new TextEncoder().encode(s),r=await window.crypto.subtle.importKey("raw",t,{name:"PBKDF2"},!1,["deriveKey"]),n={name:"PBKDF2",salt:e,iterations:1e5,hash:"SHA-256"},o={name:"AES-GCM",length:256};return await window.crypto.subtle.deriveKey(n,r,o,!1,["encrypt","decrypt"])}async function f(s,e){let t=window.crypto.getRandomValues(new Uint8Array(12)),r=new TextEncoder().encode(e);try{let n=await window.crypto.subtle.encrypt({name:"AES-GCM",iv:t},s,r);return new Uint8Array([...t,...new Uint8Array(n)]).buffer}catch(n){throw console.error("Encryption failed:",n),n}}async function w(s,e){let t=e.slice(0,12),r=e.slice(12);try{let n=await window.crypto.subtle.decrypt({name:"AES-GCM",iv:new Uint8Array(t)},s,r);return new TextDecoder().decode(n)}catch(n){throw console.error("Decryption failed:",n),n}}var A=u;export{A as default};
// src/proxy-handler.ts
var proxyHandler = {
get(target, key) {
return typeof target[key] === "function" ? target[key].bind(target) : key in target ? target[key] : target.getItem(key);
},
set(target, key, value) {
return target.setItem(key, value);
},
deleteProperty(target, key) {
return target.removeItem(key);
}
};
var proxy_handler_default = proxyHandler;
// src/vault.ts
var r = "readonly";
var rw = "readwrite";
var s = "store";
var Vault = class {
storageName = "vault-storage";
db = null;
/**
* Creates new custom instance of custom Vault Storage.
* @param {string} [storageName] - The name of the storage.
* @param {boolean} [isParent=false] - A flag to indicate if this instance
* is a parent. This property should be ignored by the user unless they are
* extending the Vault class.
*/
constructor(storageName, isParent = false) {
this.storageName = storageName || this.storageName;
if (!isParent)
return new Proxy(this, proxy_handler_default);
}
/**
* Set an item in the database with additional metadata.
* @param {string} key - The key of the item.
* @param {any} value - The value of the item.
* @param {any} meta - The metadata for the item (e.g., ttl, expiration) or
* any other data that should be stored alongside the value.
* @returns {Promise<void>}
*/
async setItem(key, value, meta = null) {
return this.do(rw, (s2) => s2.put({ key, value, meta }));
}
/**
* Get an item from the database.
* @param {string} key - The key of the item.
* @returns {Promise<any>} - The value of the item.
*/
async getItem(key) {
return this.do(r, (s2) => s2.get(key)).then((r2) => r2?.value ?? null);
}
/**
* Remove an item from the database.
* @param {string} key - The key of the item.
* @returns {Promise<void>}
*/
async removeItem(key) {
return this.do(rw, (s2) => s2.delete(key));
}
/**
* Clear the database.
* @returns {Promise<void>}
*/
async clear() {
return this.do(rw, (s2) => s2.clear());
}
/**
* Get all keys in the database.
* @returns {Promise<string[]>} - An array of keys.
*/
async keys() {
return this.do(r, (s2) => s2.getAllKeys());
}
/**
* Get the number of items in the database.
* @returns {Promise<number>} - The number of items.
*/
async length() {
return this.do(r, (s2) => s2.count());
}
// Initialize the database and return a promise.
async init() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.storageName, 1);
request.onupgradeneeded = (e) => {
e.target.result.createObjectStore(s, { keyPath: "key" });
};
request.onsuccess = () => {
this.db = request.result;
resolve();
};
request.onerror = (e) => reject(e);
});
}
/**
* Get an item's metadata from the database.
* @param {string} key - The key of the item.
* @returns {Promise<any>} - The metadata of the item.
*/
async getItemMeta(key) {
return this.do(r, (s2) => s2.get(key)).then((r2) => r2?.meta ?? null);
}
// Execute a transaction and return a promise.
async do(operationType, operation) {
if (!this.db)
await this.init();
const transaction = this.db.transaction(s, operationType);
const request = operation(transaction.objectStore(s));
return new Promise((resolve, reject) => {
request.onsuccess = () => resolve(operationType === r ? request.result : void 0);
request.onerror = () => reject(request.error);
});
}
};
// src/secured-vault.ts
var SecuredVault = class extends Vault {
encConfig;
keyCache = /* @__PURE__ */ new Map();
/**
* Constructs a new instance of the SecuredVault class.
* @constructor
* @param {string} dbName - The name of the database.
* @param {EncConfig} encConfig - The encrypted configuration.
*/
constructor(dbName, encConfig) {
super(dbName, true);
this.encConfig = encConfig;
return new Proxy(this, proxy_handler_default);
}
/**
* Asynchronously sets the value of the specified key in the vault.
* If the encryption configuration is not null and the value is not null or
* undefined, the value is encrypted before being stored.
*
* @async
* @param {string} key - The key of the item to set.
* @param {any} value - The value of the item to set.
* @param {any} meta - The metadata for the item (e.g., ttl, expiration) or
* any other data that should be stored alongside the value.
* @returns {Promise<void>} A Promise that resolves when the value has been set.
*/
async setItem(key, value, meta = null) {
if (this.encConfig === null || value === null || value === void 0) {
return super.setItem(key, value, meta);
}
const encKey = await this.getKey(key);
const encValue = await encrypt(encKey, typeof value === "string" ? value : JSON.stringify(value));
return super.setItem(key, encValue, meta);
}
/**
* Asynchronously gets the value of the specified key from the vault.
* If the encryption configuration is not null and the retrieved value is not
* null, the value is decrypted before being returned.
*
* @async
* @param {string} key - The key of the item to get.
* @returns {Promise<any>} A Promise that resolves to the value of the specified key. If the key does not exist, the Promise resolves to null.
*/
async getItem(key) {
const encValue = await super.getItem(key);
if (encValue === null)
return null;
if (this.encConfig === null)
return encValue;
return decrypt(await this.getKey(key), encValue);
}
// Asynchronously gets or generates the encryption key for the specified key.
getKey = async (key) => {
if (this.keyCache.has(key))
return this.keyCache.get(key);
if (typeof this.encConfig === "function") {
const encKeySalt = await this.encConfig(key);
return await generateKey(encKeySalt.password, await generateSalt(encKeySalt.salt));
}
const encKey = await generateKey(this.encConfig.password, await generateSalt(this.encConfig.salt));
this.keyCache.set(key, encKey);
return encKey;
};
};
async function generateSalt(userInput) {
const encoder = new TextEncoder();
const encodedInput = encoder.encode(userInput);
const hashBuffer = await crypto.subtle.digest("SHA-256", encodedInput);
const salt = new Uint8Array(hashBuffer);
return salt;
}
async function generateKey(password, salt) {
const passwordBuffer = new TextEncoder().encode(password);
const importedKey = await window.crypto.subtle.importKey(
"raw",
passwordBuffer,
{ name: "PBKDF2" },
false,
["deriveKey"]
);
const keyDerivationAlgorithm = {
name: "PBKDF2",
salt,
iterations: 1e5,
// A high number of iterations for security
hash: "SHA-256"
// The hash function to use
};
const derivedKeyAlgorithm = {
name: "AES-GCM",
// Algorithm for the derived key
length: 256
// Length of the derived key
};
return await window.crypto.subtle.deriveKey(
keyDerivationAlgorithm,
importedKey,
derivedKeyAlgorithm,
false,
// Whether the derived key is extractable
["encrypt", "decrypt"]
// The usage of the derived key
);
}
async function encrypt(key, data) {
const iv = window.crypto.getRandomValues(new Uint8Array(12));
const encodedData = new TextEncoder().encode(data);
try {
const encryptedData = await window.crypto.subtle.encrypt({
name: "AES-GCM",
iv
}, key, encodedData);
return new Uint8Array([...iv, ...new Uint8Array(encryptedData)]).buffer;
} catch (error) {
console.error("Encryption failed:", error);
throw error;
}
}
async function decrypt(key, encryptedData) {
const iv = encryptedData.slice(0, 12);
const data = encryptedData.slice(12);
try {
const decryptedData = await window.crypto.subtle.decrypt({
name: "AES-GCM",
iv: new Uint8Array(iv)
}, key, data);
return new TextDecoder().decode(decryptedData);
} catch (error) {
console.error("Decryption failed:", error);
throw error;
}
}
var secured_vault_default = SecuredVault;
export {
secured_vault_default as default
};
//# sourceMappingURL=secured-vault.js.map

@@ -1,1 +0,119 @@

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)}},d=l;var o="readonly",a="readwrite",i="store",c=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,s=null){return this.do(a,n=>n.put({key:e,value:t,meta:s}))}async getItem(e){return this.do(o,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(o,e=>e.getAllKeys())}async length(){return this.do(o,e=>e.count())}async init(){return new Promise((e,t)=>{let s=indexedDB.open(this.storageName,1);s.onupgradeneeded=n=>{n.target.result.createObjectStore(i,{keyPath:"key"})},s.onsuccess=()=>{this.db=s.result,e()},s.onerror=n=>t(n)})}async getItemMeta(e){return this.do(o,t=>t.get(e)).then(t=>t?.meta??null)}async do(e,t){this.db||await this.init();let s=this.db.transaction(i,e),n=t(s.objectStore(i));return new Promise((u,y)=>{n.onsuccess=()=>u(e===o?n.result:void 0),n.onerror=()=>y(n.error)})}};export{c as default};
// src/proxy-handler.ts
var proxyHandler = {
get(target, key) {
return typeof target[key] === "function" ? target[key].bind(target) : key in target ? target[key] : target.getItem(key);
},
set(target, key, value) {
return target.setItem(key, value);
},
deleteProperty(target, key) {
return target.removeItem(key);
}
};
var proxy_handler_default = proxyHandler;
// src/vault.ts
var r = "readonly";
var rw = "readwrite";
var s = "store";
var Vault = class {
storageName = "vault-storage";
db = null;
/**
* Creates new custom instance of custom Vault Storage.
* @param {string} [storageName] - The name of the storage.
* @param {boolean} [isParent=false] - A flag to indicate if this instance
* is a parent. This property should be ignored by the user unless they are
* extending the Vault class.
*/
constructor(storageName, isParent = false) {
this.storageName = storageName || this.storageName;
if (!isParent)
return new Proxy(this, proxy_handler_default);
}
/**
* Set an item in the database with additional metadata.
* @param {string} key - The key of the item.
* @param {any} value - The value of the item.
* @param {any} meta - The metadata for the item (e.g., ttl, expiration) or
* any other data that should be stored alongside the value.
* @returns {Promise<void>}
*/
async setItem(key, value, meta = null) {
return this.do(rw, (s2) => s2.put({ key, value, meta }));
}
/**
* Get an item from the database.
* @param {string} key - The key of the item.
* @returns {Promise<any>} - The value of the item.
*/
async getItem(key) {
return this.do(r, (s2) => s2.get(key)).then((r2) => r2?.value ?? null);
}
/**
* Remove an item from the database.
* @param {string} key - The key of the item.
* @returns {Promise<void>}
*/
async removeItem(key) {
return this.do(rw, (s2) => s2.delete(key));
}
/**
* Clear the database.
* @returns {Promise<void>}
*/
async clear() {
return this.do(rw, (s2) => s2.clear());
}
/**
* Get all keys in the database.
* @returns {Promise<string[]>} - An array of keys.
*/
async keys() {
return this.do(r, (s2) => s2.getAllKeys());
}
/**
* Get the number of items in the database.
* @returns {Promise<number>} - The number of items.
*/
async length() {
return this.do(r, (s2) => s2.count());
}
// Initialize the database and return a promise.
async init() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.storageName, 1);
request.onupgradeneeded = (e) => {
e.target.result.createObjectStore(s, { keyPath: "key" });
};
request.onsuccess = () => {
this.db = request.result;
resolve();
};
request.onerror = (e) => reject(e);
});
}
/**
* Get an item's metadata from the database.
* @param {string} key - The key of the item.
* @returns {Promise<any>} - The metadata of the item.
*/
async getItemMeta(key) {
return this.do(r, (s2) => s2.get(key)).then((r2) => r2?.meta ?? null);
}
// Execute a transaction and return a promise.
async do(operationType, operation) {
if (!this.db)
await this.init();
const transaction = this.db.transaction(s, operationType);
const request = operation(transaction.objectStore(s));
return new Promise((resolve, reject) => {
request.onsuccess = () => resolve(operationType === r ? request.result : void 0);
request.onerror = () => reject(request.error);
});
}
};
export {
Vault as default
};
//# sourceMappingURL=vault.js.map

2

package.json
{
"name": "vault-storage",
"description": "Vault, a micro yet robust browser storage library",
"version": "1.2.2",
"version": "1.2.3",
"author": "ManiarTech®️ - Mohamed Aamir Maniar",

@@ -6,0 +6,0 @@ "license": "MIT",

@@ -203,8 +203,12 @@ # vault

vault.setItem('yourKey', { any: 'data' }, {
expires: new Date('2024-12-31'),
roles: ['editor', 'moderator'],
});
// Get the meta data along with the item value.
// Get the meta data for the specified item.
const meta = await vault.getItemMeta('yourKey');
console.log("yourKey expires on ", meta.expires);
console.log(`yourKey is marked for '${meta.roles}' roles! `);
if (user.roles.some(role => meta.roles.includes(role))) {
// User has access to the specified item in the vault.
}
```

@@ -257,4 +261,3 @@

values. `(v1.2.*)`
- [ ] Automatic expiration of values based on TTL, Session Timeout and other
expiration policies `(Future)`
- [ ] Automatic expiration of values through `expires` meta data. `(Future)`
- [ ] Support for vault data backup and restore `(Future)`

@@ -269,12 +272,1 @@

`vault-storage` is [MIT licensed](./LICENSE).
```javascript
// 1 week
vault.setItem('yourKey', true, { ttl: 60 * 24 * 7 });
// 2024-01-01 00:00:00
vault.setItem('yourKey', true, {
expires: new Date('2024-01-01'),
});
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc