Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
random-access-layered-storage
Advanced tools
A flexible, layered storage solution building on the random-access-storage interface. Provides an in-memory overlay with configurable eviction, paging, and strict size enforcement options, designed for complex data management scenarios.
RandomAccessLayeredStorage
is an extension of the random-access-storage
interface that provides a memory overlay over any underlying random-access-storage implementation. It supports in-memory caching, layered storage systems, and eviction of memory pages to underlying storage.
To use this class in your project, ensure you have the following dependencies installed:
npm install random-access-layered-storage
Then, import RandomAccessLayeredStorage
into your project:
import RandomAccessLayeredStorage from 'random-access-layered-storage';
This library aims to provide a flexible, layered storage solution, building upon the random-access-storage
(RAS) interface. Currently, RandomAccessLayeredStorage offers a range of functionality similar to the RAS interface, but with some deviations. These deviations are primarily due to the fact that this version of the library is still evolving towards full compliance with the RAS interface.
write
, read
, delete
, truncate
, and flush
.Future versions of RandomAccessLayeredStorage will work towards full RAS interface compliance. These enhancements will include:
random-access-storage
interface methods and behaviors.If you are using this library in environments where strict random-access-storage
compliance is necessary, it is recommended to:
By providing an interface that is mostly aligned with RAS, this library offers many of the benefits of the standard RAS system while also introducing additional flexibility in how data is managed in layered storage. Over time, as we achieve full compliance, you will be able to use this library in scenarios requiring strict adherence to the RAS specification.
RandomAccessLayeredStorage
The RandomAccessLayeredStorage
class provides an in-memory overlay for random-access storage (such as RAM
, RAF
, etc.), allowing for efficient caching, eviction, and flushing of data to underlying storage.
const RAS = require('random-access-storage');
const RandomAccessLayeredStorage = require('./path-to-your-layered-storage');
const underlyingStorage = new RAM(); // or RAF, IndexedDB, etc.
const layeredStorage = new RandomAccessLayeredStorage(underlyingStorage, {
pageSize: 1024 * 1024, // 1MB page size
maxPages: 100, // Max pages in memory
createIfMissing: true, // Create file if it does not exist
strictSizeEnforcement: 1024 * 1024 * 10, // 10MB strict size limit
flushOnClose: true, // Automatically flush on close
autoFlushOnEvict: true // Automatically flush evicted pages
});
underlyingStorage
RandomAccessStorage
interface (e.g., RAM
, RAF
)RandomAccessLayeredStorage
instance.opts
Object
pageSize
number
1024 * 1024
(1MB)maxPages
number
100
autoFlushOnEvict
is enabled, modified pages will be flushed to the underlying storage before being evicted.createIfMissing
boolean
true
true
, the underlying storage will be created if it doesn’t already exist. This is useful when dealing with new storage files.strictSizeEnforcement
number
undefined
flushOnClose
boolean
true
true
, the storage will automatically flush all modified pages to the underlying storage when it is closed.autoFlushOnEvict
boolean
true
true
, pages evicted from memory due to LRU constraints will be automatically flushed to the underlying storage if they were modified.const ram = new RAM();
const layeredStorage = new RandomAccessLayeredStorage(ram, {
pageSize: 1024, // 1KB page size
maxPages: 50, // Store up to 50 pages in memory
createIfMissing: true, // Create if missing
strictSizeEnforcement: 1024 * 1024 * 5, // Enforce 5MB size limit
flushOnClose: true, // Flush data when storage is closed
autoFlushOnEvict: true // Flush pages when evicted from memory
});
layeredStorage.write(0, Buffer.from('Hello, world!'), err => {
if (err) throw err;
layeredStorage.flush(0, 13, err => {
if (err) throw err;
console.log('Data flushed to underlying storage.');
});
});
RandomAccessLayeredStorage
class extends the random-access-storage
interface, so it supports the standard methods like write
, read
, del
, truncate
, flush
, and close
.open(callback)
Opens the storage. This is required before any other operations are performed.
function (error)
- Called when the storage is opened or if an error occurs.read(offset, size, callback)
Reads data from the specified offset and size.
function (error, buffer)
- Called with the read data or an error.write(offset, buffer, callback)
Writes data to the specified offset.
Buffer
containing data to write.function (error)
- Called when the write completes or if an error occurs.del(offset, size, callback)
Deletes data starting at the given offset for a specified size. Internally fills the space with zeroes.
function (error)
- Called when the deletion is complete or if an error occurs.truncate(offset, callback)
Truncates the file at the specified offset. It can grow or shrink the file.
function (error)
- Called when truncation is complete or if an error occurs.flush(offset = 0, size = this.size, callback)
Flushes modified pages in memory to the underlying storage. Optionally specify a byte range to flush.
function (error)
- Called when flushing is complete or if an error occurs.stat(callback)
Retrieves the current size of the storage.
function (error, stats)
- Called with the size or if an error occurs.close(callback)
Closes the storage. Optionally flushes all modified data before closing if flushOnClose
is enabled.
function (error)
- Called when the storage is closed or if an error occurs.unlink(callback)
Unlinks (removes) the storage, clearing the memory cache and optionally removing the underlying storage.
function (error)
- Called when the storage is unlinked or if an error occurs.evict(percent = 1, flushBeforeEvict = false, callback = () => {})
Evicts a percentage of the least recently used pages from memory. If flushBeforeEvict
is true
, modified pages will be flushed before eviction.
false
).function (error)
- Called when eviction is complete or if an error occurs.pin(offset, size)
Pins a range of pages in memory, preventing them from being evicted.
unpin(offset, size)
Unpins a range of pages, allowing them to be evicted if necessary.
setBitmask(bitmaskBuffer)
Sets a bitmask for controlling write access to parts of the storage.
clearBitmask()
Clears the bitmask, allowing writes to all parts of the storage.
length
Returns the current size of the storage.
size
Alias for length
. Returns the current size of the storage.
import RandomAccessLayeredStorage from 'random-access-layered-storage';
import RAM from 'random-access-memory';
// Create a layered storage with an underlying RAM store
const underlyingStorage = new RAM();
const storage = new RandomAccessLayeredStorage(underlyingStorage, { pageSize: 1024, maxPages: 10 });
storage.open((err) => {
if (err) throw err;
// Write some data
const data = Buffer.from('Hello, world!');
storage.write(0, data, (err) => {
if (err) throw err;
// Read the data back
storage.read(0, data.length, (err, readData) => {
if (err) throw err;
console.log(readData.toString()); // Outputs: 'Hello, world!'
});
});
});
This project is licensed under the MIT License.
FAQs
A flexible, layered storage solution building on the random-access-storage interface. Provides an in-memory overlay with configurable eviction, paging, and strict size enforcement options, designed for complex data management scenarios.
The npm package random-access-layered-storage receives a total of 0 weekly downloads. As such, random-access-layered-storage popularity was classified as not popular.
We found that random-access-layered-storage demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.