random-access-web-storage
Advanced tools
31
index.js
@@ -5,3 +5,4 @@ const RandomAccess = require('random-access-storage') | ||
const DEFAULT_PAGE_SIZE = 1024 * 1024 | ||
const DEFAULT_PAGE_SIZE = 32 * 1024 | ||
const DELIM = '\0' | ||
@@ -26,4 +27,5 @@ /** @param {WebStorageAPI} storage */ | ||
/** @param {Opts} [opts] **/ | ||
/** @type {(opts: Opts) => RAS} **/ | ||
function RAWS (opts) { | ||
// @ts-ignore | ||
if (!(this instanceof RAWS)) return new RAWS(opts) | ||
@@ -42,3 +44,3 @@ | ||
get: function () { | ||
const len = this.storage.getItem(this.filename + ':length') | ||
const len = this.storage.getItem(this._formatKey('length')) | ||
return len ? Number(len) : 0 | ||
@@ -48,3 +50,3 @@ }, | ||
if (!len) return | ||
this.storage.setItem(this.filename + ':length', len.toString()) | ||
this.storage.setItem(this._formatKey('length'), len.toString()) | ||
} | ||
@@ -73,6 +75,3 @@ }) | ||
b4a.copy(req.data, page, rel, start, end) | ||
this.storage.setItem( | ||
this.filename + ':' + i.toString(), | ||
b4a.toString(page, 'hex') | ||
) | ||
this.storage.setItem(this._formatKey(i), b4a.toString(page, 'hex')) | ||
@@ -154,6 +153,10 @@ i++ | ||
RAWS.prototype._page = function (i) { | ||
let val = this.storage.getItem(this.filename + ':' + i.toString()) | ||
let val = this.storage.getItem(this._formatKey(i)) | ||
return val ? b4a.from(val, 'hex') : b4a.alloc(this.pageSize) | ||
} | ||
RAWS.prototype._formatKey = function (key) { | ||
return this.filename + DELIM + key.toString() | ||
} | ||
/** | ||
@@ -166,2 +169,12 @@ * @typedef {{ | ||
* }} WebStorageAPI | ||
* | ||
* @typedef {{ | ||
* open : () => void | ||
* read : (offset: number, size: number, cb: (err: Error |null, val: Buffer) => void) => void | ||
* write : (offset: number, buf: Buffer, cb: (err: Error |null) => void) => void | ||
* del : (offset: number, size: number, cb: (err: Error |null) => void) => void | ||
* stat : (cb: (err: Error | null, stat: {size: number}) => void) => void | ||
* close : (cb: (err: Error | null) => void) => void | ||
* destroy : (cb: (err: Error | null) => void) => void | ||
* }} RAS | ||
*/ |
{ | ||
"name": "random-access-web-storage", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Exposes the same interface as random-access-file but instead of writing/reading data to a file it writes to any Web Storage API compatible database", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
12601
4.76%321
3.88%