Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
promised-db
Advanced tools
IndexedDB with a better API using contextual interfaces and timeout support.
A library with TypeScript support for a better experience using IndexedDB, hiding the event handlers and weird cursor API stuff that I don't like while still using IndexedDB as normal as possible.
In return you get a simple class that wraps the transaction flow of IndexedDB with (optional) Promise-wrapped actions performed on the IDB interfaces. It also adds timeout functionality to transactions.
const pdb = new PromiseDB("mydb", 1,
(db, onDiskVersion, newVersion) => {
// This callback is called when there is no DB on disk or if the version
// number you specified is greater than the one on disk.
// This function is the _only_ place where you can make schema changes to
// the database.
// db is just an IDBDatabase instance
// proceed as in a normal upgradeneeded callback
const stuff = db.createObjectStore("stuff", { keyPath: "index" });
stuff.createIndex("userID", "userID", { unique: true });
});
The PromisedDB
instance you get back only has 1 method: transaction
, every
operation on the db is done with a transaction, like so:
// as with IDB, you specify the stores involved in this request
// and either "readonly" or "readwrite" as access type
const trans = pdb.transaction(["stuff", "morestuff"], "readonly",
// you pass a function that constitutes the actual transaction
// you get the IDBTransaction and a context object as parameters (see doc below)
(tr, {request, getAll, getAllKeys, timeout, cursor, keyCursor}) => {
// have this request timeout and abort after 5 seconds (optional)
timeout(5000);
// tr is a standard IDBTransaction interface
const stuff = tr.objectStore("stuff");
// use request(r: IDBRequest) to Promise-wrap any IDB request
// this includes: get(), put(), update(), delete(), count(), etc.
const itemProm = request(stuff.get(someKey));
// itemProm is of type Promise<any>
// getAll and getAllKeys are provided to either call native IDB 2
// methods or a polyfill for implementations of IDB 1.
const allRecords = getAll<RecordType>(someIndex or someStore, someKey);
// allRecords is of type Promise<RecordType[]>
// Use cursor or keyCursor to build a fluent cursor object to iterate
// over rows with full control.
// direction is "next" | "prev" | "nextunique" | "prevunique", default "next"
cursor(stuff, optionalRange, direction)
.next(cur => {
// cur is an IDBCursor, `value` will be present for non-key cursors
myProcessFunc(cur.value);
// NOTE: you still MUST call cur.continue() to proceed to the next record
cur.continue();
})
.complete(() => {
// (optional) do something when the cursor has iterated to the end of the range
})
.catch(error => {
// (optional) handle an error occuring inside cursor handling
});
// if you don't need to wait for the result you don't have to wrap requests
stuff.delete(someOtherKey);
// the optional return value of this function is the result type of
// the transaction function's Promise.
return Promise.all([itemProm, allRecords]);
});
// Then just handle the transaction's Promise:
trans
.then(result => {
// ... process whatever you returned in your transaction function
})
.catch(error => {
// ... handle any errors, including timeouts
});
type PDBTransactionMode = "readonly" | "readwrite";
type PDBCursorDirection = "next" | "prev" | "nextunique" | "prevunique";
interface PDBCursorResult<C extends IDBCursor> {
next(callback: (cursor: C) => void): PDBCursorResult<C>;
complete(callback: () => void): PDBCursorResult<C>;
catch(callback: (error: any) => void): PDBCursorResult<C>;
}
interface PDBTransactionContext {
request: (req: IDBRequest, fn?: (req: IDBRequest) => void) => Promise<any>;
cursor: (container: IDBIndex | IDBObjectStore, range?: IDBKeyRange | IDBValidKey, direction?: PDBCursorDirection) => PDBCursorResult<IDBCursorWithValue>;
keyCursor: (index: IDBIndex, range?: IDBKeyRange | IDBValidKey, direction?: PDBCursorDirection) => PDBCursorResult<IDBCursor>;
getAll: <T>(container: IDBIndex | IDBObjectStore, range?: IDBKeyRange | IDBValidKey, direction?: PDBCursorDirection, limit?: number) => Promise<T[]>;
getAllKeys: <K extends IDBValidKey>(index: IDBIndex, range?: IDBKeyRange | IDBValidKey, direction?: PDBCursorDirection, limit?: number) => Promise<K[]>;
timeout: (ms: number) => void;
}
type PDBUpgradeCallback = (db: IDBDatabase, fromVersion: number, toVersion: number) => void;
type PDBTransactionFunc = (tr: IDBTransaction, context: PDBTransactionContext) => Promise<T | void>;
class PromisedDB {
constructor(name: string, version: number, upgrade: PDBUpgradeCallback);
close(): void;
transaction<T>(storeNames: string | string[], mode: PDBTransactionMode, fn: PDBTransactionFunc): Promise<T>;
}
License: MIT License
(c) 2016-Present by Arthur Langereis (@zenmumbler)
1.0.2 - 2017-02-19
FAQs
Use IndexedDB with Promises and quality of life features
The npm package promised-db receives a total of 0 weekly downloads. As such, promised-db popularity was classified as not popular.
We found that promised-db demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.