vault-storage
Advanced tools
Comparing version 1.2.3 to 1.3.0
@@ -1,123 +0,1 @@ | ||
// 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 | ||
var m={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=m;var o="readonly",c="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,d)}async setItem(e,t,n=null){return this.do(c,s=>s.put({key:e,value:t,meta:n}))}async getItem(e){return this.do(o,t=>t.get(e)).then(t=>t?.value??null)}async removeItem(e){return this.do(c,t=>t.delete(e))}async clear(){return this.do(c,e=>e.clear())}async keys(){return this.do(o,e=>e.getAllKeys())}async length(){return this.do(o,e=>e.count())}async getItemMeta(e){return this.do(o,t=>t.get(e)).then(t=>t?.meta??null)}async init(){return new Promise((e,t)=>{let n=indexedDB.open(this.storageName,1);n.onupgradeneeded=s=>{s.target.result.createObjectStore(u,{keyPath:"key"})},n.onsuccess=()=>{this.db=n.result,e()},n.onerror=s=>{t(s)}})}async do(e,t){this.db||await this.init();let s=this.db.transaction(u,e).objectStore(u),i=t(s);return new Promise((y,l)=>{i.onsuccess=()=>{y(e===o?i.result:void 0)},i.onerror=()=>{l(i.error)}})}};var g=new a,f=g;export{f as default}; |
@@ -1,17 +0,1 @@ | ||
// 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 | ||
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}; |
@@ -1,254 +0,1 @@ | ||
// 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 | ||
var f={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=f;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 getItemMeta(e){return this.do(i,t=>t.get(e)).then(t=>t?.meta??null)}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 do(e,t){this.db||await this.init();let n=this.db.transaction(d,e).objectStore(d),o=t(n);return new Promise((p,m)=>{o.onsuccess=()=>{p(e===i?o.result:void 0)},o.onerror=()=>{m(o.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 w(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:h(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 g(r.password,await l(r.salt))}let t=await g(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 g(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 w(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 h(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 I=u;export{I as default}; |
@@ -8,2 +8,3 @@ /** | ||
protected db: IDBDatabase | null; | ||
[key: string]: any; | ||
/** | ||
@@ -25,3 +26,3 @@ * Creates new custom instance of custom Vault Storage. | ||
*/ | ||
setItem(key: string, value: any, meta?: any): Promise<void>; | ||
setItem(key: string, value: any, meta?: Record<string, any> | null): Promise<void>; | ||
/** | ||
@@ -54,3 +55,2 @@ * Get an item from the database. | ||
length(): Promise<number>; | ||
protected init(): Promise<void>; | ||
/** | ||
@@ -62,3 +62,4 @@ * Get an item's metadata from the database. | ||
getItemMeta(key: string): Promise<any>; | ||
protected init(): Promise<void>; | ||
protected do(operationType: IDBTransactionMode, operation: (store: IDBObjectStore) => IDBRequest): Promise<any>; | ||
} |
@@ -1,119 +0,1 @@ | ||
// 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 | ||
var m={get(n,e){return typeof n[e]=="function"?n[e].bind(n):e in n?n[e]:n.getItem(e)},set(n,e,t){return n.setItem(e,t)},deleteProperty(n,e){return n.removeItem(e)}},u=m;var o="readonly",i="readwrite",c="store",d=class{storageName="vault-storage";db=null;constructor(e,t=!1){if(this.storageName=e||this.storageName,!t)return new Proxy(this,u)}async setItem(e,t,r=null){return this.do(i,s=>s.put({key:e,value:t,meta:r}))}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 getItemMeta(e){return this.do(o,t=>t.get(e)).then(t=>t?.meta??null)}async init(){return new Promise((e,t)=>{let r=indexedDB.open(this.storageName,1);r.onupgradeneeded=s=>{s.target.result.createObjectStore(c,{keyPath:"key"})},r.onsuccess=()=>{this.db=r.result,e()},r.onerror=s=>{t(s)}})}async do(e,t){this.db||await this.init();let s=this.db.transaction(c,e).objectStore(c),a=t(s);return new Promise((y,l)=>{a.onsuccess=()=>{y(e===o?a.result:void 0)},a.onerror=()=>{l(a.error)}})}};export{d as default}; |
{ | ||
"name": "vault-storage", | ||
"description": "Vault, a micro yet robust browser storage library", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"author": "ManiarTech®️ - Mohamed Aamir Maniar", | ||
@@ -40,3 +40,4 @@ "license": "MIT", | ||
"./vault": "./dist/vault.js", | ||
"./secured-vault": "./dist/secured-vault.js" | ||
"./secured-vault": "./dist/secured-vault.js", | ||
"./backup": "./dist/backup.js" | ||
}, | ||
@@ -43,0 +44,0 @@ "files": [ |
@@ -1,7 +0,8 @@ | ||
# vault | ||
# Vault Storage | ||
`vault` is a sophisticated browser-based storage library that leverages the power | ||
Vault Storage is a sophisticated browser-based storage library that leverages the power | ||
of IndexedDB, offering significant improvements over traditional LocalStorage. | ||
As a high-performance, asynchronous solution for client-side storage, `vault` | ||
provides an intuitive and easy-to-use API similar to local and session storage, increasing the capacity of the storage, and adding support for structured data, | ||
As a high-performance, asynchronous solution for client-side storage, it | ||
provides an intuitive and easy-to-use API similar to local and session storage, | ||
increasing the capacity of the storage, and adding support for structured data, | ||
and support for multiple stores. It also provides a secure storage for sensitive | ||
@@ -19,2 +20,3 @@ data. | ||
- **Encrypted Vault**: Provides a secure storage for sensitive data. | ||
- **Backup and Restore**: Export and import the vault storage data. | ||
- **Asynchronous**: Non-blocking, asynchronous API. | ||
@@ -216,4 +218,36 @@ - **Structured Data**: Supports structured data, including objects and arrays. | ||
### Backup and Restore Vault Storage | ||
With version 1.3 and above, you can export and import the vault storage data. Please note that while exporting the secured storage data, the data is exported in non-encrypted form. You must be careful while exporting the data and ensure that the data is exported in a secure manner. | ||
> We are still considering the best way to export the secured storage data in an encrypted form. If you have any suggestions, please let us know. | ||
```javascript | ||
import { importData, exportData } from 'vault-storage/backup'; | ||
const data = await exportData(vault); | ||
// You can now save the data to a file or send it to the server. | ||
// For example, you can save the data to a file using the following code. | ||
const blob = new Blob([JSON.stringify(data)], { type: 'application/json' }); | ||
const url = URL.createObjectURL(blob); | ||
const a = document.createElement('a'); | ||
a.href = url | ||
a.download = 'vault-data.json'; | ||
a.click(); | ||
// To import the data back to the vault, you can use the following code. | ||
const importedData = await importData(data); | ||
``` | ||
## API Reference | ||
### `Vault` Class | ||
The `Vault` class is the cornerstone of our storage capabilities, providing a functionality akin to `localStorage` and `sessionStorage`. It empowers you to establish custom storage instances, offering a intuitive and user-friendly API for data storage and retrieval. Here's a rundown of the methods available in the `Vault` class: | ||
```javascript | ||
import Vault from 'vault-storage/vault'; | ||
``` | ||
- `setItem(key: string, value: any, meta: any)`: Store data in the storage. | ||
@@ -225,2 +259,25 @@ - `getItem(key: string)`: Retrieve data from the storage. | ||
### `vault` Default Instance | ||
The `vault` is a default instance of the `Vault` class, providing a ready-to-use storage solution without any setup or initialization. | ||
```javascript | ||
import vault from 'vault-storage'; | ||
``` | ||
### `SecuredVault` Class | ||
The `SecuredVault` class is a provides a secure storage for sensitive data. 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). For more information, refer to the [usage section above](#secured-storage). | ||
### Import and Export Functions | ||
Additionally, the `vault-storage` library offers two functions for exporting and importing vault storage data: | ||
```javascript | ||
import { importData, exportData } from 'vault-storage/backup'; | ||
``` | ||
- `exportData(vault: Vault)`: Export the vault storage data. | ||
- `importData(data: any)`: Import the vault storage data. | ||
## Comparing Vault with LocalStorage | ||
@@ -236,2 +293,3 @@ | ||
| **Data Types** | Supports structured data, including objects and arrays | Only stores strings | | ||
| **Built-in Data Import/Export** | Supports backup and restore of the vault storage | No built-in support for data import/export | | ||
| **Performance** | Asynchronous, non-blocking | Synchronous, can block UI | | ||
@@ -264,4 +322,4 @@ | ||
values. `(v1.2.*)` | ||
- [x] Support for vault data backup and restore `(v1.3.*)` | ||
- [ ] Automatic expiration of values through `expires` meta data. `(Future)` | ||
- [ ] Support for vault data backup and restore `(Future)` | ||
@@ -268,0 +326,0 @@ ## Contributing |
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
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
328
27872
13
206