New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

random-access-web-storage

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

random-access-web-storage - npm Package Compare versions

Comparing version 1.2.0 to 1.3.0

192

index.js
const RandomAccess = require('random-access-storage')
const inherits = require('inherits')
const b4a = require('b4a')

@@ -14,5 +13,5 @@

* @param {Opts} [opts]
* **/
**/
(filename, opts) =>
RAWS({ storage, filename, ...opts })
new RAWS({ storage, filename, ...opts })

@@ -27,132 +26,133 @@ /**

/** @type {(opts: Opts) => RAS} **/
function RAWS (opts) {
// @ts-ignore
if (!(this instanceof RAWS)) return new RAWS(opts)
class RAWS extends RandomAccess {
/**
*
* @param {Opts} opts
*/
constructor (opts) {
super()
this.filename = opts.filename
this.storage = opts.storage
this.pageSize = opts.pageSize || DEFAULT_PAGE_SIZE
}
RandomAccess.call(this)
this.buffers = []
this.storage = opts.storage
this.pageSize = opts.pageSize || DEFAULT_PAGE_SIZE
this.filename = opts.filename
}
Object.defineProperty(RAWS.prototype, 'length', {
get: function () {
get length () {
const len = this.storage.getItem(this._formatKey('length'))
return len ? Number(len) : 0
},
set (len) {
}
set length (len) {
if (!len) return
this.storage.setItem(this._formatKey('length'), len.toString())
}
})
inherits(RAWS, RandomAccess)
// @ts-ignore
_stat (req) {
req.callback(null, { size: this.length })
}
RAWS.prototype._stat = function (req) {
req.callback(null, { size: this.length })
}
// @ts-ignore
_write (req) {
var i = Math.floor(req.offset / this.pageSize)
var rel = req.offset - i * this.pageSize
var start = 0
RAWS.prototype._write = function (req) {
var i = Math.floor(req.offset / this.pageSize)
var rel = req.offset - i * this.pageSize
var start = 0
const len = req.offset + req.size
if (len > this.length) this.length = len
const len = req.offset + req.size
if (len > this.length) this.length = len
while (start < req.size) {
const page = this._page(i)
const free = this.pageSize - rel
const end = free < req.size - start ? start + free : req.size
while (start < req.size) {
const page = this._page(i)
const free = this.pageSize - rel
const end = free < req.size - start ? start + free : req.size
b4a.copy(req.data, page, rel, start, end)
this.storage.setItem(this._formatKey(i), b4a.toString(page, 'hex'))
b4a.copy(req.data, page, rel, start, end)
this.storage.setItem(this._formatKey(i), b4a.toString(page, 'hex'))
i++
start = end
rel = 0
}
i++
start = end
rel = 0
req.callback(null, null)
}
req.callback(null, null)
}
// @ts-ignore
_read (req) {
var i = Math.floor(req.offset / this.pageSize)
var rel = req.offset - i * this.pageSize
var start = 0
RAWS.prototype._read = function (req) {
var i = Math.floor(req.offset / this.pageSize)
var rel = req.offset - i * this.pageSize
var start = 0
if (req.offset + req.size > this.length) {
return req.callback(new Error('Could not satisfy length'), null)
}
if (req.offset + req.size > this.length) {
return req.callback(new Error('Could not satisfy length'), null)
}
const data = b4a.alloc(req.size)
const data = b4a.alloc(req.size)
while (start < req.size) {
const page = this._page(i)
const avail = this.pageSize - rel
const wanted = req.size - start
const len = avail < wanted ? avail : wanted
while (start < req.size) {
const page = this._page(i)
const avail = this.pageSize - rel
const wanted = req.size - start
const len = avail < wanted ? avail : wanted
if (page) b4a.copy(page, data, start, rel, rel + len)
if (page) b4a.copy(page, data, start, rel, rel + len)
i++
start += len
rel = 0
}
i++
start += len
rel = 0
req.callback(null, data)
}
req.callback(null, data)
}
// @ts-ignore
_del (req) {
var i = Math.floor(req.offset / this.pageSize)
var rel = req.offset - i * this.pageSize
var start = 0
RAWS.prototype._del = function (req) {
var i = Math.floor(req.offset / this.pageSize)
var rel = req.offset - i * this.pageSize
var start = 0
const len = this.length
const len = this.length
if (rel && req.offset + req.size >= len) {
const buf = this._page(i)
if (buf) buf.fill(0, rel)
this.storage.setItem(i.toString(), b4a.toString(buf, 'hex'))
}
if (rel && req.offset + req.size >= len) {
const buf = this._page(i)
if (buf) buf.fill(0, rel)
this.storage.setItem(i.toString(), b4a.toString(buf, 'hex'))
}
if (req.offset + req.size > len) {
req.size = Math.max(0, len - req.offset)
}
if (req.offset + req.size > len) {
req.size = Math.max(0, len - req.offset)
}
while (start < req.size) {
if (rel === 0 && req.size - start >= this.pageSize) {
this.storage.removeItem(i.toString())
}
while (start < req.size) {
if (rel === 0 && req.size - start >= this.pageSize) {
this.storage.removeItem(i.toString())
rel = 0
i += 1
start += this.pageSize - rel
}
rel = 0
i += 1
start += this.pageSize - rel
if (req.offset + req.size >= len) {
this.length = req.offset
}
req.callback(null, null)
}
if (req.offset + req.size >= len) {
this.length = req.offset
// @ts-ignore
_destroy (req) {
this.storage.clear && this.storage.clear()
req.callback(null, null)
}
req.callback(null, null)
}
_formatKey (key) {
return this.filename + DELIM + key.toString()
}
RAWS.prototype._destroy = function (req) {
this.storage.clear && this.storage.clear()
req.callback(null, null)
_page (i) {
let val = this.storage.getItem(this._formatKey(i))
return val ? b4a.from(val, 'hex') : b4a.alloc(this.pageSize)
}
}
RAWS.prototype._page = function (i) {
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()
}
/**

@@ -159,0 +159,0 @@ * @typedef {{

{
"name": "random-access-web-storage",
"version": "1.2.0",
"version": "1.3.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",

@@ -11,3 +11,2 @@ "main": "index.js",

"b4a": "^1.1.0",
"inherits": "^2.0.3",
"random-access-storage": "^1.1.1"

@@ -14,0 +13,0 @@ },

@@ -1,2 +0,2 @@

var tape = require('tape')
const tape = require('tape')

@@ -3,0 +3,0 @@ const RAWS = require('.')

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